1 20 package org.apache.cactus.integration.ant.container; 21 22 import java.io.IOException ; 23 import java.net.ServerSocket ; 24 import java.net.Socket ; 25 import java.util.Vector ; 26 27 35 public abstract class AbstractServerRun extends Thread 36 { 37 40 private int port = 7777; 41 42 46 private String host = "127.0.0.1"; 47 48 51 private String [] args; 52 53 57 private boolean isStarted = false; 58 59 62 private Thread runningServerThread; 63 64 67 public AbstractServerRun(String [] theArgs) 68 { 69 this.args = theArgs; 70 } 71 72 79 protected abstract Thread doStartServer(String [] theArgs) throws Exception ; 80 81 91 protected abstract void doStopServer(String [] theArgs, 92 Thread theRunningServerThread) throws Exception ; 93 94 97 protected final void doRun() 98 { 99 boolean isStart = true; 101 Vector newArgs = new Vector (); 102 103 for (int i = 0; i < this.args.length; i++) 104 { 105 if (this.args[i].equalsIgnoreCase("-start")) 106 { 107 isStart = true; 108 } 109 else if (this.args[i].equalsIgnoreCase("-stop")) 110 { 111 isStart = false; 112 } 113 else if (this.args[i].equalsIgnoreCase("-port")) 114 { 115 this.port = Integer.parseInt(this.args[i + 1]); 116 i++; 117 } 118 else 119 { 120 newArgs.add(this.args[i]); 121 } 122 } 123 124 String [] strArgs = new String [0]; 127 128 this.args = (String []) newArgs.toArray(strArgs); 129 130 if (isStart) 131 { 132 startServer(); 133 } 134 else 135 { 136 stopServer(); 137 } 138 } 139 140 143 private void startServer() 144 { 145 if (this.isStarted) 147 { 148 return; 149 } 150 151 try 152 { 153 this.runningServerThread = doStartServer(this.args); 154 } 155 catch (Exception e) 156 { 157 e.printStackTrace(); 158 throw new RuntimeException ("Error starting server"); 159 } 160 161 this.isStarted = true; 163 164 start(); 166 } 167 168 171 private void stopServer() 172 { 173 Socket clientSocket = null; 175 176 try 177 { 178 clientSocket = new Socket (this.host, this.port); 179 } 180 catch (Exception e) 181 { 182 e.printStackTrace(); 183 throw new RuntimeException ("Error opening socket to " + this.host 184 + ":" + this.port + "]"); 185 } 186 finally 187 { 188 try 189 { 190 if (clientSocket != null) 191 { 192 clientSocket.close(); 193 } 194 } 195 catch (IOException e) 196 { 197 throw new RuntimeException ("Cannot close client socket"); 198 } 199 } 200 } 201 202 206 public void run() 207 { 208 ServerSocket serverSocket = setUpListenerSocket(); 209 210 try 212 { 213 serverSocket.accept(); 214 } 215 catch (IOException e) 216 { 217 throw new RuntimeException ("Error accepting connection for " 218 + "server socket [" + serverSocket + "]"); 219 } 220 finally 221 { 222 try 224 { 225 serverSocket.close(); 226 } 227 catch (IOException e) 228 { 229 throw new RuntimeException ("Cannot close server socket [" 230 + serverSocket + "]"); 231 } 232 } 233 234 try 236 { 237 this.doStopServer(this.args, this.runningServerThread); 238 } 239 catch (Exception e) 240 { 241 e.printStackTrace(); 242 throw new RuntimeException ("Cannot stop server"); 243 } 244 245 try 247 { 248 serverSocket.close(); 249 } 250 catch (IOException e) 251 { 252 throw new RuntimeException ("Cannot close server socket [" 253 + serverSocket + "]"); 254 } 255 } 256 257 262 private ServerSocket setUpListenerSocket() 263 { 264 ServerSocket serverSocket = null; 265 266 try 267 { 268 serverSocket = new ServerSocket (this.port); 269 } 270 catch (IOException e) 271 { 272 e.printStackTrace(); 273 throw new RuntimeException ("Error setting up the server " 274 + "listener socket"); 275 } 276 277 return serverSocket; 278 } 279 } 280 | Popular Tags |