|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
CachingGroovyEngine.java | 0% | 0% | 0% | 0% |
|
1 |
/*
|
|
2 |
* $Id: CachingGroovyEngine.java,v 1.4 2004/05/17 22:27:11 glaforge Exp $
|
|
3 |
*
|
|
4 |
* Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
|
|
5 |
*
|
|
6 |
* Redistribution and use of this software and associated documentation
|
|
7 |
* ("Software"), with or without modification, are permitted provided that the
|
|
8 |
* following conditions are met: 1. Redistributions of source code must retain
|
|
9 |
* copyright statements and notices. Redistributions must also contain a copy
|
|
10 |
* of this document. 2. Redistributions in binary form must reproduce the above
|
|
11 |
* copyright notice, this list of conditions and the following disclaimer in
|
|
12 |
* the documentation and/or other materials provided with the distribution. 3.
|
|
13 |
* The name "groovy" must not be used to endorse or promote products derived
|
|
14 |
* from this Software without prior written permission of The Codehaus. For
|
|
15 |
* written permission, please contact info@codehaus.org. 4. Products derived
|
|
16 |
* from this Software may not be called "groovy" nor may "groovy" appear in
|
|
17 |
* their names without prior written permission of The Codehaus. "groovy" is a
|
|
18 |
* registered trademark of The Codehaus. 5. Due credit should be given to The
|
|
19 |
* Codehaus - http://groovy.codehaus.org/
|
|
20 |
*
|
|
21 |
* THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS ``AS IS'' AND ANY
|
|
22 |
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
23 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
24 |
* DISCLAIMED. IN NO EVENT SHALL THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR
|
|
25 |
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
26 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
27 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
28 |
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
29 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
30 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
|
|
31 |
* DAMAGE.
|
|
32 |
*
|
|
33 |
*/
|
|
34 |
package org.codehaus.groovy.bsf;
|
|
35 |
|
|
36 |
import groovy.lang.GroovyClassLoader;
|
|
37 |
import groovy.lang.GroovyShell;
|
|
38 |
import groovy.lang.Script;
|
|
39 |
import groovy.lang.Binding;
|
|
40 |
|
|
41 |
import java.io.ByteArrayInputStream;
|
|
42 |
import java.security.AccessController;
|
|
43 |
import java.security.PrivilegedAction;
|
|
44 |
import java.util.HashMap;
|
|
45 |
import java.util.Map;
|
|
46 |
import java.util.Vector;
|
|
47 |
|
|
48 |
import org.apache.bsf.BSFDeclaredBean;
|
|
49 |
import org.apache.bsf.BSFException;
|
|
50 |
import org.apache.bsf.BSFManager;
|
|
51 |
import org.apache.bsf.util.BSFFunctions;
|
|
52 |
import org.codehaus.groovy.runtime.InvokerHelper;
|
|
53 |
import org.codehaus.groovy.control.CompilerConfiguration;
|
|
54 |
|
|
55 |
/**
|
|
56 |
* A Caching implementation of the GroovyEngine
|
|
57 |
*
|
|
58 |
* @author James Birchfield
|
|
59 |
*/
|
|
60 |
public class CachingGroovyEngine extends GroovyEngine { |
|
61 |
private static final String[] EMPTY_ARGS = { |
|
62 |
}; |
|
63 |
|
|
64 |
private Map evalScripts;
|
|
65 |
private Map execScripts;
|
|
66 |
private Binding context;
|
|
67 |
private ClassLoader parent;
|
|
68 |
private GroovyClassLoader loader;
|
|
69 |
|
|
70 |
|
|
71 |
/**
|
|
72 |
* Evaluate an expression.
|
|
73 |
*/
|
|
74 | 0 |
public Object eval(String source, int lineNo, int columnNo, Object script) throws BSFException { |
75 | 0 |
try {
|
76 |
// Object result = shell.evaluate(script.toString(), source);
|
|
77 | 0 |
Class scriptClass = (Class) evalScripts.get(script); |
78 | 0 |
if (scriptClass == null) { |
79 | 0 |
scriptClass = loader.parseClass(new ByteArrayInputStream(script.toString().getBytes()), source);
|
80 | 0 |
evalScripts.put(script, scriptClass); |
81 |
} |
|
82 |
else {
|
|
83 | 0 |
System.out.println("eval() - Using cached script...");
|
84 |
} |
|
85 |
//can't cache the script because the context may be different.
|
|
86 |
//but don't bother loading parsing the class again
|
|
87 | 0 |
Script s = InvokerHelper.createScript(scriptClass, context); |
88 | 0 |
return s.run();
|
89 |
} |
|
90 |
catch (Exception e) {
|
|
91 | 0 |
e.printStackTrace(); |
92 | 0 |
throw new BSFException(BSFException.REASON_EXECUTION_ERROR, "exception from Groovy: " + e, e); |
93 |
} |
|
94 |
} |
|
95 |
|
|
96 |
/**
|
|
97 |
* Execute a script.
|
|
98 |
*/
|
|
99 | 0 |
public void exec(String source, int lineNo, int columnNo, Object script) throws BSFException { |
100 | 0 |
try {
|
101 |
// shell.run(script.toString(), source, EMPTY_ARGS);
|
|
102 |
|
|
103 | 0 |
Class scriptClass = (Class) execScripts.get(script); |
104 | 0 |
if (scriptClass == null) { |
105 | 0 |
scriptClass = loader.parseClass(new ByteArrayInputStream(script.toString().getBytes()), source);
|
106 | 0 |
execScripts.put(script, scriptClass); |
107 |
} |
|
108 |
else {
|
|
109 | 0 |
System.out.println("exec() - Using cached version of class...");
|
110 |
} |
|
111 | 0 |
InvokerHelper.invokeMethod(scriptClass, "main", EMPTY_ARGS);
|
112 |
} |
|
113 |
catch (Exception e) {
|
|
114 | 0 |
e.printStackTrace(); |
115 | 0 |
throw new BSFException(BSFException.REASON_EXECUTION_ERROR, "exception from Groovy: " + e, e); |
116 |
} |
|
117 |
} |
|
118 |
|
|
119 |
/**
|
|
120 |
* Initialize the engine.
|
|
121 |
*/
|
|
122 | 0 |
public void initialize(final BSFManager mgr, String lang, Vector declaredBeans) throws BSFException { |
123 | 0 |
super.initialize(mgr, lang, declaredBeans);
|
124 | 0 |
parent = mgr.getClassLoader(); |
125 | 0 |
if (parent == null) |
126 | 0 |
parent = GroovyShell.class.getClassLoader();
|
127 | 0 |
final ClassLoader finalParent = parent; |
128 | 0 |
this.loader =
|
129 |
(GroovyClassLoader) AccessController.doPrivileged(new PrivilegedAction() {
|
|
130 | 0 |
public Object run() {
|
131 | 0 |
CompilerConfiguration configuration = new CompilerConfiguration();
|
132 | 0 |
configuration.setClasspath(mgr.getClassPath()); |
133 | 0 |
return new GroovyClassLoader(finalParent, configuration); |
134 |
} |
|
135 |
}); |
|
136 | 0 |
execScripts = new HashMap();
|
137 | 0 |
evalScripts = new HashMap();
|
138 | 0 |
context = shell.getContext(); |
139 |
// create a shell
|
|
140 |
|
|
141 |
// register the mgr with object name "bsf"
|
|
142 | 0 |
context.setVariable("bsf", new BSFFunctions(mgr, this)); |
143 |
|
|
144 | 0 |
int size = declaredBeans.size();
|
145 | 0 |
for (int i = 0; i < size; i++) { |
146 | 0 |
declareBean((BSFDeclaredBean) declaredBeans.elementAt(i)); |
147 |
} |
|
148 |
} |
|
149 |
} |
|
150 |
|
|