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