KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > kohsuke > stapler > jelly > groovy > ClosureScript


1 package org.kohsuke.stapler.jelly.groovy;
2
3 import org.apache.commons.jelly.Script;
4 import org.apache.commons.jelly.JellyException;
5 import org.apache.commons.jelly.JellyContext;
6 import org.apache.commons.jelly.XMLOutput;
7 import org.apache.commons.jelly.JellyTagException;
8 import groovy.lang.Closure;
9
10 /**
11  * {@link Script} that invokes a {@link Closure}.
12  *
13  * @author Kohsuke Kawaguchi
14  */

15 public class ClosureScript implements Script {
16
17     private final JellyBuilder builder;
18     private final Closure closure;
19
20     public ClosureScript(JellyBuilder builder, Closure closure) {
21         this.builder = builder;
22         this.closure = closure;
23     }
24
25     public Script compile() throws JellyException {
26         return this;
27     }
28
29     public void run(JellyContext context, XMLOutput output) throws JellyTagException {
30         JellyContext oldc = builder.setContext(context);
31         XMLOutput oldo = builder.setOutput(output);
32         try {
33             closure.setDelegate(builder);
34             closure.call();
35         } finally {
36             builder.setContext(oldc);
37             builder.setOutput(oldo);
38         }
39     }
40 }
41
Popular Tags