1 20 package fr.dyade.aaa.agent; 21 22 import java.io.*; 23 import java.net.*; 24 import java.util.*; 25 26 import org.objectweb.util.monolog.api.BasicLevel; 27 import org.objectweb.util.monolog.api.Logger; 28 29 import fr.dyade.aaa.agent.*; 30 31 public class SCAdminHelper { 32 33 protected Hashtable ASP = null; 34 35 protected Logger logmon = null; 36 37 public SCAdminHelper() { 38 logmon = Debug.getLogger("fr.dyade.aaa.agent.SCAdmin"); 40 41 ASP = new Hashtable(); 42 } 43 44 49 public String startAgentServer(short sid) throws Exception { 50 return startAgentServer(sid, null, null); 51 } 52 53 59 public String startAgentServer(short sid, 60 String [] jvmarg) throws Exception { 61 return startAgentServer(sid, null, jvmarg, null); 62 } 63 64 72 public String startAgentServer(short sid, 73 File dir, 74 String [] jvmarg) throws Exception { 75 return startAgentServer(sid, dir, jvmarg, 76 "fr.dyade.aaa.agent.AgentServer", null); 77 } 78 79 public String startAgentServer(short sid, 80 File dir, 81 String [] jvmarg, 82 String [] servarg) throws Exception { 83 return startAgentServer(sid, dir, jvmarg, 84 "fr.dyade.aaa.agent.AgentServer", servarg); 85 } 86 87 97 public String startAgentServer(short sid, 98 File dir, 99 String [] jvmarg, 100 String className, 101 String [] servarg) throws Exception { 102 logmon.log(BasicLevel.DEBUG, 103 "SCAdmin: start AgentServer#" + sid); 104 105 Process p = (Process ) ASP.get(new Short (sid)); 106 if (p != null) { 107 try { 108 logmon.log(BasicLevel.DEBUG, 109 "SCAdmin: AgentServer#" + sid + " -> " + p.exitValue()); 110 } catch (IllegalThreadStateException exc) { 111 logmon.log(BasicLevel.WARN, 113 "SCAdmin: AgentServer#" + sid + " already running."); 114 throw new IllegalStateException ("AgentServer#" + sid + 115 " already running."); 116 } 117 } 118 119 String javapath = 120 new File(new File(System.getProperty("java.home"), "bin"), 121 "java").getPath(); 122 String classpath = System.getProperty("java.class.path"); 123 124 Vector argv = new Vector(); 125 argv.addElement(javapath); 126 argv.addElement("-classpath"); 127 argv.addElement(classpath); 128 if (jvmarg != null) { 129 for (int i=0; i<jvmarg.length; i++) 130 argv.addElement(jvmarg[i]); 131 } 132 argv.addElement(className); 133 argv.addElement(Short.toString(sid)); 134 argv.addElement("s" + sid); 135 if (servarg != null) { 136 for (int i=0; i<servarg.length; i++) 137 argv.addElement(servarg[i]); 138 } 139 140 String [] command = new String [argv.size()]; 141 argv.copyInto(command); 142 143 logmon.log(BasicLevel.DEBUG, 144 "SCAdmin" + ": starts AgentServer#" + sid); 145 if (dir == null) { 146 p = Runtime.getRuntime().exec(command); 147 } else { 148 p = Runtime.getRuntime().exec(command, null, dir); 149 } 150 ASP.put(new Short (sid), p); 151 BufferedReader br = 152 new BufferedReader(new InputStreamReader(p.getInputStream())); 153 String line = br.readLine(); 154 if (line != null) { 155 if (line.endsWith(AgentServer.ERRORSTRING)) { 156 StringBuffer strBuf = new StringBuffer (); 157 strBuf.append(line); 158 while (((line = br.readLine()) != null) && 159 (! line.equals(AgentServer.ENDSTRING))) { 160 strBuf.append('\n'); 161 strBuf.append(line); 162 } 163 line = strBuf.toString(); 164 } 165 } 166 try { 169 p.getInputStream().close(); 170 } catch (Exception exc) {} 171 try { 172 p.getOutputStream().close(); 173 } catch (Exception exc) {} 174 try { 175 p.getErrorStream().close(); 176 } catch (Exception exc) {} 177 return line; 178 } 179 180 185 public void killAgentServer(short sid) throws Exception { 186 Process p = (Process ) ASP.get(new Short (sid)); 187 188 logmon.log(BasicLevel.DEBUG, 189 "SCAdmin: kill AgentServer#" + sid + " [" + p + ']'); 190 191 if (p != null) p.destroy(); 192 } 193 194 202 public int joinAgentServer(short sid) throws Exception { 203 Process p = (Process ) ASP.get(new Short (sid)); 204 205 logmon.log(BasicLevel.DEBUG, 206 "SCAdmin: join AgentServer#" + sid + " [" + p + ']'); 207 208 if (p != null) { 210 return p.waitFor(); 211 } else { 212 throw new UnknownServerException(); 213 } 214 } 215 216 226 public int exitValue(short sid) 227 throws IllegalThreadStateException , UnknownServerException { 228 Process p = (Process )ASP.get(new Short (sid)); 229 if (p != null) { 230 int res = p.exitValue(); 231 return res; 232 } else { 233 throw new UnknownServerException(); 234 } 235 } 236 237 242 public void destroyAgentServer(short sid) throws Exception { 243 Short key = new Short (sid); 244 Process p = (Process )ASP.get(key); 245 if (p != null) { 246 ASP.remove(key); 247 p.destroy(); 248 } 249 } 250 251 257 public void stopAgentServer(short sid, int port) throws Exception { 258 stopAgentServer(sid, "localhost", port); 259 } 260 261 268 public void stopAgentServer(short sid, String host, int port) throws Exception { 269 Socket socket = null; 270 271 logmon.log(BasicLevel.DEBUG, "SCAdmin: stop AgentServer#" + sid); 272 273 try { 274 socket = new Socket(host, port); 275 socket.getOutputStream().write(AdminProxy.STOP_SERVER.getBytes()); 276 socket.getOutputStream().write('\n'); 277 socket.getOutputStream().flush(); 278 try { 279 socket.getInputStream().read(); 280 } catch (SocketException exc) { 281 } 283 } catch (Throwable exc) { 284 if (logmon.isLoggable(BasicLevel.DEBUG)) 285 logmon.log(BasicLevel.DEBUG, 286 "SCAdmin: Can't stop server#" + sid, exc); 287 throw new Exception ("Can't stop server#" + sid + 288 ": " + exc.getMessage()); 289 } finally { 290 close(socket); 291 socket = null; 292 } 293 } 294 295 301 public void crashAgentServer(short sid, int port) throws Exception { 302 crashAgentServer(sid, "localhost", port); 303 } 304 305 312 public void crashAgentServer(short sid, String host, int port) throws Exception { 313 Socket socket = null; 314 315 logmon.log(BasicLevel.DEBUG, "SCAdmin: crash AgentServer#" + sid); 316 317 try { 318 socket = new Socket(host, port); 319 socket.getOutputStream().write(AdminProxy.CRASH_SERVER.getBytes()); 320 socket.getOutputStream().write('\n'); 321 socket.getOutputStream().flush(); 322 try { 323 socket.getInputStream().read(); 324 } catch (SocketException exc) { 325 } 327 } catch (Throwable exc) { 328 if (logmon.isLoggable(BasicLevel.DEBUG)) 329 logmon.log(BasicLevel.DEBUG, 330 "SCAdmin: Can't crash server#" + sid, exc); 331 throw new Exception ("Can't crash server#" + sid + 332 ": " + exc.getMessage()); 333 } finally { 334 close(socket); 335 socket = null; 336 } 337 } 338 339 static void close(Socket socket) { 340 try { 341 socket.getInputStream().close(); 342 } catch (Exception exc) {} 343 try { 344 socket.getOutputStream().close(); 345 } catch (Exception exc) {} 346 try { 347 socket.close(); 348 } catch (Exception exc) {} 349 } 350 } 351 | Popular Tags |