00001
00018 package org.objectweb.jonas.adm;
00019
00020 import java.io.BufferedReader;
00021 import java.io.IOException;
00022 import java.io.InputStreamReader;
00023 import java.rmi.RemoteException;
00024 import java.util.Enumeration;
00025 import java.util.Properties;
00026 import java.util.Vector;
00027
00028 import javax.naming.InitialContext;
00029 import javax.rmi.PortableRemoteObject;
00030
00031 import org.objectweb.jonas.ear.EarServiceException;
00032 import org.objectweb.jonas.resource.ResourceServiceException;
00033 import org.objectweb.jonas.web.JWebContainerServiceException;
00034
00035
00036
00037
00038
00039
00040
00041 public class JonasAdmin {
00042
00043 private static final String SERVERNAME = "JOnAS server ";
00044
00045 private static String jonasName = null;
00046
00047 private static AdmInterface admI = null;
00048
00049 private static int state = -1;
00050
00051 private static boolean reachable = false;
00052
00053 private static boolean isNotEJB = false;
00054
00055 private static boolean isError = false;
00056
00057 private static void checkAdm() {
00058
00059
00060 String admName = jonasName + Adm.ADMNAME_SUFFIX;
00061 try {
00062 InitialContext initialContext = new InitialContext();
00063 admI = (AdmInterface) PortableRemoteObject.narrow(initialContext.lookup(admName), AdmInterface.class);
00064 state = admI.getServerState();
00065 reachable = (state == Adm.READY);
00066 if (reachable) {
00067 isNotEJB = !admI.isEJBContainer();
00068 }
00069 } catch (Exception e) {
00070 reachable = false;
00071 }
00072 }
00073
00078 private static void waitServer() {
00079 for (int i = 0; i < 200; i++) {
00080 try {
00081
00082 Thread.sleep(500);
00083 } catch (InterruptedException e) {
00084 return;
00085 }
00086
00087 if (reachable) {
00088 return;
00089 }
00090 checkAdm();
00091 if (state == Adm.STOPPED) {
00092 return;
00093 }
00094 }
00095 System.out.println(SERVERNAME + jonasName + " unreachable");
00096 }
00097
00108 private static void addFile(String fileName) {
00109
00110 if (!reachable) {
00111 System.err.println(SERVERNAME + jonasName + " unreachable");
00112 isError = true;
00113 return;
00114 }
00115 try {
00116 if (fileName.toLowerCase().endsWith(".jar") || fileName.toLowerCase().endsWith(".xml")) {
00117
00118 if (isNotEJB) {
00119 System.err.println("no beans on TMServer");
00120 isError = true;
00121 return;
00122 }
00123 admI.addBeans(fileName);
00124 } else if (fileName.toLowerCase().endsWith(".war")) {
00125
00126 admI.addWar(fileName);
00127 } else if (fileName.toLowerCase().endsWith(".ear")) {
00128
00129 admI.addEar(fileName);
00130 } else if (fileName.toLowerCase().endsWith(".rar")) {
00131
00132 admI.addRar(fileName);
00133 } else {
00134
00135 System.err.println("Valid file extensions are : .jar, .xml, .war, .ear, .rar");
00136 return;
00137 }
00138 } catch (JWebContainerServiceException e) {
00139 System.err.println("admI.addWar: " + e);
00140 isError = true;
00141 return;
00142 } catch (EarServiceException e) {
00143 System.err.println("admI.addEar: " + e);
00144 isError = true;
00145 return;
00146 } catch (ResourceServiceException e) {
00147 System.err.println("admI.addRar: " + e);
00148 isError = true;
00149 return;
00150 } catch (RemoteException e) {
00151 System.err.println("RemoteException : " + e);
00152 isError = true;
00153 return;
00154 }
00155 }
00156
00167 private static void removeFile(String fileName) {
00168 if (!reachable) {
00169 System.err.println(SERVERNAME + jonasName + " unreachable");
00170 isError = true;
00171 return;
00172 }
00173
00174 try {
00175 if (fileName.toLowerCase().endsWith(".jar") || fileName.toLowerCase().endsWith(".xml")) {
00176
00177 if (isNotEJB) {
00178 System.err.println("no beans on TMServer");
00179 isError = true;
00180 return;
00181 }
00182 admI.removeBeans(fileName);
00183 } else if (fileName.toLowerCase().endsWith(".war")) {
00184
00185 admI.removeWar(fileName);
00186 } else if (fileName.toLowerCase().endsWith(".ear")) {
00187
00188 admI.removeEar(fileName);
00189 } else if (fileName.toLowerCase().endsWith(".rar")) {
00190
00191 admI.removeRar(fileName);
00192 } else {
00193
00194 System.err.println("Valid file extensions are : .jar, xml, .war, .ear, *.rar");
00195 return;
00196 }
00197 } catch (JWebContainerServiceException e) {
00198 System.err.println("admI.removeWar: " + e.getMessage());
00199 isError = true;
00200 return;
00201 } catch (EarServiceException e) {
00202 System.err.println("admI.removeEar: " + e.getMessage());
00203 isError = true;
00204 return;
00205 } catch (ResourceServiceException e) {
00206 System.err.println("admI.removeRar: " + e);
00207 isError = true;
00208 return;
00209 } catch (RemoteException e) {
00210 System.err.println("RemoteException : " + e.getMessage());
00211 isError = true;
00212 return;
00213 }
00214 }
00215
00216 private static void listbeans() {
00217 if (isNotEJB) {
00218 System.err.println("no beans on TMServer");
00219 isError = true;
00220 return;
00221 }
00222 if (!reachable) {
00223 System.err.println(SERVERNAME + jonasName + " unreachable");
00224 isError = true;
00225 return;
00226 }
00227 try {
00228 String[] result = admI.listBeans();
00229 if (result.length == 0) {
00230 System.out.println("No bean in " + jonasName);
00231 }
00232 for (int i = 0; i < result.length; i++) {
00233 System.out.println(result[i]);
00234 }
00235 } catch (RemoteException e) {
00236 System.err.println("admI.listBeans() : " + e);
00237 isError = true;
00238 return;
00239 }
00240 }
00241
00242 private static void listnames() {
00243 if (!reachable) {
00244 System.err.println(SERVERNAME + jonasName + " unreachable");
00245 isError = true;
00246 return;
00247 }
00248 Vector result = new Vector();
00249 try {
00250 result = admI.listContext();
00251 } catch (RemoteException ex) {
00252 System.err.println("admI.listBeans() : " + ex);
00253 isError = true;
00254 return;
00255 }
00256
00257 if (result.size() == 0) {
00258 System.out.println("No name in JNDI context.");
00259 }
00260 for (int i = 0; i < result.size(); i++) {
00261 System.out.println(result.elementAt(i));
00262 }
00263 }
00264
00265 private static void listproperties() {
00266 if (!reachable) {
00267 System.err.println(SERVERNAME + jonasName + " unreachable");
00268 isError = true;
00269 return;
00270 }
00271 Properties p = new Properties();
00272 try {
00273 p = admI.listEnv();
00274 } catch (RemoteException ex) {
00275 System.err.println("admI.listBeans() : " + ex);
00276 isError = true;
00277 return;
00278 }
00279 for (Enumeration e = p.keys(); e.hasMoreElements();) {
00280 Object key = e.nextElement();
00281 Object value = p.get(key);
00282 System.out.println(key.toString() + "=" + value.toString());
00283 }
00284 }
00285
00286 private static void sync(boolean passivate) {
00287 if (isNotEJB) {
00288 System.err.println("no beans on TMServer");
00289 isError = true;
00290 return;
00291 }
00292 if (!reachable) {
00293 System.err.println(SERVERNAME + jonasName + " unreachable");
00294 isError = true;
00295 return;
00296 }
00297 try {
00298 admI.syncAllEntities(passivate);
00299 } catch (RemoteException e) {
00300 System.err.println("admI.syncAllEntities : " + e);
00301 isError = true;
00302 return;
00303 }
00304 }
00305
00306 private static void runGC() {
00307 if (!reachable) {
00308 System.err.println(SERVERNAME + jonasName + " unreachable");
00309 isError = true;
00310 return;
00311 }
00312 try {
00313 admI.runGC();
00314 } catch (RemoteException e) {
00315 System.err.println("admI.runGC : " + e);
00316 isError = true;
00317 return;
00318 }
00319 }
00320
00321 private static void setTTimeout(String tstr) {
00322 if (!reachable) {
00323 System.err.println(SERVERNAME + jonasName + " unreachable");
00324 isError = true;
00325 return;
00326 }
00327 Integer i = new Integer(tstr);
00328 try {
00329 admI.setTransactionTimeout(i.intValue());
00330 } catch (RemoteException e) {
00331 System.err.println("admI.setTransactionTimeout : " + e);
00332 isError = true;
00333 return;
00334 }
00335 }
00336
00337 private static void listTopics() {
00338 if (!reachable) {
00339 System.err.println(SERVERNAME + jonasName + " unreachable");
00340 isError = true;
00341 return;
00342 }
00343 try {
00344 String[] result = admI.getTopics();
00345 if (result.length == 0) {
00346 System.out.println("No topics in " + jonasName);
00347 }
00348 for (int i = 0; i < result.length; i++) {
00349 String level = admI.getTopicLevel(result[i]);
00350 System.out.println(level + "\t" + result[i]);
00351 }
00352 } catch (RemoteException e) {
00353 System.err.println("admI.getTopics : " + e);
00354 isError = true;
00355 return;
00356 }
00357 }
00358
00359 private static void setTopic(String t, String l) {
00360 try {
00361 admI.setTopicLevel(t, l.toUpperCase());
00362 } catch (RemoteException e) {
00363 System.err.println("admI.setTopics : " + e);
00364 isError = true;
00365 return;
00366 }
00367 }
00368
00369 private static void stopserver() {
00370 if (!reachable) {
00371 System.err.println(SERVERNAME + jonasName + " unreachable");
00372 isError = true;
00373 return;
00374 }
00375 try {
00376 admI.killServer();
00377 } catch (RemoteException e) {
00378
00379 }
00380 System.out.println(SERVERNAME + jonasName + " stopped");
00381 reachable = false;
00382 }
00383
00384 private static void help() {
00385 System.out.println("addbeans adds beans in a new JOnAS container");
00386 System.out.println("addfile adds beans/servlets/j2ee app/rars based upon the file extension");
00387 System.out.println("env JOnAS properties used by the server");
00388 System.out.println("gc run the garbage collector");
00389 System.out.println("help help");
00390 System.out.println("jndinames lists registered JNDI names");
00391 System.out.println("listbeans lists beans");
00392 System.out.println("name to identify a current JOnAS server");
00393 System.out.println("quit quit JonasAdmin");
00394 System.out.println("removebeans remove beans in a new JOnAS container");
00395 System.out.println("removefile remove beans/servlets/j2ee app/rars (based upon the file extension)");
00396 System.out.println("stop stop current JOnAS");
00397 System.out.println("sync synchronize all entities");
00398 System.out.println("passivate passivate all entities");
00399 System.out.println("trace get/set monolog topics");
00400 System.out.println("ttimeout set default transaction timeout");
00401 }
00402
00403 private static void usage() {
00404 System.out.println("usage : jonas admin <options>");
00405 System.out.println("if no option(except -n), mode is interactive.");
00406 System.out.println("list of available options:");
00407 System.out.println(" -n JonasName : to identify an JOnAS Server");
00408 System.out.println(" -s : stops the EJB server.");
00409 System.out.println(" -l : lists beans currently in the JOnAS Server.");
00410 System.out.println(" -j : lists registered JNDI names.");
00411 System.out.println(" -e : lists JOnAS properties currently used by the JOnAS Server.");
00412 System.out.println(" -a fileName : dynamically adds : - beans from fileName in a new container");
00413 System.out.println(" : - servlets from a WAR file");
00414 System.out.println(" : - j2ee application from an EAR file");
00415 System.out.println(" : - resource adapter from a RAR file");
00416 System.out.println(" -r fileName : dynamically remove : - beans from container fileName");
00417 System.out.println(" : - servlets of a WAR file");
00418 System.out.println(" : - j2ee application of an EAR file");
00419 System.out.println(" : - resource adapter from a RAR file");
00420 System.out.println(" -sync: synchronize all entities");
00421 System.out.println(" -passivate: passivate all entities");
00422 System.out.println(" -gc: run the garbage collector");
00423 System.out.println(" -tt timeout: set default transaction timeout");
00424 System.out.println(" -t list monolog topics");
00425 System.out.println(" -debug topic : set DEBUG for a monolog topic");
00426 System.out.println(" -h : help message.");
00427 System.out.println(" -? : help message.");
00428 }
00429
00430
00431
00432
00433 public static void menu() throws IOException {
00434
00435 if (!reachable) {
00436 System.out.println("You must first choose a jonas server. (command `name`)");
00437 System.out.println("Type `help` to get the list of available commands");
00438 }
00439
00440
00441 BufferedReader inbuf = new BufferedReader(new InputStreamReader(System.in));
00442 while (true) {
00443 System.out.print("Admin (" + jonasName + ") > ");
00444 String command = inbuf.readLine();
00445 if (command.length() == 0) {
00446 continue;
00447 }
00448 if ("addbeans".startsWith(command) || "addfile".startsWith(command)) {
00449 String fName = null;
00450 System.out.print("file name ? > ");
00451 if ((fName = inbuf.readLine()).length() != 0) {
00452 addFile(fName);
00453 }
00454 continue;
00455 }
00456 if ("env".startsWith(command)) {
00457 listproperties();
00458 continue;
00459 }
00460 if ("gc".startsWith(command)) {
00461 runGC();
00462 continue;
00463 }
00464 if ("help".startsWith(command) || command.equals("?")) {
00465 help();
00466 continue;
00467 }
00468 if ("jndinames".startsWith(command)) {
00469 listnames();
00470 continue;
00471 }
00472 if ("listbeans".startsWith(command)) {
00473 listbeans();
00474 continue;
00475 }
00476 if ("name".startsWith(command)) {
00477 System.out.print("Enter the " + SERVERNAME + "'s name (jonas.name property) : ");
00478 jonasName = new String(inbuf.readLine());
00479 checkAdm();
00480 continue;
00481 }
00482 if ("quit".startsWith(command) || "exit".startsWith(command)) {
00483 return;
00484 }
00485 if ("removebeans".startsWith(command) || "removefile".startsWith(command)) {
00486 String fName = null;
00487 System.out.print("file name ? > ");
00488 if ((fName = inbuf.readLine()).length() != 0) {
00489 removeFile(fName);
00490 }
00491 continue;
00492 }
00493 if ("trace".startsWith(command)) {
00494 while (true) {
00495 listTopics();
00496 System.out.print("topic name ? > ");
00497 String tname = inbuf.readLine().trim();
00498 if (tname.length() == 0) {
00499 break;
00500 }
00501 System.out.print("topic level ? (DEBUG | WARN | INFO | ERROR | INHERIT) > ");
00502 String levstr = inbuf.readLine().trim();
00503 setTopic(tname, levstr);
00504 }
00505 continue;
00506 }
00507 if ("stop".startsWith(command)) {
00508 stopserver();
00509 continue;
00510 }
00511 if ("sync".startsWith(command)) {
00512 sync(false);
00513 continue;
00514 }
00515 if ("passivate".startsWith(command)) {
00516 sync(true);
00517 continue;
00518 }
00519 if ("ttimeout".startsWith(command)) {
00520 String tstr = null;
00521 System.out.print("transaction timeout in seconds ? > ");
00522 if ((tstr = inbuf.readLine()).length() != 0) {
00523 setTTimeout(tstr);
00524 }
00525 continue;
00526 }
00527 System.out.println("Unknown command. Type help to get for the list of commands.");
00528 }
00529 }
00530
00531 public static void main(String[] args) throws IOException {
00532
00533 String fileName = null;
00534 String timeout = null;
00535 String topic = null;
00536
00537 boolean lOption = false;
00538 boolean pingOption = false;
00539 boolean jOption = false;
00540 boolean eOption = false;
00541 boolean aOption = false;
00542 boolean tOption = false;
00543 boolean sOption = false;
00544 boolean rOption = false;
00545 boolean syncOpt = false;
00546 boolean passivateOpt = false;
00547 boolean debugOption = false;
00548 boolean ttOpt = false;
00549 boolean gcOpt = false;
00550
00551 boolean interactive = true;
00552 boolean namedServer = false;
00553
00554 for (int argn = 0; argn < args.length; argn++) {
00555 String arg = args[argn];
00556 boolean nextArgument = argn < args.length - 1;
00557
00558 if (arg.equals("-a") && nextArgument) {
00559 fileName = args[++argn];
00560 aOption = true;
00561 interactive = false;
00562 continue;
00563 }
00564 if (arg.equals("-ping")) {
00565 pingOption = true;
00566 continue;
00567 }
00568 if (arg.equals("-e")) {
00569 eOption = true;
00570 interactive = false;
00571 continue;
00572 }
00573 if (arg.equals("-h") || arg.equals("-?")) {
00574 usage();
00575 System.exit(0);
00576 }
00577 if (arg.equals("-gc")) {
00578 gcOpt = true;
00579 interactive = false;
00580 continue;
00581 }
00582 if (arg.equals("-j")) {
00583 jOption = true;
00584 interactive = false;
00585 continue;
00586 }
00587 if (arg.equals("-l")) {
00588 lOption = true;
00589 interactive = false;
00590 continue;
00591 }
00592 if (arg.equals("-n") && nextArgument) {
00593 jonasName = args[++argn];
00594 namedServer = true;
00595 continue;
00596 }
00597 if (arg.equals("-r") && nextArgument) {
00598 fileName = args[++argn];
00599 rOption = true;
00600 interactive = false;
00601 continue;
00602 }
00603 if (arg.equals("-s")) {
00604 sOption = true;
00605 interactive = false;
00606 continue;
00607 }
00608 if (arg.equals("-sync")) {
00609 syncOpt = true;
00610 interactive = false;
00611 continue;
00612 }
00613 if (arg.equals("-passivate")) {
00614 passivateOpt = true;
00615 interactive = false;
00616 continue;
00617 }
00618 if (arg.equals("-debug") && nextArgument) {
00619 topic = args[++argn];
00620 debugOption = true;
00621 interactive = false;
00622 continue;
00623 }
00624 if (arg.equals("-t")) {
00625 tOption = true;
00626 interactive = false;
00627 continue;
00628 }
00629 if (arg.equals("-tt") && nextArgument) {
00630 timeout = args[++argn];
00631 ttOpt = true;
00632 interactive = false;
00633 continue;
00634 }
00635 System.out.println("Bad option: " + arg);
00636 usage();
00637 System.exit(2);
00638 }
00639
00640 if (!namedServer) {
00641 jonasName = "jonas";
00642 }
00643
00644 if (pingOption) {
00645
00646 waitServer();
00647 return;
00648 }
00649
00650
00651 checkAdm();
00652
00653 if (aOption) {
00654
00655 addFile(fileName);
00656 }
00657 if (rOption) {
00658
00659 removeFile(fileName);
00660 }
00661 if (lOption) {
00662
00663 listbeans();
00664 }
00665 if (jOption) {
00666
00667 listnames();
00668 }
00669 if (eOption) {
00670
00671 listproperties();
00672 }
00673 if (tOption) {
00674
00675 listTopics();
00676 }
00677 if (debugOption) {
00678
00679 setTopic(topic, "DEBUG");
00680 }
00681 if (sOption) {
00682
00683 stopserver();
00684 }
00685 if (ttOpt) {
00686
00687 setTTimeout(timeout);
00688 }
00689 if (syncOpt) {
00690
00691 sync(false);
00692 }
00693 if (passivateOpt) {
00694
00695 sync(true);
00696 }
00697 if (gcOpt) {
00698
00699 runGC();
00700 }
00701
00702
00703 if (interactive) {
00704 menu();
00705 }
00706
00707
00708 if (!interactive) {
00709 if (isError) {
00710 System.exit(2);
00711 }
00712 }
00713 }
00714 }