Search in sources :

Example 1 with ClassLoaderInterface

use of com.opensymphony.xwork2.util.finder.ClassLoaderInterface 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 ClassLoaderInterface

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

the class JSPLoader method compileJava.

/**
 * Compiles the given source code into java bytecode
 */
private void compileJava(String className, final String source, Set<String> extraClassPath) throws IOException {
    LOG.trace("Compiling [{}], source: [{}]", className, source);
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
    // the generated bytecode is fed to the class loader
    JavaFileManager jfm = new ForwardingJavaFileManager<StandardJavaFileManager>(compiler.getStandardFileManager(diagnostics, null, null)) {

        @Override
        public JavaFileObject getJavaFileForOutput(Location location, String name, JavaFileObject.Kind kind, FileObject sibling) throws IOException {
            MemoryJavaFileObject fileObject = new MemoryJavaFileObject(name, kind);
            classLoader.addMemoryJavaFileObject(name, fileObject);
            return fileObject;
        }
    };
    // read java source code from memory
    String fileName = className.replace('.', '/') + ".java";
    SimpleJavaFileObject sourceCodeObject = new SimpleJavaFileObject(toURI(fileName), JavaFileObject.Kind.SOURCE) {

        @Override
        public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException, IllegalStateException, UnsupportedOperationException {
            return source;
        }
    };
    // build classpath
    // some entries will be added multiple times, hence the set
    List<String> optionList = new ArrayList<String>();
    Set<String> classPath = new HashSet<String>();
    // find available jars
    ClassLoaderInterface classLoaderInterface = getClassLoaderInterface();
    UrlSet urlSet = new UrlSet(classLoaderInterface);
    // find jars
    List<URL> urls = urlSet.getUrls();
    if (urls != null && urls.size() > 0) {
        final FileManagerFactory fileManagerFactoryGetInstance = ServletActionContext.getActionContext().getInstance(FileManagerFactory.class);
        final FileManagerFactory contextFileManagerFactory = (fileManagerFactoryGetInstance != null ? fileManagerFactoryGetInstance : (FileManagerFactory) ServletActionContext.getActionContext().get(StrutsConstants.STRUTS_FILE_MANAGER_FACTORY));
        final FileManagerFactory fileManagerFactory = (contextFileManagerFactory != null ? contextFileManagerFactory : new DefaultFileManagerFactory());
        final FileManager fileManagerGetInstance = fileManagerFactory.getFileManager();
        final FileManager contextFileManager = (fileManagerGetInstance != null ? fileManagerGetInstance : (FileManager) ServletActionContext.getActionContext().get(StrutsConstants.STRUTS_FILE_MANAGER));
        final FileManager fileManager = (contextFileManager != null ? contextFileManager : new DefaultFileManager());
        for (URL url : urls) {
            URL normalizedUrl = fileManager.normalizeToFileProtocol(url);
            File file = FileUtils.toFile(ObjectUtils.defaultIfNull(normalizedUrl, url));
            if (file.exists())
                classPath.add(file.getAbsolutePath());
        }
    }
    // these should be in the list already, but I am feeling paranoid
    // this jar
    classPath.add(getJarUrl(EmbeddedJSPResult.class));
    // servlet api
    classPath.add(getJarUrl(Servlet.class));
    // jsp api
    classPath.add(getJarUrl(JspPage.class));
    try {
        Class instanceManager = Class.forName("org.apache.tomcat.InstanceManager");
        classPath.add(getJarUrl(instanceManager));
    } catch (ClassNotFoundException e) {
    // ok ignore
    }
    // add extra classpath entries (jars where tlds were found will be here)
    for (Iterator<String> iterator = extraClassPath.iterator(); iterator.hasNext(); ) {
        String entry = iterator.next();
        classPath.add(entry);
    }
    String classPathString = StringUtils.join(classPath, File.pathSeparator);
    if (LOG.isDebugEnabled()) {
        LOG.debug("Compiling [#0] with classpath [#1]", className, classPathString);
    }
    optionList.addAll(Arrays.asList("-classpath", classPathString));
    // compile
    JavaCompiler.CompilationTask task = compiler.getTask(null, jfm, diagnostics, optionList, null, Arrays.asList(sourceCodeObject));
    if (!task.call()) {
        throw new StrutsException("Compilation failed:" + diagnostics.getDiagnostics().get(0).toString());
    }
}
Also used : SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) DefaultFileManagerFactory(com.opensymphony.xwork2.util.fs.DefaultFileManagerFactory) ArrayList(java.util.ArrayList) JspPage(javax.servlet.jsp.JspPage) URL(java.net.URL) SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) MemoryJavaFileObject(org.apache.struts2.compiler.MemoryJavaFileObject) JavaFileObject(javax.tools.JavaFileObject) UrlSet(com.opensymphony.xwork2.util.finder.UrlSet) Servlet(javax.servlet.Servlet) DiagnosticCollector(javax.tools.DiagnosticCollector) FileObject(javax.tools.FileObject) SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) MemoryJavaFileObject(org.apache.struts2.compiler.MemoryJavaFileObject) JavaFileObject(javax.tools.JavaFileObject) MemoryJavaFileObject(org.apache.struts2.compiler.MemoryJavaFileObject) HashSet(java.util.HashSet) FileManagerFactory(com.opensymphony.xwork2.FileManagerFactory) DefaultFileManagerFactory(com.opensymphony.xwork2.util.fs.DefaultFileManagerFactory) JavaFileManager(javax.tools.JavaFileManager) ForwardingJavaFileManager(javax.tools.ForwardingJavaFileManager) StandardJavaFileManager(javax.tools.StandardJavaFileManager) ClassLoaderInterface(com.opensymphony.xwork2.util.finder.ClassLoaderInterface) JavaCompiler(javax.tools.JavaCompiler) DefaultFileManager(com.opensymphony.xwork2.util.fs.DefaultFileManager) JavaFileManager(javax.tools.JavaFileManager) ForwardingJavaFileManager(javax.tools.ForwardingJavaFileManager) FileManager(com.opensymphony.xwork2.FileManager) StandardJavaFileManager(javax.tools.StandardJavaFileManager) ForwardingJavaFileManager(javax.tools.ForwardingJavaFileManager) DefaultFileManager(com.opensymphony.xwork2.util.fs.DefaultFileManager) File(java.io.File)

