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

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

Introduction

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

Prototype

public void setVariables(Map variables) 

Source Link

Document

Sets the Map of variables to use

Usage

From source file:hudson.widgets.RenderOnDemandClosure.java

/**
 * Renders the captured fragment./*from  www. j av a  2 s .  co m*/
 */
@JavaScriptMethod
public HttpResponse render() {
    return new HttpResponse() {
        public void generateResponse(StaplerRequest req, StaplerResponse rsp, Object node)
                throws IOException, ServletException {
            try {
                new DefaultScriptInvoker() {
                    @Override
                    protected JellyContext createContext(StaplerRequest req, StaplerResponse rsp, Script script,
                            Object it) {
                        JellyContext context = super.createContext(req, rsp, script, it);
                        for (int i = bodyStack.length - 1; i > 0; i--) {// exclude bodyStack[0]
                            context = new JellyContext(context);
                            context.setVariable("org.apache.commons.jelly.body", bodyStack[i]);
                        }
                        try {
                            AdjunctsInPage.get().assumeIncluded(adjuncts);
                        } catch (IOException e) {
                            LOGGER.log(Level.WARNING, "Failed to resurrect adjunct context", e);
                        } catch (SAXException e) {
                            LOGGER.log(Level.WARNING, "Failed to resurrect adjunct context", e);
                        }
                        return context;
                    }

                    @Override
                    protected void exportVariables(StaplerRequest req, StaplerResponse rsp, Script script,
                            Object it, JellyContext context) {
                        super.exportVariables(req, rsp, script, it, context);
                        context.setVariables(variables);
                        req.setAttribute("currentDescriptorByNameUrl", currentDescriptorByNameUrl);
                    }
                }.invokeScript(req, rsp, bodyStack[0], null);
            } catch (JellyTagException e) {
                LOGGER.log(Level.WARNING, "Failed to evaluate the template closure", e);
                throw new IOException("Failed to evaluate the template closure", e);
            }
        }
    };
}