Search in sources :

Example 1 with Resource

use of org.eclipse.jetty.util.resource.Resource in project dropwizard by dropwizard.

the class ServletEnvironmentTest method setsBaseResourceStringList.

@Test
public void setsBaseResourceStringList() throws Exception {
    String wooResource = tempDir.newFolder().getAbsolutePath();
    String fooResource = tempDir.newFolder().getAbsolutePath();
    final String[] testResources = new String[] { wooResource, fooResource };
    environment.setBaseResource(testResources);
    ArgumentCaptor<Resource> captor = ArgumentCaptor.forClass(Resource.class);
    verify(handler).setBaseResource(captor.capture());
    Resource actualResource = captor.getValue();
    assertThat(actualResource).isInstanceOf(ResourceCollection.class);
    ResourceCollection actualResourceCollection = (ResourceCollection) actualResource;
    assertThat(actualResourceCollection.getResources()).contains(Resource.newResource(wooResource), Resource.newResource(fooResource));
}
Also used : Resource(org.eclipse.jetty.util.resource.Resource) ResourceCollection(org.eclipse.jetty.util.resource.ResourceCollection) Test(org.junit.Test)

Example 2 with Resource

use of org.eclipse.jetty.util.resource.Resource in project jetty.project by eclipse.

the class AnnotationConfiguration method isFromExcludedJar.

/**
     * Check to see if the ServletContainerIntializer loaded via the ServiceLoader came
     * from a jar that is excluded by the fragment ordering. See ServletSpec 3.0 p.85.
     * 
     * @param context the context for the jars
     * @param sci the servlet container initializer
     * @param sciResource  the resource for the servlet container initializer
     * @return true if excluded
     * @throws Exception if unable to determine exclusion
     */
public boolean isFromExcludedJar(WebAppContext context, ServletContainerInitializer sci, Resource sciResource) throws Exception {
    if (sci == null)
        throw new IllegalArgumentException("ServletContainerInitializer null");
    if (context == null)
        throw new IllegalArgumentException("WebAppContext null");
    //of WEB-INF/lib jars
    if (isFromContainerClassPath(context, sci)) {
        if (LOG.isDebugEnabled())
            LOG.debug("!Excluded {} from container classpath", sci);
        return false;
    }
    //If no ordering, nothing is excluded
    if (context.getMetaData().getOrdering() == null) {
        if (LOG.isDebugEnabled())
            LOG.debug("!Excluded {} no ordering", sci);
        return false;
    }
    List<Resource> orderedJars = context.getMetaData().getOrderedWebInfJars();
    //there is an ordering, but there are no jars resulting from the ordering, everything excluded
    if (orderedJars.isEmpty()) {
        if (LOG.isDebugEnabled())
            LOG.debug("Excluded {} empty ordering", sci);
        return true;
    }
    if (sciResource == null) {
        //not from a jar therefore not from WEB-INF so not excludable
        if (LOG.isDebugEnabled())
            LOG.debug("!Excluded {} not from jar", sci);
        return false;
    }
    URI loadingJarURI = sciResource.getURI();
    boolean found = false;
    Iterator<Resource> itor = orderedJars.iterator();
    while (!found && itor.hasNext()) {
        Resource r = itor.next();
        found = r.getURI().equals(loadingJarURI);
    }
    if (LOG.isDebugEnabled())
        LOG.debug("{}Excluded {} found={}", found ? "!" : "", sci, found);
    return !found;
}
Also used : Resource(org.eclipse.jetty.util.resource.Resource) URI(java.net.URI)

Example 3 with Resource

use of org.eclipse.jetty.util.resource.Resource in project jetty.project by eclipse.

the class AnnotationConfiguration method getNonExcludedInitializers.

/**
     * Get SCIs that are not excluded from consideration
     * @param context the web app context 
     * @return the list of non-excluded servlet container initializers
     * @throws Exception if unable to get list 
     */
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();
    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);
    //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 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 (//add in SCIs not in a jar, as they must be from WEB-INF/classes and can't be ordered
                entry.getValue() == null)
                    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: {} {} from {}", (++i), sci.getClass().getName(), sciResourceMap.get(sci));
    }
    return nonExcludedInitializers;
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ArrayList(java.util.ArrayList) Resource(org.eclipse.jetty.util.resource.Resource) ServletContainerInitializer(javax.servlet.ServletContainerInitializer) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 4 with Resource

