Search in sources :

Example 1 with ClassLoaderInterfaceDelegate

use of com.opensymphony.xwork2.util.finder.ClassLoaderInterfaceDelegate in project struts by apache.

the class PackageBasedActionConfigBuilder method getClassLoaderInterface.

protected ClassLoaderInterface getClassLoaderInterface() {
    if (isReloadEnabled()) {
        return new ClassLoaderInterfaceDelegate(this.reloadingClassLoader);
    } else {
        /*
            if there is a ClassLoaderInterface in the context, use it, otherwise
            default to the default ClassLoaderInterface (a wrapper around the current
            thread classloader)
            using this, other plugins (like OSGi) can plugin their own classloader for a while
            and it will be used by Convention (it cannot be a bean, as Convention is likely to be
            called multiple times, and it needs to use the default ClassLoaderInterface during normal startup)
            */
        ClassLoaderInterface classLoaderInterface = null;
        ActionContext ctx = ActionContext.getContext();
        if (ctx != null) {
            classLoaderInterface = (ClassLoaderInterface) ctx.get(ClassLoaderInterface.CLASS_LOADER_INTERFACE);
        }
        return ObjectUtils.defaultIfNull(classLoaderInterface, new ClassLoaderInterfaceDelegate(getClassLoader()));
    }
}
Also used : ActionContext(com.opensymphony.xwork2.ActionContext)

Example 2 with ClassLoaderInterfaceDelegate

use of com.opensymphony.xwork2.util.finder.ClassLoaderInterfaceDelegate in project struts by apache.

the class DefaultResultMapBuilder method getClassLoaderInterface.

protected ClassLoaderInterface getClassLoaderInterface() {
    /*
        if there is a ClassLoaderInterface in the context, use it, otherwise
        default to the default ClassLoaderInterface (a wrapper around the current
        thread classloader)
        using this, other plugins (like OSGi) can plugin their own classloader for a while
        and it will be used by Convention (it cannot be a bean, as Convention is likely to be
        called multiple times, and it need to use the default ClassLoaderInterface during normal startup)
        */
    ClassLoaderInterface classLoaderInterface = null;
    ActionContext ctx = ActionContext.getContext();
    if (ctx != null)
        classLoaderInterface = (ClassLoaderInterface) ctx.get(ClassLoaderInterface.CLASS_LOADER_INTERFACE);
    return ObjectUtils.defaultIfNull(classLoaderInterface, new ClassLoaderInterfaceDelegate(Thread.currentThread().getContextClassLoader()));
}
Also used : ClassLoaderInterfaceDelegate(com.opensymphony.xwork2.util.finder.ClassLoaderInterfaceDelegate) ClassLoaderInterface(com.opensymphony.xwork2.util.finder.ClassLoaderInterface) ActionContext(com.opensymphony.xwork2.ActionContext)

Example 3 with ClassLoaderInterfaceDelegate

use of com.opensymphony.xwork2.util.finder.ClassLoaderInterfaceDelegate in project struts by apache.

the class PackageBasedActionConfigBuilder method buildUrlSet.

private UrlSet buildUrlSet(List<URL> resourceUrls) throws IOException {
    ClassLoaderInterface classLoaderInterface = getClassLoaderInterface();
    UrlSet urlSet = new UrlSet(resourceUrls);
    urlSet = urlSet.include(new UrlSet(classLoaderInterface, this.fileProtocols));
    // excluding the urls found by the parent class loader is desired, but fails in JBoss (all urls are removed)
    if (excludeParentClassLoader) {
        // exclude parent of classloaders
        ClassLoaderInterface parent = classLoaderInterface.getParent();
        // this happens because the parent of the reloading class loader is the web app classloader
        if (parent != null && isReloadEnabled())
            parent = parent.getParent();
        if (parent != null)
            urlSet = urlSet.exclude(parent);
        try {
            // This may fail in some sandboxes, ie GAE
            ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
            urlSet = urlSet.exclude(new ClassLoaderInterfaceDelegate(systemClassLoader.getParent()));
        } catch (SecurityException e) {
            LOG.warn("Could not get the system classloader due to security constraints, there may be improper urls left to scan");
        }
    }
    // try to find classes dirs inside war files
    urlSet = urlSet.includeClassesUrl(classLoaderInterface, new UrlSet.FileProtocolNormalizer() {

        public URL normalizeToFileProtocol(URL url) {
            return fileManager.normalizeToFileProtocol(url);
        }
    });
    urlSet = urlSet.excludeJavaExtDirs().excludeJavaEndorsedDirs().excludeUserExtensionsDir();
    try {
        urlSet = urlSet.excludeJavaHome();
    } catch (NullPointerException e) {
        // This happens in GAE since the sandbox contains no java.home directory
        LOG.warn("Could not exclude JAVA_HOME, is this a sandbox jvm?");
    }
    urlSet = urlSet.excludePaths(System.getProperty("sun.boot.class.path", ""));
    urlSet = urlSet.exclude(".*/JavaVM.framework/.*");
    if (includeJars == null) {
        urlSet = urlSet.exclude(".*?\\.jar(!/|/)?");
    } else {
        if (LOG.isDebugEnabled()) {
            LOG.debug("jar urls regexes were specified: {}", Arrays.asList(includeJars));
        }
        List<URL> rawIncludedUrls = urlSet.getUrls();
        Set<URL> includeUrls = new HashSet<>();
        boolean[] patternUsed = new boolean[includeJars.length];
        for (URL url : rawIncludedUrls) {
            if (fileProtocols.contains(url.getProtocol())) {
                // it is a jar file, make sure it matches at least a url regex
                for (int i = 0; i < includeJars.length; i++) {
                    String includeJar = includeJars[i];
                    if (Pattern.matches(includeJar, url.toExternalForm())) {
                        includeUrls.add(url);
                        patternUsed[i] = true;
                        break;
                    }
                }
            } else {
                LOG.debug("It is not a jar [{}]", url);
                includeUrls.add(url);
            }
        }
        if (LOG.isWarnEnabled()) {
            for (int i = 0; i < patternUsed.length; i++) {
                if (!patternUsed[i]) {
                    LOG.warn("The includeJars pattern [{}] did not match any jars in the classpath", includeJars[i]);
                }
            }
        }
        return new UrlSet(includeUrls);
    }
    return urlSet;
}
Also used : URL(java.net.URL) ReloadingClassLoader(com.opensymphony.xwork2.util.classloader.ReloadingClassLoader)

