1 19 package org.java.plugin.boot; 20 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.io.OutputStream ; 24 import java.net.InetAddress ; 25 import java.net.ServerSocket ; 26 import java.net.Socket ; 27 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 31 34 final class ControlThread extends Thread { 35 static boolean isApplicationRunning(final InetAddress host, 36 final int port) { 37 try { 38 Socket socket = new Socket (host, port); 39 try { 40 socket.setKeepAlive(true); 41 String test = "" + System.currentTimeMillis(); OutputStream out = socket.getOutputStream(); 43 InputStream in = null; 44 try { 45 System.out.println("found running control service on " + host + ":" + port); out.write(("PING " + test).getBytes()); out.flush(); 49 socket.shutdownOutput(); 50 in = socket.getInputStream(); 51 StringBuffer commandResult = new StringBuffer (); 52 byte[] buf = new byte[16]; 53 int len; 54 while ((len = in.read(buf)) != -1) { 55 commandResult.append(new String (buf, 0, len)); 56 } 57 socket.shutdownInput(); 58 if (commandResult.toString().startsWith("OK") && (commandResult.toString().indexOf(test) != -1)) { 60 System.out.println("PING command succeed"); return true; 62 } 63 System.out.println("PING command failed"); } finally { 65 try { 66 out.close(); 67 } catch (IOException ioe) { 68 } 70 if (in != null) { 71 try { 72 in.close(); 73 } catch (IOException ioe) { 74 } 76 } 77 } 78 } finally { 79 socket.close(); 80 } 81 } catch (IOException ioe) { 82 System.out.println( 83 "seems that there is no control service running on " + host + ":" + port); } 87 return false; 88 } 89 90 static boolean stopRunningApplication(final InetAddress host, 91 final int port) { 92 boolean result = false; 93 try { 94 Socket socket = new Socket (host, port); 95 try { 96 socket.setKeepAlive(true); 97 OutputStream out = socket.getOutputStream(); 98 InputStream in = null; 99 try { 100 System.out.println("found running control service on " + host + ":" + port); out.write("STOP".getBytes()); out.flush(); 104 socket.shutdownOutput(); 105 in = socket.getInputStream(); 106 StringBuffer commandResult = new StringBuffer (); 107 byte[] buf = new byte[16]; 108 int len; 109 while ((len = in.read(buf)) != -1) { 110 commandResult.append(new String (buf, 0, len)); 111 } 112 socket.shutdownInput(); 113 if (commandResult.toString().startsWith("OK")) { System.out.println("STOP command succeed"); result = true; 116 } else { 117 System.out.println("STOP command failed"); } 119 } finally { 120 try { 121 out.close(); 122 } catch (IOException ioe) { 123 } 125 if (in != null) { 126 try { 127 in.close(); 128 } catch (IOException ioe) { 129 } 131 } 132 } 133 } finally { 134 socket.close(); 135 } 136 } catch (IOException ioe) { 137 System.out.println( 138 "seems that there is no control service running on " + host + ":" + port); } 142 if (result) { 143 try { 144 Thread.sleep(2000); 145 } catch (InterruptedException ie) { 146 } 148 } 149 return result; 150 } 151 152 private Log log; 153 154 private ServerSocket serverSocket; 155 private final ServiceApplication application; 156 private boolean appRunning; 157 158 ControlThread(final InetAddress host, final int port, 159 final ServiceApplication server) throws Exception { 160 log = LogFactory.getLog(this.getClass()); 161 application = server; 162 serverSocket = new ServerSocket (port, 1, host); 163 appRunning = true; 164 setName("jpf-application-control-thread"); } 166 167 170 public void run() { 171 try { 172 while (true) { 173 try { 174 Socket clientSocket = serverSocket.accept(); 175 try { 176 if (handleRequest(clientSocket)) { 177 break; 178 } 179 } finally { 180 try { 181 clientSocket.close(); 182 } catch (IOException ioe) { 183 } 185 } 186 } catch (Exception e) { 187 warn("error on server socket", e); break; 189 } 190 } 191 } catch (Exception e) { 192 error(e); 193 } finally { 194 try { 195 serverSocket.close(); 196 } catch (IOException ioe) { 197 warn("error closing server socket", ioe); } 199 if (appRunning) { 200 stopApplication(); 201 } 202 } 203 } 204 205 private synchronized boolean handleRequest(final Socket clientSocket) { 206 debug("handling control request"); if (!isValidRemoteHost(clientSocket.getInetAddress())) { 208 warn("incoming connection to control socket registered" + " from REMOTE address " + clientSocket.getInetAddress() + ", attempt to execute command was IGNORED"); try { 212 clientSocket.close(); 213 } catch (IOException e) { 214 } 216 return false; 217 } 218 debug("processing control request"); boolean result = false; 220 try { 221 String commandResult; 222 InputStream in = clientSocket.getInputStream(); 223 OutputStream out = null; 224 try { 225 StringBuffer command = new StringBuffer (); 226 byte[] buf = new byte[16]; 227 int len; 228 while ((len = in.read(buf)) != -1) { 229 command.append(new String (buf, 0, len)); 230 } 231 clientSocket.shutdownInput(); 232 debug("got command - " + command); if ("STOP".equals(command.toString())) { stopApplication(); 235 result = true; 236 commandResult = "OK: stop done"; } else if (command.toString().startsWith("PING")) { commandResult = "OK: " + command.substring("PING".length()); } else { 241 commandResult = "ERROR: unknown command"; } 243 out = clientSocket.getOutputStream(); 246 out.write(commandResult.getBytes()); 247 out.flush(); 248 clientSocket.shutdownOutput(); 249 } finally { 251 try { 252 in.close(); 253 } catch (IOException ioe) { 254 } 256 if (out != null) { 257 try { 258 out.close(); 259 } catch (IOException ioe) { 260 } 262 } 263 } 264 } catch (IOException ioe) { 265 error("error processing control request", ioe); } 267 return result; 268 } 269 270 private void stopApplication() { 271 if (!appRunning) { 272 debug("application not running"); return; 274 } 275 appRunning = false; 276 debug("stopping application"); try { 278 Boot.stopApplication(application); 279 log = null; 280 } catch (Exception e) { 281 error("an error has occurred while stopping" + " application", e); } 284 debug("application stopped from control thread"); } 286 287 private boolean isValidRemoteHost(final InetAddress addr) { 288 byte[] localAddr = serverSocket.getInetAddress().getAddress(); 289 byte[] remoteAddr = addr.getAddress(); 290 if (localAddr.length != remoteAddr.length) { 291 return false; 292 } 293 for (int i = 0; i < remoteAddr.length; i++) { 294 if (localAddr[i] != remoteAddr[i]) { 295 return false; 296 } 297 } 298 return true; 299 } 300 301 private void debug(final String msg) { 302 if (log != null) { 303 log.debug(msg); 304 } else { 305 System.out.println(msg); 306 } 307 } 308 309 private void warn(final String msg) { 310 if (log != null) { 311 log.warn(msg); 312 } else { 313 System.out.println(msg); 314 } 315 } 316 317 private void warn(final String msg, final Exception e) { 318 if (log != null) { 319 log.warn(msg, e); 320 } else { 321 System.out.println(msg); 322 e.printStackTrace(); 323 } 324 } 325 326 private void error(final String msg, final Exception e) { 327 if (log != null) { 328 log.error(msg, e); 329 } else { 330 System.err.println(msg); 331 e.printStackTrace(); 332 } 333 } 334 335 private void error(final Exception e) { 336 if (log != null) { 337 log.error(e); 338 } else { 339 e.printStackTrace(); 340 } 341 } 342 } 343 | Popular Tags |