1 25 26 package org.objectweb.jonas.web.catalina55; 27 28 import java.io.File ; 29 import java.io.FileInputStream ; 30 import java.io.FileNotFoundException ; 31 import java.io.InputStream ; 32 import java.lang.reflect.Method ; 33 import java.net.URL ; 34 import java.rmi.RemoteException ; 35 import java.util.ArrayList ; 36 import java.util.Iterator ; 37 import java.util.List ; 38 import java.util.StringTokenizer ; 39 import java.util.Vector ; 40 41 import javax.management.InstanceAlreadyExistsException ; 42 import javax.management.MBeanRegistrationException ; 43 import javax.management.MBeanServer ; 44 import javax.management.MalformedObjectNameException ; 45 import javax.management.NotCompliantMBeanException ; 46 import javax.management.ObjectName ; 47 import javax.naming.LinkRef ; 48 import javax.naming.NamingException ; 49 import javax.naming.Reference ; 50 import javax.naming.StringRefAddr ; 51 52 import org.xml.sax.InputSource ; 53 54 import org.apache.catalina.Container; 55 import org.apache.catalina.Context; 56 import org.apache.catalina.Engine; 57 import org.apache.catalina.Host; 58 import org.apache.catalina.Lifecycle; 59 import org.apache.catalina.LifecycleException; 60 import org.apache.catalina.Realm; 61 import org.apache.catalina.Server; 62 import org.apache.catalina.Service; 63 import org.apache.catalina.connector.Connector; 64 import org.apache.catalina.core.StandardContext; 65 import org.apache.catalina.core.StandardEngine; 66 import org.apache.catalina.core.StandardServer; 67 import org.apache.catalina.deploy.ContextResource; 68 import org.apache.catalina.deploy.NamingResources; 69 import org.apache.catalina.startup.ContextConfig; 70 import org.apache.commons.modeler.Registry; 71 import org.apache.tomcat.util.digester.Digester; 72 73 import org.objectweb.jonas_lib.I18n; 74 75 import org.objectweb.jonas.jmx.J2eeObjectName; 76 import org.objectweb.jonas.jmx.JonasObjectName; 77 import org.objectweb.jonas.security.realm.web.catalina55.JACC; 78 import org.objectweb.jonas.server.LoaderManager; 79 import org.objectweb.jonas.service.ServiceException; 80 import org.objectweb.jonas.web.AbsJWebContainerServiceImpl; 81 import org.objectweb.jonas.web.JWebContainerServiceException; 82 import org.objectweb.jonas.web.lib.PermissionManager; 83 import org.objectweb.jonas.web.wrapper.CatalinaJWebContainerService; 84 85 import org.objectweb.util.monolog.api.BasicLevel; 86 87 93 public class CatalinaJWebContainerServiceImpl 94 extends AbsJWebContainerServiceImpl 95 implements CatalinaJWebContainerService { 96 97 100 protected static final String CONFIG_FILE = "conf/server.xml"; 101 102 105 private static I18n i18n = null; 106 107 110 private Server server = null; 111 112 115 private ClassLoader parentClassLoader = null; 116 117 120 private ClassLoader catalinaLoader = null; 121 122 125 private boolean tomcatStarted = false; 126 127 128 133 protected void doInit(javax.naming.Context ctx) throws ServiceException { 134 super.doInit(ctx); 135 136 initCatalinaEnvironment(); 138 139 LoaderManager lm = LoaderManager.getInstance(); 141 try { 142 parentClassLoader = lm.getAppsLoader(); 143 catalinaLoader = lm.getCatalinaLoader(); 144 } catch (Exception e1) { 145 throw new ServiceException("Cannot get Application/Catalina ClassLoader", e1); 146 } 147 148 i18n = I18n.getInstance(CatalinaJWebContainerServiceImpl.class, catalinaLoader); 150 151 if (getLogger().isLoggable(BasicLevel.DEBUG)) { 153 getLogger().log(BasicLevel.DEBUG, "Add mbeans descriptions to the JMX Registry"); 154 } 155 InputStream stream = this.getClass().getResourceAsStream("/org/objectweb/jonas/web/catalina55/mbeans-descriptors.xml"); 156 try { 157 Registry.loadRegistry(stream); 158 stream.close(); 159 } catch (Exception e) { 160 getLogger().log(BasicLevel.ERROR, "Can't add the mbeans description into Tomcat" + e.getMessage()); 161 } 162 163 CatalinaConnectorFactory cf = new CatalinaConnectorFactory(); 165 MBeanServer mbeanServer = getMbeanServer(); 166 cf.setMyServer(mbeanServer); 167 if (mbeanServer != null) { 168 ObjectName on = null; 169 try { 170 on = JonasObjectName.catalinaConnectorFactory(getDomainName()); 171 mbeanServer.registerMBean(cf, on); 172 } catch (MalformedObjectNameException e1) { 173 getLogger().log(BasicLevel.WARN, "Can't create Objectname for CatalinaConnectorFactory MBean"); 174 } catch (InstanceAlreadyExistsException e2) { 175 getLogger().log(BasicLevel.WARN, "MBean named " + on.toString() + "already registered in the MBean server"); 176 } catch (MBeanRegistrationException e2) { 177 getLogger().log(BasicLevel.WARN, "Can't register MBean " + on.toString()); 178 e2.printStackTrace(); 179 } catch (NotCompliantMBeanException e2) { 180 getLogger().log(BasicLevel.WARN, "Can't register MBean " + on.toString()); 181 e2.printStackTrace(); 182 } 183 } 184 185 WebModuleProxy px = new WebModuleProxy(); 187 px.setMyServer(mbeanServer); 188 if (mbeanServer != null) { 189 ObjectName on = null; 190 try { 191 on = JonasObjectName.webModuleProxy(getDomainName()); 192 mbeanServer.registerMBean(px, on); 193 } catch (MalformedObjectNameException e1) { 194 getLogger().log(BasicLevel.WARN, "Can't create Objectname for WebModuleProxy MBean"); 195 } catch (InstanceAlreadyExistsException e2) { 196 getLogger().log(BasicLevel.WARN, "MBean named " + on.toString() + "already registered in the MBean server"); 197 } catch (MBeanRegistrationException e2) { 198 getLogger().log(BasicLevel.WARN, "Can't register MBean " + on.toString()); 199 e2.printStackTrace(); 200 } catch (NotCompliantMBeanException e2) { 201 getLogger().log(BasicLevel.WARN, "Can't register MBean " + on.toString()); 202 e2.printStackTrace(); 203 } 204 } 205 } 206 207 208 213 protected void initCatalinaEnvironment() throws ServiceException { 214 215 String catalinaHome = System.getProperty("catalina.home"); 218 if (catalinaHome != null) { 219 if (System.getProperty("catalina.base") == null) { 220 System.setProperty("catalina.base", catalinaHome); 221 } 222 } else { 223 throw new ServiceException("catalina.home property is not set"); 224 } 225 226 System.setProperty("catalina.useNaming", "false"); 228 } 229 230 231 235 public void doStart() throws ServiceException { 236 237 Digester digester = createServerDigester(); 239 240 File configFile = null; 243 244 try { 245 configFile = getConfigFile(); 246 } catch (FileNotFoundException e) { 247 String err = i18n.getMessage("CatJWebServ.doStart.fileNotFound", configFile, e.getMessage()); 248 getLogger().log(BasicLevel.ERROR, err); 249 throw new ServiceException(err, e); 250 } 251 252 try { 253 InputSource is = new InputSource ("file://" + configFile.getAbsolutePath()); 254 FileInputStream fis = new FileInputStream (configFile); 255 is.setByteStream(fis); 256 digester.push(this); 257 digester.parse(is); 258 fis.close(); 259 } catch (Exception e) { 260 String err = i18n.getMessage("CatJWebServ.doStart.readfileError", configFile, e.getMessage()); 261 getLogger().log(BasicLevel.ERROR, err); 262 throw new ServiceException(err, e); 263 } 264 265 javax.naming.Context ctxOld = setGlobalNamingResources(); 266 267 Iterator it = getEngines().iterator(); 269 while (it.hasNext()) { 270 StandardEngine oSE = (StandardEngine) it.next(); 271 oSE.setDomain(getDomainName()); 277 oSE.setName(getDomainName()); 278 } 279 280 if (server instanceof Lifecycle) { 282 try { 283 server.initialize(); 284 ((Lifecycle) server).start(); 285 } catch (Exception e) { 286 String err = i18n.getMessage("CatJWebServ.doStart.startCatalinaError"); 287 getLogger().log(BasicLevel.ERROR, err); 288 throw new ServiceException(e.getMessage(), e); 289 } 290 } 291 getNaming().setComponentContext(ctxOld); 293 294 tomcatStarted = true; 296 297 super.doStart(); 299 } 300 301 308 protected javax.naming.Context setGlobalNamingResources() throws ServiceException { 309 NamingResources namingResources = server.getGlobalNamingResources(); 311 javax.naming.Context globalCtx = null; 312 javax.naming.Context ctxold = null; 313 try { 314 globalCtx = getNaming().createEnvironmentContext("catalina_global"); 315 ctxold = getNaming().setComponentContext(globalCtx); 316 317 int i; 318 ContextResource[] resources = namingResources.findResources(); 320 for (i = 0; i < resources.length; i++) { 321 addResource(globalCtx, resources[i]); 322 } 323 } catch (NamingException e) { 324 String err = i18n.getMessage("CatJWebServ.doStart.populateEnvError", e.getMessage()); 325 getLogger().log(BasicLevel.ERROR, err); 326 throw new ServiceException(err, e); 327 } 328 ((StandardServer) server).setGlobalNamingContext(globalCtx); 330 return ctxold; 331 } 332 333 334 338 protected void doStop() throws ServiceException { 339 super.doStop(); 341 342 if (server instanceof Lifecycle) { 344 try { 345 ((Lifecycle) server).stop(); 346 } catch (Exception e) { 347 String err = i18n.getMessage("CatJWebServ.doStop.stopCatalinaError"); 348 getLogger().log(BasicLevel.ERROR, err); 349 throw new ServiceException(e.getMessage(), e); 350 } 351 } 352 353 } 354 355 356 363 protected void doRegisterWar(javax.naming.Context ctx) 364 throws JWebContainerServiceException { 365 378 URL warURL = null; 379 URL earURL = null; 380 URL unpackedWarURL = null; 381 String contextRoot = null; 382 boolean java2DelegationModel = true; 383 PermissionManager permissionManager = null; 384 boolean inEarCase = true; 385 String earAppName = null; 386 String jonasDD = null; 387 try { 388 warURL = (URL ) ctx.lookup("warURL"); 389 unpackedWarURL = (URL ) ctx.lookup("unpackedWarURL"); 390 contextRoot = (String ) ctx.lookup("contextRoot"); 391 Boolean bool = (Boolean ) ctx.lookup("java2DelegationModel"); 392 java2DelegationModel = bool.booleanValue(); 393 jonasDD = (String ) ctx.lookup("jonasDD"); 394 permissionManager = (PermissionManager) ctx.lookup("permissionManager"); 395 } catch (NamingException e) { 396 String err = i18n.getMessage("CatJWebServ.doRegisterWar.gettingParamError"); 397 getLogger().log(BasicLevel.ERROR, err + e.getMessage()); 398 throw new JWebContainerServiceException(err, e); 399 } 400 401 try { 402 earAppName = (String ) ctx.lookup("earAppName"); 403 earURL = (URL ) ctx.lookup("earURL"); 404 } catch (NamingException e) { 405 inEarCase = false; 407 earURL = warURL; 409 } 410 411 ClassLoader webClassLoader = null; 412 413 try { 414 webClassLoader = (ClassLoader ) ctx.lookup("parentCL"); 415 } catch (NamingException e) { 416 String err = i18n.getMessage("CatJWebServ.doRegisterWar.gettingParamError"); 417 getLogger().log(BasicLevel.ERROR, err + e.getMessage()); 418 throw new JWebContainerServiceException(err, e); 419 } 420 421 String hostName = null; 422 try { 423 hostName = (String ) ctx.lookup("hostName"); 424 } catch (NamingException e) { 425 hostName = ""; 426 } 427 428 435 if (contextRoot.equals("/")) { 436 contextRoot = ""; 437 } else { 438 contextRoot = "/" + contextRoot; 439 } 440 441 File fWar = new File (warURL.getFile()); 443 445 String destDir = null; 447 if (fWar.isDirectory()) { 448 destDir = warURL.getPath(); 449 } else { 450 destDir = unpackedWarURL.getPath(); 451 } 452 453 File contextXmlFile = new File (destDir + File.separator + "META-INF" + File.separator + "context.xml"); 455 456 457 List jonasContexts = getConfiguredMatchingJonasContexts(contextRoot, fWar, destDir); 459 460 Host deployer = null; 461 if (jonasContexts.isEmpty()) { 463 deployer = findHost(hostName); 465 466 JOnASStandardContext context = new JOnASStandardContext(false, java2DelegationModel, inEarCase); 468 context.setDocBase(destDir); 469 context.setPath(contextRoot); 470 ContextConfig config = new ContextConfig(); 471 472 ((Lifecycle) context).addLifecycleListener(config); 474 jonasContexts.add(context); 475 } 476 477 Iterator it = jonasContexts.iterator(); 479 while (it.hasNext()) { 480 JOnASStandardContext jStdCtx = (JOnASStandardContext) it.next(); 481 482 jStdCtx.setParentClassLoader(webClassLoader); 484 485 jStdCtx.setDelegate(java2DelegationModel); 487 if (getLogger().isLoggable(BasicLevel.DEBUG)) { 488 getLogger().log(BasicLevel.DEBUG, "Webapp class loader java 2 delegation model set to " + java2DelegationModel); 489 } 490 491 if (contextXmlFile.exists()) { 493 jStdCtx.setConfigFile(contextXmlFile.getAbsolutePath()); 494 } 495 496 jStdCtx.setJ2EEServer(getJonasServerName()); 498 jStdCtx.setServer(J2eeObjectName.J2EEServer(getDomainName(), getJonasServerName()).toString()); 499 if (getMbeanServer() != null) { 500 try { 501 String [] as = (String []) getMbeanServer().getAttribute(J2eeObjectName.J2EEServer(getDomainName() 502 , getJonasServerName()), "javaVMs"); 503 jStdCtx.setJavaVMs(as); 504 } catch (Exception e) { 505 getLogger().log(BasicLevel.WARN, "Set MBean JVM error : " + e.getClass().getName() + " " + e.getMessage()); 508 } 509 } 510 if (earAppName != null) { 511 jStdCtx.setJ2EEApplication(earAppName); 512 } 513 jStdCtx.setJonasDeploymentDescriptor(jonasDD); 514 515 Realm ctxRealm = jStdCtx.getRealm(); 517 518 if (ctxRealm == null) { 520 ctxRealm = deployer.getRealm(); 521 } 522 523 524 if (ctxRealm != null && ctxRealm instanceof JACC) { 525 JACC jaccRealm = null; 526 try { 527 jaccRealm = (JACC) ((JACC) ctxRealm).clone(); 528 } catch (CloneNotSupportedException cnse) { 529 String err = "Cannot clone the realm used by the existing context or its parent realm"; 530 getLogger().log(BasicLevel.ERROR, err + cnse.getMessage()); 531 throw new JWebContainerServiceException(err, cnse); 532 } 533 if (getLogger().isLoggable(BasicLevel.DEBUG)) { 534 getLogger().log(BasicLevel.DEBUG, "Setting permission manager to " + permissionManager); 535 } 536 jaccRealm.setPermissionManager(permissionManager); 537 jaccRealm.setContext(jStdCtx); 538 jStdCtx.setRealm(jaccRealm); 540 } 541 542 jStdCtx.setWarURL(warURL); 546 if (inEarCase) { 547 jStdCtx.setEarURL(earURL); 548 } 549 550 if (jStdCtx.isInServerXml()) { 552 try { 553 jStdCtx.setLoader(null); 554 jStdCtx.start(); 555 } catch (LifecycleException lce) { 556 String err = i18n.getMessage("CatJWebServ.doRegisterWar.startContextError", jStdCtx); 557 getLogger().log(BasicLevel.ERROR, err + lce.getMessage()); 558 throw new JWebContainerServiceException(err, lce); 559 560 } 561 } else { 562 if (deployer == null) { 564 String err = i18n.getMessage("CatJWebServ.doRegisterWar.deployerIsNull", jStdCtx); 565 getLogger().log(BasicLevel.ERROR, err); 566 throw new JWebContainerServiceException(err); 567 } 568 deployer.addChild(jStdCtx); 569 } 570 571 checkStartedContext(jStdCtx, permissionManager); 573 574 try { 576 ctx.rebind("WebModule", jStdCtx.createObjectName(getDomainName(), jStdCtx.getParentName())); 577 } catch(Exception e) { 578 String err = "Cannot rebind WebModule ObjectName in Context"; 581 getLogger().log(BasicLevel.ERROR, err, e); 582 throw new JWebContainerServiceException(err, e); 583 } 584 } 585 } 586 587 588 596 protected List getConfiguredMatchingJonasContexts(String contextRoot, File fpackedWar, String destDir) { 597 ArrayList jonasContexts = new ArrayList (); 598 Iterator it = getContexts().iterator(); 602 while (it.hasNext()) { 603 Context ctx = (Context) it.next(); 604 if (!(ctx instanceof JOnASStandardContext)) { 606 continue; 607 } 608 JOnASStandardContext jStdCtx = (JOnASStandardContext) ctx; 609 if (jStdCtx != null) { 610 if (!jStdCtx.isInServerXml()) { 612 continue; 613 } 614 615 String serverCtxRoot = jStdCtx.getPath(); 617 if (!serverCtxRoot.equals(contextRoot)) { 618 continue; 619 } 620 if (jStdCtx.getPrivileged()) { 621 if (fpackedWar.isDirectory()) { 623 String err = i18n.getMessage("CatJWebServ.getConfiguredMatchingJonasContexts.privilegedContextError"); 624 getLogger().log(BasicLevel.ERROR, err); 625 } 626 jStdCtx.setDocBase(fpackedWar.getPath()); 627 } else { 628 jStdCtx.setDocBase(destDir); 629 } 630 jonasContexts.add(jStdCtx); 631 } 632 } 633 return jonasContexts; 634 } 635 636 637 643 protected void checkStartedContext(Context context, PermissionManager permissionManager) throws JWebContainerServiceException { 644 if (!context.getConfigured()) { 646 String err = i18n.getMessage("CatJWebServ.checkStartedContext.configuredFail", context.getName()); 647 getLogger().log(BasicLevel.ERROR, err); 648 throw new JWebContainerServiceException(err); 649 } 650 ((JOnASStandardContext) context).unsetAuthenticationCaching(); 651 652 Realm ctxRealm = context.getRealm(); 654 if (ctxRealm != null && ctxRealm instanceof JACC) { 655 JACC jaccRealm = (JACC) ctxRealm; 656 PermissionManager ctxPerm = jaccRealm.getPermissionManager(); 657 if (ctxPerm == null && permissionManager != null) { 658 jaccRealm.setPermissionManager(permissionManager); 659 } 660 } 661 if (getLogger().isLoggable(BasicLevel.DEBUG)) { 662 getLogger().log(BasicLevel.DEBUG, "context " + context + " started"); 663 } 664 665 } 666 667 672 protected synchronized List getEngines() throws JWebContainerServiceException { 673 List engines = new ArrayList (); 674 Service[] services = server.findServices(); 675 676 for (int s = 0; s < services.length; s++) { 678 Container cont = services[s].getContainer(); 679 if (!(cont instanceof Engine)) { 680 String err = "The container of the service must be an engine (server.xml)"; 681 throw new JWebContainerServiceException(err); 682 } 683 engines.add(cont); 684 } 685 return engines; 686 } 687 688 693 protected synchronized List getContexts() throws JWebContainerServiceException { 694 List contexts = new ArrayList (); 695 696 Iterator itEngines = getEngines().iterator(); 697 while (itEngines.hasNext()) { 698 Engine engine = (Engine) itEngines.next(); 699 Container[] hosts = engine.findChildren(); 700 701 for (int j = 0; j < hosts.length; j++) { 702 Container[] containers = hosts[j].findChildren(); 703 for (int k = 0; k < containers.length; k++) { 704 Container container = containers[k]; 705 if (container instanceof Context) { 706 contexts.add(container); 707 } 708 } 709 } 710 } 711 return contexts; 712 } 713 714 715 721 protected void doUnRegisterWar(javax.naming.Context ctx) 722 throws JWebContainerServiceException { 723 724 String contextRoot = null; 728 try { 729 contextRoot = (String ) ctx.lookup("contextRoot"); 730 } catch (NamingException e) { 731 String err = i18n.getMessage("CatJWebServ.doUnRegisterWar.gettingParamError"); 732 getLogger().log(BasicLevel.ERROR, err + e.getMessage()); 733 throw new JWebContainerServiceException(err, e); 734 } 735 736 if (contextRoot.equals("/")) { 737 contextRoot = ""; 738 } else { 739 contextRoot = "/" + contextRoot; 740 } 741 742 String hostName = null; 743 try { 744 hostName = (String ) ctx.lookup("hostName"); 745 } catch (NamingException e) { 746 if (getLogger().isLoggable(BasicLevel.DEBUG)) { 747 getLogger().log(BasicLevel.DEBUG, "No define hostname for this context"); 748 } 749 } 750 751 if (hostName == null) { 753 boolean found = false; 754 Iterator it = getContexts().iterator(); 755 while (it.hasNext()) { 756 Context context = (Context) it.next(); 757 String serverCtxRoot = context.getPath(); 758 if (serverCtxRoot.equals(contextRoot)) { 759 removeContext(context); 760 found = true; 761 } 762 } 763 if (!found) { 764 String err = i18n.getMessage("CatJWebServ.doUnRegisterWar.undeployError", contextRoot); 765 getLogger().log(BasicLevel.ERROR, err); 766 throw new JWebContainerServiceException(err); 767 } 768 } else { 769 Host host = findHost(hostName); 772 org.apache.catalina.Context context = host.map(contextRoot); 773 if (context != null) { 774 removeContext(context); 775 } else { 776 String err = i18n.getMessage("CatJWebServ.doUnRegisterWar.undeployError", contextRoot); 777 getLogger().log(BasicLevel.ERROR, err); 778 throw new JWebContainerServiceException(err); 779 } 780 } 781 } 782 783 784 792 public synchronized void removeContext(Context context) throws JWebContainerServiceException { 793 794 boolean found = false; 796 Iterator it = getContexts().iterator(); 797 while (it.hasNext() && !found) { 798 if (context == (Context) it.next()) { 799 found = true; 800 } 801 } 802 803 if (!found) { 804 return; 805 } 806 if (context instanceof JOnASStandardContext) { 809 JOnASStandardContext jctx = (JOnASStandardContext) context; 810 if (jctx.isInServerXml()) { 811 try { 812 ((JOnASStandardContext) context).stop(); 813 } catch (LifecycleException le) { 814 throw new JWebContainerServiceException("Cannot stop context (" + le.getMessage() + ")"); 815 } 816 } else { 817 context.getParent().removeChild(context); 819 if (getMbeanServer() != null) { 823 try { 824 getMbeanServer().unregisterMBean(jctx.getJmxName()); 826 } catch (Exception e) { 827 getLogger().log(BasicLevel.ERROR, "Cannot remove the MBean for the WebModule " + jctx.getObjectName() + " : " + e.getMessage()); 828 } 829 } 830 } 831 } else { 832 context.getParent().removeChild(context); 833 if (context instanceof StandardContext) { 837 StandardContext ctx = (StandardContext) context; 838 if (getMbeanServer() != null) { 839 try { 840 getMbeanServer().unregisterMBean(ctx.getJmxName()); 842 } catch (Exception e) { 843 getLogger().log(BasicLevel.ERROR 844 , "Cannot remove the MBean for the WebModule " + ctx.getObjectName() 845 + " : " + e.getMessage()); 846 } 847 } 848 } 849 } 850 851 } 852 853 854 858 public boolean isTomcatStarted() { 859 return tomcatStarted; 860 } 861 862 863 868 public void setServer(Server server) { 869 this.server = server; 870 } 871 872 873 878 protected File getConfigFile() throws FileNotFoundException { 879 String fileName = System.getProperty("catalina.base"); 880 fileName = fileName + File.separator + CONFIG_FILE; 881 File file = new File (fileName); 882 if (!file.exists()) { 883 String err = i18n.getMessage("CatJWebServ.getConfigFile.findfileError", fileName); 884 throw new FileNotFoundException (err); 885 } 886 return (file); 887 } 888 889 890 897 public Host findHost(String hostName) 898 throws JWebContainerServiceException { 899 900 Service[] services = server.findServices(); 901 if (services.length < 1) { 903 String err = "At least one service must be define in the server.xml of Tomcat"; 904 throw new JWebContainerServiceException(err); 905 } 906 907 913 914 if (hostName == null || hostName.equals("")) { 915 917 Service service = services[0]; 919 920 Container cont = service.getContainer(); 921 if (!(cont instanceof Engine)) { 922 String err = "The container of the service must be an engine"; 923 throw new JWebContainerServiceException(err); 924 } 925 926 Engine engine = (Engine) cont; 927 String defaultHost = engine.getDefaultHost(); 928 if (defaultHost == null) { 929 String err = "Default host must be specified in server.xml or host must be specified in jonas-web.xml"; 930 throw new JWebContainerServiceException(err); 931 } 932 Container child = engine.findChild(defaultHost); 933 if (child instanceof Host) { 935 return (Host) child; 936 } 937 String err = "Default host " + defaultHost + " not found"; 939 throw new JWebContainerServiceException(err); 940 } 941 Vector hosts = new Vector (); 943 944 for (int s = 0; s < services.length; s++) { 945 Container cont = services[s].getContainer(); 946 if (!(cont instanceof Engine)) { 947 String err = "The container of a service must be an engine"; 948 throw new JWebContainerServiceException(err); 949 } 950 Engine engine = (Engine) cont; 951 Container child = engine.findChild(hostName); 952 if (child instanceof Host) { 953 hosts.addElement(child); 954 } 955 } 956 957 if (hosts.size() == 0) { 959 String err = "Host " + hostName + " not found in all services/Engine of server.xml"; 961 throw new JWebContainerServiceException(err); 962 } 963 964 return (Host) hosts.elementAt(0); 966 } 967 968 969 975 private void addResource(javax.naming.Context ctx, ContextResource resource) throws JWebContainerServiceException { 976 977 String name = resource.getName(); 978 String type = resource.getType(); 979 String registryName = name; 980 981 if (!type.equals("org.apache.catalina.UserDatabase")) { 983 return; 984 } 985 986 Reference ref = new Reference ("org.apache.catalina.UserDatabase", "org.apache.catalina.users.MemoryUserDatabaseFactory", null); 988 989 Iterator params = resource.listProperties(); 991 while (params.hasNext()) { 992 String paramName = (String ) params.next(); 993 String paramValue = (String ) resource.getProperty(paramName); 994 StringRefAddr refAddr = new StringRefAddr (paramName, paramValue); 995 ref.add(refAddr); 996 } 997 try { 998 javax.naming.Context ictx = getNaming().getInitialContext(); 999 1000 ictx.rebind(registryName, ref); 1002 1003 LinkRef lref = new LinkRef (registryName); 1005 ctx.rebind(name, lref); 1006 1007 } catch (Exception e) { 1008 e.printStackTrace(); 1009 String err = "Can't add the resource " + name; 1010 throw new JWebContainerServiceException(err, e); 1011 } 1012 } 1013 1014 1015 1021 protected Digester createServerDigester() { 1022 1023 Digester digester = new Digester(); 1025 digester.setValidating(false); 1026 digester.addRuleSet(new JCatalinaRuleSet(parentClassLoader)); 1027 1028 digester.setUseContextClassLoader(true); 1031 return (digester); 1032 1033 } 1034 1035 1036 1039 protected void updateServerInfos() { 1040 String infos = org.apache.catalina.util.ServerInfo.getServerInfo(); 1041 StringTokenizer st = new StringTokenizer (infos, "/"); 1042 if (st.countTokens() != 2) { 1043 setServerName(infos); 1044 setServerVersion(""); 1045 } else { 1046 setServerName(st.nextToken()); 1047 setServerVersion(st.nextToken()); 1048 } 1049 } 1050 1051 1057 public String getDefaultHost() throws JWebContainerServiceException { 1058 Engine engine = (Engine) getFirstService().getContainer(); 1059 return engine.getDefaultHost(); 1060 } 1061 1062 1063 1070 public String getDefaultHttpPort() throws JWebContainerServiceException { 1071 return String.valueOf(getFirstConnectorFromScheme("http").getPort()); 1072 } 1073 1074 1075 1079 private Connector getFirstConnectorFromScheme(String scheme) { 1080 Service svc = getFirstService(); 1081 Connector[] conn = svc.findConnectors(); 1083 Vector httpConn = new Vector (); 1084 for (int i = 0; i < conn.length; i++) { 1085 if (conn[i].getScheme().equalsIgnoreCase(scheme)) { 1086 httpConn.add(conn[i]); 1087 } 1088 } 1089 1090 if (httpConn.isEmpty()) { 1091 throw new JWebContainerServiceException("No Connectors found for scheme '" + scheme + "' in " + CONFIG_FILE); 1093 } 1094 1095 Connector c = (Connector) httpConn.get(0); 1096 1097 if (httpConn.size() > 1) { 1099 if (getLogger().isLoggable(BasicLevel.WARN)) { 1100 getLogger().log(BasicLevel.WARN, "Found multiple Connectors for scheme '" + scheme + "' in " + CONFIG_FILE 1101 + ", using first by default! (port:" + c.getPort() + ")"); 1102 } 1103 } 1104 1105 return c; 1106 } 1107 1108 1109 1112 private Service getFirstService() { 1113 Service[] svc = server.findServices(); 1114 if ((svc == null) || (svc.length == 0)) { 1116 throw new JWebContainerServiceException("No Services found in " + CONFIG_FILE); 1118 } 1119 if (svc.length > 1) { 1121 if (getLogger().isLoggable(BasicLevel.WARN)) { 1122 getLogger().log(BasicLevel.WARN, "Found multiple Services in " + CONFIG_FILE + ", using first by default!"); 1123 } 1124 } 1125 return svc[0]; 1126 } 1127 1128 1129 1136 public String getDefaultHttpsPort() throws JWebContainerServiceException { 1137 return String.valueOf(getFirstConnectorFromScheme("https").getPort()); 1138 } 1139 1140 1147 public void registerWarMBean(String fileName) throws RemoteException , JWebContainerServiceException { 1148 ClassLoader old = null; 1149 try { 1150 old = Thread.currentThread().getContextClassLoader(); 1151 Thread.currentThread().setContextClassLoader(catalinaLoader); 1152 super.registerWarMBean(fileName); 1153 } catch (Exception e) { 1154 throw new ServiceException("Exception during registering war", e); 1155 } finally { 1156 if (old != null) { 1157 Thread.currentThread().setContextClassLoader(old); 1158 } 1159 } 1160 } 1161 1162 1169 public void unRegisterWarMBean(String fileName) throws RemoteException , JWebContainerServiceException { 1170 ClassLoader old = null; 1171 try { 1172 old = Thread.currentThread().getContextClassLoader(); 1173 Thread.currentThread().setContextClassLoader(catalinaLoader); 1174 super.unRegisterWarMBean(fileName); 1175 } catch (Exception e) { 1176 throw new ServiceException("Exception during unregistering war", e); 1177 } finally { 1178 if (old != null) { 1179 Thread.currentThread().setContextClassLoader(old); 1180 } 1181 } 1182 } 1183} 1184 | Popular Tags |