View Javadoc

1   package org.votech.plastic.managers;
2   
3   
4   import java.util.Timer;
5   import java.util.TimerTask;
6   
7   import org.apache.commons.logging.Log;
8   import org.apache.commons.logging.LogFactory;
9   import org.astrogrid.acr.ACRException;
10  import org.astrogrid.acr.Finder;
11  import org.astrogrid.acr.InvalidArgumentException;
12  import org.astrogrid.acr.NotFoundException;
13  import org.astrogrid.acr.builtin.ACR;
14  import org.astrogrid.acr.builtin.Shutdown;
15  import org.astrogrid.acr.builtin.ShutdownListener;
16  import org.astrogrid.acr.system.WebServer;
17  
18  /**
19   * @author jdt
20   * 
21   */
22  public class AcrManagerImpl extends AbstractObservableManager implements	ShutdownListener, AcrManager {
23  
24  
25  	public static class ObserverAdaptor implements ManagerObserver {
26  
27  		private AcrObserver observer;
28  
29  		public ObserverAdaptor(AcrObserver observer) {
30  			this.observer = observer;
31  		}
32  
33  		public void down() {
34  			observer.acrDown();
35  		}
36  
37  		public void up() {
38  			observer.acrUp();
39  		}
40  
41  	}
42  
43  
44  	/**
45  	 * Logger for this class
46  	 */
47  	private static final Log logger = LogFactory.getLog(AcrManagerImpl.class);
48  
49  	public interface AcrObserver {
50  		public void acrUp();
51  		public void acrDown();
52  	}
53  	
54  
55  	private int autoReconnect;
56  
57  
58  
59  	private String name;
60  
61  	private boolean startIfNotRunning;
62  
63  	/**
64  	 * 
65  	 * @param connectOnCreation
66  	 * @param autoReconnectTimeMillis
67  	 * @param parent
68  	 *            the gui frame that is the parent of any dialogs.
69  	 */
70  	public AcrManagerImpl(boolean connectOnCreation, int autoReconnectTimeMillis,
71  			String name) {
72  		this.autoReconnect = autoReconnectTimeMillis;
73  		this.name = name;
74  		if (connectOnCreation) {
75  			connect();
76  		}
77  	}
78  
79  	public AcrManagerImpl(boolean connectOnCreation, int autoReconnectTimeMillis) {
80  		this(connectOnCreation, autoReconnectTimeMillis,  "");
81  	}
82  
83  	/**
84  	 * @param args
85  	 * @throws ACRException
86  	 * @throws NotFoundException
87  	 * @throws InvalidArgumentException
88  	 */
89  	public static void main(String[] args) throws InvalidArgumentException,
90  			NotFoundException, ACRException {
91  		AcrManagerImpl manager = new AcrManagerImpl(false, 3000,
92  				"AcrManager Test");
93  
94  		manager.addObserver(new ObserverAdaptor(new AcrObserver() {
95  
96  			public void acrUp() {
97  				System.out.println("Acr is up");
98  			}
99  			public void acrDown() {
100 				System.out.println("Acr is down");
101 			}
102 
103 		}));
104 
105 		manager.connect();
106 		ACR acr = manager.getAcr();
107 		WebServer server = (WebServer) acr.getService(WebServer.class);
108 		System.out.println(server.getUrlRoot());
109 
110 		manager.disconnect();
111 		manager.connect();
112 		//try something impossible
113 	//	Registry reg = (Registry) acr.getService(Registry.class);
114 	//	System.out.println(reg.resolveIdentifier(URI.create("ivo://dummy")));
115 		
116 		System.exit(0);
117 	}
118 
119 	
120 	/* (non-Javadoc)
121      * @see org.votech.plastic.managers.AcrManager#getAcr()
122      */
123 	public ACR getAcr() {
124 		return acr;
125 	}
126 
127     /* (non-Javadoc)
128      * @see org.votech.plastic.managers.AcrManager#connect()
129      */
130 	public void connect() {
131 		connect(true);
132 	}
133 	/* (non-Javadoc)
134 	 * @see org.votech.plastic.managers.Manager1#connect()
135 	 */
136 	/* (non-Javadoc)
137      * @see org.votech.plastic.managers.AcrManager#connect(boolean)
138      */
139 	public void connect(boolean startIfNotRunning) {
140 		logger.debug("Connecting to ACR...");
141 		this.startIfNotRunning = startIfNotRunning;
142 		if (isConnected()) {
143 			logger.debug("...already connected.");
144 			return;
145 		}
146 		logger.debug("Using finder");
147 		Finder finder = new Finder();
148 		try {
149 			acr = finder.find(startIfNotRunning,true);
150 		} catch (ACRException e1) {
151 			logger.error("ACR failed to start - setting up retry");
152 			setupRetry();
153 			return;
154 		}
155 
156 		try {
157 			Shutdown shutdown = (Shutdown) acr.getService(Shutdown.class);
158 			shutdown.addShutdownListener(this);
159 		} catch (ACRException e) {
160 			logger.error("Unable to register shutdown listener ", e);
161 		}
162 
163 		
164 		notifyObserversUp();
165 		return;
166 	}
167     
168     /* (non-Javadoc)
169      * @see org.votech.plastic.managers.AcrManager#connectWhenReady()
170      */
171     public void connectWhenReady() {
172         connect(false);
173     }
174 
175 	private ACR acr;
176 
177 	/* (non-Javadoc)
178 	 * @see org.votech.plastic.managers.Manager1#isConnected()
179 	 */
180 	/* (non-Javadoc)
181      * @see org.votech.plastic.managers.AcrManager#isConnected()
182      */
183 	public boolean isConnected() {
184 		return acr != null;
185 	}
186 
187 	Timer timer = new Timer();
188 
189 	/* (non-Javadoc)
190 	 * @see org.votech.plastic.managers.Manager1#disconnect()
191 	 */
192 	/* (non-Javadoc)
193      * @see org.votech.plastic.managers.AcrManager#disconnect()
194      */
195 	public void disconnect() {
196 		disconnect(-1);
197 	}
198 
199 	/* (non-Javadoc)
200 	 * @see org.votech.plastic.managers.Manager1#disconnect(int)
201 	 */
202 	/* (non-Javadoc)
203      * @see org.votech.plastic.managers.AcrManager#disconnect(int)
204      */
205 	public void disconnect(int newReconnectTime) {
206 		if (!isConnected())
207 			return;// don't want to bother people a second time
208 		this.autoReconnect = newReconnectTime;
209 		/*
210 		 * TODO talk to Noel about why this doesn't work try {
211 		 * logger.debug("Deregistering shutdown listener"); Shutdown shutdown =
212 		 * (Shutdown) acr.getService(Shutdown.class);
213 		 * //shutdown.removeShutdownListener(this);
214 		 * logger.debug("Deregistered"); } catch (ACRException e) {
215 		 * logger.error("Unable to deregister shutdown listener ",e); }
216 		 */
217 		acr = null;
218 		
219 		notifyObserversDown();
220 		if (autoReconnect >= 0) {
221 			setupRetry();
222 		}
223 	}
224 
225 	private void setupRetry() {
226 		logger.info("Attempting to reconnect in " + autoReconnect
227 				+ " millis");
228 		timer.schedule(new TimerTask() {
229 			public void run() {
230 				logger.debug("Attempt reconnection");
231 				connect(startIfNotRunning);
232 				if (isConnected()) notifyObserversUp();
233 			}
234 		}, autoReconnect);
235 	}
236 /*//put this idea on hold for now - problems seem to come from the service instance
237  * rather than the acr itself, so difficult to trap
238 	private class WrappedAcr implements ACR {
239 
240 		private ACR acr;
241 
242 		public WrappedAcr(ACR acr) {
243 			this.acr = acr;
244 		}
245 
246 		// Idea is that this will cause the manager to disconnect
247 		public Object getService(Class arg0) throws ACRException,
248 				InvalidArgumentException, NotFoundException {
249 			Object obj;
250 			try {
251 				obj = acr.getService(arg0);
252 			} catch (InvalidArgumentException e) {
253 				// TODO Auto-generated catch block
254 				e.printStackTrace();
255 				throw e;
256 			} catch (NotFoundException e) {
257 				// TODO Auto-generated catch block
258 				e.printStackTrace();
259 				throw e;
260 			} catch (ACRException e) {
261 				// TODO Auto-generated catch block
262 				e.printStackTrace();
263 				throw e;
264 			}
265 			return obj;
266 		}
267 
268 		public Object getService(String arg0) throws ACRException,
269 				InvalidArgumentException, NotFoundException {
270 			// TODO Auto-generated method stub
271 			return null;
272 		}
273 
274 	}
275 */
276 	public void halting() {
277 		logger.info("Going, going gone");
278 		disconnect(autoReconnect);
279 	}
280 
281 	public String lastChance() {
282 		logger.info("ACR is shutting down");
283 		return name;
284 	}
285 
286 
287 
288 
289 }