Search in sources :

Example 6 with WebResource

use of org.apache.catalina.WebResource in project tomcat by apache.

the class WebappClassLoaderBase method findResources.

/**
     * Return an enumeration of <code>URLs</code> representing all of the
     * resources with the given name.  If no resources with this name are
     * found, return an empty enumeration.
     *
     * @param name Name of the resources to be found
     *
     * @exception IOException if an input/output error occurs
     */
@Override
public Enumeration<URL> findResources(String name) throws IOException {
    if (log.isDebugEnabled())
        log.debug("    findResources(" + name + ")");
    checkStateForResourceLoading(name);
    LinkedHashSet<URL> result = new LinkedHashSet<>();
    String path = nameToPath(name);
    WebResource[] webResources = resources.getClassLoaderResources(path);
    for (WebResource webResource : webResources) {
        if (webResource.exists()) {
            result.add(webResource.getURL());
        }
    }
    // Adding the results of a call to the superclass
    if (hasExternalRepositories) {
        Enumeration<URL> otherResourcePaths = super.findResources(name);
        while (otherResourcePaths.hasMoreElements()) {
            result.add(otherResourcePaths.nextElement());
        }
    }
    return Collections.enumeration(result);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) WebResource(org.apache.catalina.WebResource) URL(java.net.URL)

Example 7 with WebResource

use of org.apache.catalina.WebResource in project tomcat by apache.

the class WebappClassLoaderBase method modified.

/**
     * Have one or more classes or resources been modified so that a reload
     * is appropriate?
     * @return <code>true</code> if there's been a modification
     */
public boolean modified() {
    if (log.isDebugEnabled())
        log.debug("modified()");
    for (Entry<String, ResourceEntry> entry : resourceEntries.entrySet()) {
        long cachedLastModified = entry.getValue().lastModified;
        long lastModified = resources.getClassLoaderResource(entry.getKey()).getLastModified();
        if (lastModified != cachedLastModified) {
            if (log.isDebugEnabled())
                log.debug(sm.getString("webappClassLoader.resourceModified", entry.getKey(), new Date(cachedLastModified), new Date(lastModified)));
            return true;
        }
    }
    // Check if JARs have been added or removed
    WebResource[] jars = resources.listResources("/WEB-INF/lib");
    // Filter out non-JAR resources
    int jarCount = 0;
    for (WebResource jar : jars) {
        if (jar.getName().endsWith(".jar") && jar.isFile() && jar.canRead()) {
            jarCount++;
            Long recordedLastModified = jarModificationTimes.get(jar.getName());
            if (recordedLastModified == null) {
                // Jar has been added
                log.info(sm.getString("webappClassLoader.jarsAdded", resources.getContext().getName()));
                return true;
            }
            if (recordedLastModified.longValue() != jar.getLastModified()) {
                // Jar has been changed
                log.info(sm.getString("webappClassLoader.jarsModified", resources.getContext().getName()));
                return true;
            }
        }
    }
    if (jarCount < jarModificationTimes.size()) {
        log.info(sm.getString("webappClassLoader.jarsRemoved", resources.getContext().getName()));
        return true;
    }
    // No classes have been modified
    return false;
}
Also used : WebResource(org.apache.catalina.WebResource) Date(java.util.Date)

Example 8 with WebResource

use of org.apache.catalina.WebResource in project tomcat by apache.

the class WebappClassLoaderBase method getResourceAsStream.

/**
     * Find the resource with the given name, and return an input stream
     * that can be used for reading it.  The search order is as described
     * for <code>getResource()</code>, after checking to see if the resource
     * data has been previously cached.  If the resource cannot be found,
     * return <code>null</code>.
     *
     * @param name Name of the resource to return an input stream for
     */