Example 3 with ClassLoaderInterface

use of com.opensymphony.xwork2.util.finder.ClassLoaderInterface 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 4 with ClassLoaderInterface

use of com.opensymphony.xwork2.util.finder.ClassLoaderInterface 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 5 with ClassLoaderInterface

use of com.opensymphony.xwork2.util.finder.ClassLoaderInterface 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)

Aggregations

ClassLoaderInterface (com.opensymphony.xwork2.util.finder.ClassLoaderInterface)4 ClassLoaderInterfaceDelegate (com.opensymphony.xwork2.util.finder.ClassLoaderInterfaceDelegate)3 URL (java.net.URL)3 ActionContext (com.opensymphony.xwork2.ActionContext)2 FileManager (com.opensymphony.xwork2.FileManager)1 FileManagerFactory (com.opensymphony.xwork2.FileManagerFactory)1 ReloadingClassLoader (com.opensymphony.xwork2.util.classloader.ReloadingClassLoader)1 UrlSet (com.opensymphony.xwork2.util.finder.UrlSet)1 DefaultFileManager (com.opensymphony.xwork2.util.fs.DefaultFileManager)1 DefaultFileManagerFactory (com.opensymphony.xwork2.util.fs.DefaultFileManagerFactory)1 BufferedInputStream (java.io.BufferedInputStream)1 File (java.io.File)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Properties (java.util.Properties)1 Servlet (javax.servlet.Servlet)1 ServletContext (javax.servlet.ServletContext)1 JspPage (javax.servlet.jsp.JspPage)1 DiagnosticCollector (javax.tools.DiagnosticCollector)1