Example 4 with ClassLoaderInterfaceDelegate

use of com.opensymphony.xwork2.util.finder.ClassLoaderInterfaceDelegate in project struts by apache.

the class JSPLoader method getClassLoaderInterface.

private ClassLoaderInterface getClassLoaderInterface() {
    ClassLoaderInterface classLoaderInterface = null;
    ServletContext ctx = ServletActionContext.getServletContext();
    if (ctx != null)
        classLoaderInterface = (ClassLoaderInterface) ctx.getAttribute(ClassLoaderInterface.CLASS_LOADER_INTERFACE);
    return (ClassLoaderInterface) ObjectUtils.defaultIfNull(classLoaderInterface, new ClassLoaderInterfaceDelegate(JSPLoader.class.getClassLoader()));
}
Also used : ClassLoaderInterfaceDelegate(com.opensymphony.xwork2.util.finder.ClassLoaderInterfaceDelegate) ClassLoaderInterface(com.opensymphony.xwork2.util.finder.ClassLoaderInterface) ServletContext(javax.servlet.ServletContext)

Example 5 with ClassLoaderInterfaceDelegate

use of com.opensymphony.xwork2.util.finder.ClassLoaderInterfaceDelegate in project struts by apache.

the class BaseOsgiHost method findPropertiesFileInRelativePath.

/**
 * Attempt to read a properties file from the relative path of the current classloader.
 * Intended as an alternate configuration fallback for special scenarios where the default lookup
 * is not functional (such as for unit tests).
 *
 * @param fileName the filename (relative path) of the properties file.
 * @return a Properties bundle loaded from the provided fileName.
 * @throws IOException if the properties file does not exist or cannot be loaded.
 */
protected Properties findPropertiesFileInRelativePath(String fileName) throws IOException {
    if (fileName == null || fileName.toLowerCase().endsWith(".class") || fileName.toLowerCase().endsWith(".jar")) {
        throw new IllegalArgumentException("Provided file name cannot be null, nor should it be a class or jar file");
    }
    final ClassLoaderInterface classLoaderInterface = new ClassLoaderInterfaceDelegate(Thread.currentThread().getContextClassLoader());
    final URL fileUrl = classLoaderInterface.getResource(fileName);
    try (InputStream reader = new BufferedInputStream(fileUrl.openStream())) {
        Properties properties = new Properties();
        properties.load(reader);
        return properties;
    }
}
Also used : ClassLoaderInterfaceDelegate(com.opensymphony.xwork2.util.finder.ClassLoaderInterfaceDelegate) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) ClassLoaderInterface(com.opensymphony.xwork2.util.finder.ClassLoaderInterface) Properties(java.util.Properties) URL(java.net.URL)

Aggregations

ClassLoaderInterface (com.opensymphony.xwork2.util.finder.ClassLoaderInterface)3 ClassLoaderInterfaceDelegate (com.opensymphony.xwork2.util.finder.ClassLoaderInterfaceDelegate)3 ActionContext (com.opensymphony.xwork2.ActionContext)2 URL (java.net.URL)2 ReloadingClassLoader (com.opensymphony.xwork2.util.classloader.ReloadingClassLoader)1 BufferedInputStream (java.io.BufferedInputStream)1 InputStream (java.io.InputStream)1 Properties (java.util.Properties)1 ServletContext (javax.servlet.ServletContext)1