public void unpack(WebAppContext context) throws IOException { Resource web_app = context.getBaseResource(); _preUnpackBaseResource = context.getBaseResource(); if (web_app == null) { String war = context.getWar(); if (war != null && war.length() > 0) web_app = context.newResource(war); else web_app = context.getBaseResource(); // Accept aliases for WAR files if (web_app.getAlias() != null) { LOG.debug(web_app + " anti-aliased to " + web_app.getAlias()); web_app = context.newResource(web_app.getAlias()); } if (LOG.isDebugEnabled()) LOG.debug( "Try webapp=" + web_app + ", exists=" + web_app.exists() + ", directory=" + web_app.isDirectory() + " file=" + (web_app.getFile())); // Is the WAR usable directly? if (web_app.exists() && !web_app.isDirectory() && !web_app.toString().startsWith("jar:")) { // No - then lets see if it can be turned into a jar URL. Resource jarWebApp = JarResource.newJarResource(web_app); if (jarWebApp.exists() && jarWebApp.isDirectory()) web_app = jarWebApp; } // If we should extract or the URL is still not usable if (web_app.exists() && ((context.isCopyWebDir() && web_app.getFile() != null && web_app.getFile().isDirectory()) || (context.isExtractWAR() && web_app.getFile() != null && !web_app.getFile().isDirectory()) || (context.isExtractWAR() && web_app.getFile() == null) || !web_app.isDirectory())) { // Look for sibling directory. File extractedWebAppDir = null; if (war != null) { // look for a sibling like "foo/" to a "foo.war" File warfile = Resource.newResource(war).getFile(); if (warfile != null && warfile.getName().toLowerCase(Locale.ENGLISH).endsWith(".war")) { File sibling = new File( warfile.getParent(), warfile.getName().substring(0, warfile.getName().length() - 4)); if (sibling.exists() && sibling.isDirectory() && sibling.canWrite()) extractedWebAppDir = sibling; } } if (extractedWebAppDir == null) // Then extract it if necessary to the temporary location extractedWebAppDir = new File(context.getTempDirectory(), "webapp"); if (web_app.getFile() != null && web_app.getFile().isDirectory()) { // Copy directory LOG.info("Copy " + web_app + " to " + extractedWebAppDir); web_app.copyTo(extractedWebAppDir); } else { // Use a sentinel file that will exist only whilst the extraction is taking place. // This will help us detect interrupted extractions. File extractionLock = new File(context.getTempDirectory(), ".extract_lock"); if (!extractedWebAppDir.exists()) { // it hasn't been extracted before so extract it extractionLock.createNewFile(); extractedWebAppDir.mkdir(); LOG.info("Extract " + web_app + " to " + extractedWebAppDir); Resource jar_web_app = JarResource.newJarResource(web_app); jar_web_app.copyTo(extractedWebAppDir); extractionLock.delete(); } else { // only extract if the war file is newer, or a .extract_lock file is left behind meaning // a possible partial extraction if (web_app.lastModified() > extractedWebAppDir.lastModified() || extractionLock.exists()) { extractionLock.createNewFile(); IO.delete(extractedWebAppDir); extractedWebAppDir.mkdir(); LOG.info("Extract " + web_app + " to " + extractedWebAppDir); Resource jar_web_app = JarResource.newJarResource(web_app); jar_web_app.copyTo(extractedWebAppDir); extractionLock.delete(); } } } web_app = Resource.newResource(extractedWebAppDir.getCanonicalPath()); } // Now do we have something usable? if (!web_app.exists() || !web_app.isDirectory()) { LOG.warn("Web application not found " + war); throw new java.io.FileNotFoundException(war); } context.setBaseResource(web_app); if (LOG.isDebugEnabled()) LOG.debug("webapp=" + web_app); } // Do we need to extract WEB-INF/lib? if (context.isCopyWebInf() && !context.isCopyWebDir()) { Resource web_inf = web_app.addPath("WEB-INF/"); File extractedWebInfDir = new File(context.getTempDirectory(), "webinf"); if (extractedWebInfDir.exists()) IO.delete(extractedWebInfDir); extractedWebInfDir.mkdir(); Resource web_inf_lib = web_inf.addPath("lib/"); File webInfDir = new File(extractedWebInfDir, "WEB-INF"); webInfDir.mkdir(); if (web_inf_lib.exists()) { File webInfLibDir = new File(webInfDir, "lib"); if (webInfLibDir.exists()) IO.delete(webInfLibDir); webInfLibDir.mkdir(); LOG.info("Copying WEB-INF/lib " + web_inf_lib + " to " + webInfLibDir); web_inf_lib.copyTo(webInfLibDir); } Resource web_inf_classes = web_inf.addPath("classes/"); if (web_inf_classes.exists()) { File webInfClassesDir = new File(webInfDir, "classes"); if (webInfClassesDir.exists()) IO.delete(webInfClassesDir); webInfClassesDir.mkdir(); LOG.info( "Copying WEB-INF/classes from " + web_inf_classes + " to " + webInfClassesDir.getAbsolutePath()); web_inf_classes.copyTo(webInfClassesDir); } web_inf = Resource.newResource(extractedWebInfDir.getCanonicalPath()); ResourceCollection rc = new ResourceCollection(web_inf, web_app); if (LOG.isDebugEnabled()) LOG.debug("context.resourcebase = " + rc); context.setBaseResource(rc); } }
public void configureContextHandler() throws Exception { if (_configured) return; _configured = true; // Override for bundle root may have been set String bundleOverrideLocation = (String) _properties.get(OSGiWebappConstants.JETTY_BUNDLE_INSTALL_LOCATION_OVERRIDE); if (bundleOverrideLocation == null) bundleOverrideLocation = (String) _properties.get(OSGiWebappConstants.SERVICE_PROP_BUNDLE_INSTALL_LOCATION_OVERRIDE); // Location on filesystem of bundle or the bundle override location File bundleLocation = BundleFileLocatorHelperFactory.getFactory().getHelper().getBundleInstallLocation(_bundle); File root = (bundleOverrideLocation == null ? bundleLocation : new File(bundleOverrideLocation)); Resource rootResource = Resource.newResource( BundleFileLocatorHelperFactory.getFactory() .getHelper() .getLocalURL(root.toURI().toURL())); // try and make sure the rootResource is useable - if its a jar then make it a jar file url if (rootResource.exists() && !rootResource.isDirectory() && !rootResource.toString().startsWith("jar:")) { Resource jarResource = JarResource.newJarResource(rootResource); if (jarResource.exists() && jarResource.isDirectory()) rootResource = jarResource; } // Set the base resource of the ContextHandler, if not already set, can also be overridden by // the context xml file if (_contextHandler != null && _contextHandler.getBaseResource() == null) { _contextHandler.setBaseResource(rootResource); } // Use a classloader that knows about the common jetty parent loader, and also the bundle OSGiClassLoader classLoader = new OSGiClassLoader(getServerInstanceWrapper().getParentClassLoaderForWebapps(), _bundle); // if there is a context file, find it and apply it if (_contextFile == null && _contextHandler == null) throw new IllegalStateException("No context file or ContextHandler"); if (_contextFile != null) { // apply the contextFile, creating the ContextHandler, the DeploymentManager will register // it in the ContextHandlerCollection Resource res = null; // try to find the context file in the filesystem if (_contextFile.startsWith("/")) res = getFileAsResource(_contextFile); // try to find it relative to jetty home if (res == null) { // See if the specific server we are related to has jetty.home set String jettyHome = (String) getServerInstanceWrapper() .getServer() .getAttribute(OSGiServerConstants.JETTY_HOME); if (jettyHome != null) res = getFileAsResource(jettyHome, _contextFile); // try to see if a SystemProperty for jetty.home is set if (res == null) { jettyHome = System.getProperty(OSGiServerConstants.JETTY_HOME); if (jettyHome != null) { if (jettyHome.startsWith("\"") || jettyHome.startsWith("'")) jettyHome = jettyHome.substring(1); if (jettyHome.endsWith("\"") || (jettyHome.endsWith("'"))) jettyHome = jettyHome.substring(0, jettyHome.length() - 1); res = getFileAsResource(jettyHome, _contextFile); if (LOG.isDebugEnabled()) LOG.debug("jetty home context file:" + res); } } } // try to find it relative to an override location that has been specified if (res == null) { if (bundleOverrideLocation != null) { res = getFileAsResource( Resource.newResource(bundleOverrideLocation).getFile(), _contextFile); if (LOG.isDebugEnabled()) LOG.debug("Bundle override location context file:" + res); } } // try to find it relative to the bundle in which it is being deployed if (res == null) { if (_contextFile.startsWith("./")) _contextFile = _contextFile.substring(1); if (!_contextFile.startsWith("/")) _contextFile = "/" + _contextFile; URL contextURL = _bundle.getEntry(_contextFile); if (contextURL != null) res = Resource.newResource(contextURL); } // apply the context xml file, either to an existing ContextHandler, or letting the // it create the ContextHandler as necessary if (res != null) { ClassLoader cl = Thread.currentThread().getContextClassLoader(); LOG.debug("Context classloader = " + cl); try { Thread.currentThread().setContextClassLoader(classLoader); XmlConfiguration xmlConfiguration = new XmlConfiguration(res.getInputStream()); HashMap properties = new HashMap(); // put the server instance in properties.put("Server", getServerInstanceWrapper().getServer()); // put in the location of the bundle root properties.put("bundle.root", rootResource.toString()); // insert the bundle's location as a property. xmlConfiguration.getProperties().putAll(properties); if (_contextHandler == null) _contextHandler = (ContextHandler) xmlConfiguration.configure(); else xmlConfiguration.configure(_contextHandler); } finally { Thread.currentThread().setContextClassLoader(cl); } } } // Set up the class loader we created _contextHandler.setClassLoader(classLoader); // If a bundle/service property specifies context path, let it override the context xml String contextPath = (String) _properties.get(OSGiWebappConstants.RFC66_WEB_CONTEXTPATH); if (contextPath == null) contextPath = (String) _properties.get(OSGiWebappConstants.SERVICE_PROP_CONTEXT_PATH); if (contextPath != null) _contextHandler.setContextPath(contextPath); // osgi Enterprise Spec r4 p.427 _contextHandler.setAttribute( OSGiWebappConstants.OSGI_BUNDLECONTEXT, _bundle.getBundleContext()); // make sure we protect also the osgi dirs specified by OSGi Enterprise spec String[] targets = _contextHandler.getProtectedTargets(); int length = (targets == null ? 0 : targets.length); String[] updatedTargets = null; if (targets != null) { updatedTargets = new String[length + OSGiWebappConstants.DEFAULT_PROTECTED_OSGI_TARGETS.length]; System.arraycopy(targets, 0, updatedTargets, 0, length); } else updatedTargets = new String[OSGiWebappConstants.DEFAULT_PROTECTED_OSGI_TARGETS.length]; System.arraycopy( OSGiWebappConstants.DEFAULT_PROTECTED_OSGI_TARGETS, 0, updatedTargets, length, OSGiWebappConstants.DEFAULT_PROTECTED_OSGI_TARGETS.length); _contextHandler.setProtectedTargets(updatedTargets); }
public void configureWebApp() throws Exception { // TODO turn this around and let any context.xml file get applied first, and have the // properties override _webApp.setContextPath(_contextPath); // osgi Enterprise Spec r4 p.427 _webApp.setAttribute(OSGiWebappConstants.OSGI_BUNDLECONTEXT, _bundle.getBundleContext()); String overrideBundleInstallLocation = (String) _properties.get(OSGiWebappConstants.JETTY_BUNDLE_INSTALL_LOCATION_OVERRIDE); File bundleInstallLocation = (overrideBundleInstallLocation == null ? BundleFileLocatorHelperFactory.getFactory() .getHelper() .getBundleInstallLocation(_bundle) : new File(overrideBundleInstallLocation)); URL url = null; Resource rootResource = Resource.newResource( BundleFileLocatorHelperFactory.getFactory() .getHelper() .getLocalURL(bundleInstallLocation.toURI().toURL())); // try and make sure the rootResource is useable - if its a jar then make it a jar file url if (rootResource.exists() && !rootResource.isDirectory() && !rootResource.toString().startsWith("jar:")) { Resource jarResource = JarResource.newJarResource(rootResource); if (jarResource.exists() && jarResource.isDirectory()) rootResource = jarResource; } // if the path wasn't set or it was ., then it is the root of the bundle's installed location if (_webAppPath == null || _webAppPath.length() == 0 || ".".equals(_webAppPath)) { url = bundleInstallLocation.toURI().toURL(); } else { // Get the location of the root of the webapp inside the installed bundle if (_webAppPath.startsWith("/") || _webAppPath.startsWith("file:")) { url = new File(_webAppPath).toURI().toURL(); } else if (bundleInstallLocation != null && bundleInstallLocation.isDirectory()) { url = new File(bundleInstallLocation, _webAppPath).toURI().toURL(); } else if (bundleInstallLocation != null) { Enumeration<URL> urls = BundleFileLocatorHelperFactory.getFactory() .getHelper() .findEntries(_bundle, _webAppPath); if (urls != null && urls.hasMoreElements()) url = urls.nextElement(); } } if (url == null) { throw new IllegalArgumentException( "Unable to locate " + _webAppPath + " in " + (bundleInstallLocation != null ? bundleInstallLocation.getAbsolutePath() : "unlocated bundle '" + _bundle.getSymbolicName() + "'")); } // Sets the location of the war file // converts bundleentry: protocol if necessary _webApp.setWar( BundleFileLocatorHelperFactory.getFactory().getHelper().getLocalURL(url).toString()); // Set up what has been configured on the provider _webApp.setParentLoaderPriority(isParentLoaderPriority()); _webApp.setExtractWAR(isExtract()); if (getConfigurationClasses() != null) _webApp.setConfigurationClasses(getConfigurationClasses()); else _webApp.setConfigurationClasses(__defaultConfigurations); if (getDefaultsDescriptor() != null) _webApp.setDefaultsDescriptor(getDefaultsDescriptor()); // Set up configuration from manifest headers // extra classpath String tmp = (String) _properties.get(OSGiWebappConstants.JETTY_EXTRA_CLASSPATH); if (tmp != null) _webApp.setExtraClasspath(tmp); // web.xml tmp = (String) _properties.get(OSGiWebappConstants.JETTY_WEB_XML_PATH); if (tmp != null && tmp.trim().length() != 0) { File webXml = getFile(tmp, bundleInstallLocation); if (webXml != null && webXml.exists()) _webApp.setDescriptor(webXml.getAbsolutePath()); } // webdefault.xml tmp = (String) _properties.get(OSGiWebappConstants.JETTY_DEFAULT_WEB_XML_PATH); if (tmp != null) { File defaultWebXml = getFile(tmp, bundleInstallLocation); if (defaultWebXml != null) { if (defaultWebXml.exists()) _webApp.setDefaultsDescriptor(defaultWebXml.getAbsolutePath()); else LOG.warn(defaultWebXml.getAbsolutePath() + " does not exist"); } } // Handle Require-TldBundle // This is a comma separated list of names of bundles that contain tlds that this webapp uses. // We add them to the webapp classloader. String requireTldBundles = (String) _properties.get(OSGiWebappConstants.REQUIRE_TLD_BUNDLE); String pathsToTldBundles = getPathsToRequiredBundles(requireTldBundles); // make sure we provide access to all the jetty bundles by going // through this bundle. OSGiWebappClassLoader webAppLoader = new OSGiWebappClassLoader( _serverWrapper.getParentClassLoaderForWebapps(), _webApp, _bundle); if (pathsToTldBundles != null) webAppLoader.addClassPath(pathsToTldBundles); _webApp.setClassLoader(webAppLoader); // apply any META-INF/context.xml file that is found to configure // the webapp first applyMetaInfContextXml(rootResource); // pass the value of the require tld bundle so that the TagLibOSGiConfiguration // can pick it up. _webApp.setAttribute(OSGiWebappConstants.REQUIRE_TLD_BUNDLE, requireTldBundles); // Set up some attributes // rfc66 _webApp.setAttribute( OSGiWebappConstants.RFC66_OSGI_BUNDLE_CONTEXT, _bundle.getBundleContext()); // spring-dm-1.2.1 looks for the BundleContext as a different attribute. // not a spec... but if we want to support // org.springframework.osgi.web.context.support.OsgiBundleXmlWebApplicationContext // then we need to do this to: _webApp.setAttribute( "org.springframework.osgi.web." + BundleContext.class.getName(), _bundle.getBundleContext()); // also pass the bundle directly. sometimes a bundle does not have a // bundlecontext. // it is still useful to have access to the Bundle from the servlet // context. _webApp.setAttribute(OSGiWebappConstants.JETTY_OSGI_BUNDLE, _bundle); }