1 21 package fr.dyade.aaa.agent; 22 23 import java.io.*; 24 import java.net.*; 25 import java.text.*; 26 import java.util.*; 27 import java.lang.reflect.*; 28 29 import org.objectweb.util.monolog.api.BasicLevel; 30 import org.objectweb.util.monolog.api.Logger; 31 32 import fr.dyade.aaa.util.Daemon; 33 import fr.dyade.aaa.agent.conf.A3CML; 34 import fr.dyade.aaa.agent.conf.A3CMLConfig; 35 36 45 public class AdminProxy { 46 47 static AdminProxy proxy = null; 48 49 public static boolean debug = true; 50 51 52 public final static String LISTENPORT = "fr.dyade.aaa.agent.AdminProxy.port"; 53 54 private static int port = 8091; 55 56 private static int nbm; 57 58 public final static String NBMONITOR = "fr.dyade.aaa.agent.AdminProxy.nbm"; 59 60 62 AdminMonitor monitors[] = null; 63 ServerSocket listen = null; 64 65 static Logger xlogmon = null; 66 67 75 public static void init(String args, boolean firstTime) throws Exception { 76 try { 77 if (args.length()!=0) { 78 port = Integer.parseInt(args); 79 } else { 80 port = Integer.parseInt(AgentServer.getProperty(LISTENPORT, "8091")); 81 } 82 } catch (NumberFormatException exc) { 83 port = 8091; 84 } 85 86 try { 87 nbm = Integer.parseInt(AgentServer.getProperty(NBMONITOR, "1")); 88 } catch (NumberFormatException exc) { 89 nbm = 1; 90 } 91 92 xlogmon = Debug.getLogger(Debug.A3Service + ".AdminProxy" + 94 ".#" + AgentServer.getServerId()); 95 96 if (proxy != null) { 97 xlogmon.log(BasicLevel.ERROR, 98 "AdminProxy#" + AgentServer.getServerId() + 99 ": already initialized."); 100 throw new Exception ("AdminProxy" + ".#" + AgentServer.getServerId() + 101 ": already initialized."); 102 } 103 104 try { 105 proxy = new AdminProxy(); 106 } catch (IOException exc) { 107 xlogmon.log(BasicLevel.ERROR, 108 "AdminProxy#" + AgentServer.getServerId() + 109 ", can't get listen port", exc); 110 throw exc; 111 } 112 start(); 113 } 114 115 120 private AdminProxy() throws IOException { 121 for (int i=0; ; i++) { 122 try { 123 listen = new ServerSocket(port); 124 break; 125 } catch (BindException exc) { 126 if (i > 5) throw exc; 127 try { 128 Thread.sleep(i * 500); 130 } catch (InterruptedException e) {} 131 } 132 } 133 134 136 monitors = new AdminMonitor[nbm]; 137 for (int i=0; i<monitors.length; i++) { 138 monitors[i] = new AdminMonitor("AdminProxy#" + 139 AgentServer.getServerId() + '.' + i); 140 } 141 } 142 143 public static void start() { 144 for (int i=0; i<proxy.monitors.length; i++) { 145 proxy.monitors[i].start(); 146 } 147 } 148 149 public static void stopService() { 150 for (int i=0; i<proxy.monitors.length; i++) { 151 if (proxy.monitors[i] != null) proxy.monitors[i].stop(); 152 proxy.monitors[i] = null; 153 } 154 proxy = null; 155 } 156 157 static final String HELP = "help"; 158 static final String NONE = ""; 159 160 public static final String STOP_SERVER = "halt"; 162 public static final String CRASH_SERVER = "crash"; 163 public static final String PING = "ping"; 164 public static final String CONFIG = "config"; 165 166 static final String SET_VARIABLE = "set"; 168 static final String GET_VARIABLE = "get"; 169 170 static final String GC = "gc"; 172 static final String THREADS = "threads"; 173 174 static final String LIST_MCONS = "consumers"; 176 static final String START_MCONS = "start"; 177 static final String STOP_MCONS = "stop"; 178 179 static final String LIST_SERVICE = "services"; 181 static final String ADD_SERVICE = "add"; 182 static final String REMOVE_SERVICE = "remove"; 183 184 static final String DUMP = "dump"; 186 187 public static final String LOG = "log"; 189 190 193 public String toString() { 194 StringBuffer strBuf = new StringBuffer (); 195 196 strBuf.append("(").append(super.toString()); 197 strBuf.append(",port=").append(port); 198 strBuf.append(",monitors=["); 199 for (int i=0; i<monitors.length; i++) { 200 strBuf.append(monitors[i].toString()).append(","); 201 } 202 strBuf.append("]"); 203 strBuf.append(")"); 204 205 return strBuf.toString(); 206 } 207 208 class AdminMonitor extends Daemon { 209 Socket socket = null; 210 BufferedReader reader = null; 211 PrintWriter writer = null; 212 213 216 protected AdminMonitor(String name) { 217 super(name, AdminProxy.xlogmon); 219 this.setThreadGroup(AgentServer.getThreadGroup()); 220 } 221 222 227 public String toString() { 228 return "(" + super.toString() + 229 ",socket=" + socket + ")"; 230 } 231 232 public void run() { 233 try { 234 while (running) { 235 canStop = true; 236 try { 237 logmon.log(BasicLevel.DEBUG, getName() + ", waiting: " + listen); 238 socket = listen.accept(); 239 logmon.log(BasicLevel.DEBUG, getName() + ", receiving."); 240 canStop = false; 241 } catch (IOException exc) { 242 if (running) 243 logmon.log(BasicLevel.ERROR, 244 getName() + ", error during accept", exc); 245 } 246 247 if (! running) break; 248 249 try { 250 reader = new BufferedReader( 252 new InputStreamReader(socket.getInputStream())); 253 writer = new PrintWriter(socket.getOutputStream(), true); 254 255 doRequest(reader.readLine()); 257 258 writer.flush(); 259 } catch (Exception exc) { 260 logmon.log(BasicLevel.ERROR, 261 getName() + ", error during connection", exc); 262 } finally { 263 try { 265 reader.close(); 266 } catch (Exception exc) {} 267 reader = null; 268 try { 269 writer.close(); 270 } catch (Exception exc) {} 271 writer = null; 272 try { 273 socket.close(); 274 } catch (Exception exc) {} 275 socket = null; 276 } 277 } 278 } finally { 279 logmon.log(BasicLevel.DEBUG, getName() + ", finishing."); 280 finish(); 281 } 282 } 283 284 protected void close() { 285 try { 286 logmon.log(BasicLevel.DEBUG, getName() + ", closing: " + listen); 287 listen.close(); 288 } catch (Exception exc) {} 289 listen = null; 290 } 291 292 protected void shutdown() { 293 logmon.log(BasicLevel.DEBUG, getName() + ", close(): "); 294 close(); 295 } 296 297 public void doRequest(String request) { 298 String cmd = null; 299 300 logmon.log(BasicLevel.DEBUG, getName() + ", request=" + request); 301 302 try { 303 StringTokenizer st = new StringTokenizer(request); 305 306 cmd = st.nextToken(); 307 if (cmd.equals(STOP_SERVER)) { 308 AgentServer.stop(false); 310 logmon.log(BasicLevel.WARN, getName() + ", bye."); 311 } else if (cmd.equals(CRASH_SERVER)) { 312 logmon.log(BasicLevel.WARN, getName() + ", crash!"); 314 System.exit(0); 315 } else if (cmd.equals(GC)) { 316 Runtime runtime = Runtime.getRuntime(); 317 writer.println("before: " + 318 runtime.freeMemory() + " octets free / " + 319 runtime.totalMemory() + " octets."); 320 runtime.gc(); 321 writer.println("after: " + 322 runtime.freeMemory() + " octets free / " + 323 runtime.totalMemory() + " octets."); 324 } else if (cmd.equals(SET_VARIABLE)) { 325 try { 326 if (st.countTokens() != 2) 327 throw new Exception ("Usage: set property value"); 328 329 String property = st.nextToken(); 330 String value = st.nextToken(); 331 332 int pindex = property.lastIndexOf('.'); 334 if (pindex == -1) { 335 throw new Exception ("bad formed property name: " + property); 337 } 338 String varClassName = property.substring(0, pindex); 339 String varName = property.substring(pindex + 1); 340 341 try { 342 Class varClass = Class.forName(varClassName); 344 Field var = varClass.getDeclaredField(varName); 345 String varType = var.getType().getName(); 347 if (varType.equals("boolean") || 348 varType.equals("java.lang.Boolean")) { 349 var.set(null, new Boolean (value)); 350 } else if (varType.equals("int") || 351 varType.equals("java.lang.Integer")) { 352 var.set(null, new Integer (value)); 353 } else if (varType.equals("java.lang.String")) { 354 var.set(null, value); 355 } else { 356 throw new Exception ("error setting property " + 357 varClassName + "." + varName + 358 ": unexpected type " + varType); 359 } 360 } catch (Exception exc) { 361 if (debug) exc.printStackTrace(writer); 362 throw new Exception ("error setting property " + 363 varClassName + "." + varName + 364 ": " + exc.getMessage()); 365 } 366 writer.println("done."); 367 } catch (Exception exc) { 368 writer.println(exc.getMessage()); 369 } 370 } else if (cmd.equals(GET_VARIABLE)) { 371 try { 372 if (st.countTokens() != 1) 373 throw new Exception ("Usage: get property"); 374 375 String property = st.nextToken(); 376 377 int pindex = property.lastIndexOf('.'); 379 if (pindex == -1) { 380 throw new Exception ("bad formed property name: " + property); 382 } 383 String varClassName = property.substring(0, pindex); 384 String varName = property.substring(pindex + 1); 385 386 try { 387 Class varClass = Class.forName(varClassName); 389 Field var = varClass.getDeclaredField(varName); 390 Object value = var.get(null); 392 writer.println(property + " = " + value); 393 } catch (Exception exc) { 394 if (debug) exc.printStackTrace(writer); 395 throw new Exception ("error getting property " + 396 varClassName + "." + varName + 397 ": " + exc.getMessage()); 398 } 399 } catch (Exception exc) { 400 writer.println(exc.getMessage()); 401 } 402 } else if (cmd.equals(THREADS)) { 403 String group = null; 404 if (st.hasMoreTokens()) 405 group = st.nextToken(); 406 407 ThreadGroup tg = Thread.currentThread().getThreadGroup(); 408 while (tg.getParent() != null) 409 tg = tg.getParent(); 410 int nbt = tg.activeCount(); 411 Thread [] tab = new Thread [nbt]; 412 tg.enumerate(tab); 413 414 for (int j=0; j<nbt; j++) { 415 if ((group != null) && 416 ! tab[j].getThreadGroup().getName().equals(group)) 417 continue; 418 writer.println("+----------------------------------------"); 419 writer.println("[" + 420 ((group==null)?(tab[j].getThreadGroup().getName() + "."):"") + 421 tab[j].getName() + "]" + 422 (tab[j].isAlive()?" alive":"") + 423 (tab[j].isDaemon()?" daemon":"") + "\n " + 424 tab[j]); 425 } 426 } else if (cmd.equals(LIST_MCONS)) { 427 for (Enumeration c=AgentServer.getConsumers(); 428 c.hasMoreElements(); ) { 429 MessageConsumer cons = (MessageConsumer) c.nextElement(); 430 writer.println("+----------------------------------------"); 431 writer.println(cons); 432 } 433 } else if (cmd.equals(START_MCONS)) { 434 String domain = null; 435 if (st.hasMoreTokens()) { 436 domain = st.nextToken(); 438 } 439 for (Enumeration c=AgentServer.getConsumers(); 440 c.hasMoreElements(); ) { 441 MessageConsumer cons = (MessageConsumer) c.nextElement(); 442 443 if (((domain == null) || domain.equals(cons.getName()))) { 444 try { 445 cons.start(); 446 writer.println("start " + cons.getName() + " done."); 447 } catch (Exception exc) { 448 writer.println("Can't start "+ cons.getName() + ": " + 449 exc.getMessage()); 450 if (debug) exc.printStackTrace(writer); 451 } 452 } 453 } 454 } else if (cmd.equals(STOP_MCONS)) { 455 String domain = null; 456 if (st.hasMoreTokens()) { 457 domain = st.nextToken(); 459 } 460 for (Enumeration c=AgentServer.getConsumers(); 461 c.hasMoreElements(); ) { 462 MessageConsumer cons = (MessageConsumer) c.nextElement(); 463 464 if (((domain == null) || domain.equals(cons.getName()))) { 465 cons.stop(); 466 writer.println("stop " + cons.getName() + " done."); 467 } 468 } 469 } else if (cmd.equals(LIST_SERVICE)) { 470 ServiceDesc services[] = ServiceManager.getServices(); 471 for (int i=0; i<services.length; i++ ){ 472 writer.println("+----------------------------------------"); 473 writer.println(services[i].getClassName() + " (" + 474 services[i].getArguments() + ")" + 475 (services[i].isInitialized()?" initialized ":"") + 476 (services[i].isRunning()?" running":"")); 477 } 478 } else if (cmd.equals(ADD_SERVICE)) { 479 try { 480 String sclass = null; 482 String args = null; 483 try { 484 sclass = st.nextToken(); 485 if (st.hasMoreTokens()) 486 args = st.nextToken(); 487 } catch (NoSuchElementException exc) { 488 throw new Exception ("Usage: add <sclass> [<args>]"); 489 } 490 try { 491 ServiceManager.register(sclass, args); 492 writer.println("Service <" + sclass + "> registred."); 493 ServiceManager.start(sclass); 494 writer.println("Service <" + sclass + "> started."); 495 } catch (Exception exc) { 496 writer.println("Can't start service: " + exc.getMessage()); 498 if (debug) exc.printStackTrace(writer); 499 } 500 } catch (Exception exc) { 501 writer.println(exc.getMessage()); 502 } 503 } else if (cmd.equals(REMOVE_SERVICE)) { 504 String sclass = null; 506 try { 507 sclass = st.nextToken(); 508 } catch (NoSuchElementException exc) { 509 writer.println("Usage: " + REMOVE_SERVICE + " <sclass> [<args>]"); 510 return; 511 } 512 try { 513 ServiceManager.stop(sclass); 514 writer.println("Service <" + sclass + "> stopped."); 515 } catch (Exception exc) { 516 writer.println("Can't stop service: " + exc.getMessage()); 517 if (debug) exc.printStackTrace(writer); 518 } 519 try { 520 ServiceManager.unregister(sclass); 521 writer.println("Service <" + sclass + "> unregistred."); 522 } catch (Exception exc) { 523 writer.println("Can't unregister service: " + exc.getMessage()); 524 if (debug) exc.printStackTrace(writer); 525 } 526 } else if (cmd.equals(DUMP)) { 527 AgentId id = null; 528 try { 529 id = AgentId.fromString(st.nextToken()); 530 } catch (IllegalArgumentException exc) { 531 writer.println("Usage: " + DUMP + " #x.y.z"); 532 return; 533 } 534 try { 535 writer.println(AgentServer.getEngine().dumpAgent(id)); 536 } catch (Exception exc) { 537 writer.println("Can't launch server: " + exc.getMessage()); 538 if (debug) exc.printStackTrace(writer); 539 } 540 } else if (cmd.equals(NONE)) { 541 } else if (cmd.equals(PING)) { 542 writer.println(AgentServer.getServerId()); 543 } else if (cmd.equals(CONFIG)) { 544 try { 545 A3CMLConfig a3CMLConfig = AgentServer.getConfig(); 546 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 547 PrintWriter out = new PrintWriter(baos); 548 A3CML.toXML(a3CMLConfig, out); 549 out.flush(); 550 baos.flush(); 551 baos.close(); 552 byte[] bytes = baos.toByteArray(); 553 writer.println(new String (bytes)); 554 } catch (Exception exc) { 555 writer.println("Can't load configuration: " + exc.getMessage()); 556 if (debug) exc.printStackTrace(writer); 557 } 558 } else if (cmd.equals(LOG)){ 559 PrintStream oldErr = System.err; 560 try { 561 System.setErr(new PrintStream(socket.getOutputStream())); 562 String topic = st.nextToken(); 563 String level = st.nextToken(); 564 int intLevel; 565 if (level.equals("DEBUG")) { 566 intLevel = org.objectweb.util.monolog.api.BasicLevel.DEBUG; 567 } else if (level.equals("ERROR")) { 568 intLevel = org.objectweb.util.monolog.api.BasicLevel.ERROR; 569 } else if (level.equals("FATAL")) { 570 intLevel = org.objectweb.util.monolog.api.BasicLevel.FATAL; 571 } else if (level.equals("INFO")) { 572 intLevel = org.objectweb.util.monolog.api.BasicLevel.INFO; 573 } else if (level.equals("INHERIT")) { 574 intLevel = org.objectweb.util.monolog.api.BasicLevel.INHERIT; 575 } else if (level.equals("WARN")) { 576 intLevel = org.objectweb.util.monolog.api.BasicLevel.WARN; 577 } else { 578 writer.println("Unknown level: " + level); 579 return; 580 } 581 Debug.setLoggerLevel(topic, intLevel); 582 } catch(Exception exc){ 583 writer.println(exc.getMessage()); 584 } finally{ 585 System.setErr(oldErr); 586 writer.println("OK"); 587 } 588 } else if (cmd.equals(HELP)) { 589 writer.println( 590 "Description of available commands:\n" + 591 "\t" + HELP + 592 "\n\t\tGives the summary of the options.\n" + 593 "\t" + STOP_SERVER + 594 "\n\t\tStops the server.\n" + 595 "\t" + SET_VARIABLE + "variable value" + 596 "\n\t\tSet the specified static variable with the given value.\n" + 597 "\t" + GET_VARIABLE + 598 "\n\t\tReturn the value of the specified static variable.\n" + 599 "\t" + GC + 600 "\n\t\tRun the garbage collector in the specified A3 server.\n" + 601 "\t" + THREADS + " [group]" + 602 "\n\t\tList all threads in server JVM.\n" + 603 "\t" + LIST_MCONS + 604 "\n\t\tList all defined consumers.\n" + 605 "\t" + START_MCONS + " [domain]" + 606 "\n\t\tStarts the specified MessageConsumer.\n" + 607 "\t" + STOP_MCONS + " [domain]" + 608 "\n\t\tStops the specified MessageConsumer.\n" + 609 "\t" + LIST_SERVICE + 610 "\n\t\tList all registered services.\n" + 611 "\t" + ADD_SERVICE + " classname arguments" + 612 "\n\t\tRegisters and starts the specified Service.\n" + 613 "\t" + REMOVE_SERVICE + " classname" + 614 "\n\t\tStops then unregister the specified Service.\n" + 615 "\t" + CONFIG + 616 "\n\t\tReturns the configuration of the server in XML format.\n"); 617 } else { 618 writer.println("unknown command:" + cmd); 619 } 620 } finally { 621 } 622 } 623 } 624 } 625 | Popular Tags |