Search in sources :

Example 1 with ResourceEntry

use of org.glassfish.api.deployment.ResourceEntry in project Payara by payara.

the class WebappClassLoader method addGeneratedResourceEntry.

@Override
public Class addGeneratedResourceEntry(String mainClassName, String generatedClassName, byte[] generatedBinaryContent, ProtectionDomain protectionDomain) {
    Class generatedClass;
    if (this.resourceEntries.containsKey(mainClassName)) {
        generatedClass = defineClass(generatedClassName, generatedBinaryContent, 0, generatedBinaryContent.length, protectionDomain);
        GeneratedResourceEntry generatedResourceEntry = new GeneratedResourceEntry();
        generatedResourceEntry.binaryContent = generatedBinaryContent;
        generatedResourceEntry.loadedClass = generatedClass;
        generatedResourceEntry.protectionDomain = protectionDomain;
        ResourceEntry entry = this.resourceEntries.get(mainClassName);
        entry.generatedResources.put(generatedClassName, generatedResourceEntry);
    } else {
        generatedClass = defineClass(generatedClassName, generatedBinaryContent, 0, generatedBinaryContent.length, protectionDomain);
    }
    return generatedClass;
}
Also used : GeneratedResourceEntry(org.glassfish.api.deployment.GeneratedResourceEntry) GeneratedResourceEntry(org.glassfish.api.deployment.GeneratedResourceEntry) ResourceEntry(org.glassfish.api.deployment.ResourceEntry)

Example 2 with ResourceEntry

use of org.glassfish.api.deployment.ResourceEntry in project Payara by payara.

the class WebappClassLoader method findResourceInternalFromRepositories.

/**
 * Attempts to load the requested resource from this classloader's
 * internal repositories.
 *
 * @return The requested resource, or null if not found
 */
private ResourceEntry findResourceInternalFromRepositories(String name, String path) {
    if (repositories == null) {
        return null;
    }
    ResourceEntry entry = null;
    int contentLength = -1;
    InputStream binaryStream = null;
    int repositoriesLength = repositories.length;
    Resource resource = null;
    for (int i = 0; (entry == null) && (i < repositoriesLength); i++) {
        try {
            String fullPath = repositories[i] + path;
            Object lookupResult = resources.lookup(fullPath);
            if (lookupResult instanceof Resource) {
                resource = (Resource) lookupResult;
            }
            // found
            if (securityManager != null) {
                PrivilegedAction<ResourceEntry> dp = new PrivilegedFindResource(files[i], path);
                entry = AccessController.doPrivileged(dp);
            } else {
                entry = findResourceInternal(files[i], path);
            }
            ResourceAttributes attributes = (ResourceAttributes) resources.getAttributes(fullPath);
            contentLength = (int) attributes.getContentLength();
            entry.lastModified = attributes.getLastModified();
            if (resource != null) {
                try {
                    binaryStream = resource.streamContent();
                } catch (IOException e) {
                    return null;
                }
                // Note: Only syncing on a 'constant' object is needed
                synchronized (ALL_PERMISSION) {
                    int j;
                    long[] result2 = new long[lastModifiedDates.length + 1];
                    for (j = 0; j < lastModifiedDates.length; j++) {
                        result2[j] = lastModifiedDates[j];
                    }
                    result2[lastModifiedDates.length] = entry.lastModified;
                    lastModifiedDates = result2;
                    String[] result = new String[paths.length + 1];
                    for (j = 0; j < paths.length; j++) {
                        result[j] = paths[j];
                    }
                    result[paths.length] = fullPath;
                    paths = result;
                }
            }
        } catch (NamingException e) {
        }
    }
    if (entry != null) {
        readEntryData(entry, name, binaryStream, contentLength, null);
    }
    return entry;
}
Also used : Resource(org.apache.naming.resources.Resource) ResourceAttributes(org.apache.naming.resources.ResourceAttributes) GeneratedResourceEntry(org.glassfish.api.deployment.GeneratedResourceEntry) ResourceEntry(org.glassfish.api.deployment.ResourceEntry) NamingException(javax.naming.NamingException)

Example 3 with ResourceEntry

use of org.glassfish.api.deployment.ResourceEntry in project Payara by payara.

the class WebappClassLoader method findResourceInternal.

/**
 * Find specified resource in local repositories. This block
 * will execute under an AccessControl.doPrivilege block.
 *
 * @return the loaded resource, or null if the resource isn't found
 */
private ResourceEntry findResourceInternal(File file, String path) {
    ResourceEntry entry = new ResourceEntry();
    try {
        entry.source = getURI(new File(file, path));
        entry.codeBase = getURL(new File(file, path));
    } catch (MalformedURLException e) {
        return null;
    }
    return entry;
}
Also used : MalformedURLException(java.net.MalformedURLException) GeneratedResourceEntry(org.glassfish.api.deployment.GeneratedResourceEntry) ResourceEntry(org.glassfish.api.deployment.ResourceEntry) JarFile(java.util.jar.JarFile) ZipFile(java.util.zip.ZipFile)

Example 4 with ResourceEntry

use of org.glassfish.api.deployment.ResourceEntry in project Payara by payara.