use of org.eclipse.jetty.util.resource.Resource in project jetty.project by eclipse.

the class AnnotationConfiguration method parseWebInfLib.

/**
     * Scan jars in WEB-INF/lib
     * 
     * @param context the context for the scan
     * @param parser the annotation parser to use
     * @throws Exception if unable to scan and/or parse
     */
public void parseWebInfLib(final WebAppContext context, final AnnotationParser parser) throws Exception {
    List<FragmentDescriptor> frags = context.getMetaData().getFragments();
    //email from Rajiv Mordani jsrs 315 7 April 2010
    //jars that do not have a web-fragment.xml are still considered fragments
    //they have to participate in the ordering
    ArrayList<URI> webInfUris = new ArrayList<URI>();
    List<Resource> jars = null;
    if (context.getMetaData().getOrdering() != null)
        jars = context.getMetaData().getOrderedWebInfJars();
    else
        //No ordering just use the jars in any order
        jars = context.getMetaData().getWebInfJars();
    _webInfLibStats = new CounterStatistic();
    for (Resource r : jars) {
        //for each jar, we decide which set of annotations we need to parse for
        final Set<Handler> handlers = new HashSet<Handler>();
        FragmentDescriptor f = getFragmentFromJar(r, frags);
        //or if it has a fragment we scan it if it is not metadata complete
        if (f == null || !isMetaDataComplete(f) || _classInheritanceHandler != null || !_containerInitializerAnnotationHandlers.isEmpty()) {
            //register the classinheritance handler if there is one
            if (_classInheritanceHandler != null)
                handlers.add(_classInheritanceHandler);
            //register the handlers for the @HandlesTypes values that are themselves annotations if there are any
            handlers.addAll(_containerInitializerAnnotationHandlers);
            //only register the discoverable annotation handlers if this fragment is not metadata complete, or has no fragment descriptor
            if (f == null || !isMetaDataComplete(f))
                handlers.addAll(_discoverableAnnotationHandlers);
            if (_parserTasks != null) {
                ParserTask task = new ParserTask(parser, handlers, r);
                _parserTasks.add(task);
                _webInfLibStats.increment();
                if (LOG.isDebugEnabled())
                    task.setStatistic(new TimeStatistic());
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) Resource(org.eclipse.jetty.util.resource.Resource) Handler(org.eclipse.jetty.annotations.AnnotationParser.Handler) URI(java.net.URI) CounterStatistic(org.eclipse.jetty.util.statistic.CounterStatistic) FragmentDescriptor(org.eclipse.jetty.webapp.FragmentDescriptor) ConcurrentHashSet(org.eclipse.jetty.util.ConcurrentHashSet) HashSet(java.util.HashSet)

Example 5 with Resource

use of org.eclipse.jetty.util.resource.Resource in project jetty.project by eclipse.

the class ExampleServerXml method main.

public static void main(String[] args) throws Exception {
    // Find Jetty XML (in classpath) that configures and starts Server.
    Resource serverXml = Resource.newSystemResource("exampleserver.xml");
    XmlConfiguration.main(serverXml.getFile().getAbsolutePath());
}
Also used : Resource(org.eclipse.jetty.util.resource.Resource)

Aggregations

Resource (org.eclipse.jetty.util.resource.Resource)196 Test (org.junit.Test)79 File (java.io.File)46 URL (java.net.URL)39 ArrayList (java.util.ArrayList)38 Matchers.containsString (org.hamcrest.Matchers.containsString)31 IOException (java.io.IOException)28 ResourceCollection (org.eclipse.jetty.util.resource.ResourceCollection)18 JarResource (org.eclipse.jetty.util.resource.JarResource)16 XmlConfiguration (org.eclipse.jetty.xml.XmlConfiguration)16 Server (org.eclipse.jetty.server.Server)13 HashSet (java.util.HashSet)12 InputStream (java.io.InputStream)9 HashMap (java.util.HashMap)9 URI (java.net.URI)8 MalformedURLException (java.net.MalformedURLException)7 StringTokenizer (java.util.StringTokenizer)7 URISyntaxException (java.net.URISyntaxException)6 Properties (java.util.Properties)6 Set (java.util.Set)6