KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > pooka > NetworkConnection


1 package net.suberic.pooka;
2
3 import java.net.*;
4 import net.suberic.util.*;
5
6 /**
7  * <p>Represents a Network connection. This is primarily used to keep
8  * track of connections, so that Pooka will know whether or not to attempt
9  * to contact a particular server or not.</p>
10  *
11  * @author Allen Petersen
12  * @version $Revision: 1296 $
13  */

14 public class NetworkConnection implements net.suberic.util.Item {
15
16   String JavaDoc id = null;
17
18   String JavaDoc propertyName = null;
19
20   String JavaDoc connectCommand = null;
21
22   String JavaDoc disconnectCommand = null;
23
24   InetAddress testAddress = null;
25
26   int testPort = -1;
27
28   boolean disconnectRequested = false;
29
30   boolean disconnectCommandRequested = false;
31
32   int status = DISCONNECTED;
33
34   public static int CONNECTED = 0;
35   
36   public static int DISCONNECTED = 5;
37
38   public static int UNAVAILABLE = 10;
39
40   private java.util.LinkedList JavaDoc listenerList = new java.util.LinkedList JavaDoc();
41
42   private java.util.LinkedList JavaDoc lockList = new java.util.LinkedList JavaDoc();
43
44   /**
45    * <p>Creates a new NetworkConnection from the given property.</p>
46    */

47   public NetworkConnection (String JavaDoc newId) {
48     id = newId;
49     propertyName = "Connection." + newId;
50
51   }
52
53   /**
54    * <p>A command that should be run when this network connection is
55    * brought up.</p>
56    *
57    * <p>Returns <code>null</code> if no command needs to be run on
58    * connection.</p>
59    */

60   public String JavaDoc getConnectCommand() {
61     return connectCommand;
62   }
63
64   /**
65    * <p>Configures this conection.</p>
66    */

67   protected void configure() {
68     VariableBundle bundle = Pooka.getResources();
69
70     connectCommand = bundle.getProperty(getItemProperty() + ".connectCommand", "");
71     disconnectCommand = bundle.getProperty(getItemProperty() + ".disconnectCommand", "");
72
73     String JavaDoc testAddressString = bundle.getProperty(getItemProperty() + ".testAddress", "");
74     String JavaDoc testPortString = bundle.getProperty(getItemProperty() + ".testPort", "");
75     if (testAddressString != "" && testPortString != "") {
76       try {
77     testAddress = InetAddress.getByName(testAddressString);
78     testPort = Integer.parseInt(testPortString);
79       } catch (Exception JavaDoc e) {
80     testAddress = null;
81     testPort = -1;
82       }
83     }
84
85     String JavaDoc onStartup = bundle.getProperty(getItemProperty() + ".valueOnStartup", "Unavailable");
86
87     if (onStartup.equalsIgnoreCase("Connected")) {
88       this.connect();
89     } else if (onStartup.equalsIgnoreCase("Unavailable")) {
90       status = UNAVAILABLE;
91     }
92
93   }
94   
95   /**
96    * <p>A command that should be run when this network connection is
97    * brought down.</p>
98    *
99    * <p>Returns <code>null</code> if no command needs to be run on
100    * disconnection.</p>
101    */

102   public String JavaDoc getDisconnectCommand() {
103     return disconnectCommand;
104   }
105
106   /**
107    * <p>Connect to this network service.</p>
108    *
109    * @param runConnectCommand whether or not we should run the
110    * <code>connectCommand</code>, if there is one.
111    * @return the new status of the server.
112    */

113   public int connect(boolean runConnectCommand) {
114     return connect(runConnectCommand, false);
115   }
116
117   /**
118    * <p>Connect to this network service.</p>
119    *
120    * @param runConnectCommand whether or not we should run the
121    * <code>connectCommand</code>, if there is one.
122    * @param isInteractive whether or not we should prompt the user if
123    * the connection test fails after the connection.
124    * @return the new status of the server.
125    */

126   public int connect(boolean runConnectCommand, boolean isInteractive) {
127     
128     try {
129       if (runConnectCommand) {
130     String JavaDoc preCommand = getConnectCommand();
131     if (preCommand != null && preCommand.length() > 0) {
132       Process JavaDoc p = Runtime.getRuntime().exec(preCommand);
133       p.waitFor();
134     }
135       }
136
137       if (status != CONNECTED) {
138     boolean connectionSucceeded = checkConnection();
139     if (! connectionSucceeded && isInteractive) {
140       // check to see if we want to mark this as connected anyway.
141
int response = Pooka.getUIFactory().showConfirmDialog("Connection to test port " + testAddress.getHostAddress() + ":" + testPort + " failed. Mark this connection as unavailable?", "Test of " + getItemID() + " failed.", javax.swing.JOptionPane.YES_NO_OPTION);
142       if (response == javax.swing.JOptionPane.NO_OPTION)
143         connectionSucceeded = true;
144       else
145         status = UNAVAILABLE;
146     }
147
148     if (connectionSucceeded) {
149       status = CONNECTED;
150       fireConnectionEvent();
151     }
152       }
153     } catch (Exception JavaDoc ex) {
154       System.out.println("Could not run connect command:");
155       ex.printStackTrace();
156     }
157
158     return status;
159   }
160
161   /**
162    * <p>Connect to this network service.</p>
163    *
164    * @return the new status of the server.
165    */

166   public int connect() {
167     return connect(true);
168   }
169
170   /**
171    * <p>Disconnect from this network service.</p>
172    *
173    * @param runDisonnectCommand whether or not we should run the
174    * <code>disconnectCommand</code>, if there is one.
175    * @return the new status of the server.
176    */

177   public int disconnect(boolean runDisconnectCommand) {
178     synchronized(this) {
179       if (lockList.isEmpty()) {
180     return doDisconnect(runDisconnectCommand);
181       } else {
182     disconnectRequested = true;
183     if (runDisconnectCommand)
184       disconnectCommandRequested = true;
185
186     return status;
187       }
188       
189     }
190   }
191
192   private int doDisconnect(boolean runDisconnectCommand) {
193     try {
194       if (status != DISCONNECTED) {
195     if (runDisconnectCommand) {
196       String JavaDoc postCommand = getDisconnectCommand();
197       if (postCommand != null && postCommand.length() > 0) {
198         Process JavaDoc p = Runtime.getRuntime().exec(postCommand);
199         p.waitFor();
200       }
201     }
202     
203     status = DISCONNECTED;
204     fireConnectionEvent();
205       } else {
206       }
207     } catch (Exception JavaDoc ex) {
208       System.out.println("Could not run disconnect command:");
209       ex.printStackTrace();
210     }
211
212     return status;
213   }
214
215   /**
216    * <p>Disconnect from this network service.</p>
217    *
218    * @return the new status of the server.
219    */

220   public int disconnect() {
221     return disconnect(true);
222   }
223
224   /**
225    * <p>Checks this connection to see if it's actually up. This
226    * implementation uses the testAddress and testPort settings to open
227    * a TCP connection. This returns whether or not the connection
228    * succeeds.
229    */

230   public boolean checkConnection() {
231     if (testAddress != null && testPort > -1) {
232       try {
233     Socket testSocket = new Socket(testAddress, testPort);
234     testSocket.close();
235     return true;
236       } catch (Exception JavaDoc e) {
237     return false;
238       }
239     }
240
241     // if there's no test case, assume that we're ok.
242
return true;
243   }
244
245   /**
246    * <p>Mark this network service as unavailable. Note that if there
247    * is a disconnectCommand, this does <em>not</em> run it.</p>
248    *
249    * @return the new status of the server.
250    */

251   public int makeUnavailable() {
252     if (status != UNAVAILABLE) {
253       status = UNAVAILABLE;
254       fireConnectionEvent();
255     }
256     return status;
257   }
258
259   /**
260    * <p>Returns the current status of this NetworkConnection.</p>
261    */

262   public int getStatus() {
263     return status;
264   }
265
266   /**
267    * <p>Gets an ConnectionLock for this NetworkConnection.
268    */

269   public synchronized ConnectionLock getConnectionLock() {
270     if (getStatus() == CONNECTED) {
271       ConnectionLock cl = new ConnectionLock();
272       lockList.add(cl);
273       return cl;
274     } else
275       return null;
276   }
277
278   /**
279    * <p>Releases the given ConnectionLock.
280    */

281   public synchronized void releaseLock(ConnectionLock cl) {
282     lockList.remove(cl);
283     if (lockList.isEmpty() && disconnectRequested) {
284       doDisconnect(disconnectCommandRequested);
285       disconnectRequested = false;
286       disconnectRequested = false;
287     }
288   }
289
290   /**
291    * Notifies all listeners that the Network connection status has
292    * changed.
293    */

294   public void fireConnectionEvent() {
295     for (int i = 0; i < listenerList.size(); i++) {
296       ((NetworkConnectionListener) listenerList.get(i)).connectionStatusChanged(this, getStatus());
297     }
298   }
299
300   /**
301    * Adds a NetworkConnectionListener to the listener list.
302    */

303   public void addConnectionListener(NetworkConnectionListener newListener) {
304     if (!listenerList.contains(newListener))
305       listenerList.add(newListener);
306   }
307
308   /**
309    * Removes a NetworkConnectionListener from the listener list.
310    */

311   public void removeConnectionListener(NetworkConnectionListener oldListener) {
312     if (listenerList.contains(oldListener))
313       listenerList.remove(oldListener);
314   }
315
316   /**
317    * <p>The Item ID for this NetworkConnection.</p>
318    */

319   public String JavaDoc getItemID() {
320     return id;
321   }
322
323   /**
324    * <p>The Item property for this NetworkConnection. This is usually
325    * Connection.<i>itemID</i>.</p>
326    */

327   public String JavaDoc getItemProperty() {
328     return propertyName;
329   }
330
331   /**
332    * Returns the ItemID of this NetworkConnection.
333    */

334   public String JavaDoc toString() {
335     return getItemID();
336   }
337
338   /**
339    * an object that represents a lock on this connection. useful if you
340    * have several threads depending on having this connection stay open.
341    */

342   public class ConnectionLock {
343     public ConnectionLock() {
344       
345     }
346
347     /**
348      * Releases this lock.
349      */

350     public void release() {
351       releaseLock(this);
352     }
353   }
354 }
355
Popular Tags