Search in sources :

Example 1 with ResourceCollection

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

the class ResourceCacheTest method testUncacheable.

@Test
public void testUncacheable() throws Exception {
    ResourceCollection rc = new ResourceCollection(new String[] { "../jetty-util/src/test/resources/org/eclipse/jetty/util/resource/one/", "../jetty-util/src/test/resources/org/eclipse/jetty/util/resource/two/", "../jetty-util/src/test/resources/org/eclipse/jetty/util/resource/three/" });
    Resource[] r = rc.getResources();
    MimeTypes mime = new MimeTypes();
    CachedContentFactory rc3 = new CachedContentFactory(null, r[2], mime, false, false, CompressedContentFormat.NONE);
    CachedContentFactory rc2 = new CachedContentFactory(rc3, r[1], mime, false, false, CompressedContentFormat.NONE) {

        @Override
        public boolean isCacheable(Resource resource) {
            return super.isCacheable(resource) && resource.getName().indexOf("2.txt") < 0;
        }
    };
    CachedContentFactory rc1 = new CachedContentFactory(rc2, r[0], mime, false, false, CompressedContentFormat.NONE);
    assertEquals("1 - one", getContent(rc1, "1.txt"));
    assertEquals("2 - two", getContent(rc1, "2.txt"));
    assertEquals("3 - three", getContent(rc1, "3.txt"));
    assertEquals("1 - two", getContent(rc2, "1.txt"));
    assertEquals("2 - two", getContent(rc2, "2.txt"));
    assertEquals("3 - three", getContent(rc2, "3.txt"));
    assertEquals(null, getContent(rc3, "1.txt"));
    assertEquals("2 - three", getContent(rc3, "2.txt"));
    assertEquals("3 - three", getContent(rc3, "3.txt"));
}
Also used : Resource(org.eclipse.jetty.util.resource.Resource) MimeTypes(org.eclipse.jetty.http.MimeTypes) ResourceCollection(org.eclipse.jetty.util.resource.ResourceCollection) Test(org.junit.Test)

Example 2 with ResourceCollection

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

the class WebAppClassLoader method addClassPath.

/* ------------------------------------------------------------ */
/**
     * @param classPath Comma or semicolon separated path of filenames or URLs
     * pointing to directories or jar files. Directories should end
     * with '/'.
     * @throws IOException if unable to add classpath
     */