the class WebappClassLoader 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(String name) {
    if (logger.isLoggable(Level.FINER)) {
        logger.log(Level.FINER, "    findResource({0})", name);
    }
    URL url = null;
    if (".".equals(name)) {
        name = "";
    }
    ResourceEntry entry = resourceEntries.get(name);
    if (entry == null) {
        entry = findResourceInternal(name, name);
    }
    if (entry != null) {
        url = entry.source;
    }
    if ((url == null) && hasExternalRepositories) {
        url = super.findResource(name);
    }
    if (logger.isLoggable(Level.FINER)) {
        if (url != null) {
            logger.log(Level.FINER, "    --> Returning ''{0}''", url.toString());
        } else {
            logger.log(Level.FINER, "    --> Resource not found, returning null");
        }
    }
    return NonCachedJarStreamHandler.forceNonCachedJarURL(url);
}
Also used : GeneratedResourceEntry(org.glassfish.api.deployment.GeneratedResourceEntry) ResourceEntry(org.glassfish.api.deployment.ResourceEntry) URL(java.net.URL)

Example 5 with ResourceEntry

use of org.glassfish.api.deployment.ResourceEntry in project Payara by payara.

the class WebappClassLoader method getResource.

/**
 * Find the resource with the given name.  A resource is some data
 * (images, audio, text, etc.) that can be accessed by class code in a
 * way that is independent of the location of the code.  The name of a
 * resource is a "/"-separated path name that identifies the resource.
 * If the resource cannot be found, return <code>null</code>.
 * <p>
 * This method searches according to the following algorithm, returning
 * as soon as it finds the appropriate URL.  If the resource cannot be
 * found, returns <code>null</code>.
 * <ul>
 * <li>If the <code>delegate</code> property is set to <code>true</code>,
 *     call the <code>getResource()</code> method of the parent class
 *     loader, if any.</li>
 * <li>Call <code>findResource()</code> to find this resource in our
 *     locally defined repositories.</li>
 * <li>Call the <code>getResource()</code> method of the parent class
 *     loader, if any.</li>
 * </ul>
 *
 * @param name Name of the resource to return a URL for
 */
@Override
public URL getResource(String name) {
    if (logger.isLoggable(Level.FINER)) {
        logger.log(Level.FINER, "getResource({0})", name);
    }
    URL url = null;
    /*
         * (1) Delegate to parent if requested, or if the requested resource
         * belongs to one of the packages that are part of the Java EE platform
         */
    if (isResourceDelegate(name)) {
        if (logger.isLoggable(Level.FINER)) {
            logger.log(Level.FINER, "{0}  Delegating to parent classloader ", parent);
        }
        ClassLoader loader = parent;
        if (loader == null) {
            loader = system;
        }
        url = loader.getResource(name);
        if (url != null) {
            if (logger.isLoggable(Level.FINER)) {
                logger.log(Level.FINER, "  --> Returning ''{0}''", url.toString());
            }
            return (url);
        }
    }
    // (2) Search local repositories
    url = findResource(name);
    if (url != null) {
        if (antiJARLocking) {
            // Locating the repository for special handling in the case
            // of a JAR
            ResourceEntry entry = resourceEntries.get(name);
            try {
                String repository = entry.codeBase.toString();
                if ((repository.endsWith(".jar")) && !(name.endsWith(".class")) && !(name.endsWith(".jar"))) {
                    // Copy binary content to the work directory if not present
                    File resourceFile = new File(loaderDir, name);
                    url = resourceFile.toURI().toURL();
                }
            } catch (Exception e) {
                logger.log(Level.FINEST, null, e);
            }
        }
        if (logger.isLoggable(Level.FINER)) {
            logger.log(Level.FINER, "  --> Returning ''{0}''", url.toString());
        }
        return (url);
    }
    // (3) Delegate to parent unconditionally if not already attempted
    if (!delegate) {
        ClassLoader loader = parent;
        if (loader == null) {
            loader = system;
        }
        url = loader.getResource(name);
        if (url != null) {
            if (logger.isLoggable(Level.FINER)) {
                logger.log(Level.FINER, "  --> Returning ''{0}''", url.toString());
            }
            return (url);
        }
    }
    // (4) Resource was not found
    if (logger.isLoggable(Level.FINER)) {
        logger.log(Level.FINER, "  --> Resource not found, returning null");
    }
    return null;
}
Also used : GeneratedResourceEntry(org.glassfish.api.deployment.GeneratedResourceEntry) ResourceEntry(org.glassfish.api.deployment.ResourceEntry) URLClassLoader(java.net.URLClassLoader) InstrumentableClassLoader(org.glassfish.api.deployment.InstrumentableClassLoader) ResourceClassLoader(org.glassfish.api.deployment.ResourceClassLoader) JarFile(java.util.jar.JarFile) ZipFile(java.util.zip.ZipFile) URL(java.net.URL) NamingException(javax.naming.NamingException) IllegalClassFormatException(java.lang.instrument.IllegalClassFormatException) InvocationTargetException(java.lang.reflect.InvocationTargetException) MalformedURLException(java.net.MalformedURLException)

Aggregations

ResourceEntry (org.glassfish.api.deployment.ResourceEntry)11 GeneratedResourceEntry (org.glassfish.api.deployment.GeneratedResourceEntry)10 MalformedURLException (java.net.MalformedURLException)4 URL (java.net.URL)3 JarFile (java.util.jar.JarFile)3 ZipFile (java.util.zip.ZipFile)3 NamingException (javax.naming.NamingException)3 ResourceClassLoader (org.glassfish.api.deployment.ResourceClassLoader)3 IllegalClassFormatException (java.lang.instrument.IllegalClassFormatException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 URLClassLoader (java.net.URLClassLoader)2 InstrumentableClassLoader (org.glassfish.api.deployment.InstrumentableClassLoader)2 HotSwapHelper (com.sun.enterprise.glassfish.bootstrap.MainHelper.HotSwapHelper)1 File (java.io.File)1 IOException (java.io.IOException)1 Field (java.lang.reflect.Field)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1