@Override
public InputStream getResourceAsStream(String name) {
    if (log.isDebugEnabled())
        log.debug("getResourceAsStream(" + name + ")");
    checkStateForResourceLoading(name);
    InputStream stream = null;
    boolean delegateFirst = delegate || filter(name, false);
    // (1) Delegate to parent if requested
    if (delegateFirst) {
        if (log.isDebugEnabled())
            log.debug("  Delegating to parent classloader " + parent);
        stream = parent.getResourceAsStream(name);
        if (stream != null) {
            if (log.isDebugEnabled())
                log.debug("  --> Returning stream from parent");
            return stream;
        }
    }
    // (2) Search local repositories
    if (log.isDebugEnabled())
        log.debug("  Searching local repositories");
    String path = nameToPath(name);
    WebResource resource = resources.getClassLoaderResource(path);
    if (resource.exists()) {
        stream = resource.getInputStream();
        trackLastModified(path, resource);
    }
    try {
        if (hasExternalRepositories && stream == null) {
            URL url = super.findResource(name);
            if (url != null) {
                stream = url.openStream();
            }
        }
    } catch (IOException e) {
    // Ignore
    }
    if (stream != null) {
        if (log.isDebugEnabled())
            log.debug("  --> Returning stream from local");
        return stream;
    }
    // (3) Delegate to parent unconditionally
    if (!delegateFirst) {
        if (log.isDebugEnabled())
            log.debug("  Delegating to parent classloader unconditionally " + parent);
        stream = parent.getResourceAsStream(name);
        if (stream != null) {
            if (log.isDebugEnabled())
                log.debug("  --> Returning stream from parent");
            return stream;
        }
    }
    // (4) Resource was not found
    if (log.isDebugEnabled())
        log.debug("  --> Resource not found, returning null");
    return null;
}
Also used : InputStream(java.io.InputStream) WebResource(org.apache.catalina.WebResource) IOException(java.io.IOException) URL(java.net.URL)

Example 9 with WebResource

use of org.apache.catalina.WebResource in project tomcat by apache.

the class WebappClassLoaderBase method findResource.

/**
     * Find the specified resource in our local repository, and return a
     * <code>URL</code> referring to it, or <code>null</code> if this resource
     * cannot be found.
     *
     * @param name Name of the resource to be found
     */
@Override
public URL findResource(final String name) {
    if (log.isDebugEnabled())
        log.debug("    findResource(" + name + ")");
    checkStateForResourceLoading(name);
    URL url = null;
    String path = nameToPath(name);
    WebResource resource = resources.getClassLoaderResource(path);
    if (resource.exists()) {
        url = resource.getURL();
        trackLastModified(path, resource);
    }
    if ((url == null) && hasExternalRepositories) {
        url = super.findResource(name);
    }
    if (log.isDebugEnabled()) {
        if (url != null)
            log.debug("    --> Returning '" + url.toString() + "'");
        else
            log.debug("    --> Resource not found, returning null");
    }
    return url;
}
Also used : WebResource(org.apache.catalina.WebResource) URL(java.net.URL)

Example 10 with WebResource

use of org.apache.catalina.WebResource in project tomcat by apache.

the class WebappClassLoaderBase method findClassInternal.

/**
     * Find specified class in local repositories.
     *
     * @param name The binary name of the class to be loaded
     *
     * @return the loaded class, or null if the class isn't found
     */