public void addClassPath(String classPath) throws IOException {
    if (classPath == null)
        return;
    StringTokenizer tokenizer = new StringTokenizer(classPath, ",;");
    while (tokenizer.hasMoreTokens()) {
        Resource resource = _context.newResource(tokenizer.nextToken().trim());
        if (LOG.isDebugEnabled())
            LOG.debug("Path resource=" + resource);
        // Add the resource
        if (resource.isDirectory() && resource instanceof ResourceCollection)
            addClassPath(resource);
        else {
            // Resolve file path if possible
            File file = resource.getFile();
            if (file != null) {
                URL url = resource.getURI().toURL();
                addURL(url);
            } else if (resource.isDirectory()) {
                addURL(resource.getURI().toURL());
            } else {
                if (LOG.isDebugEnabled())
                    LOG.debug("Check file exists and is not nested jar: " + resource);
                throw new IllegalArgumentException("File not resolvable or incompatible with URLClassloader: " + resource);
            }
        }
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) Resource(org.eclipse.jetty.util.resource.Resource) File(java.io.File) URL(java.net.URL) ResourceCollection(org.eclipse.jetty.util.resource.ResourceCollection)

Example 3 with ResourceCollection

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

the class MavenQuickStartConfiguration method deconfigure.

@Override
public void deconfigure(WebAppContext context) throws Exception {
    //if we're not persisting the temp dir, get rid of any overlays
    if (!context.isPersistTempDirectory()) {
        Resource originalBases = (Resource) context.getAttribute("org.eclipse.jetty.resources.originalBases");
        String originalBaseStr = originalBases.toString();
        //Iterate over all of the resource bases and ignore any that were original bases, just
        //deleting the overlays
        Resource res = context.getBaseResource();
        if (res instanceof ResourceCollection) {
            for (Resource r : ((ResourceCollection) res).getResources()) {
                if (originalBaseStr.contains(r.toString()))
                    continue;
                IO.delete(r.getFile());
            }
        }
    }
}
Also used : Resource(org.eclipse.jetty.util.resource.Resource) ResourceCollection(org.eclipse.jetty.util.resource.ResourceCollection)

Example 4 with ResourceCollection

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

the class MavenWebInfConfiguration method unpack.

/** 
     * @see org.eclipse.jetty.webapp.WebInfConfiguration#unpack(org.eclipse.jetty.webapp.WebAppContext)
     */
@Override
public void unpack(WebAppContext context) throws IOException {
    //Unpack and find base resource as normal
    super.unpack(context);
    //Get the base resource for the "virtual" webapp
    _originalResourceBase = context.getBaseResource();
    JettyWebAppContext jwac = (JettyWebAppContext) context;
    //determine sequencing of overlays
    _unpackedOverlayResources = new ArrayList<Resource>();
    if (jwac.getOverlays() != null && !jwac.getOverlays().isEmpty()) {
        List<Resource> resourceBaseCollection = new ArrayList<Resource>();
        for (Overlay o : jwac.getOverlays()) {
            //can refer to the current project in list of overlays for ordering purposes
            if (o.getConfig() != null && o.getConfig().isCurrentProject() && _originalResourceBase.exists()) {
                resourceBaseCollection.add(_originalResourceBase);
                LOG.debug("Adding virtual project to resource base list");
                continue;
            }
            Resource unpacked = unpackOverlay(jwac, o);
            //remember the unpacked overlays for later so we can delete the tmp files
            _unpackedOverlayResources.add(unpacked);
            //add in the selectively unpacked overlay in the correct order to the webapps resource base
            resourceBaseCollection.add(unpacked);
            LOG.debug("Adding " + unpacked + " to resource base list");
        }
        if (!resourceBaseCollection.contains(_originalResourceBase) && _originalResourceBase.exists()) {
            if (jwac.getBaseAppFirst()) {
                LOG.debug("Adding virtual project first in resource base list");
                resourceBaseCollection.add(0, _originalResourceBase);
            } else {
                LOG.debug("Adding virtual project last in resource base list");
                resourceBaseCollection.add(_originalResourceBase);
            }
        }
        jwac.setBaseResource(new ResourceCollection(resourceBaseCollection.toArray(new Resource[resourceBaseCollection.size()])));
    }
    jwac.setAttribute(RESOURCE_BASES_POST_OVERLAY, jwac.getBaseResource());
}
Also used : Resource(org.eclipse.jetty.util.resource.Resource) ArrayList(java.util.ArrayList) ResourceCollection(org.eclipse.jetty.util.resource.ResourceCollection)

Example 5 with ResourceCollection

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

the class OSGiWebInfConfiguration method configure.

/* ------------------------------------------------------------ */
/** 
     * Allow fragments to supply some resources that are added to the baseResource of the webapp.
     * 
     * The resources can be either prepended or appended to the baseResource.
     * 
     * @see org.eclipse.jetty.webapp.WebInfConfiguration#configure(org.eclipse.jetty.webapp.WebAppContext)
     */
@Override
public void configure(WebAppContext context) throws Exception {
    TreeMap<String, Resource> prependedResourcesPath = new TreeMap<String, Resource>();
    TreeMap<String, Resource> appendedResourcesPath = new TreeMap<String, Resource>();
    Bundle bundle = (Bundle) context.getAttribute(OSGiWebappConstants.JETTY_OSGI_BUNDLE);
    if (bundle != null) {
        Set<Bundle> fragments = (Set<Bundle>) context.getAttribute(FRAGMENT_AND_REQUIRED_BUNDLES);
        if (fragments != null && !fragments.isEmpty()) {
            // looked up.
            for (Bundle frag : fragments) {
                String path = Util.getManifestHeaderValue(OSGiWebappConstants.JETTY_WAR_FRAGMENT_FOLDER_PATH, OSGiWebappConstants.JETTY_WAR_FRAGMENT_RESOURCE_PATH, frag.getHeaders());
                convertFragmentPathToResource(path, frag, appendedResourcesPath);
                path = Util.getManifestHeaderValue(OSGiWebappConstants.JETTY_WAR_PATCH_FRAGMENT_FOLDER_PATH, OSGiWebappConstants.JETTY_WAR_PREPEND_FRAGMENT_RESOURCE_PATH, frag.getHeaders());
                convertFragmentPathToResource(path, frag, prependedResourcesPath);
            }
            if (!appendedResourcesPath.isEmpty()) {
                LinkedHashSet<Resource> resources = new LinkedHashSet<Resource>();
                //Add in any existing setting of extra resource dirs
                Set<Resource> resourceDirs = (Set<Resource>) context.getAttribute(WebInfConfiguration.RESOURCE_DIRS);
                if (resourceDirs != null && !resourceDirs.isEmpty())
                    resources.addAll(resourceDirs);
                //Then append the values from JETTY_WAR_FRAGMENT_FOLDER_PATH
                resources.addAll(appendedResourcesPath.values());
                context.setAttribute(WebInfConfiguration.RESOURCE_DIRS, resources);
            }
        }
    }
    super.configure(context);
    // place the prepended resources at the beginning of the contexts's resource base
    if (!prependedResourcesPath.isEmpty()) {
        Resource[] resources = new Resource[1 + prependedResourcesPath.size()];
        System.arraycopy(prependedResourcesPath.values().toArray(new Resource[prependedResourcesPath.size()]), 0, resources, 0, prependedResourcesPath.size());
        resources[resources.length - 1] = context.getBaseResource();
        context.setBaseResource(new ResourceCollection(resources));
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Bundle(org.osgi.framework.Bundle) Resource(org.eclipse.jetty.util.resource.Resource) TreeMap(java.util.TreeMap) ResourceCollection(org.eclipse.jetty.util.resource.ResourceCollection)

Aggregations

ResourceCollection (org.eclipse.jetty.util.resource.ResourceCollection)30 Resource (org.eclipse.jetty.util.resource.Resource)22 File (java.io.File)13 ArrayList (java.util.ArrayList)8 URL (java.net.URL)7 IOException (java.io.IOException)6 Test (org.junit.Test)6 JarResource (org.eclipse.jetty.util.resource.JarResource)5 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)4 MalformedURLException (java.net.MalformedURLException)3 MimeTypes (org.eclipse.jetty.http.MimeTypes)3 Server (org.eclipse.jetty.server.Server)3 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)3 URLClassLoader (java.net.URLClassLoader)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 LinkedList (java.util.LinkedList)2 Set (java.util.Set)2 TreeMap (java.util.TreeMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2