Example usage for org.apache.commons.jelly JellyContext JellyContext

List of usage examples for org.apache.commons.jelly JellyContext JellyContext

Introduction

In this page you can find the example usage for org.apache.commons.jelly JellyContext JellyContext.

Prototype

public JellyContext(JellyContext parent) 

Source Link

Document

Create a new context with the given parent context.

Usage

From source file:com.cyclopsgroup.waterview.jelly.JellyEngine.java

/**
 * Get script with give path and package name
 *
 * @param scriptPath Script path// ww w .j  av  a 2s .  c  o m
 * @param packageName Look for it in this package
 * @param defaultScript Return as default value
 * @return Script object
 * @throws JellyException Throw it out
 */
public Script getScript(String packageName, String scriptPath, Script defaultScript) throws JellyException {
    String fullPath = scriptPath;
    String pkg = moduleManager.getPackageName(packageName);
    if (StringUtils.isNotEmpty(pkg)) {
        fullPath = pkg.replace('.', '/') + scriptPath;
    }
    Script script = null;
    synchronized (this) {
        if (getCacheManager().contains(this, fullPath)) {
            script = (Script) getCacheManager().get(this, fullPath);
        } else {
            final URL resource = getClass().getClassLoader().getResource(fullPath);

            if (resource == null) {
                script = DUMMY_SCRIPT;
            } else {
                JellyContext jc = new JellyContext(getGlobalContext());
                final Script s = jc.compileScript(resource);
                script = new Script() {

                    public Script compile() throws JellyException {
                        return this;
                    }

                    public void run(JellyContext context, XMLOutput output) throws JellyTagException {
                        TagSupport.addScriptResource(resource, context);
                        try {
                            s.run(context, output);
                        } catch (JellyTagException e) {
                            throw e;
                        } finally {
                            TagSupport.removeScriptResource(resource, context);
                        }
                    }
                };
            }
            getCacheManager().put(this, fullPath, script);
        }
    }
    return script == DUMMY_SCRIPT ? defaultScript : script;
}

From source file:org.kohsuke.stapler.jelly.groovy.JellyBuilder.java

private void _include(Object it, Klass clazz, String view) throws IOException, JellyException {
    JellyClassTearOff t = request.getWebApp().getMetaClass(clazz).getTearOff(JellyClassTearOff.class);
    Script s = t.findScript(view);//from w ww .  j  a va 2  s  .c o  m
    if (s == null)
        throw new IllegalArgumentException("No such view: " + view + " for " + clazz);

    JellyContext context = new JellyContext(getContext());
    if (it != null)
        context.setVariable("it", it);
    context.setVariable("from", it);

    ClassLoader old = Thread.currentThread().getContextClassLoader();
    if (clazz.clazz instanceof Class)
        Thread.currentThread().setContextClassLoader(((Class) clazz.clazz).getClassLoader());
    try {
        s.run(context, output);
    } finally {
        Thread.currentThread().setContextClassLoader(old);
    }
}