protected Class<?> findClassInternal(String name) {
    checkStateForResourceLoading(name);
    if (name == null) {
        return null;
    }
    String path = binaryNameToPath(name, true);
    ResourceEntry entry = resourceEntries.get(path);
    WebResource resource = null;
    if (entry == null) {
        resource = resources.getClassLoaderResource(path);
        if (!resource.exists()) {
            return null;
        }
        entry = new ResourceEntry();
        entry.lastModified = resource.getLastModified();
        // Add the entry in the local resource repository
        synchronized (resourceEntries) {
            // Ensures that all the threads which may be in a race to load
            // a particular class all end up with the same ResourceEntry
            // instance
            ResourceEntry entry2 = resourceEntries.get(path);
            if (entry2 == null) {
                resourceEntries.put(path, entry);
            } else {
                entry = entry2;
            }
        }
    }
    Class<?> clazz = entry.loadedClass;
    if (clazz != null)
        return clazz;
    synchronized (getClassLoadingLock(name)) {
        clazz = entry.loadedClass;
        if (clazz != null)
            return clazz;
        if (resource == null) {
            resource = resources.getClassLoaderResource(path);
        }
        if (!resource.exists()) {
            return null;
        }
        byte[] binaryContent = resource.getContent();
        Manifest manifest = resource.getManifest();
        URL codeBase = resource.getCodeBase();
        Certificate[] certificates = resource.getCertificates();
        if (transformers.size() > 0) {
            // If the resource is a class just being loaded, decorate it
            // with any attached transformers
            String className = name.endsWith(CLASS_FILE_SUFFIX) ? name.substring(0, name.length() - CLASS_FILE_SUFFIX.length()) : name;
            String internalName = className.replace(".", "/");
            for (ClassFileTransformer transformer : this.transformers) {
                try {
                    byte[] transformed = transformer.transform(this, internalName, null, null, binaryContent);
                    if (transformed != null) {
                        binaryContent = transformed;
                    }
                } catch (IllegalClassFormatException e) {
                    log.error(sm.getString("webappClassLoader.transformError", name), e);
                    return null;
                }
            }
        }
        // Looking up the package
        String packageName = null;
        int pos = name.lastIndexOf('.');
        if (pos != -1)
            packageName = name.substring(0, pos);
        Package pkg = null;
        if (packageName != null) {
            pkg = getPackage(packageName);
            // Define the package (if null)
            if (pkg == null) {
                try {
                    if (manifest == null) {
                        definePackage(packageName, null, null, null, null, null, null, null);
                    } else {
                        definePackage(packageName, manifest, codeBase);
                    }
                } catch (IllegalArgumentException e) {
                // Ignore: normal error due to dual definition of package
                }
                pkg = getPackage(packageName);
            }
        }
        if (securityManager != null) {
            // Checking sealing
            if (pkg != null) {
                boolean sealCheck = true;
                if (pkg.isSealed()) {
                    sealCheck = pkg.isSealed(codeBase);
                } else {
                    sealCheck = (manifest == null) || !isPackageSealed(packageName, manifest);
                }
                if (!sealCheck)
                    throw new SecurityException("Sealing violation loading " + name + " : Package " + packageName + " is sealed.");
            }
        }
        try {
            clazz = defineClass(name, binaryContent, 0, binaryContent.length, new CodeSource(codeBase, certificates));
        } catch (UnsupportedClassVersionError ucve) {
            throw new UnsupportedClassVersionError(ucve.getLocalizedMessage() + " " + sm.getString("webappClassLoader.wrongVersion", name));
        }
        entry.loadedClass = clazz;
    }
    return clazz;
}
Also used : ClassFileTransformer(java.lang.instrument.ClassFileTransformer) WebResource(org.apache.catalina.WebResource) Manifest(java.util.jar.Manifest) CodeSource(java.security.CodeSource) URL(java.net.URL) IllegalClassFormatException(java.lang.instrument.IllegalClassFormatException) Certificate(java.security.cert.Certificate)

Aggregations

WebResource (org.apache.catalina.WebResource)47 Test (org.junit.Test)14 InputStream (java.io.InputStream)10 File (java.io.File)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8 IOException (java.io.IOException)7 URL (java.net.URL)7 BufferedInputStream (java.io.BufferedInputStream)5 FileInputStream (java.io.FileInputStream)5 InputSource (org.xml.sax.InputSource)5 PrintWriter (java.io.PrintWriter)3 RandomAccessFile (java.io.RandomAccessFile)3 Manifest (java.util.jar.Manifest)3 DocumentBuilder (javax.xml.parsers.DocumentBuilder)3 WebResourceSet (org.apache.catalina.WebResourceSet)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2 StringWriter (java.io.StringWriter)2 Hashtable (java.util.Hashtable)2 LinkedHashSet (java.util.LinkedHashSet)2