Search in sources :

Example 71 with Resource

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

the class WebAppClassLoader method addJars.

/* ------------------------------------------------------------ */
/** Add elements to the class path for the context from the jar and zip files found
     *  in the specified resource.
     * @param lib the resource that contains the jar and/or zip files.
     */
public void addJars(Resource lib) {
    if (lib.exists() && lib.isDirectory()) {
        String[] files = lib.list();
        for (int f = 0; files != null && f < files.length; f++) {
            try {
                Resource fn = lib.addPath(files[f]);
                if (LOG.isDebugEnabled())
                    LOG.debug("addJar - {}", fn);
                String fnlc = fn.getName().toLowerCase(Locale.ENGLISH);
                // don't check if this is a directory, see Bug 353165
                if (isFileSupported(fnlc)) {
                    String jar = fn.toString();
                    jar = StringUtil.replace(jar, ",", "%2C");
                    jar = StringUtil.replace(jar, ";", "%3B");
                    addClassPath(jar);
                }
            } catch (Exception ex) {
                LOG.warn(Log.EXCEPTION, ex);
            }
        }
    }
}
Also used : Resource(org.eclipse.jetty.util.resource.Resource) IllegalClassFormatException(java.lang.instrument.IllegalClassFormatException) IOException(java.io.IOException)

Example 72 with Resource

use of org.eclipse.jetty.util.resource.Resource 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 73 with Resource

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

the class WebAppContext method getResource.

/* ------------------------------------------------------------ */
@Override
public Resource getResource(String uriInContext) throws MalformedURLException {
    if (uriInContext == null || !uriInContext.startsWith(URIUtil.SLASH))
        throw new MalformedURLException(uriInContext);
    IOException ioe = null;
    Resource resource = null;
    int loop = 0;
    while (uriInContext != null && loop++ < 100) {
        try {
            resource = super.getResource(uriInContext);
            if (resource != null && resource.exists())
                return resource;
            uriInContext = getResourceAlias(uriInContext);
        } catch (IOException e) {
            LOG.ignore(e);
            if (ioe == null)
                ioe = e;
        }
    }
    if (ioe != null && ioe instanceof MalformedURLException)
        throw (MalformedURLException) ioe;
    return resource;
}
Also used : MalformedURLException(java.net.MalformedURLException) Resource(org.eclipse.jetty.util.resource.Resource) IOException(java.io.IOException)

Example 74 with Resource

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

the class SslContextFactoryTest method testResourceTsResourceKs.

@Test
public void testResourceTsResourceKs() throws Exception {
    Resource keystoreResource = Resource.newSystemResource("keystore");
    Resource truststoreResource = Resource.newSystemResource("keystore");
    cf.setKeyStoreResource(keystoreResource);
    cf.setTrustStoreResource(truststoreResource);
    cf.setKeyStorePassword("storepwd");
    cf.setKeyManagerPassword("keypwd");
    cf.setTrustStorePassword("storepwd");
    cf.start();
    assertTrue(cf.getSslContext() != null);
}
Also used : Resource(org.eclipse.jetty.util.resource.Resource) Test(org.junit.Test)

Example 75 with Resource

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

the class MetaData method resolve.

/**
     * Resolve all servlet/filter/listener metadata from all sources: descriptors and annotations.
     * 
     * @param context the context to resolve servlets / filters / listeners metadata from 
     * @throws Exception if unable to resolve metadata
     */
public void resolve(WebAppContext context) throws Exception {
    LOG.debug("metadata resolve {}", context);
    //Ensure origins is fresh
    _origins.clear();
    // Set the ordered lib attribute
    List<Resource> orderedWebInfJars = null;
    if (getOrdering() != null) {
        orderedWebInfJars = getOrderedWebInfJars();
        List<String> orderedLibs = new ArrayList<String>();
        for (Resource webInfJar : orderedWebInfJars) {
            //get just the name of the jar file
            String fullname = webInfJar.getName();
            int i = fullname.indexOf(".jar");
            int j = fullname.lastIndexOf("/", i);
            orderedLibs.add(fullname.substring(j + 1, i + 4));
        }
        context.setAttribute(ServletContext.ORDERED_LIBS, orderedLibs);
    }
    // set the webxml version
    if (_webXmlRoot != null) {
        context.getServletContext().setEffectiveMajorVersion(_webXmlRoot.getMajorVersion());
        context.getServletContext().setEffectiveMinorVersion(_webXmlRoot.getMinorVersion());
    }
    for (DescriptorProcessor p : _descriptorProcessors) {
        p.process(context, getWebDefault());
        p.process(context, getWebXml());
        for (WebDescriptor wd : getOverrideWebs()) {
            LOG.debug("process {} {}", context, wd);
            p.process(context, wd);
        }
    }
    //get an apply the annotations that are not associated with a fragment (and hence for
    //which no ordering applies
    List<DiscoveredAnnotation> nonFragAnnotations = _annotations.get(NON_FRAG_RESOURCE);
    if (nonFragAnnotations != null) {
        for (DiscoveredAnnotation a : nonFragAnnotations) {
            LOG.debug("apply {}", a);
            a.apply();
        }
    }
    //apply the annotations that are associated with a fragment, according to the 
    //established ordering
    List<Resource> resources = null;
    if (getOrdering() != null)
        resources = orderedWebInfJars;
    else
        resources = getWebInfJars();
    for (Resource r : resources) {
        FragmentDescriptor fd = _webFragmentResourceMap.get(r);
        if (fd != null) {
            for (DescriptorProcessor p : _descriptorProcessors) {
                LOG.debug("process {} {}", context, fd);
                p.process(context, fd);
            }
        }
        List<DiscoveredAnnotation> fragAnnotations = _annotations.get(r);
        if (fragAnnotations != null) {
            for (DiscoveredAnnotation a : fragAnnotations) {
                LOG.debug("apply {}", a);
                a.apply();
            }
        }
    }
}
Also used : Resource(org.eclipse.jetty.util.resource.Resource) EmptyResource(org.eclipse.jetty.util.resource.EmptyResource) ArrayList(java.util.ArrayList)

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