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
113
114
115
116 System.exit(0);
117 }
118
119
120
121
122
123 public ACR getAcr() {
124 return acr;
125 }
126
127
128
129
130 public void connect() {
131 connect(true);
132 }
133
134
135
136
137
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
169
170
171 public void connectWhenReady() {
172 connect(false);
173 }
174
175 private ACR acr;
176
177
178
179
180
181
182
183 public boolean isConnected() {
184 return acr != null;
185 }
186
187 Timer timer = new Timer();
188
189
190
191
192
193
194
195 public void disconnect() {
196 disconnect(-1);
197 }
198
199
200
201
202
203
204
205 public void disconnect(int newReconnectTime) {
206 if (!isConnected())
207 return;
208 this.autoReconnect = newReconnectTime;
209
210
211
212
213
214
215
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
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 }