1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package org.codehaus.groovy.bsf;
35
36 import groovy.lang.Binding;
37 import groovy.lang.GroovyClassLoader;
38 import groovy.lang.GroovyShell;
39 import groovy.lang.Script;
40 import org.apache.bsf.BSFDeclaredBean;
41 import org.apache.bsf.BSFException;
42 import org.apache.bsf.BSFManager;
43 import org.apache.bsf.util.BSFFunctions;
44 import org.codehaus.groovy.control.CompilerConfiguration;
45 import org.codehaus.groovy.runtime.InvokerHelper;
46
47 import java.io.ByteArrayInputStream;
48 import java.security.AccessController;
49 import java.security.PrivilegedAction;
50 import java.util.HashMap;
51 import java.util.Map;
52 import java.util.Vector;
53
54 /***
55 * A Caching implementation of the GroovyEngine
56 *
57 * @author James Birchfield
58 */
59 public class CachingGroovyEngine extends GroovyEngine {
60 private static final String[] EMPTY_ARGS = {
61 };
62
63 private Map evalScripts;
64 private Map execScripts;
65 private Binding context;
66 private ClassLoader parent;
67 private GroovyClassLoader loader;
68
69
70 /***
71 * Evaluate an expression.
72 */
73 public Object eval(String source, int lineNo, int columnNo, Object script) throws BSFException {
74 try {
75
76 Class scriptClass = (Class) evalScripts.get(script);
77 if (scriptClass == null) {
78 scriptClass = loader.parseClass(new ByteArrayInputStream(script.toString().getBytes()), source);
79 evalScripts.put(script, scriptClass);
80 } else {
81 System.out.println("eval() - Using cached script...");
82 }
83
84
85 Script s = InvokerHelper.createScript(scriptClass, context);
86 return s.run();
87 } catch (Exception e) {
88 throw new BSFException(BSFException.REASON_EXECUTION_ERROR, "exception from Groovy: " + e, e);
89 }
90 }
91
92 /***
93 * Execute a script.
94 */
95 public void exec(String source, int lineNo, int columnNo, Object script) throws BSFException {
96 try {
97
98
99 Class scriptClass = (Class) execScripts.get(script);
100 if (scriptClass == null) {
101 scriptClass = loader.parseClass(new ByteArrayInputStream(script.toString().getBytes()), source);
102 execScripts.put(script, scriptClass);
103 } else {
104 System.out.println("exec() - Using cached version of class...");
105 }
106 InvokerHelper.invokeMethod(scriptClass, "main", EMPTY_ARGS);
107 } catch (Exception e) {
108 throw new BSFException(BSFException.REASON_EXECUTION_ERROR, "exception from Groovy: " + e, e);
109 }
110 }
111
112 /***
113 * Initialize the engine.
114 */
115 public void initialize(final BSFManager mgr, String lang, Vector declaredBeans) throws BSFException {
116 super.initialize(mgr, lang, declaredBeans);
117 parent = mgr.getClassLoader();
118 if (parent == null)
119 parent = GroovyShell.class.getClassLoader();
120 final ClassLoader finalParent = parent;
121 this.loader =
122 (GroovyClassLoader) AccessController.doPrivileged(new PrivilegedAction() {
123 public Object run() {
124 CompilerConfiguration configuration = new CompilerConfiguration();
125 configuration.setClasspath(mgr.getClassPath());
126 return new GroovyClassLoader(finalParent, configuration);
127 }
128 });
129 execScripts = new HashMap();
130 evalScripts = new HashMap();
131 context = shell.getContext();
132
133
134
135 context.setVariable("bsf", new BSFFunctions(mgr, this));
136
137 int size = declaredBeans.size();
138 for (int i = 0; i < size; i++) {
139 declareBean((BSFDeclaredBean) declaredBeans.elementAt(i));
140 }
141 }
142 }