1 25 26 package org.objectweb.jonas.adm; 27 28 import java.io.File ; 29 import java.io.FileOutputStream ; 30 import java.io.IOException ; 31 import java.net.MalformedURLException ; 32 import java.net.URL ; 33 import java.net.URLClassLoader ; 34 import java.rmi.RemoteException ; 35 import java.util.ArrayList ; 36 import java.util.Collection ; 37 import java.util.Enumeration ; 38 import java.util.Iterator ; 39 import java.util.List ; 40 import java.util.Properties ; 41 import java.util.Vector ; 42 43 import javax.naming.InitialContext ; 44 import javax.naming.NameClassPair ; 45 import javax.naming.NamingEnumeration ; 46 import javax.naming.NamingException ; 47 import javax.rmi.PortableRemoteObject ; 48 49 import org.objectweb.common.Cmd; 50 import org.objectweb.jonas.common.JProp; 51 import org.objectweb.jonas.common.LogManagement; 52 import org.objectweb.jonas.container.EJBServiceImpl; 53 import org.objectweb.jonas.dbm.ConnectionManager; 54 import org.objectweb.jonas.dbm.DataBaseServiceImpl; 55 import org.objectweb.jonas.dbm.Pool; 56 import org.objectweb.jonas.ear.EarServiceException; 57 import org.objectweb.jonas.ear.EarServiceImplMBean; 58 import org.objectweb.jonas.jtm.TransactionServiceImpl; 59 import org.objectweb.jonas.mail.MailServiceImplMBean; 60 import org.objectweb.jonas.naming.NamingManager; 61 import org.objectweb.jonas.resource.ResourceServiceException; 62 import org.objectweb.jonas.resource.ResourceServiceImplMBean; 63 import org.objectweb.jonas.server.Server; 64 import org.objectweb.jonas.service.ServiceException; 65 import org.objectweb.jonas.service.ServiceManager; 66 import org.objectweb.jonas.web.AbsJWebContainerServiceImpl; 67 import org.objectweb.jonas.web.AbsJWebContainerServiceImplMBean; 68 import org.objectweb.jonas.web.JWebContainerServiceException; 69 import org.objectweb.jonas_ear.deployment.api.EarDeploymentDesc; 70 import org.objectweb.jonas_ear.deployment.api.EarDeploymentDescException; 71 import org.objectweb.jonas_ear.deployment.lib.wrapper.EarManagerWrapper; 72 import org.objectweb.jonas_ejb.container.Container; 73 import org.objectweb.jonas_ejb.container.JFactory; 74 import org.objectweb.jonas_ejb.genic.GenIC; 75 import org.objectweb.jonas_web.deployment.api.WebContainerDeploymentDesc; 76 import org.objectweb.jonas_web.deployment.lib.wrapper.WebManagerWrapper; 77 78 88 public class Adm extends PortableRemoteObject implements AdmInterface { 89 90 public static final String ADMNAME_SUFFIX = "_Adm"; 91 92 private ServiceManager sm = null; 93 94 private EJBServiceImpl ejbserv = null; 95 96 private DataBaseServiceImpl dbmserv = null; 97 98 private TransactionServiceImpl tm = null; 99 100 private JProp jonasProperties = null; 101 102 private InitialContext ictx = null; 103 104 private boolean isEJBContainer; 105 106 public static final int NOT_READY = 0; 108 109 public static final int READY = 1; 110 111 public static final int STOPPED = 2; 112 113 private int serverState = NOT_READY; 114 115 118 public static final int SLEEP_DELAY = 2000; 119 120 123 private AbsJWebContainerServiceImplMBean webContainerService = null; 124 125 128 private EarServiceImplMBean earService = null; 129 130 133 private ResourceServiceImplMBean resourceService = null; 134 135 138 private MailServiceImplMBean mailService = null; 139 140 144 public Adm(JProp jp) throws RemoteException , NamingException , ServiceException, Exception { 145 146 jonasProperties = jp; 147 148 sm = ServiceManager.getInstance(); 150 151 String name = null; 154 name = jonasProperties.getValue("jonas.name", "jonas") + ADMNAME_SUFFIX; 155 ictx = NamingManager.getInstance().getInitialContext(); 156 ictx.rebind(name, this); 157 } 158 159 private TransactionServiceImpl getTM() { 160 if (tm == null) { 161 try { 162 tm = (TransactionServiceImpl) sm.getTransactionService(); 163 } catch (ServiceException e) { 164 } 165 } 166 return tm; 167 } 168 169 private DataBaseServiceImpl getDBM() { 170 if (dbmserv == null) { 171 try { 172 dbmserv = (DataBaseServiceImpl) sm.getDataBaseService(); 173 }catch (ServiceException e) { 174 } 175 } 176 return dbmserv; 177 } 178 179 183 private AbsJWebContainerServiceImplMBean getWebContainerService() { 184 if (webContainerService == null) { 185 try { 186 webContainerService = (AbsJWebContainerServiceImplMBean) sm.getWebContainerService(); 187 } catch (ServiceException e) { 188 } 190 } 191 return webContainerService; 192 } 193 194 198 private MailServiceImplMBean getMailService() { 199 if (mailService == null) { 200 try { 201 mailService = (MailServiceImplMBean) sm.getMailService(); 202 } catch (Exception e) { 203 } 205 } 206 return mailService; 207 } 208 209 213 private EarServiceImplMBean getEarService() { 214 if (earService == null) { 215 try { 216 earService = (EarServiceImplMBean) sm.getEarService(); 217 } catch (Exception e) { 218 } 220 } 221 return earService; 222 } 223 224 228 private ResourceServiceImplMBean getResourceService() { 229 if (resourceService == null) { 230 try { 231 resourceService = (ResourceServiceImplMBean) sm.getResourceService(); 232 } catch (Exception e) { 233 } 235 } 236 return resourceService; 237 } 238 239 243 246 public String [] getTopics() throws RemoteException { 247 return LogManagement.getInstance().getTopics(); 248 } 249 250 253 public String getTopicLevel(String topic) throws RemoteException { 254 return LogManagement.getInstance().getTopicLevel(topic); 255 } 256 257 260 public void setTopicLevel(String topic, String l) throws RemoteException { 261 LogManagement.getInstance().setTopicLevel(topic, l); 262 } 263 264 268 public void addBeans(String fileName) throws RemoteException { 269 if (!isEJBContainer) { 270 return; 271 } 272 try { 273 ejbserv.createContainer(fileName); 274 } catch (Exception e) { 275 throw new RemoteException ("Cannot add beans", e); 276 } 277 } 278 279 285 public void addEar(String fileName) throws RemoteException , EarServiceException { 286 if (getEarService() != null) { 287 try { 288 getEarService().deployEarMBean(fileName); 289 } catch (EarServiceException ear) { 290 throw ear; 291 } catch (Exception e) { 292 throw new RemoteException ("Unable to deploy the EAR '" + fileName + "'.", e); 293 } 294 } else { 295 throw new EarServiceException("Can't add the Ear file " + fileName + ". The ear service is not started"); 296 } 297 } 298 299 305 public boolean isEarLoaded(String fileName) throws RemoteException , EarServiceException { 306 boolean isLoaded = false; 307 if (getEarService() != null) { 308 isLoaded = getEarService().isEarLoaded(fileName); 309 } else { 310 throw new EarServiceException("Can't test if the ear file " + fileName 311 + " is deployed or not. The ear service is not started"); 312 } 313 return isLoaded; 314 } 315 316 322 public void addRar(String fileName) throws RemoteException , ResourceServiceException { 323 if (getResourceService() != null) { 324 getResourceService().deployRarMBean(fileName); 325 } else { 326 throw new ResourceServiceException("Can't add the Rar file " + fileName 327 + ". The resource service is not started"); 328 } 329 } 330 331 338 public boolean isRarLoaded(String fileName) throws RemoteException , ResourceServiceException { 339 boolean isLoaded = false; 340 if (getResourceService() != null) { 341 isLoaded = getResourceService().isRarLoaded(fileName); 342 } else { 343 throw new ResourceServiceException("Can't test if the rar file " + fileName 344 + " is deployed or not. The rar service is not started"); 345 } 346 return isLoaded; 347 } 348 349 355 public void addWar(String fileName) throws RemoteException , JWebContainerServiceException { 356 if (getWebContainerService() != null) { 357 getWebContainerService().registerWarMBean(fileName); 358 } else { 359 throw new JWebContainerServiceException("Can't add the war file " + fileName 360 + ". The web container service is not started"); 361 } 362 } 363 364 370 public boolean isWarLoaded(String fileName) throws RemoteException , JWebContainerServiceException { 371 boolean isLoaded = false; 372 if (getWebContainerService() != null) { 373 isLoaded = getWebContainerService().isWarLoaded(fileName); 374 } else { 375 throw new JWebContainerServiceException("Can't test if the war file " + fileName 376 + " is deployed or not. The war service is not started"); 377 } 378 return isLoaded; 379 } 380 381 387 public void removeEar(String fileName) throws RemoteException , EarServiceException { 388 if (getEarService() != null) { 389 try { 390 getEarService().unDeployEarMBean(fileName); 391 runGC(); 393 } catch (EarServiceException ear) { 394 throw ear; 395 } catch (Exception e) { 396 throw new RemoteException ("Cannot remove ear", e); 397 } 398 } else { 399 throw new EarServiceException("Can't remove of the Ear file " + fileName 400 + ". The ear service is not started"); 401 } 402 } 403 404 410 public void removeRar(String fileName) throws RemoteException , ResourceServiceException { 411 if (getResourceService() != null) { 412 getResourceService().unDeployRarMBean(fileName); 413 runGC(); 415 } else { 416 throw new ResourceServiceException("Can't remove of the Rar file " + fileName 417 + ". The resource service is not started"); 418 } 419 } 420 421 427 public void removeWar(String fileName) throws RemoteException , JWebContainerServiceException { 428 if (getWebContainerService() != null) { 429 getWebContainerService().unRegisterWarMBean(fileName); 430 runGC(); 432 } else { 433 throw new JWebContainerServiceException("Can't remove of the war file " + fileName 434 + ". The web container service is not started"); 435 } 436 } 437 438 442 public void removeBeans(String fileName) throws RemoteException { 443 if (!isEJBContainer) { 444 return; 445 } 446 try { 447 ejbserv.removeContainer(fileName); 448 } catch (Exception e) { 449 throw new RemoteException ("Cannot remove bean", e); 450 } 451 runGC(); 453 } 454 455 459 public boolean isLoaded(String fileName) throws RemoteException { 460 return (ejbserv.getContainer(fileName) != null); 461 } 462 463 public String dumpCustom() throws RemoteException { 464 String ret = ""; 465 if (!isEJBContainer) { 466 return ret; 467 } 468 ret += Server.jvmInfos(); 470 471 if (getTM() != null) { 473 ret += "TM timeout=" + getTM().getTimeout(); 474 ret += "\n"; 475 } 476 477 if (getDBM() != null) { 479 Collection dslist = getDBM().getDSList(); 480 for (Iterator i = dslist.iterator(); i.hasNext(); ) { 481 ConnectionManager cm = (ConnectionManager) i.next(); 482 ret += cm.getDSName() + ":lockPolicy=" + cm.getTransactionIsolation(); 483 Pool pool = cm.getPool(); 484 ret += ":minPoolSize=" + pool.getPoolMin(); 485 ret += ":maxPoolSize=" + pool.getPoolMax(); 486 ret += ":maxOpenTime=" + pool.getMaxOpenTime(); 487 ret += ":maxWaitTime=" + pool.getMaxWaitTime(); 488 ret += ":maxWaiters=" + pool.getMaxWaiters(); 489 ret += "\n"; 490 } 491 } 492 493 Container[] lcont = (Container[]) ejbserv.listContainers(); 495 for (int j = 0; j < lcont.length; j++) { 496 Container cont = lcont[j]; 497 String contname = cont.getName(); 498 String [] lbn = cont.listBeanNames(); 499 for (int k = 0; k < lbn.length; k++) { 500 JFactory bf = (JFactory) cont.getBeanFactory(lbn[k]); 501 ret += contname + ":=" + lbn[k]; 502 ret += ":minPoolSize=" + bf.getMinPoolSize(); 503 ret += ":maxCacheSize=" + bf.getMaxCacheSize(); 504 ret += "\n"; 505 } 506 } 507 return ret; 508 } 509 510 513 public String [] listBeans() throws RemoteException { 514 515 if (!isEJBContainer) { 516 return new String [0]; 517 } 518 519 Container[] lcont = ejbserv.listContainers(); 520 Vector lbeans = new Vector (); 521 for (int j = 0; j < lcont.length; j++) { 522 String [] lbn = lcont[j].listBeanNames(); 523 String contname = lcont[j].getName(); 524 for (int k = 0; k < lbn.length; k++) { 525 String s = new String (contname + ": " + lbn[k]); 526 lbeans.addElement(s); 527 } 528 } 529 String [] ret = new String [lbeans.size()]; 530 lbeans.copyInto(ret); 531 532 return ret; 533 } 534 535 538 public Vector listContext() throws RemoteException { 539 String name = null; 540 Vector ret = new Vector (); 541 try { 542 NamingEnumeration ne; 543 ne = NamingManager.getInstance().getInitialContext().list(""); 544 for (Enumeration e = ne; e.hasMoreElements();) { 545 name = ((NameClassPair ) e.nextElement()).getName(); 546 ret.addElement(name); 547 } 548 } catch (Exception ex) { 549 throw new RemoteException ("Cannot list JNDI context", ex); 550 } 551 return ret; 552 } 553 554 558 public Properties listEnv() { 559 return jonasProperties.getConfigFileEnv(); 560 } 561 562 565 public void stopServer() throws RemoteException { 566 try { 567 serverState = STOPPED; 568 Thread.sleep(5000); 571 sm.stopServices(); 572 } catch (ServiceException e) { 573 throw new RemoteException ("Cannot stop services: ", e); 574 } catch (InterruptedException e) { 575 } 576 } 577 578 581 public void killServer() throws RemoteException { 582 stopServer(); 583 new Thread (new Runnable () { 586 587 public void run() { 588 try { 589 Thread.sleep(SLEEP_DELAY); 591 } catch (InterruptedException ie) { 592 ie.printStackTrace(); 593 throw new IllegalStateException ("Cannot wait: " + ie.getMessage()); 594 } 595 System.exit(0); 596 } 597 }).start(); 598 599 } 600 601 605 public int getServerState() throws RemoteException { 606 return serverState; 607 } 608 609 612 public boolean isEJBContainer() throws RemoteException { 613 return isEJBContainer; 614 } 615 616 619 public void setTransactionTimeout(int timeout) throws RemoteException { 620 getTM().setTimeout(timeout); 621 } 622 623 626 public void runGC() throws RemoteException { 627 Runtime.getRuntime().gc(); 628 } 629 630 634 public void syncAllEntities(boolean passivate) throws RemoteException { 635 if (!isEJBContainer) { 636 return; 637 } 638 ejbserv.syncAllEntities(passivate); 639 } 640 641 645 648 public void serverReady(boolean isEJB) { 649 serverState = READY; 650 isEJBContainer = isEJB; 651 if (isEJBContainer) { 652 ejbserv = (EJBServiceImpl) sm.getEjbService(); 653 } 654 } 655 656 668 public String deployFile(int type, byte[] bfile, String filename) throws RemoteException , EarServiceException, 669 JWebContainerServiceException { 670 try { 671 String directory = ""; 672 673 if (type == TYPE_EJB) { 674 directory = ejbserv.getEjbjarsDirectory(); 675 } else if (type == TYPE_EAR) { 676 directory = getEarService().getAppsDirectory(); 677 } else if (type == TYPE_WAR) { 678 directory = getWebContainerService().getWebappsDirectory(); 679 } else if (type == TYPE_RAR) { 680 directory = getResourceService().getRarsDirectory(); 681 } else if (type == TYPE_CAR) { 682 throw new UnsupportedOperationException ("Not Supported yet"); 683 } 684 685 File file = new File (directory + filename); 686 687 FileOutputStream out = new FileOutputStream (file); 688 out.write(bfile); 689 out.close(); 690 if (type == TYPE_EJB) { 691 String args[] = new String [1]; 692 args[0] = directory + filename; 693 GenIC.main(args); 694 } else if (type == TYPE_EAR) { 695 unpackAndCompileEar(file); 696 } 697 698 return file.getAbsolutePath(); 699 700 } catch (Exception e) { 701 e.printStackTrace(); 702 throw new RemoteException (e.toString(), e); 703 } 704 } 705 706 private void unpackAndCompileEar(File file) throws EarServiceException, IOException , EarDeploymentDescException { 707 URL earUrl[] = new URL [1]; 708 709 try { 710 earUrl[0] = file.toURL(); 711 } catch (MalformedURLException e) { 712 String err = "Invalid ear file name '" + file; 713 throw new EarServiceException(err, e); 714 } 715 716 ClassLoader currentLoader = Thread.currentThread().getContextClassLoader(); 719 URLClassLoader loaderCls = new URLClassLoader (earUrl, currentLoader); 720 721 EarDeploymentDesc desc = EarManagerWrapper.getDeploymentDesc(earUrl[0].getFile(), loaderCls); 722 723 String [] ejbTags = desc.getEjbTags(); 724 729 String javaHomeBin = System.getProperty("java.home", ""); 730 if (!("".equals(javaHomeBin))) { 731 javaHomeBin = javaHomeBin + File.separator + ".." + File.separator + "bin" + File.separator; 732 } 733 734 Cmd cmd = null; 735 cmd = new Cmd(javaHomeBin + "jar"); 736 cmd.addArgument("-xf"); 737 cmd.addArgument(file.getAbsolutePath()); 738 739 for (int i = 0; i < ejbTags.length; i++) { 740 cmd.addArgument(ejbTags[i]); 741 } 742 743 boolean exitCmd = cmd.run(); 744 if (!exitCmd) { 746 throw new EarServiceException("Failed when extracting the the ejb jar " + "in the given jar file '" + file 747 + "'."); 748 } 749 750 for (int i = 0; i < ejbTags.length; i++) { 751 String [] args = new String [1]; 752 args[0] = ejbTags[i]; 753 GenIC.main(args); 754 } 755 756 cmd = new Cmd(javaHomeBin + "jar"); 757 cmd.addArgument("-uf"); 758 cmd.addArgument(file.getAbsolutePath()); 759 760 for (int i = 0; i < ejbTags.length; i++) { 761 cmd.addArgument(ejbTags[i]); 762 } 763 exitCmd = cmd.run(); 764 if (!exitCmd) { 766 throw new EarServiceException("Failed when extracting the the ejb jar " + "in the given jar file '" + file 767 + "'."); 768 } 769 770 for (int i = 0; i < ejbTags.length; i++) { 771 File del = new File (ejbTags[i]); 772 del.delete(); 773 } 774 775 } 776 777 785 786 public List listModules(int type, int state) throws RemoteException { 787 try { 788 789 List modules = new ArrayList (); 791 if (type == TYPE_EJB) { 792 List jars = null; 794 if (state == STATUS_RUNNING || state == STATUS_ALL) { 795 jars = ejbserv.getDeployedJars(); 796 for (int i = 0; i < jars.size(); i++) { 797 ModuleDesc desc = new ModuleDesc((String ) jars.get(i), STATUS_RUNNING, null); 798 modules.add(desc); 799 } 800 } else if (state == STATUS_STOPPED || state == STATUS_ALL) { 801 jars = ejbserv.getDeployableJars(); 802 for (int i = 0; i < jars.size(); i++) { 803 ModuleDesc desc = new ModuleDesc((String ) jars.get(i), STATUS_STOPPED, null); 804 modules.add(desc); 805 } 806 } 807 } else if (type == TYPE_EAR) { 808 List ears = null; 810 if (state == STATUS_RUNNING || state == STATUS_ALL) { 811 ears = getEarService().getDeployedEars(); 812 for (int i = 0; i < ears.size(); i++) { 813 ModuleDesc desc = new ModuleDesc((String ) ears.get(i), STATUS_RUNNING, null); 814 modules.add(desc); 815 } 816 } else if (state == STATUS_STOPPED || state == STATUS_ALL) { 817 ears = getEarService().getDeployableEars(); 818 for (int i = 0; i < ears.size(); i++) { 819 ModuleDesc desc = new ModuleDesc((String ) ears.get(i), STATUS_STOPPED, null); 820 modules.add(desc); 821 } 822 } 823 } else if (type == TYPE_WAR) { 824 List wars = null; 826 AbsJWebContainerServiceImpl webService = (AbsJWebContainerServiceImpl) getWebContainerService(); if (state == STATUS_RUNNING || state == STATUS_ALL) { 829 wars = webService.getDeployedWars(); 830 for (int i = 0; i < wars.size(); i++) { 831 String warName = (String ) wars.get(i); 833 ClassLoader cl = webService.getClassLoader(new File (warName).toURL(), null, null); 834 WebContainerDeploymentDesc wdesc = WebManagerWrapper.getDeploymentDesc(warName, cl); 835 int start = warName.lastIndexOf("/"); 836 if (start < 0) { 837 start = 0; 838 } 839 String url = wdesc.getContextRoot() != null ? wdesc.getContextRoot() : warName.substring(start, 840 warName.indexOf(".war")); 841 ModuleDesc desc = new ModuleDesc(warName, STATUS_RUNNING, url); 842 modules.add(desc); 843 } 844 } else if (state == STATUS_STOPPED || state == STATUS_ALL) { 845 wars = webService.getDeployableWars(); 846 for (int i = 0; i < wars.size(); i++) { 847 String warName = (String ) wars.get(i); 849 ClassLoader cl = webService.getClassLoader(new File (warName).toURL(), null, null); 850 WebContainerDeploymentDesc wdesc = WebManagerWrapper.getDeploymentDesc(warName, cl); 851 int start = warName.lastIndexOf("/"); 852 if (start < 0) { 853 start = 0; 854 } 855 856 String url = wdesc.getContextRoot() != null ? wdesc.getContextRoot() : warName.substring(start, 857 warName.indexOf(".war")); 858 ModuleDesc desc = new ModuleDesc(warName, STATUS_STOPPED, url); 859 modules.add(desc); 860 } 861 } 862 } else if (type == TYPE_RAR) { 863 List rars = getResourceService().getInstalledRars(); 865 for (int i = 0; i < rars.size(); i++) { 866 ModuleDesc desc = new ModuleDesc((String ) rars.get(i), STATUS_RUNNING, null); 867 modules.add(desc); 868 } 869 } else if (type == TYPE_CAR) { 870 throw new UnsupportedOperationException ("Not Supported yet"); 871 } 872 873 return modules; 874 } catch (Exception e) { 875 throw new RemoteException ("Failed to list modules", e); 877 } 878 879 } 880 881 887 public void undeployFile(String filename) throws RemoteException { 888 try { 889 File f = new File (filename); 890 f.delete(); 891 } catch (Exception ioe) { 892 throw new RemoteException ("Failed to remove the module " + filename + ":", ioe); 893 } 894 895 } 896 897 } 898 | Popular Tags |