1 25 26 package org.objectweb.jonas.web; 27 28 import java.io.File ; 29 import java.io.IOException ; 30 import java.net.MalformedURLException ; 31 import java.net.URL ; 32 import java.net.URLClassLoader ; 33 import java.rmi.RemoteException ; 34 import java.util.ArrayList ; 35 import java.util.Enumeration ; 36 import java.util.HashSet ; 37 import java.util.Hashtable ; 38 import java.util.Iterator ; 39 import java.util.List ; 40 import java.util.Set ; 41 import java.util.StringTokenizer ; 42 import java.util.Vector ; 43 44 import javax.management.InstanceAlreadyExistsException ; 45 import javax.management.MBeanRegistrationException ; 46 import javax.management.MBeanServer ; 47 import javax.management.NotCompliantMBeanException ; 48 import javax.naming.Context ; 49 import javax.naming.LinkRef ; 50 import javax.naming.NamingException ; 51 import javax.naming.Reference ; 52 import javax.naming.StringRefAddr ; 53 54 import org.objectweb.jonas_lib.deployment.api.EjbLocalRefDesc; 55 import org.objectweb.jonas_lib.deployment.api.EjbRefDesc; 56 import org.objectweb.jonas_lib.deployment.api.EnvEntryDesc; 57 import org.objectweb.jonas_lib.deployment.api.MessageDestinationRefDesc; 58 import org.objectweb.jonas_lib.deployment.api.ResourceEnvRefDesc; 59 import org.objectweb.jonas_lib.deployment.api.ResourceRefDesc; 60 import org.objectweb.jonas_lib.loader.SimpleWebappClassLoader; 61 import org.objectweb.jonas_lib.loader.WebappClassLoader; 62 import org.objectweb.jonas_lib.naming.ContainerNaming; 63 import org.objectweb.jonas_lib.security.PermissionManagerException; 64 65 import org.objectweb.jonas_web.deployment.api.WebContainerDeploymentDesc; 66 import org.objectweb.jonas_web.deployment.api.WebContainerDeploymentDescException; 67 import org.objectweb.jonas_web.deployment.lib.wrapper.WebManagerWrapper; 68 69 import org.objectweb.jonas_ws.deployment.api.ServiceRefDesc; 70 71 import org.objectweb.jonas.common.JModule; 72 import org.objectweb.jonas.common.JProp; 73 import org.objectweb.jonas.common.Log; 74 import org.objectweb.jonas.ear.EarServiceImpl; 75 import org.objectweb.jonas.jmx.JmxService; 76 import org.objectweb.jonas.jmx.JonasObjectName; 77 import org.objectweb.jonas.naming.CompNamingContext; 78 import org.objectweb.jonas.naming.NamingManager; 79 import org.objectweb.jonas.server.LoaderManager; 80 import org.objectweb.jonas.service.AbsServiceImpl; 81 import org.objectweb.jonas.service.ServiceException; 82 import org.objectweb.jonas.service.ServiceManager; 83 import org.objectweb.jonas.web.lib.JarTools; 84 import org.objectweb.jonas.web.lib.PermissionManager; 85 import org.objectweb.jonas.ws.JServiceFactory; 86 import org.objectweb.jonas.ws.JServiceFactoryFinder; 87 import org.objectweb.jonas.ws.WebServicesService; 88 89 import org.objectweb.util.monolog.api.BasicLevel; 90 import org.objectweb.util.monolog.api.Logger; 91 92 100 public abstract class AbsJWebContainerServiceImpl extends AbsServiceImpl implements JWebContainerService, 101 AbsJWebContainerServiceImplMBean { 102 103 106 protected static final String JONAS_BASE = JProp.getJonasBase(); 107 108 111 protected static final String WEBAPPS_DIR = JONAS_BASE + File.separator + "webapps"; 112 113 116 protected static final String WORK_DIR = JProp.getWorkDir(); 117 118 121 protected static final String WORK_WEBAPPS_DIR = WORK_DIR + File.separator + "webapps"; 122 123 126 public static final String DESCRIPTORS = "jonas.service.web.descriptors"; 127 128 132 public static final String AUTOLOADDIR = "jonas.service.web.autoloaddir"; 133 134 137 public static final String PARSINGWITHVALIDATION = "jonas.service.web.parsingwithvalidation"; 138 139 142 private static final int WAR_EXTENSION_LENGTH = 4; 143 144 148 public static final String CLASS = "jonas.service.web.class"; 149 150 153 private static Logger logger = null; 154 155 158 private static String nameOfServer = null; 159 160 163 private ContainerNaming naming; 164 165 168 private Hashtable warLoaders = new Hashtable (); 169 170 173 private Hashtable warBindings = new Hashtable (); 174 175 178 private MBeanServer mbeanServer = null; 182 185 private Vector warNames = new Vector (); 186 187 190 private Vector warDeployed = new Vector (); 191 192 195 private List autoloadDirectories = new ArrayList (); 196 197 200 private String serverName = null; 201 202 205 private String serverVersion = null; 206 207 210 private WebServicesService wsService = null; 211 212 215 private ClassLoader appsClassLoader; 216 217 222 protected void doInit(Context ctx) throws ServiceException { 223 224 logger = Log.getLogger(Log.JONAS_WEB_PREFIX); 226 227 try { 229 LoaderManager lm = LoaderManager.getInstance(); 230 appsClassLoader = lm.getAppsLoader(); 231 } catch (Exception e) { 232 logger.log(BasicLevel.ERROR, "Cannot get the Applications ClassLoader from Web Container Service: " + e); 233 throw new ServiceException("Cannot get the Applications ClassLoader from Web Container Service", e); 234 } 235 236 ServiceManager sm = null; 237 try { 238 sm = ServiceManager.getInstance(); 239 } catch (Exception e) { 240 String err = "Cannot get ServiceManager instance."; 241 logger.log(BasicLevel.ERROR, err); 242 throw new ServiceException(err, e); 243 } 244 245 try { 247 mbeanServer = ((JmxService) sm.getJmxService()).getJmxServer(); 248 } catch (ServiceException e) { 249 mbeanServer = null; 251 } 252 253 try { 255 wsService = (WebServicesService) sm.getWebServicesService(); 256 } catch (ServiceException e) { 257 if (logger.isLoggable(BasicLevel.DEBUG)) { 258 logger.log(BasicLevel.DEBUG, "WebServices service not started"); 259 } 260 wsService = null; 262 } 263 264 String parsingMode = "false"; 266 try { 267 parsingMode = (String ) ctx.lookup(PARSINGWITHVALIDATION); 268 } catch (NamingException e) { 269 if (logger.isLoggable(BasicLevel.DEBUG)) { 270 logger.log(BasicLevel.DEBUG, "No value for parsingWithValidation"); 271 } 272 } 275 276 WebManagerWrapper.setParsingWithValidation("true".equalsIgnoreCase(parsingMode)); 277 if (logger.isLoggable(BasicLevel.DEBUG)) { 278 if ("false".equalsIgnoreCase(parsingMode)) { 279 logger.log(BasicLevel.DEBUG, "Web XML parsing without validation"); 280 } else { 281 logger.log(BasicLevel.DEBUG, "Web XML parsing with validation"); 282 } 283 } 284 285 String descsValue = null; 287 try { 288 descsValue = (String ) ctx.lookup(DESCRIPTORS); 289 } catch (NamingException e) { 290 if (logger.isLoggable(BasicLevel.DEBUG)) { 291 logger.log(BasicLevel.DEBUG, "No " + DESCRIPTORS); 292 } 293 } 294 if (descsValue != null) { 295 StringTokenizer st = new StringTokenizer (descsValue, ","); 296 while (st.hasMoreTokens()) { 297 String fileName = st.nextToken().trim(); 298 warNames.add(fileName); 299 } 300 } 301 String dirValue = null; 303 ArrayList autoDirs = new ArrayList (); 304 try { 305 dirValue = (String ) ctx.lookup(AUTOLOADDIR); 306 } catch (NamingException e) { 307 if (logger.isLoggable(BasicLevel.DEBUG)) { 308 logger.log(BasicLevel.DEBUG, "No " + AUTOLOADDIR); 309 } 310 } 311 if (dirValue != null) { 312 StringTokenizer st = new StringTokenizer (dirValue, ","); 313 while (st.hasMoreTokens()) { 314 String dirName = st.nextToken().trim(); 315 addWars(dirName); 316 autoDirs.add(dirName); 317 } 318 } 319 File oFile; 321 Iterator it = autoDirs.iterator(); 322 while (it.hasNext()) { 323 String dirName = (String ) it.next(); 324 try { 325 oFile = new File (WEBAPPS_DIR, dirName); 326 if (!oFile.exists()) { 327 oFile = new File (dirName); 328 } 329 if (oFile.exists()) { 330 autoloadDirectories.add(oFile.getCanonicalPath()); 331 } 332 } catch (Exception e) { 333 String err = "Error when trying to verify Webapps autoload directory : " + dirName; 334 logger.log(BasicLevel.ERROR, err + " " + e.getMessage()); 335 } 336 } 337 338 try { 340 naming = NamingManager.getInstance(); 341 } catch (NamingException e) { 342 throw new ServiceException("Error when getting the reference to the Naming manager"); 343 } 344 345 JProp jp = null; 346 try { 347 jp = JProp.getInstance(); 348 } catch (Exception e) { 349 String err = "Error when trying to get jonas name "; 350 logger.log(BasicLevel.ERROR, err + " " + e.getMessage()); 351 throw new ServiceException(err, e); 352 } 353 AbsJWebContainerServiceImpl.nameOfServer = jp.getValue("jonas.name", "jonas"); 354 355 } 356 357 361 protected void doStart() throws ServiceException { 362 for (Enumeration e = warNames.elements(); e.hasMoreElements();) { 364 String fileName = (String ) e.nextElement(); 365 366 URL warURL = checkWarFile(fileName); 367 368 Context contctx = null; 369 try { 370 contctx = new CompNamingContext(fileName); 371 contctx.rebind("warURL", warURL); 372 } catch (NamingException ne) { 375 String err = "Cannot start the WebContainerService"; 376 throw new ServiceException(err, ne); 377 } 378 try { 379 registerWar(contctx); 380 } catch (JWebContainerServiceException jwcse) { 381 String err = "Cannot deploy the file '" + warURL + "' : " + jwcse.getMessage(); 382 logger.log(BasicLevel.WARN, err); 383 } 384 } 385 386 try { 387 if (mbeanServer != null) { 390 mbeanServer.registerMBean(this, JonasObjectName.webContainerService()); 391 } 392 } catch (InstanceAlreadyExistsException iae) { 393 String err = "Cannot start the WebContainerService Already Exists"; 394 throw new ServiceException(err, iae); 395 } catch (MBeanRegistrationException mbre) { 396 throw new ServiceException("Cannot start the WebContainerService (MBean registration error)", mbre); 397 } catch (NotCompliantMBeanException ncmbe) { 398 throw new ServiceException("Cannot start the WebContainerService (MBean Not compliant error)", ncmbe); 399 } 400 } 401 402 406 protected void doStop() throws ServiceException { 407 408 for (int i = warDeployed.size() - 1; i >= 0; i--) { 411 War war = (War) warDeployed.elementAt(i); 412 URL warURL = war.getWarURL(); 413 String fileName = warURL.getFile(); 414 try { 415 if (!war.isInEarCase()) { 417 Context ctx = new CompNamingContext(fileName); 418 ctx.rebind("warURL", warURL); 419 ctx.rebind("isEarCase", new Boolean (false)); 420 unRegisterWar(ctx); 421 } 422 } catch (Exception e) { 423 String err = "Error when undeploying the war :" + fileName; 426 logger.log(BasicLevel.ERROR, err + e.getMessage()); 427 } 428 } 429 430 if (mbeanServer != null) { 431 try { 432 mbeanServer.unregisterMBean(JonasObjectName.webContainerService()); 434 } catch (Exception e) { 435 logger.log(BasicLevel.ERROR, "Cannot stop the WebContainerService: " + e); 436 } 437 } 438 if (logger.isLoggable(BasicLevel.DEBUG)) { 439 logger.log(BasicLevel.DEBUG, "WebContainerService stopped"); 440 } 441 } 442 443 451 protected abstract void doRegisterWar(Context ctx) throws JWebContainerServiceException; 452 453 459 protected abstract void doUnRegisterWar(Context ctx) throws JWebContainerServiceException; 460 461 469 protected URL getUnpackDir(URL warURL, String earAppName) throws JWebContainerServiceException { 470 471 String destDir = WORK_WEBAPPS_DIR + File.separator + nameOfServer + File.separator; 472 473 479 URL unpackedWarURL = null; 481 482 String fileName = warURL.getFile(); 483 484 if (new File (fileName).isFile()) { 486 if (earAppName != null) { 489 destDir += earAppName + File.separator; 490 } 491 492 String fTemp = new File (fileName).getName(); 493 destDir += fTemp.substring(0, fTemp.length() - WAR_EXTENSION_LENGTH); 494 495 JarTools.unpack(fileName, destDir); 497 } else { 498 destDir = fileName; 500 } 501 502 try { 503 unpackedWarURL = new File (destDir).toURL(); 504 } catch (MalformedURLException mue) { 505 throw new JWebContainerServiceException("Error", mue); 506 } 507 508 return unpackedWarURL; 509 510 } 511 512 523 public URLClassLoader getClassLoader(URL warURL, String earAppName, ClassLoader parentLoader) 524 throws JWebContainerServiceException { 525 526 URLClassLoader loaderForCls = null; 527 try { 528 WebLoaderHolder holder = (WebLoaderHolder) warLoaders.get(warURL); 529 if (holder != null) { 530 loaderForCls = holder.getJonasWebLoader(); 531 } 532 } catch (Exception e) { 533 throw new JWebContainerServiceException("Error when getting '" + warURL + "' in cache", e); 534 } 535 536 if (loaderForCls == null) { 537 539 URL unpackedWarURL = getUnpackDir(warURL, earAppName); 541 542 try { 543 if (parentLoader != null) { 544 loaderForCls = new WebappClassLoader(unpackedWarURL, parentLoader); 546 } else { 547 loaderForCls = new WebappClassLoader(unpackedWarURL, appsClassLoader); 549 } 550 } catch (IOException ioe) { 551 throw new JWebContainerServiceException( 552 "Cannot create WebAppClassLoader from '" + unpackedWarURL + "'", ioe); 553 } 554 555 try { 557 WebLoaderHolder holder = new WebLoaderHolder(loaderForCls, null); 558 warLoaders.put(warURL, holder); 559 } catch (Exception e) { 560 throw new JWebContainerServiceException("Error when adding '" + warURL + "' in cache", e); 561 } 562 } 563 564 return loaderForCls; 565 566 } 567 568 572 public ClassLoader getContextLinkedClassLoader(URL warURL) { 573 WebLoaderHolder holder = (WebLoaderHolder) warLoaders.get(warURL); 574 if (holder != null) { 575 return holder.getEnvWebLoader(); 576 } 577 return null; 578 } 579 580 588 private void registerWar(Context ctx) throws JWebContainerServiceException { 589 601 URL earURL = null; 604 String contextRoot = null; 605 String earAppName = null; 606 try { 607 earURL = (URL ) ctx.lookup("earURL"); 608 contextRoot = (String ) ctx.lookup("contextRoot"); 609 earAppName = EarServiceImpl.buildJ2eeApplicationName(earURL); 610 } catch (NamingException e) { 611 if (earURL != null || contextRoot != null) { 612 String err = "Error while getting parameter from context param :" + e.getMessage(); 613 logger.log(BasicLevel.ERROR, err); 614 throw new JWebContainerServiceException(err, e); 615 } } 617 618 URL warURL = null; 620 try { 621 warURL = (URL ) ctx.lookup("warURL"); 622 } catch (NamingException e) { 623 String err = "Error while getting parameter from context param :" + e.getMessage(); 624 logger.log(BasicLevel.ERROR, err); 625 throw new JWebContainerServiceException(err, e); 626 } 627 628 File warFile = new File (warURL.getFile()); 630 if (!warFile.exists()) { 631 String err = "registerWar: '" + warFile.getPath() + "' not found"; 632 logger.log(BasicLevel.ERROR, err); 633 throw new JWebContainerServiceException(err); 634 } 635 636 War war = getWar(warURL); 638 if (war != null) { 639 String err = "Cannot deploy war '" + warURL.getFile() + "' is already deployed." 641 + " You must undeploy the war before a new deployment."; 642 throw new JWebContainerServiceException(err); 643 } 644 645 URLClassLoader parentLoader = null; 646 URLClassLoader earClassLoader = null; 647 try { 648 parentLoader = (URLClassLoader ) ctx.lookup("parentClassLoader"); 649 earClassLoader = (URLClassLoader ) ctx.lookup("earClassLoader"); 650 } catch (NamingException ne) { 651 if (logger.isLoggable(BasicLevel.DEBUG)) { 652 logger.log(BasicLevel.DEBUG, "Not an ear case"); 653 } 654 } 656 657 URLClassLoader loaderForCls = getClassLoader(warURL, earAppName, parentLoader); 659 660 URL unpackedWarURL = getUnpackDir(warURL, earAppName); 662 663 if (wsService != null && earClassLoader == null) { 666 try { 667 CompNamingContext contctx = null; 668 try { 669 contctx = new CompNamingContext(unpackedWarURL.getFile()); 670 contctx.rebind("unpackDir", unpackedWarURL.toExternalForm()); 671 contctx.rebind("jarUrls", new URL [0]); 672 contctx.rebind("warUrls", new URL [] {warURL}); 673 if (parentLoader != null) { 674 contctx.rebind("ejbClassLoader", parentLoader); 675 } 676 } catch (NamingException e) { 677 String err = "Can not bind params for the WebServices service, " 678 + "Can't deploy Web Services Endpoint"; 679 throw new JWebContainerServiceException(err, e); 680 } 681 wsService.deployWebServices(contctx); 682 } catch (ServiceException se) { 683 String err = "Error during the deployment of the WebServices of the War file '" + warURL + "'"; 684 logger.log(BasicLevel.ERROR, err + " : " + se.getMessage()); 685 throw new JWebContainerServiceException(err, se); 686 } 687 } 688 689 WebContainerDeploymentDesc webDD = null; 691 try { 692 webDD = WebManagerWrapper.getDeploymentDesc(warURL, loaderForCls, earClassLoader); 693 } catch (WebContainerDeploymentDescException e) { 694 String err = "Cannot read the deployment descriptors '" + warURL.getFile() + "'"; 695 logger.log(BasicLevel.ERROR, err + ": " + e); 696 e.printStackTrace(System.err); 697 throw new JWebContainerServiceException(err, e); 698 } 699 700 URLClassLoader webClassLoader = null; 702 try { 703 if (logger.isLoggable(BasicLevel.DEBUG)) { 704 logger.log(BasicLevel.DEBUG, "Populating environment of the file " + warURL.getFile()); 705 } 706 Context ctxParam = new CompNamingContext(unpackedWarURL.getFile()); 707 ctxParam.rebind("DeploymentDesc", webDD); 708 ctxParam.rebind("warName", unpackedWarURL.getFile()); 709 if (parentLoader == null) { 710 webClassLoader = new SimpleWebappClassLoader(unpackedWarURL, appsClassLoader); 711 } else { 712 webClassLoader = new SimpleWebappClassLoader(unpackedWarURL, parentLoader); 713 } 714 ctxParam.rebind("parentCL", webClassLoader); 715 setWebEnvironment(ctxParam); 717 } catch (Exception e) { 718 String err = "Error when populating "; 720 logger.log(BasicLevel.ERROR, err + e.getMessage()); 721 throw new JWebContainerServiceException(err, e); 722 } 723 724 WebLoaderHolder holder = (WebLoaderHolder) warLoaders.get(warURL); 726 holder.setEnvWebLoader(webClassLoader); 727 728 if (earClassLoader == null && contextRoot == null) { 734 String cRoot = webDD.getContextRoot(); 735 if (cRoot == null) { 736 String file = new File (unpackedWarURL.getFile()).getName(); 737 if (file.toLowerCase().endsWith(".war")) { 738 contextRoot = file.substring(0, file.length() - WAR_EXTENSION_LENGTH); 739 } else { 740 contextRoot = file.substring(0, file.length()); 742 } 743 } else { 744 contextRoot = cRoot; 745 } 746 } 747 748 String hostName = webDD.getHost(); 751 752 List deployedWars = getWar(contextRoot); 754 for (Iterator itDeployed = deployedWars.iterator(); itDeployed.hasNext();) { 755 War deployedWar = (War) itDeployed.next(); 756 String hostDeployed = deployedWar.getHostName(); 757 if ((hostDeployed == null && hostName == null) || (hostDeployed != null && hostDeployed.equals(hostName))) { 758 String err = "Cannot deploy war '" + warURL.getFile() + "' is already deployed with the context '" 760 + contextRoot + "'." + " You must undeploy the war before a new deployment."; 761 throw new JWebContainerServiceException(err); 762 } 763 } 764 765 boolean java2DelegationModel = webDD.getJava2DelegationModel(); 767 768 war = new War(warURL, earURL, hostName, contextRoot, java2DelegationModel, webDD.getXmlContent(), webDD 770 .getJOnASXmlContent(), webDD.getServletsName()); 771 772 PermissionManager permissionManager = null; 774 try { 775 permissionManager = new PermissionManager(webDD, war.getContextId()); 776 permissionManager.translateServletDeploymentDescriptor(); 777 if (earClassLoader == null) { 780 permissionManager.commit(); 781 } 782 } catch (Exception e) { 783 e.printStackTrace(); 784 String err = "Cannot build permission manager object for the webapp '" + unpackedWarURL + "' : "; 785 logger.log(BasicLevel.ERROR, err + e.getMessage()); 786 throw new JWebContainerServiceException(err, e); 787 } 788 789 Context ctxParam = null; 791 try { 792 ctxParam = new CompNamingContext(unpackedWarURL.getFile()); 793 ctxParam.rebind("warURL", warURL); 794 if (earURL != null) { 795 ctxParam.rebind("earURL", earURL); 796 } 797 ctxParam.rebind("unpackedWarURL", unpackedWarURL); 798 ctxParam.rebind("parentCL", webClassLoader); 799 if (hostName != null) { 800 ctxParam.rebind("hostName", hostName); 801 } 802 ctxParam.rebind("contextRoot", contextRoot); 803 if (earAppName != null) { 804 ctxParam.rebind("earAppName", earAppName); 805 } 806 ctxParam.rebind("jonasDD", webDD.getJOnASXmlContent()); 807 ctxParam.rebind("java2DelegationModel", new Boolean (java2DelegationModel)); 808 ctxParam.rebind("permissionManager", permissionManager); 809 } catch (NamingException e) { 810 String err = "Error when deploying the war '" + unpackedWarURL.getFile() + "'"; 811 logger.log(BasicLevel.ERROR, err + e.getMessage()); 812 throw new JWebContainerServiceException(err, e); 813 } 814 815 war.setPermissionManager(permissionManager); 816 817 doRegisterWar(ctxParam); 819 820 warDeployed.addElement(war); 822 warBindings.put(warURL, webClassLoader); 823 824 try { 825 if (mbeanServer != null) { 827 mbeanServer.registerMBean(war, JonasObjectName.war(warURL.getFile())); 828 } 829 } catch (Exception ware) { 830 logger.log(BasicLevel.ERROR, "Can not register the MBean for the war" + unpackedWarURL + " :" 831 + ware.getMessage()); 832 } 833 834 if (wsService != null && earClassLoader == null) { 838 try { 839 CompNamingContext contctx = null; 840 try { 841 contctx = new CompNamingContext(unpackedWarURL.getFile()); 842 contctx.rebind(WebServicesService.CLASSLOADER_CTX_PARAM, loaderForCls); 843 contctx.rebind(WebServicesService.PARENT_OBJECTNAME_CTX_PARAM , ctxParam.lookup("WebModule")); 844 contctx.rebind(WebServicesService.ISINEAR_CTX_PARAM, Boolean.FALSE); 845 } catch (NamingException e) { 846 String err = "Can not bind params for the WebServices service, " 847 + "can't complete deployment of Web Services Endpoints"; 848 throw new JWebContainerServiceException(err, e); 849 } 850 wsService.completeWSDeployment(contctx); 851 } catch (ServiceException se) { 852 String err = "Error during the deployment of the WebServices of the War file '" + warURL + "'"; 853 logger.log(BasicLevel.ERROR, err + " : " + se.getMessage()); 854 throw new JWebContainerServiceException(err, se); 855 } 856 } 857 858 StringBuffer txtInfo = new StringBuffer ("War " + warURL.getFile() + " available at the context "); 859 if (!contextRoot.startsWith("/")) { 860 txtInfo.append("/"); 861 } 862 txtInfo.append(contextRoot); 863 864 if (hostName != null) { 865 txtInfo.append(" on the host "); 866 txtInfo.append(hostName); 867 } 868 txtInfo.append("."); 869 logger.log(BasicLevel.INFO, txtInfo.toString()); 870 871 if (wsService != null && earClassLoader == null) { 873 wsService.removeCache(loaderForCls); 874 } 875 } 876 877 884 public void registerWarMBean(String fileName) throws RemoteException , JWebContainerServiceException { 885 URL warURL = checkWarFile(fileName); 888 889 Context ctx = null; 891 try { 892 ctx = new CompNamingContext(fileName); 893 ctx.rebind("warURL", warURL); 894 } catch (NamingException e) { 895 String err = "Error when deploying the war '" + fileName + "'"; 896 logger.log(BasicLevel.ERROR, err + e.getMessage()); 897 throw new JWebContainerServiceException(err, e); 898 } 899 900 registerWar(ctx); 902 } 903 904 911 private void setWebEnvironment(Context ctxParam) throws JWebContainerServiceException { 912 913 WebContainerDeploymentDesc dd = null; 914 String warName = null; 915 ClassLoader parentClassLoader = null; 916 917 921 try { 922 dd = (WebContainerDeploymentDesc) ctxParam.lookup("DeploymentDesc"); 923 warName = (String ) ctxParam.lookup("warName"); 924 } catch (NamingException e) { 925 String err = "Error while getting parameter from context param "; 926 logger.log(BasicLevel.ERROR, err + e.getMessage()); 927 throw new JWebContainerServiceException(err, e); 928 } 929 930 try { 932 parentClassLoader = (ClassLoader ) ctxParam.lookup("parentCL"); 933 } catch (NamingException e) { 934 String err = "Error while getting parameter from context param "; 935 logger.log(BasicLevel.ERROR, err + e.getMessage()); 936 throw new JWebContainerServiceException(err, e); 937 } 938 939 try { 941 Context javaCtx = naming.createEnvironmentContext(warName); 943 naming.setComponentContext(javaCtx, parentClassLoader); 944 Context envCtx = javaCtx.createSubcontext("comp/env"); 945 946 EnvEntryDesc[] envt = dd.getEnvEntryDesc(); 948 for (int i = 0; i < envt.length; i++) { 949 String name = envt[i].getName(); 951 Object obj = envt[i].getValue(); 952 953 if (logger.isLoggable(BasicLevel.DEBUG)) { 955 logger.log(BasicLevel.DEBUG, warName + ": Binding object " + name + " -> " + obj); 956 } 957 envCtx.rebind(name, obj); 958 } 959 960 ResourceRefDesc[] resref = dd.getResourceRefDesc(); 962 for (int i = 0; i < resref.length; i++) { 963 String name = resref[i].getName(); 965 String type = resref[i].getTypeName(); 966 String resname = resref[i].getJndiName(); 967 if (logger.isLoggable(BasicLevel.DEBUG)) { 969 logger.log(BasicLevel.DEBUG, warName + ": Linking resource " + name + " -> " + resname); 970 } 971 972 if (type.equalsIgnoreCase("java.net.URL")) { 973 Reference ref = new Reference ("java.net.URL", "org.objectweb.jonas_lib.naming.factory.URLFactory", 975 null); 976 StringRefAddr refAddr = new StringRefAddr ("url", resname); 977 ref.add(refAddr); 978 envCtx.rebind(name, ref); 979 } else { 980 LinkRef lref = new LinkRef (resname); 983 envCtx.rebind(name, lref); 984 } 985 } 986 987 ResourceEnvRefDesc[] resEnvref = dd.getResourceEnvRefDesc(); 989 for (int i = 0; i < resEnvref.length; i++) { 990 String name = resEnvref[i].getName(); 992 String resname = resEnvref[i].getJndiName(); 993 LinkRef lref = new LinkRef (resname); 994 995 if (logger.isLoggable(BasicLevel.DEBUG)) { 996 logger.log(BasicLevel.DEBUG, warName + ": Linking resource environment " + name + " -> " + resname); 997 } 998 envCtx.rebind(name, lref); 999 } 1000 1001 EjbRefDesc[] ejbref = dd.getEjbRefDesc(); 1003 for (int i = 0; i < ejbref.length; i++) { 1004 String name = ejbref[i].getEjbRefName(); 1006 String ejbname = null; 1007 ejbname = ejbref[i].getJndiName(); 1008 1009 LinkRef lref = new LinkRef (ejbname); 1010 1011 if (logger.isLoggable(BasicLevel.DEBUG)) { 1012 logger.log(BasicLevel.DEBUG, warName + ": Linking ejb " + name + " -> " + ejbname); 1013 } 1014 envCtx.rebind(name, lref); 1015 } 1016 1017 EjbLocalRefDesc[] ejblocalref = dd.getEjbLocalRefDesc(); 1022 for (int i = 0; i < ejblocalref.length; i++) { 1023 String name = ejblocalref[i].getEjbRefName(); 1024 String ejbname = ejblocalref[i].getJndiLocalName(); 1025 LinkRef lref = new LinkRef (ejbname); 1026 if (logger.isLoggable(BasicLevel.DEBUG)) { 1027 logger.log(BasicLevel.DEBUG, warName + ": Linking ejb " + name + " -> " + ejbname); 1028 } 1029 envCtx.rebind(name, lref); 1030 } 1031 1032 MessageDestinationRefDesc[] mdref = dd.getMessageDestinationRefDesc(); 1034 for (int i = 0; i < mdref.length; i++) { 1035 String name = mdref[i].getMessageDestinationRefName(); 1037 String mdname = null; 1038 mdname = mdref[i].getJndiName(); 1039 1040 LinkRef lref = new LinkRef (mdname); 1041 1042 if (logger.isLoggable(BasicLevel.DEBUG)) { 1043 logger.log(BasicLevel.DEBUG, warName + ": Linking message-destination " + name + " -> " + mdname); 1044 } 1045 envCtx.rebind(name, lref); 1046 } 1047 1048 1052 JServiceFactory factory = null; 1054 1055 ServiceRefDesc[] serviceRefs = dd.getServiceRefDesc(); 1056 for (int i = 0; i < serviceRefs.length; i++) { 1057 1058 if (factory == null) { 1059 factory = JServiceFactoryFinder.getJOnASServiceFactory(); 1060 } 1061 1062 String name = serviceRefs[i].getServiceRefName(); 1064 Reference ref = factory.getServiceReference(serviceRefs[i], parentClassLoader); 1066 envCtx.rebind(name, ref); 1067 if (logger.isLoggable(BasicLevel.DEBUG)) { 1068 logger.log(BasicLevel.DEBUG, "Adding service-ref 'java:comp/env/" + name + "'"); 1069 } 1070 } 1071 1072 } catch (NamingException e) { 1073 String err = "Error while populating environment of the war file " + warName; 1074 logger.log(BasicLevel.ERROR, err + " :" + e.getMessage()); 1075 throw new JWebContainerServiceException(err, e); 1076 } 1077 } 1078 1079 1086 private void unRegisterWar(Context ctx) throws JWebContainerServiceException { 1087 1088 URL warURL = null; 1091 boolean isEarCase = true; 1092 try { 1093 warURL = (URL ) ctx.lookup("warURL"); 1094 isEarCase = ((Boolean ) ctx.lookup("isEarCase")).booleanValue(); 1095 } catch (NamingException e) { 1096 String err = "Error while getting parameter from context param."; 1097 logger.log(BasicLevel.ERROR, err + e.getMessage()); 1098 throw new JWebContainerServiceException(err, e); 1099 } 1100 1101 String fileName = warURL.getFile(); 1103 1104 War war = null; 1106 war = getWar(warURL); 1107 if (war == null) { 1108 String err = "Cannot undeploy war: '" + fileName + "' is not deployed."; 1109 logger.log(BasicLevel.ERROR, err); 1110 throw new JWebContainerServiceException(err); 1111 } 1112 1113 if (isEarCase != war.isInEarCase()) { 1117 String err = "Cannot undeploy war: '" + fileName 1118 + "' it is in an ear application. You must undeploy the ear associated."; 1119 logger.log(BasicLevel.ERROR, err); 1120 throw new JWebContainerServiceException(err); 1121 } 1122 1123 try { 1125 ctx.rebind("contextRoot", war.getContextRoot()); 1127 ctx.rebind("webClassLoader", warBindings.get(warURL)); 1128 } catch (NamingException e) { 1129 String err = "Error when undeploying the war '" + fileName + "'"; 1130 logger.log(BasicLevel.ERROR, err + e.getMessage()); 1131 throw new JWebContainerServiceException(err, e); 1132 } 1133 1134 PermissionManager permissionManager = war.getPermissionManager(); 1136 try { 1137 permissionManager.delete(); 1138 permissionManager = null; 1139 } catch (PermissionManagerException pme) { 1140 logger.log(BasicLevel.ERROR, "Cannot remove permission manager for file '" + fileName + "'.", pme); 1141 } 1142 1143 doUnRegisterWar(ctx); 1144 1145 if (wsService != null) { 1147 wsService.undeployWebServices(ctx); 1148 } 1149 1150 URLClassLoader loader = (URLClassLoader ) warBindings.remove(warURL); 1152 naming.unSetComponentContext(loader); 1153 1154 warLoaders.remove(warURL); 1156 1157 warDeployed.removeElement(war); 1160 1161 if (mbeanServer != null) { 1163 try { 1164 mbeanServer.unregisterMBean(JonasObjectName.war(fileName)); 1166 } catch (Exception e) { 1167 logger 1168 .log(BasicLevel.ERROR, "Cannot remove the MBean for the war " + fileName + " : " 1169 + e.getMessage()); 1170 } 1171 } 1172 1173 logger.log(BasicLevel.INFO, "War " + fileName + " no longer available"); 1174 } 1175 1176 1183 public void unRegisterWarMBean(String fileName) throws RemoteException , JWebContainerServiceException { 1184 1185 URL warURL = checkWarDeployed(fileName); 1188 1189 Context ctx = null; 1191 try { 1192 ctx = new CompNamingContext(fileName); 1193 ctx.rebind("warURL", warURL); 1194 ctx.rebind("isEarCase", new Boolean (false)); 1195 } catch (NamingException e) { 1196 String err = "Error when undeploying the war file '" + fileName + "'"; 1197 logger.log(BasicLevel.ERROR, err + e.getMessage()); 1198 throw new JWebContainerServiceException(err, e); 1199 } 1200 unRegisterWar(ctx); 1201 } 1202 1203 1218 public void deployWars(Context ctx) throws JWebContainerServiceException { 1219 URL [] urls = null; 1227 URL earURL = null; 1228 ClassLoader parentClassLoader = null; 1229 ClassLoader earClassLoader = null; 1230 URL [] altDDs = null; 1231 String [] contextRoots = null; 1232 try { 1233 urls = (URL []) ctx.lookup("urls"); 1234 earURL = (URL ) ctx.lookup("earURL"); 1235 parentClassLoader = (ClassLoader ) ctx.lookup("parentClassLoader"); 1236 earClassLoader = (ClassLoader ) ctx.lookup("earClassLoader"); 1237 altDDs = (URL []) ctx.lookup("altDDs"); 1238 contextRoots = (String []) ctx.lookup("contextRoots"); 1239 } catch (NamingException e) { 1240 String err = "Error while getting parameter from context param "; 1241 logger.log(BasicLevel.ERROR, err + e.getMessage()); 1242 throw new JWebContainerServiceException(err, e); 1243 } 1244 1246 for (int i = 0; i < urls.length; i++) { 1248 String fileName = urls[i].getFile(); 1250 if (logger.isLoggable(BasicLevel.DEBUG)) { 1251 logger.log(BasicLevel.DEBUG, "Deploy war '" + fileName + "' for the ear service"); 1252 } 1253 1254 Context contctx = null; 1257 try { 1258 contctx = new CompNamingContext(fileName); 1259 contctx.rebind("warURL", urls[i]); 1260 contctx.rebind("parentClassLoader", parentClassLoader); 1261 contctx.rebind("earClassLoader", earClassLoader); 1262 contctx.rebind("earURL", earURL); 1263 if (altDDs[i] != null) { 1264 contctx.rebind("altDD", altDDs[i]); 1265 } 1266 if (contextRoots[i] != null) { 1267 contctx.rebind("contextRoot", contextRoots[i]); 1268 } 1269 registerWar(contctx); 1270 } catch (Exception e) { 1271 logger.log(BasicLevel.ERROR, "Error when deploying '" + fileName + "'"); 1274 logger.log(BasicLevel.ERROR, e.getMessage()); 1275 logger.log(BasicLevel.ERROR, "Undeploy war of the ear application"); 1276 1277 for (int j = 0; j < i; j++) { 1278 String warFileName = urls[j].getFile(); 1279 try { 1280 CompNamingContext context = new CompNamingContext(warFileName); 1282 context.rebind("warURL", urls[j]); 1283 context.rebind("isEarCase", new Boolean (true)); 1284 unRegisterWar(context); 1285 } catch (Exception ex) { 1286 logger.log(BasicLevel.ERROR, "Error when undeploying '" + warFileName + "'"); 1289 logger.log(BasicLevel.ERROR, ex.getMessage()); 1290 logger.log(BasicLevel.ERROR, "Cannot undeploy war of the ear application"); 1291 } 1292 1293 } 1294 throw new JWebContainerServiceException("Error during the deployment", e); 1295 } 1296 } 1297 } 1298 1299 1305 public void unDeployWars(URL [] urls) { 1306 for (int i = 0; i < urls.length; i++) { 1307 String warFileName = urls[i].getFile(); 1308 try { 1309 CompNamingContext context = new CompNamingContext(warFileName); 1311 context.rebind("warURL", urls[i]); 1312 context.rebind("isEarCase", new Boolean (true)); 1313 unRegisterWar(context); 1314 } catch (Exception ex) { 1315 logger.log(BasicLevel.ERROR, "Error when undeploying '" + warFileName + "'"); 1318 logger.log(BasicLevel.ERROR, ex.getMessage()); 1319 logger.log(BasicLevel.ERROR, "Cannot undeploy war of the ear application"); 1320 } 1321 } 1322 } 1323 1324 1329 public War getWar(URL url) { 1330 Enumeration wars = warDeployed.elements(); 1331 while (wars.hasMoreElements()) { 1332 War war = (War) wars.nextElement(); 1333 if (war.getWarURL().equals(url)) { 1334 return war; 1335 } 1336 } 1337 return null; 1338 } 1339 1340 1346 private List getWar(String pContext) { 1347 List checkDeployed = new ArrayList (); 1348 Enumeration wars = warDeployed.elements(); 1349 while (wars.hasMoreElements()) { 1350 War war = (War) wars.nextElement(); 1351 if (war.getContextRoot().equals(pContext)) { 1352 checkDeployed.add(war); 1353 } 1354 } 1355 return checkDeployed; 1356 } 1357 1358 1364 public void removeCache(ClassLoader earClassLoader) { 1365 WebManagerWrapper.removeCache(earClassLoader); 1366 } 1367 1368 1378 private URL checkWarFile(String fileName) throws JWebContainerServiceException { 1379 1380 File f = null; 1381 try { 1382 f = new File (fileName).getCanonicalFile(); 1383 if (!f.exists()) { 1384 boolean found = false; 1385 String warFileName = null; 1386 warFileName = WEBAPPS_DIR + File.separator + fileName; 1388 f = new File (warFileName).getCanonicalFile(); 1389 found = f.exists(); 1390 if (found) { 1391 fileName = f.getPath(); 1392 } else { 1393 String err = "File '" + f.getPath() + "' not found"; 1394 logger.log(BasicLevel.ERROR, err); 1395 throw new JWebContainerServiceException(err); 1396 } 1397 } 1398 } catch (IOException e) { 1399 String err = "Invalid war file name '" + fileName; 1400 logger.log(BasicLevel.ERROR, err); 1401 throw new JWebContainerServiceException(err, e); 1402 } 1403 1404 URL warURL = null; 1405 try { 1406 warURL = f.toURL(); 1407 } catch (MalformedURLException e) { 1408 String err = "Invalid war file name '" + fileName + "'."; 1409 logger.log(BasicLevel.ERROR, err + e.getMessage()); 1410 throw new JWebContainerServiceException(err, e); 1411 } 1412 return warURL; 1413 } 1414 1415 1423 private URL checkWarDeployed(String fileName) throws JWebContainerServiceException { 1424 1425 URL url = null; 1426 try { 1427 Enumeration wars = warDeployed.elements(); 1428 while (wars.hasMoreElements()) { 1429 War war = (War) wars.nextElement(); 1430 url = (new File (fileName).getCanonicalFile()).toURL(); 1431 if (war.getWarURL().equals(url)) { 1432 return url; 1433 } 1434 } 1435 String warFileName = WEBAPPS_DIR + File.separator + fileName; 1436 wars = warDeployed.elements(); 1437 while (wars.hasMoreElements()) { 1438 War war = (War) wars.nextElement(); 1439 url = (new File (warFileName).getCanonicalFile()).toURL(); 1440 if (war.getWarURL().equals(url)) { 1441 return url; 1442 } 1443 } 1444 1445 String err = "Cannot undeploy war: '" + fileName + "' is not deployed."; 1446 logger.log(BasicLevel.ERROR, err); 1447 throw new JWebContainerServiceException(err); 1448 1449 } catch (MalformedURLException e) { 1450 String err = "Invalid war file name '" + fileName + "'."; 1451 logger.log(BasicLevel.ERROR, err + e.getMessage()); 1452 throw new JWebContainerServiceException(err, e); 1453 } catch (IOException e) { 1454 String err = "Invalid war file name '" + fileName; 1455 logger.log(BasicLevel.ERROR, err); 1456 throw new JWebContainerServiceException(err, e); 1457 } 1458 } 1459 1460 1463 public Integer getCurrentNumberOfWars() { 1464 return new Integer (warDeployed.size()); 1465 } 1466 1467 1475 public List getInstalledWars() throws Exception { 1476 ArrayList al = JModule.getInstalledContainersInDir(WEBAPPS_DIR, JModule.WAR_EXTENSION, JModule.WAR_CHILD_DIR, 1478 JModule.WAR_CONFIRM_FILE); 1479 for (Iterator it = autoloadDirectories.iterator(); it.hasNext();) { 1481 al.addAll(JModule.getInstalledContainersInDir((String ) it.next(), JModule.WAR_EXTENSION, 1482 JModule.WAR_CHILD_DIR, JModule.WAR_CONFIRM_FILE)); 1483 } 1484 return al; 1485 } 1486 1487 1492 public Set getWarNames() { 1493 HashSet names = new HashSet (); 1494 for (Enumeration wars = warDeployed.elements(); wars.hasMoreElements();) { 1495 War war = (War) wars.nextElement(); 1496 URL warURL = war.getWarURL(); 1497 names.add(warURL.getFile()); 1498 } 1499 return names; 1500 } 1501 1502 1509 private void addWars(String dirPath) { 1510 boolean found = false; 1511 1512 File dir = new File (WEBAPPS_DIR + File.separator + dirPath); 1514 found = dir.isDirectory(); 1515 1516 if (found) { 1517 addWarsFrom(dir); 1518 } else { 1519 String err = "Warning: Cannot load dir: '" + dirPath + "' "; 1520 err += "is not a directory or directory doesn't exist"; 1521 logger.log(BasicLevel.WARN, err); 1522 } 1523 } 1524 1525 1530 private void addWarsFrom(File dir) throws JWebContainerServiceException { 1531 try { 1532 if (dir.isDirectory()) { 1533 File [] files = dir.listFiles(); 1534 for (int i = 0; i < files.length; i++) { 1535 if (files[i].isFile()) { 1536 if (files[i].getPath().toLowerCase().endsWith(".war")) { 1537 warNames.add(files[i].getCanonicalPath()); 1539 } 1540 } else { 1541 warNames.add(files[i].getCanonicalPath()); 1543 } 1544 } 1545 } 1546 } catch (IOException e) { 1547 String err = "Invalid file name '" + dir.getPath(); 1548 logger.log(BasicLevel.ERROR, err); 1549 throw new JWebContainerServiceException(err, e); 1550 } 1551 } 1552 1553 1558 public boolean isWarLoaded(String fileName) { 1559 1560 URL url = null; 1561 boolean isLoaded = false; 1562 try { 1563 try { 1565 url = new File (fileName).getCanonicalFile().toURL(); 1566 if (getWar(url) != null) { 1568 isLoaded = true; 1569 } else { 1570 url = null; 1572 } 1573 } catch (Exception e) { 1574 url = null; 1575 } 1576 if (url == null) { 1578 url = new File (WEBAPPS_DIR + File.separator + fileName).getCanonicalFile().toURL(); 1579 if (getWar(url) != null) { 1581 isLoaded = true; 1582 } 1583 } 1584 } catch (Exception e) { 1585 String err = "Can not found if the war is deployed or not"; 1586 logger.log(BasicLevel.ERROR, err); 1587 return false; 1588 } 1589 1590 return isLoaded; 1591 } 1592 1593 1597 public List getDeployedWars() { 1598 ArrayList al = new ArrayList (); 1599 for (Enumeration wars = warDeployed.elements(); wars.hasMoreElements();) { 1600 War war = (War) wars.nextElement(); 1601 URL warURL = war.getWarURL(); 1602 al.add(warURL.getFile()); 1603 } 1604 return al; 1605 } 1606 1607 1612 public List getDeployableWars() throws Exception { 1613 List al = getInstalledWars(); 1614 al.removeAll(getDeployedWars()); 1615 return al; 1616 } 1617 1618 1622 public List getAutoloadDirectories() { 1623 ArrayList al = new ArrayList (); 1624 for (Iterator it = autoloadDirectories.iterator(); it.hasNext();) { 1625 try { 1626 al.add(new File ((String ) it.next()).toURL().getPath()); 1627 } catch (Exception e) { 1628 if (logger.isLoggable(BasicLevel.DEBUG)) { 1629 logger.log(BasicLevel.DEBUG, "Can't get autoload directories " + e.getMessage()); 1630 } 1631 } 1633 } 1634 return al; 1635 } 1636 1637 1641 public String getWebappsDirectory() { 1642 String sRet = null; 1643 try { 1644 sRet = (new File (WEBAPPS_DIR)).toURL().getPath(); 1645 } catch (Exception e) { 1646 if (logger.isLoggable(BasicLevel.DEBUG)) { 1647 logger.log(BasicLevel.DEBUG, "Can't get webapps directory " + e.getMessage()); 1648 } 1649 } 1651 return sRet; 1652 } 1653 1654 1658 public String getServerName() { 1659 if (serverName == null) { 1660 updateServerInfos(); 1661 } 1662 return serverName; 1663 } 1664 1665 1669 public String getServerVersion() { 1670 if (serverVersion == null) { 1671 updateServerInfos(); 1672 } 1673 return serverVersion; 1674 } 1675 1676 1679 protected abstract void updateServerInfos(); 1680 1681 1687 public abstract String getDefaultHost() throws JWebContainerServiceException; 1688 1689 1696 public abstract String getDefaultHttpPort() throws JWebContainerServiceException; 1697 1698 1705 public abstract String getDefaultHttpsPort() throws JWebContainerServiceException; 1706 1707 1710 protected static Logger getLogger() { 1711 return logger; 1712 } 1713 1714 1717 protected ContainerNaming getNaming() { 1718 return naming; 1719 } 1720 1721 1724 protected MBeanServer getMbeanServer() { 1725 return mbeanServer; 1726 } 1727 1728 1731 protected void setServerName(String serverName) { 1732 this.serverName = serverName; 1733 } 1734 1735 1738 protected void setServerVersion(String serverVersion) { 1739 this.serverVersion = serverVersion; 1740 } 1741 1742 1747 public class WebLoaderHolder { 1748 1749 1752 private URLClassLoader jonasWebLoader; 1753 1754 1757 private ClassLoader envWebLoader; 1758 1759 1764 public WebLoaderHolder(final URLClassLoader jonas, final ClassLoader env) { 1765 jonasWebLoader = jonas; 1766 envWebLoader = env; 1767 } 1768 1769 1772 public URLClassLoader getJonasWebLoader() { 1773 return jonasWebLoader; 1774 } 1775 1776 1779 public ClassLoader getEnvWebLoader() { 1780 return envWebLoader; 1781 } 1782 1783 1786 public void setEnvWebLoader(ClassLoader envWebLoader) { 1787 this.envWebLoader = envWebLoader; 1788 } 1789 1790 1793 public void setJonasWebLoader(URLClassLoader jonasWebLoader) { 1794 this.jonasWebLoader = jonasWebLoader; 1795 } 1796 } 1797 1798} 1799 | Popular Tags |