protected void applyMetaInfContextXml(Resource rootResource) throws Exception { if (_bundle == null) return; if (_webApp == null) return; ClassLoader cl = Thread.currentThread().getContextClassLoader(); LOG.debug("Context classloader = " + cl); try { Thread.currentThread().setContextClassLoader(_webApp.getClassLoader()); // TODO replace this with getting the InputStream so we don't cache in URL // find if there is a META-INF/context.xml file URL contextXmlUrl = _bundle.getEntry("/META-INF/jetty-webapp-context.xml"); if (contextXmlUrl == null) return; // Apply it just as the standard jetty ContextProvider would do LOG.info("Applying " + contextXmlUrl + " to " + _webApp); XmlConfiguration xmlConfiguration = new XmlConfiguration(contextXmlUrl); HashMap properties = new HashMap(); properties.put("Server", getDeploymentManager().getServer()); properties.put(OSGiWebappConstants.JETTY_BUNDLE_ROOT, rootResource.toString()); properties.put( OSGiServerConstants.JETTY_HOME, getDeploymentManager().getServer().getAttribute(OSGiServerConstants.JETTY_HOME)); xmlConfiguration.getProperties().putAll(properties); xmlConfiguration.configure(_webApp); } finally { Thread.currentThread().setContextClassLoader(cl); } }
/** * @param context * @return list of non-excluded {@link ServletContainerInitializer}s * @throws Exception */ public List<ServletContainerInitializer> getNonExcludedInitializers(WebAppContext context) throws Exception { ArrayList<ServletContainerInitializer> nonExcludedInitializers = new ArrayList<ServletContainerInitializer>(); // We use the ServiceLoader mechanism to find the ServletContainerInitializer classes to inspect long start = 0; ClassLoader old = Thread.currentThread().getContextClassLoader(); ServiceLoader<ServletContainerInitializer> loadedInitializers = null; try { if (LOG.isDebugEnabled()) start = System.nanoTime(); Thread.currentThread().setContextClassLoader(context.getClassLoader()); loadedInitializers = ServiceLoader.load(ServletContainerInitializer.class); } finally { Thread.currentThread().setContextClassLoader(old); } if (LOG.isDebugEnabled()) LOG.debug( "Service loaders found in {}ms", (TimeUnit.MILLISECONDS.convert((System.nanoTime() - start), TimeUnit.NANOSECONDS))); Map<ServletContainerInitializer, Resource> sciResourceMap = new HashMap<ServletContainerInitializer, Resource>(); ServletContainerInitializerOrdering initializerOrdering = getInitializerOrdering(context); // Get initial set of SCIs that aren't from excluded jars or excluded by the // containerExclusionPattern, or excluded // because containerInitializerOrdering omits it for (ServletContainerInitializer sci : loadedInitializers) { if (matchesExclusionPattern(sci)) { if (LOG.isDebugEnabled()) LOG.debug("{} excluded by pattern", sci); continue; } Resource sciResource = getJarFor(sci); if (isFromExcludedJar(context, sci, sciResource)) { if (LOG.isDebugEnabled()) LOG.debug("{} is from excluded jar", sci); continue; } // check containerInitializerOrdering doesn't exclude it String name = sci.getClass().getName(); if (initializerOrdering != null && (!initializerOrdering.hasWildcard() && initializerOrdering.getIndexOf(name) < 0)) { if (LOG.isDebugEnabled()) LOG.debug("{} is excluded by ordering", sci); continue; } sciResourceMap.put(sci, sciResource); } // Order the SCIs that are included if (initializerOrdering != null && !initializerOrdering.isDefaultOrder()) { if (LOG.isDebugEnabled()) LOG.debug("Ordering ServletContainerInitializers with " + initializerOrdering); // There is an ordering that is not just "*". // Arrange ServletContainerInitializers according to the ordering of classnames given, // irrespective of coming from container or webapp classpaths nonExcludedInitializers.addAll(sciResourceMap.keySet()); Collections.sort( nonExcludedInitializers, new ServletContainerInitializerComparator(initializerOrdering)); } else { // No jetty-specific ordering specified, or just the wildcard value "*" specified. // Fallback to ordering the ServletContainerInitializers according to: // container classpath first, WEB-INF/classes then WEB-INF/lib (obeying any web.xml jar // ordering) // no web.xml ordering defined, add SCIs in any order if (context.getMetaData().getOrdering() == null) { if (LOG.isDebugEnabled()) LOG.debug("No web.xml ordering, ServletContainerInitializers in random order"); nonExcludedInitializers.addAll(sciResourceMap.keySet()); } else { if (LOG.isDebugEnabled()) LOG.debug( "Ordering ServletContainerInitializers with ordering {}", context.getMetaData().getOrdering()); for (Map.Entry<ServletContainerInitializer, Resource> entry : sciResourceMap.entrySet()) { // add in SCIs from the container classpath if (entry.getKey().getClass().getClassLoader() == context.getClassLoader().getParent()) nonExcludedInitializers.add(entry.getKey()); else if (entry.getValue() == null) // add in SCIs not in a jar, as they must be from WEB-INF/classes and can't // be ordered nonExcludedInitializers.add(entry.getKey()); } // add SCIs according to the ordering of its containing jar for (Resource webInfJar : context.getMetaData().getOrderedWebInfJars()) { for (Map.Entry<ServletContainerInitializer, Resource> entry : sciResourceMap.entrySet()) { if (webInfJar.equals(entry.getValue())) nonExcludedInitializers.add(entry.getKey()); } } } } if (LOG.isDebugEnabled()) { int i = 0; for (ServletContainerInitializer sci : nonExcludedInitializers) LOG.debug("ServletContainerInitializer: {} {}", (++i), sci.getClass().getName()); } return nonExcludedInitializers; }
/** * Test if the ServletContainerInitializer is from the container classpath * * @param context * @param sci * @return */ public boolean isFromContainerClassPath(WebAppContext context, ServletContainerInitializer sci) { if (sci == null) return false; return sci.getClass().getClassLoader() == context.getClassLoader().getParent(); }