1 package net.suberic.pooka; 2 3 import java.net.*; 4 import net.suberic.util.*; 5 6 14 public class NetworkConnection implements net.suberic.util.Item { 15 16 String id = null; 17 18 String propertyName = null; 19 20 String connectCommand = null; 21 22 String 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 listenerList = new java.util.LinkedList (); 41 42 private java.util.LinkedList lockList = new java.util.LinkedList (); 43 44 47 public NetworkConnection (String newId) { 48 id = newId; 49 propertyName = "Connection." + newId; 50 51 } 52 53 60 public String getConnectCommand() { 61 return connectCommand; 62 } 63 64 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 testAddressString = bundle.getProperty(getItemProperty() + ".testAddress", ""); 74 String testPortString = bundle.getProperty(getItemProperty() + ".testPort", ""); 75 if (testAddressString != "" && testPortString != "") { 76 try { 77 testAddress = InetAddress.getByName(testAddressString); 78 testPort = Integer.parseInt(testPortString); 79 } catch (Exception e) { 80 testAddress = null; 81 testPort = -1; 82 } 83 } 84 85 String 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 102 public String getDisconnectCommand() { 103 return disconnectCommand; 104 } 105 106 113 public int connect(boolean runConnectCommand) { 114 return connect(runConnectCommand, false); 115 } 116 117 126 public int connect(boolean runConnectCommand, boolean isInteractive) { 127 128 try { 129 if (runConnectCommand) { 130 String preCommand = getConnectCommand(); 131 if (preCommand != null && preCommand.length() > 0) { 132 Process p = Runtime.getRuntime().exec(preCommand); 133 p.waitFor(); 134 } 135 } 136 137 if (status != CONNECTED) { 138 boolean connectionSucceeded = checkConnection(); 139 if (! connectionSucceeded && isInteractive) { 140 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 ex) { 154 System.out.println("Could not run connect command:"); 155 ex.printStackTrace(); 156 } 157 158 return status; 159 } 160 161 166 public int connect() { 167 return connect(true); 168 } 169 170 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 postCommand = getDisconnectCommand(); 197 if (postCommand != null && postCommand.length() > 0) { 198 Process p = Runtime.getRuntime().exec(postCommand); 199 p.waitFor(); 200 } 201 } 202 203 status = DISCONNECTED; 204 fireConnectionEvent(); 205 } else { 206 } 207 } catch (Exception ex) { 208 System.out.println("Could not run disconnect command:"); 209 ex.printStackTrace(); 210 } 211 212 return status; 213 } 214 215 220 public int disconnect() { 221 return disconnect(true); 222 } 223 224 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 e) { 237 return false; 238 } 239 } 240 241 return true; 243 } 244 245 251 public int makeUnavailable() { 252 if (status != UNAVAILABLE) { 253 status = UNAVAILABLE; 254 fireConnectionEvent(); 255 } 256 return status; 257 } 258 259 262 public int getStatus() { 263 return status; 264 } 265 266 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 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 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 303 public void addConnectionListener(NetworkConnectionListener newListener) { 304 if (!listenerList.contains(newListener)) 305 listenerList.add(newListener); 306 } 307 308 311 public void removeConnectionListener(NetworkConnectionListener oldListener) { 312 if (listenerList.contains(oldListener)) 313 listenerList.remove(oldListener); 314 } 315 316 319 public String getItemID() { 320 return id; 321 } 322 323 327 public String getItemProperty() { 328 return propertyName; 329 } 330 331 334 public String toString() { 335 return getItemID(); 336 } 337 338 342 public class ConnectionLock { 343 public ConnectionLock() { 344 345 } 346 347 350 public void release() { 351 releaseLock(this); 352 } 353 } 354 } 355 | Popular Tags |