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
89
90
91
92 System.exit(0);
93 }
94
95
96
97
98
99 public ACR getAcr() {
100 return acr;
101 }
102
103
104
105
106 public void connect() {
107 connect(true);
108 }
109
110
111
112
113
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
145
146
147 public void connectWhenReady() {
148 connect(false);
149 }
150
151 private ACR acr;
152
153
154
155
156
157
158
159 public boolean isConnected() {
160 return acr != null;
161 }
162
163 Timer timer = new Timer();
164
165
166
167
168
169
170
171 public void disconnect() {
172 disconnect(-1);
173 }
174
175
176
177
178
179
180
181 public void disconnect(int newReconnectTime) {
182 if (!isConnected())
183 return;
184 this.autoReconnect = newReconnectTime;
185
186
187
188
189
190
191
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
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 }