Search in sources :

Example 76 with Resource

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

the class MetaData method getJarForFragment.

public Resource getJarForFragment(String name) {
    FragmentDescriptor f = getFragment(name);
    if (f == null)
        return null;
    Resource jar = null;
    for (Resource r : _webFragmentResourceMap.keySet()) {
        if (_webFragmentResourceMap.get(r).equals(f))
            jar = r;
    }
    return jar;
}
Also used : Resource(org.eclipse.jetty.util.resource.Resource) EmptyResource(org.eclipse.jetty.util.resource.EmptyResource)

Example 77 with Resource

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

the class MetaInfConfiguration method scanForResources.

/**
     * Scan for META-INF/resources dir in the given jar.
     * 
     * @param context the context for the scan
     * @param target the target resource to scan for
     * @param cache the resource cache
     * @throws Exception if unable to scan for resources
     */
public void scanForResources(WebAppContext context, Resource target, ConcurrentHashMap<Resource, Resource> cache) throws Exception {
    Resource resourcesDir = null;
    if (cache != null && cache.containsKey(target)) {
        resourcesDir = cache.get(target);
        if (resourcesDir == EmptyResource.INSTANCE) {
            if (LOG.isDebugEnabled())
                LOG.debug(target + " cached as containing no META-INF/resources");
            return;
        } else if (LOG.isDebugEnabled())
            LOG.debug(target + " META-INF/resources found in cache ");
    } else {
        //not using caches or not in the cache so check for the resources dir
        if (LOG.isDebugEnabled())
            LOG.debug(target + " META-INF/resources checked");
        if (target.isDirectory()) {
            //TODO think  how to handle an unpacked jar file (eg for osgi)
            resourcesDir = target.addPath("/META-INF/resources");
        } else {
            //Resource represents a packed jar
            URI uri = target.getURI();
            resourcesDir = Resource.newResource(uriJarPrefix(uri, "!/META-INF/resources"));
        }
        if (!resourcesDir.exists() || !resourcesDir.isDirectory()) {
            resourcesDir.close();
            resourcesDir = EmptyResource.INSTANCE;
        }
        if (cache != null) {
            Resource old = cache.putIfAbsent(target, resourcesDir);
            if (old != null)
                resourcesDir = old;
            else if (LOG.isDebugEnabled())
                LOG.debug(target + " META-INF/resources cache updated");
        }
        if (resourcesDir == EmptyResource.INSTANCE) {
            return;
        }
    }
    //add it to the meta inf resources for this context
    Set<Resource> dirs = (Set<Resource>) context.getAttribute(METAINF_RESOURCES);
    if (dirs == null) {
        dirs = new HashSet<Resource>();
        context.setAttribute(METAINF_RESOURCES, dirs);
    }
    if (LOG.isDebugEnabled())
        LOG.debug(resourcesDir + " added to context");
    dirs.add(resourcesDir);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) Resource(org.eclipse.jetty.util.resource.Resource) EmptyResource(org.eclipse.jetty.util.resource.EmptyResource) URI(java.net.URI)

Example 78 with Resource

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

the class MetaInfConfiguration method scanForFragment.

/**
     * Scan for META-INF/web-fragment.xml file in the given jar.
     * 
     * @param context the context for the scan
     * @param jar the jar resource to scan for fragements in
     * @param cache the resource cache
     * @throws Exception if unable to scan for fragments
     */
public void scanForFragment(WebAppContext context, Resource jar, ConcurrentHashMap<Resource, Resource> cache) throws Exception {
    Resource webFrag = null;
    if (cache != null && cache.containsKey(jar)) {
        webFrag = cache.get(jar);
        if (webFrag == EmptyResource.INSTANCE) {
            if (LOG.isDebugEnabled())
                LOG.debug(jar + " cached as containing no META-INF/web-fragment.xml");
            return;
        } else if (LOG.isDebugEnabled())
            LOG.debug(jar + " META-INF/web-fragment.xml found in cache ");
    } else {
        //not using caches or not in the cache so check for the web-fragment.xml
        if (LOG.isDebugEnabled())
            LOG.debug(jar + " META-INF/web-fragment.xml checked");
        if (jar.isDirectory()) {
            //TODO   ????
            webFrag = jar.addPath("/META-INF/web-fragment.xml");
        } else {
            URI uri = jar.getURI();
            webFrag = Resource.newResource(uriJarPrefix(uri, "!/META-INF/web-fragment.xml"));
        }
        if (!webFrag.exists() || webFrag.isDirectory()) {
            webFrag.close();
            webFrag = EmptyResource.INSTANCE;
        }
        if (cache != null) {
            //web-fragment.xml doesn't exist: put token in cache to signal we've seen the jar               
            Resource old = cache.putIfAbsent(jar, webFrag);
            if (old != null)
                webFrag = old;
            else if (LOG.isDebugEnabled())
                LOG.debug(jar + " META-INF/web-fragment.xml cache updated");
        }
        if (webFrag == EmptyResource.INSTANCE)
            return;
    }
    Map<Resource, Resource> fragments = (Map<Resource, Resource>) context.getAttribute(METAINF_FRAGMENTS);
    if (fragments == null) {
        fragments = new HashMap<Resource, Resource>();
        context.setAttribute(METAINF_FRAGMENTS, fragments);
    }
    fragments.put(jar, webFrag);
    if (LOG.isDebugEnabled())
        LOG.debug(webFrag + " added to context");
}
Also used : Resource(org.eclipse.jetty.util.resource.Resource) EmptyResource(org.eclipse.jetty.util.resource.EmptyResource) URI(java.net.URI) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Map(java.util.Map)

Example 79 with Resource

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

the class WebInfConfiguration method findExtraClasspathJars.

/**
     * Get jars from WebAppContext.getExtraClasspath as resources
     * 
     * @param context the context to find extra classpath jars in
     * @return the list of Resources with the extra classpath, or null if not found
     * @throws Exception if unable to find the extra classpath jars
     */
protected List<Resource> findExtraClasspathJars(WebAppContext context) throws Exception {
    if (context == null || context.getExtraClasspath() == null)
        return null;
    List<Resource> jarResources = new ArrayList<Resource>();
    StringTokenizer tokenizer = new StringTokenizer(context.getExtraClasspath(), ",;");
    while (tokenizer.hasMoreTokens()) {
        Resource resource = context.newResource(tokenizer.nextToken().trim());
        String fnlc = resource.getName().toLowerCase(Locale.ENGLISH);
        int dot = fnlc.lastIndexOf('.');
        String extension = (dot < 0 ? null : fnlc.substring(dot));
        if (extension != null && (extension.equals(".jar") || extension.equals(".zip"))) {
            jarResources.add(resource);
        }
    }
    return jarResources;
}
Also used : StringTokenizer(java.util.StringTokenizer) ArrayList(java.util.ArrayList) JarResource(org.eclipse.jetty.util.resource.JarResource) Resource(org.eclipse.jetty.util.resource.Resource)

Example 80 with Resource

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

the class WebInfConfiguration method findClassDirs.

protected List<Resource> findClassDirs(WebAppContext context) throws Exception {
    if (context == null)
        return null;
    List<Resource> classDirs = new ArrayList<Resource>();
    Resource webInfClasses = findWebInfClassesDir(context);
    if (webInfClasses != null)
        classDirs.add(webInfClasses);
    List<Resource> extraClassDirs = findExtraClasspathDirs(context);
    if (extraClassDirs != null)
        classDirs.addAll(extraClassDirs);
    return classDirs;
}
Also used : ArrayList(java.util.ArrayList) JarResource(org.eclipse.jetty.util.resource.JarResource) 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