1 17 18 package org.objectweb.jonas.adm; 19 20 import java.io.BufferedReader ; 21 import java.io.IOException ; 22 import java.io.InputStreamReader ; 23 import java.rmi.RemoteException ; 24 import java.util.Enumeration ; 25 import java.util.Properties ; 26 import java.util.Vector ; 27 28 import javax.naming.InitialContext ; 29 import javax.rmi.PortableRemoteObject ; 30 31 import org.objectweb.jonas.ear.EarServiceException; 32 import org.objectweb.jonas.resource.ResourceServiceException; 33 import org.objectweb.jonas.web.JWebContainerServiceException; 34 35 41 public class JonasAdmin { 42 43 private static final String SERVERNAME = "JOnAS server "; 44 45 private static String jonasName = null; 46 47 private static AdmInterface admI = null; 48 49 private static int state = -1; 50 51 private static boolean reachable = false; 52 53 private static boolean isNotEJB = false; 54 55 private static boolean isError = false; 56 57 private static void checkAdm() { 58 59 String admName = jonasName + Adm.ADMNAME_SUFFIX; 61 try { 62 InitialContext initialContext = new InitialContext (); 63 admI = (AdmInterface) PortableRemoteObject.narrow(initialContext.lookup(admName), AdmInterface.class); 64 state = admI.getServerState(); 65 reachable = (state == Adm.READY); 66 if (reachable) { 67 isNotEJB = !admI.isEJBContainer(); 68 } 69 } catch (Exception e) { 70 reachable = false; 71 } 72 } 73 74 78 private static void waitServer() { 79 for (int i = 0; i < 200; i++) { 80 try { 81 Thread.sleep(500); 83 } catch (InterruptedException e) { 84 return; 85 } 86 if (reachable) { 88 return; 89 } 90 checkAdm(); 91 if (state == Adm.STOPPED) { 92 return; 93 } 94 } 95 System.out.println(SERVERNAME + jonasName + " unreachable"); 96 } 97 98 108 private static void addFile(String fileName) { 109 110 if (!reachable) { 111 System.err.println(SERVERNAME + jonasName + " unreachable"); 112 isError = true; 113 return; 114 } 115 try { 116 if (fileName.toLowerCase().endsWith(".jar") || fileName.toLowerCase().endsWith(".xml")) { 117 if (isNotEJB) { 119 System.err.println("no beans on TMServer"); 120 isError = true; 121 return; 122 } 123 admI.addBeans(fileName); 124 } else if (fileName.toLowerCase().endsWith(".war")) { 125 admI.addWar(fileName); 127 } else if (fileName.toLowerCase().endsWith(".ear")) { 128 admI.addEar(fileName); 130 } else if (fileName.toLowerCase().endsWith(".rar")) { 131 admI.addRar(fileName); 133 } else { 134 System.err.println("Valid file extensions are : .jar, .xml, .war, .ear, .rar"); 136 return; 137 } 138 } catch (JWebContainerServiceException e) { 139 System.err.println("admI.addWar: " + e); 140 isError = true; 141 return; 142 } catch (EarServiceException e) { 143 System.err.println("admI.addEar: " + e); 144 isError = true; 145 return; 146 } catch (ResourceServiceException e) { 147 System.err.println("admI.addRar: " + e); 148 isError = true; 149 return; 150 } catch (RemoteException e) { 151 System.err.println("RemoteException : " + e); 152 e.printStackTrace(); 153 isError = true; 154 return; 155 } 156 } 157 158 168 private static void removeFile(String fileName) { 169 if (!reachable) { 170 System.err.println(SERVERNAME + jonasName + " unreachable"); 171 isError = true; 172 return; 173 } 174 try { 176 if (fileName.toLowerCase().endsWith(".jar") || fileName.toLowerCase().endsWith(".xml")) { 177 if (isNotEJB) { 179 System.err.println("no beans on TMServer"); 180 isError = true; 181 return; 182 } 183 admI.removeBeans(fileName); 184 } else if (fileName.toLowerCase().endsWith(".war")) { 185 admI.removeWar(fileName); 187 } else if (fileName.toLowerCase().endsWith(".ear")) { 188 admI.removeEar(fileName); 190 } else if (fileName.toLowerCase().endsWith(".rar")) { 191 admI.removeRar(fileName); 193 } else { 194 System.err.println("Valid file extensions are : .jar, xml, .war, .ear, *.rar"); 196 return; 197 } 198 } catch (JWebContainerServiceException e) { 199 System.err.println("admI.removeWar: " + e.getMessage()); 200 isError = true; 201 return; 202 } catch (EarServiceException e) { 203 System.err.println("admI.removeEar: " + e.getMessage()); 204 isError = true; 205 return; 206 } catch (ResourceServiceException e) { 207 System.err.println("admI.removeRar: " + e); 208 isError = true; 209 return; 210 } catch (RemoteException e) { 211 System.err.println("RemoteException : " + e.getMessage()); 212 isError = true; 213 return; 214 } 215 } 216 217 223 private static void isDeployedFile(String fileName) { 224 if (!reachable) { 225 System.err.println(SERVERNAME + jonasName + " unreachable"); 226 isError = true; 227 return; 228 } 229 try { 231 boolean isDeployed = false; 232 if (fileName.toLowerCase().endsWith(".jar") || fileName.toLowerCase().endsWith(".xml")) { 233 if (isNotEJB) { 235 System.err.println("no beans on TMServer"); 236 isError = true; 237 return; 238 } 239 isDeployed = admI.isLoaded(fileName); 240 } else if (fileName.toLowerCase().endsWith(".war")) { 241 isDeployed = admI.isWarLoaded(fileName); 243 } else if (fileName.toLowerCase().endsWith(".ear")) { 244 isDeployed = admI.isEarLoaded(fileName); 246 } else if (fileName.toLowerCase().endsWith(".rar")) { 247 isDeployed = admI.isRarLoaded(fileName); 249 } else { 250 System.err.println("Valid file extensions are : .jar, xml, .war, .ear, .rar"); 252 return; 253 } 254 if (isDeployed) { 255 System.out.println(fileName + " deployed in " + jonasName); 256 } else { 257 System.out.println(fileName + " NOT deployed in " + jonasName); 258 } 259 } catch (JWebContainerServiceException e) { 260 System.err.println("admI.removeWar: " + e.getMessage()); 261 isError = true; 262 return; 263 } catch (EarServiceException e) { 264 System.err.println("admI.removeEar: " + e.getMessage()); 265 isError = true; 266 return; 267 } catch (ResourceServiceException e) { 268 System.err.println("admI.removeRar: " + e); 269 isError = true; 270 return; 271 } catch (RemoteException e) { 272 System.err.println("RemoteException : " + e.getMessage()); 273 isError = true; 274 return; 275 } 276 } 277 278 private static void custom() { 279 if (isNotEJB) { 280 System.err.println("no beans on TMServer"); 281 isError = true; 282 return; 283 } 284 if (!reachable) { 285 System.err.println(SERVERNAME + jonasName + " unreachable"); 286 isError = true; 287 return; 288 } 289 try { 290 System.out.println(admI.dumpCustom()); 291 } catch (RemoteException e) { 292 System.err.println("admI.dumpCustom() : " + e); 293 isError = true; 294 return; 295 } 296 } 297 298 private static void listbeans() { 299 if (isNotEJB) { 300 System.err.println("no beans on TMServer"); 301 isError = true; 302 return; 303 } 304 if (!reachable) { 305 System.err.println(SERVERNAME + jonasName + " unreachable"); 306 isError = true; 307 return; 308 } 309 try { 310 String [] result = admI.listBeans(); 311 if (result.length == 0) { 312 System.out.println("No bean in " + jonasName); 313 } 314 for (int i = 0; i < result.length; i++) { 315 System.out.println(result[i]); 316 } 317 } catch (RemoteException e) { 318 System.err.println("admI.listBeans() : " + e); 319 isError = true; 320 return; 321 } 322 } 323 324 private static void listnames() { 325 if (!reachable) { 326 System.err.println(SERVERNAME + jonasName + " unreachable"); 327 isError = true; 328 return; 329 } 330 Vector result = new Vector (); 331 try { 332 result = admI.listContext(); 333 } catch (RemoteException e) { 334 System.err.println("admI.listContext() : " + e); 335 isError = true; 336 return; 337 } 338 339 if (result.size() == 0) { 340 System.out.println("No name in JNDI context."); 341 } 342 for (int i = 0; i < result.size(); i++) { 343 System.out.println(result.elementAt(i)); 344 } 345 } 346 347 private static void listproperties() { 348 if (!reachable) { 349 System.err.println(SERVERNAME + jonasName + " unreachable"); 350 isError = true; 351 return; 352 } 353 Properties p = new Properties (); 354 try { 355 p = admI.listEnv(); 356 } catch (RemoteException e) { 357 System.err.println("admI.listEnv() : " + e); 358 isError = true; 359 return; 360 } 361 for (Enumeration e = p.keys(); e.hasMoreElements();) { 362 Object key = e.nextElement(); 363 Object value = p.get(key); 364 System.out.println(key.toString() + "=" + value.toString()); 365 } 366 } 367 368 private static void sync(boolean passivate) { 369 if (isNotEJB) { 370 System.err.println("no beans on TMServer"); 371 isError = true; 372 return; 373 } 374 if (!reachable) { 375 System.err.println(SERVERNAME + jonasName + " unreachable"); 376 isError = true; 377 return; 378 } 379 try { 380 admI.syncAllEntities(passivate); 381 } catch (RemoteException e) { 382 System.err.println("admI.syncAllEntities : " + e); 383 isError = true; 384 return; 385 } 386 } 387 388 private static void runGC() { 389 if (!reachable) { 390 System.err.println(SERVERNAME + jonasName + " unreachable"); 391 isError = true; 392 return; 393 } 394 try { 395 admI.runGC(); 396 } catch (RemoteException e) { 397 System.err.println("admI.runGC : " + e); 398 isError = true; 399 return; 400 } 401 } 402 403 private static void setTTimeout(String tstr) { 404 if (!reachable) { 405 System.err.println(SERVERNAME + jonasName + " unreachable"); 406 isError = true; 407 return; 408 } 409 Integer i = new Integer (tstr); 410 try { 411 admI.setTransactionTimeout(i.intValue()); 412 } catch (RemoteException e) { 413 System.err.println("admI.setTransactionTimeout : " + e); 414 isError = true; 415 return; 416 } 417 } 418 419 private static void listTopics() { 420 if (!reachable) { 421 System.err.println(SERVERNAME + jonasName + " unreachable"); 422 isError = true; 423 return; 424 } 425 try { 426 String [] result = admI.getTopics(); 427 if (result.length == 0) { 428 System.out.println("No topics in " + jonasName); 429 } 430 for (int i = 0; i < result.length; i++) { 431 String level = admI.getTopicLevel(result[i]); 432 System.out.println(level + "\t" + result[i]); 433 } 434 } catch (RemoteException e) { 435 System.err.println("admI.getTopics : " + e); 436 isError = true; 437 return; 438 } 439 } 440 441 private static void setTopic(String t, String l) { 442 try { 443 admI.setTopicLevel(t, l.toUpperCase()); 444 } catch (RemoteException e) { 445 System.err.println("admI.setTopics : " + e); 446 isError = true; 447 return; 448 } 449 } 450 451 private static void stopserver() { 452 if (!reachable) { 453 System.err.println(SERVERNAME + jonasName + " unreachable"); 454 isError = true; 455 return; 456 } 457 try { 458 admI.killServer(); 459 } catch (RemoteException e) { 460 } 462 System.out.println(SERVERNAME + jonasName + " stopped"); 463 reachable = false; 464 } 465 466 private static void help() { 467 System.out.println("addbeans adds beans in a new JOnAS container"); 468 System.out.println("addfile adds beans/servlets/j2ee app/rars based upon the file extension"); 469 System.out.println("custom dump jonas customization"); 470 System.out.println("env JOnAS properties used by the server"); 471 System.out.println("gc run the garbage collector"); 472 System.out.println("help help"); 473 System.out.println("jndinames lists registered JNDI names"); 474 System.out.println("listbeans lists beans"); 475 System.out.println("name to identify a current JOnAS server"); 476 System.out.println("quit quit JonasAdmin"); 477 System.out.println("removebeans remove beans in a new JOnAS container"); 478 System.out.println("removefile remove beans/servlets/j2ee app/rars (based upon the file extension)"); 479 System.out.println("stop stop current JOnAS"); 480 System.out.println("sync synchronize all entities"); 481 System.out.println("passivate passivate all entities"); 482 System.out.println("trace get/set monolog topics"); 483 System.out.println("ttimeout set default transaction timeout"); 484 } 485 486 private static void usage() { 487 System.out.println("usage : jonas admin <options>"); 488 System.out.println("if no option(except -n), mode is interactive."); 489 System.out.println("list of available options:"); 490 System.out.println(" -n JonasName : to identify an JOnAS Server"); 491 System.out.println(" -s : stops the EJB server."); 492 System.out.println(" -l : lists beans currently in the JOnAS Server."); 493 System.out.println(" -j : lists registered JNDI names."); 494 System.out.println(" -e : lists JOnAS properties currently used by the JOnAS Server."); 495 System.out.println(" -a fileName : dynamically adds : - beans from fileName in a new container"); 496 System.out.println(" : - servlets from a WAR file"); 497 System.out.println(" : - j2ee application from an EAR file"); 498 System.out.println(" : - resource adapter from a RAR file"); 499 System.out.println(" -r fileName : dynamically remove : - beans from container fileName"); 500 System.out.println(" : - servlets of a WAR file"); 501 System.out.println(" : - j2ee application of an EAR file"); 502 System.out.println(" : - resource adapter from a RAR file"); 503 System.out.println(" -isdeployed fileName : tells if the file is deployed or not"); 504 System.out.println(" -sync: synchronize all entities"); 505 System.out.println(" -passivate: passivate all entities"); 506 System.out.println(" -gc: run the garbage collector"); 507 System.out.println(" -tt timeout: set default transaction timeout"); 508 System.out.println(" -t list monolog topics"); 509 System.out.println(" -debug topic : set DEBUG for a monolog topic"); 510 System.out.println(" -h : help message."); 511 System.out.println(" -? : help message."); 512 } 513 514 517 public static void menu() throws IOException { 518 if (!reachable) { 520 System.out.println("You must first choose a jonas server. (command `name`)"); 521 System.out.println("Type `help` to get the list of available commands"); 522 } 523 524 BufferedReader inbuf = new BufferedReader (new InputStreamReader (System.in)); 526 while (true) { 527 System.out.print("Admin (" + jonasName + ") > "); 528 String command = inbuf.readLine(); 529 if (command.length() == 0) { 530 continue; 531 } 532 if ("addbeans".startsWith(command) || "addfile".startsWith(command)) { 533 String fName = null; 534 System.out.print("file name ? > "); 535 if ((fName = inbuf.readLine()).length() != 0) { 536 addFile(fName); 537 } 538 continue; 539 } 540 if ("env".startsWith(command)) { 541 listproperties(); 542 continue; 543 } 544 if ("gc".startsWith(command)) { 545 runGC(); 546 continue; 547 } 548 if ("help".startsWith(command) || command.equals("?")) { 549 help(); 550 continue; 551 } 552 if ("jndinames".startsWith(command)) { 553 listnames(); 554 continue; 555 } 556 if ("listbeans".startsWith(command)) { 557 listbeans(); 558 continue; 559 } 560 if ("custom".startsWith(command)) { 561 custom(); 562 continue; 563 } 564 if ("name".startsWith(command)) { 565 System.out.print("Enter the " + SERVERNAME + "'s name (jonas.name property) : "); 566 jonasName = new String (inbuf.readLine()); 567 checkAdm(); 568 continue; 569 } 570 if ("quit".startsWith(command) || "exit".startsWith(command)) { 571 return; 572 } 573 if ("removebeans".startsWith(command) || "removefile".startsWith(command)) { 574 String fName = null; 575 System.out.print("file name ? > "); 576 if ((fName = inbuf.readLine()).length() != 0) { 577 removeFile(fName); 578 } 579 continue; 580 } 581 if ("trace".startsWith(command)) { 582 while (true) { 583 listTopics(); 584 System.out.print("topic name ? > "); 585 String tname = inbuf.readLine().trim(); 586 if (tname.length() == 0) { 587 break; 588 } 589 System.out.print("topic level ? (DEBUG | WARN | INFO | ERROR | INHERIT) > "); 590 String levstr = inbuf.readLine().trim(); 591 setTopic(tname, levstr); 592 } 593 continue; 594 } 595 if ("stop".startsWith(command)) { 596 stopserver(); 597 continue; 598 } 599 if ("sync".startsWith(command)) { 600 sync(false); 601 continue; 602 } 603 if ("passivate".startsWith(command)) { 604 sync(true); 605 continue; 606 } 607 if ("ttimeout".startsWith(command)) { 608 String tstr = null; 609 System.out.print("transaction timeout in seconds ? > "); 610 if ((tstr = inbuf.readLine()).length() != 0) { 611 setTTimeout(tstr); 612 } 613 continue; 614 } 615 System.out.println("Unknown command. Type help to get for the list of commands."); 616 } 617 } 618 619 public static void main(String [] args) throws IOException { 620 621 String fileName = null; 622 String timeout = null; 623 String topic = null; 624 625 boolean lOption = false; 626 boolean pingOption = false; 627 boolean jOption = false; 628 boolean eOption = false; 629 boolean aOption = false; 630 boolean tOption = false; 631 boolean sOption = false; 632 boolean rOption = false; 633 boolean syncOpt = false; 634 boolean customOpt = false; 635 boolean passivateOpt = false; 636 boolean debugOption = false; 637 boolean ttOpt = false; 638 boolean gcOpt = false; 639 boolean isDeployedOpt = false; 640 641 boolean interactive = true; 642 boolean namedServer = false; 643 for (int argn = 0; argn < args.length; argn++) { 645 String arg = args[argn]; 646 boolean nextArgument = argn < args.length - 1; 647 648 if (arg.equals("-a") && nextArgument) { 649 fileName = args[++argn]; 650 aOption = true; 651 interactive = false; 652 continue; 653 } 654 if (arg.equals("-ping")) { 655 pingOption = true; 656 continue; 657 } 658 if (arg.equals("-custom")) { 659 customOpt = true; 660 interactive = false; 661 continue; 662 } 663 if (arg.equals("-e")) { 664 eOption = true; 665 interactive = false; 666 continue; 667 } 668 if (arg.equals("-h") || arg.equals("-?")) { 669 usage(); 670 System.exit(0); 671 } 672 if (arg.equals("-gc")) { 673 gcOpt = true; 674 interactive = false; 675 continue; 676 } 677 if (arg.equals("-j")) { 678 jOption = true; 679 interactive = false; 680 continue; 681 } 682 if (arg.equals("-l")) { 683 lOption = true; 684 interactive = false; 685 continue; 686 } 687 if (arg.equals("-n") && nextArgument) { 688 jonasName = args[++argn]; 689 namedServer = true; 690 continue; 691 } 692 if (arg.equals("-r") && nextArgument) { 693 fileName = args[++argn]; 694 rOption = true; 695 interactive = false; 696 continue; 697 } 698 if (arg.equals("-s")) { 699 sOption = true; 700 interactive = false; 701 continue; 702 } 703 if (arg.equals("-sync")) { 704 syncOpt = true; 705 interactive = false; 706 continue; 707 } 708 if (arg.equals("-passivate")) { 709 passivateOpt = true; 710 interactive = false; 711 continue; 712 } 713 if (arg.equals("-debug") && nextArgument) { 714 topic = args[++argn]; 715 debugOption = true; 716 interactive = false; 717 continue; 718 } 719 if (arg.equals("-t")) { 720 tOption = true; 721 interactive = false; 722 continue; 723 } 724 if (arg.equals("-tt") && nextArgument) { 725 timeout = args[++argn]; 726 ttOpt = true; 727 interactive = false; 728 continue; 729 } 730 if (arg.equals("-isdeployed") && nextArgument) { 731 fileName = args[++argn]; 732 isDeployedOpt = true; 733 interactive = false; 734 continue; 735 } 736 System.out.println("Bad option: " + arg); 737 usage(); 738 System.exit(2); 739 } 740 741 if (!namedServer) { 742 jonasName = "jonas"; } 744 745 if (pingOption) { 746 waitServer(); 748 return; 749 } 750 751 checkAdm(); 753 754 if (aOption) { 755 addFile(fileName); 757 } 758 if (rOption) { 759 removeFile(fileName); 761 } 762 if (lOption) { 763 listbeans(); 765 } 766 if (jOption) { 767 listnames(); 769 } 770 if (eOption) { 771 listproperties(); 773 } 774 if (tOption) { 775 listTopics(); 777 } 778 if (debugOption) { 779 setTopic(topic, "DEBUG"); 781 } 782 if (sOption) { 783 stopserver(); 785 } 786 if (ttOpt) { 787 setTTimeout(timeout); 789 } 790 if (syncOpt) { 791 sync(false); 793 } 794 if (customOpt) { 795 custom(); 796 } 797 if (passivateOpt) { 798 sync(true); 800 } 801 if (gcOpt) { 802 runGC(); 804 } 805 if (isDeployedOpt) { 806 isDeployedFile(fileName); 808 } 809 810 if (interactive) { 812 menu(); 813 } 814 815 if (!interactive) { 817 if (isError) { 818 System.exit(2); 819 } 820 } 821 } 822 } | Popular Tags |