Search in sources :

Example 1 with ExtendedURLClassLoader

use of org.xwiki.classloader.ExtendedURLClassLoader in project xwiki-platform by xwiki.

the class ScriptClassLoaderHandlerListener method findClassLoader.

/**
 * @param jarsParameterValue the value of the macro parameters used to pass extra URLs that should be in the
 *            execution class loader
 * @param parent the parent classloader for the classloader to create (if it doesn't already exist)
 * @return the class loader to use for executing the script
 * @throws Exception in case of an error in building the class loader
 */
private ClassLoader findClassLoader(String jarsParameterValue, ClassLoader parent) throws Exception {
    // We cache the Class Loader for improved performances and we check if the saved class loader had the same
    // jar parameters value as the current execution. If not, we compute a new class loader.
    ExtendedURLClassLoader cl = (ExtendedURLClassLoader) this.execution.getContext().getProperty(EXECUTION_CONTEXT_CLASSLOADER_KEY);
    if (cl == null) {
        if (StringUtils.isNotEmpty(jarsParameterValue)) {
            cl = createOrExtendClassLoader(true, jarsParameterValue, parent);
        } else {
            cl = this.attachmentClassLoaderFactory.createAttachmentClassLoader("", parent);
        }
    } else {
        String cachedJarsParameterValue = (String) this.execution.getContext().getProperty(EXECUTION_CONTEXT_JARPARAMS_KEY);
        if (cachedJarsParameterValue != jarsParameterValue) {
            cl = createOrExtendClassLoader(false, jarsParameterValue, cl);
        }
    }
    this.execution.getContext().setProperty(EXECUTION_CONTEXT_CLASSLOADER_KEY, cl);
    return cl;
}
Also used : ExtendedURLClassLoader(org.xwiki.classloader.ExtendedURLClassLoader)

Example 2 with ExtendedURLClassLoader

use of org.xwiki.classloader.ExtendedURLClassLoader in project xwiki-platform by xwiki.

the class DefaultAttachmentClassLoaderFactoryTest method testExtendClassLoaderLoader.

@org.junit.Test
public void testExtendClassLoaderLoader() throws Exception {
    ExtendedURLClassLoader cl = new ExtendedURLClassLoader(new URL[0]);
    factory.extendAttachmentClassLoader("attach:page@filename, http://some/url", cl);
    Assert.assertEquals("attachmentjar://page%40filename", cl.getURLs()[0].toString());
    Assert.assertEquals("http://some/url", cl.getURLs()[1].toString());
}
Also used : ExtendedURLClassLoader(org.xwiki.classloader.ExtendedURLClassLoader)

Example 3 with ExtendedURLClassLoader

use of org.xwiki.classloader.ExtendedURLClassLoader in project xwiki-platform by xwiki.

the class ScriptClassLoaderHandlerListener method createOrExtendClassLoader.

/**
 * @param createNewClassLoader if true create a new classloader and if false extend an existing one with the passed
 *            additional jars
 * @param jarsParameterValue the value of the macro parameters used to pass extra URLs that should be in the
 *            execution class loader
 * @param classLoader the parent classloader for the classloader to create or the classloader to extend, depending
 *            on the value of the createNewClassLoader parameter
 * @return the new classloader or the extended one
 * @throws Exception in case of an error in building or extending the class loader
 */
private ExtendedURLClassLoader createOrExtendClassLoader(boolean createNewClassLoader, String jarsParameterValue, ClassLoader classLoader) throws Exception {
    ExtendedURLClassLoader cl;
    if (canHaveJarsParameters()) {
        if (createNewClassLoader) {
            cl = this.attachmentClassLoaderFactory.createAttachmentClassLoader(jarsParameterValue, classLoader);
        } else {
            cl = (ExtendedURLClassLoader) classLoader;
            this.attachmentClassLoaderFactory.extendAttachmentClassLoader(jarsParameterValue, cl);
        }
        this.execution.getContext().setProperty(EXECUTION_CONTEXT_JARPARAMS_KEY, jarsParameterValue);
    } else {
        throw new MacroExecutionException("You cannot pass additional jars since you don't have programming rights");
    }
    return cl;
}
Also used : ExtendedURLClassLoader(org.xwiki.classloader.ExtendedURLClassLoader) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException)

Aggregations

ExtendedURLClassLoader (org.xwiki.classloader.ExtendedURLClassLoader)3 MacroExecutionException (org.xwiki.rendering.macro.MacroExecutionException)1