|
|||||||||||||||||||
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 | |||||||||||||||
GroovyEngine.java | 0% | 0% | 0% | 0% |
|
1 |
/*
|
|
2 |
$Id: GroovyEngine.java,v 1.8 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
|
|
8 |
that the following conditions are met:
|
|
9 |
|
|
10 |
1. Redistributions of source code must retain copyright
|
|
11 |
statements and notices. Redistributions must also contain a
|
|
12 |
copy of this document.
|
|
13 |
|
|
14 |
2. Redistributions in binary form must reproduce the
|
|
15 |
above copyright notice, this list of conditions and the
|
|
16 |
following disclaimer in the documentation and/or other
|
|
17 |
materials provided with the distribution.
|
|
18 |
|
|
19 |
3. The name "groovy" must not be used to endorse or promote
|
|
20 |
products derived from this Software without prior written
|
|
21 |
permission of The Codehaus. For written permission,
|
|
22 |
please contact info@codehaus.org.
|
|
23 |
|
|
24 |
4. Products derived from this Software may not be called "groovy"
|
|
25 |
nor may "groovy" appear in their names without prior written
|
|
26 |
permission of The Codehaus. "groovy" is a registered
|
|
27 |
trademark of The Codehaus.
|
|
28 |
|
|
29 |
5. Due credit should be given to The Codehaus -
|
|
30 |
http://groovy.codehaus.org/
|
|
31 |
|
|
32 |
THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
|
|
33 |
``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
|
|
34 |
NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
35 |
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
|
36 |
THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
|
37 |
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
38 |
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
39 |
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
40 |
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
41 |
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
42 |
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|
43 |
OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
44 |
|
|
45 |
*/
|
|
46 |
package org.codehaus.groovy.bsf;
|
|
47 |
|
|
48 |
import groovy.lang.Closure;
|
|
49 |
import groovy.lang.GroovyShell;
|
|
50 |
|
|
51 |
import java.util.Vector;
|
|
52 |
|
|
53 |
import org.apache.bsf.BSFDeclaredBean;
|
|
54 |
import org.apache.bsf.BSFException;
|
|
55 |
import org.apache.bsf.BSFManager;
|
|
56 |
import org.apache.bsf.util.BSFEngineImpl;
|
|
57 |
import org.apache.bsf.util.BSFFunctions;
|
|
58 |
import org.codehaus.groovy.runtime.InvokerHelper;
|
|
59 |
|
|
60 |
/**
|
|
61 |
* A BSF Engine for the <a href="http://groovy.codehaus.org/">Groovy</a>
|
|
62 |
* scripting language.
|
|
63 |
*
|
|
64 |
* It's derived from the Jython / JPython engine
|
|
65 |
*
|
|
66 |
* @author James Strachan
|
|
67 |
*/
|
|
68 |
public class GroovyEngine extends BSFEngineImpl { |
|
69 |
private static final String[] EMPTY_ARGS = { |
|
70 |
}; |
|
71 |
|
|
72 |
protected GroovyShell shell;
|
|
73 |
|
|
74 |
/**
|
|
75 |
* Convert a non java class name to a java classname
|
|
76 |
* This is used to convert a script name to a name
|
|
77 |
* that can be used as a classname with the script is
|
|
78 |
* loaded in GroovyClassloader#load()
|
|
79 |
* The method simply replaces any invalid characters
|
|
80 |
* with "_".
|
|
81 |
*/
|
|
82 | 0 |
private String convertToValidJavaClassname(String inName) {
|
83 | 0 |
if (inName == null || inName == "") { |
84 | 0 |
return "_"; |
85 |
} |
|
86 | 0 |
StringBuffer output = new StringBuffer(inName.length());
|
87 | 0 |
boolean firstChar = true; |
88 | 0 |
for (int i = 0; i < inName.length(); ++i) { |
89 | 0 |
char ch = inName.charAt(i);
|
90 | 0 |
if (firstChar && !Character.isJavaIdentifierStart(ch)) {
|
91 | 0 |
ch = '_'; |
92 | 0 |
} else if (!firstChar |
93 |
&& !(Character.isJavaIdentifierPart(ch) || ch == '.')) { |
|
94 | 0 |
ch = '_'; |
95 |
} |
|
96 | 0 |
firstChar = (ch == '.'); |
97 | 0 |
output.append(ch); |
98 |
} |
|
99 | 0 |
return output.toString();
|
100 |
} |
|
101 |
|
|
102 |
/**
|
|
103 |
* Allow an anonymous function to be declared and invoked
|
|
104 |
*/
|
|
105 | 0 |
public Object apply(
|
106 |
java.lang.String source, |
|
107 |
int lineNo,
|
|
108 |
int columnNo,
|
|
109 |
Object funcBody, |
|
110 |
Vector paramNames, |
|
111 |
Vector arguments) |
|
112 |
throws BSFException {
|
|
113 |
|
|
114 | 0 |
Object object = eval(source, lineNo, columnNo, funcBody); |
115 | 0 |
if (object instanceof Closure) { |
116 |
// lets call the function
|
|
117 |
|
|
118 |
/** @todo we could turn the 2 vectors into a Map */
|
|
119 | 0 |
Closure closure = (Closure) object; |
120 | 0 |
return closure.call(arguments.toArray());
|
121 |
} |
|
122 | 0 |
return object;
|
123 |
} |
|
124 |
|
|
125 |
/**
|
|
126 |
* Call the named method of the given object.
|
|
127 |
*/
|
|
128 | 0 |
public Object call(Object object, String method, Object[] args) throws BSFException { |
129 | 0 |
return InvokerHelper.invokeMethod(object, method, args);
|
130 |
} |
|
131 |
|
|
132 |
/**
|
|
133 |
* Declare a bean
|
|
134 |
*/
|
|
135 | 0 |
public void declareBean(BSFDeclaredBean bean) throws BSFException { |
136 |
//System.out.println("Declaring bean: " + bean.name + " value: " + bean.bean);
|
|
137 | 0 |
shell.setVariable(bean.name, bean.bean); |
138 |
} |
|
139 |
|
|
140 |
/**
|
|
141 |
* Evaluate an expression.
|
|
142 |
*/
|
|
143 | 0 |
public Object eval(String source, int lineNo, int columnNo, Object script) throws BSFException { |
144 | 0 |
try {
|
145 | 0 |
source = convertToValidJavaClassname(source); |
146 | 0 |
Object result = getEvalShell().evaluate(script.toString(), source); |
147 | 0 |
return result;
|
148 |
} |
|
149 |
catch (Exception e) {
|
|
150 | 0 |
e.printStackTrace(); |
151 | 0 |
throw new BSFException(BSFException.REASON_EXECUTION_ERROR, "exception from Groovy: " + e, e); |
152 |
} |
|
153 |
} |
|
154 |
|
|
155 |
/**
|
|
156 |
* Execute a script.
|
|
157 |
*/
|
|
158 | 0 |
public void exec(String source, int lineNo, int columnNo, Object script) throws BSFException { |
159 | 0 |
try {
|
160 |
// use evaluate to pass in the BSF variables
|
|
161 | 0 |
source = convertToValidJavaClassname(source); |
162 | 0 |
getEvalShell().evaluate(script.toString(), source); |
163 |
//getEvalShell().run(script.toString(), source, EMPTY_ARGS);
|
|
164 |
} |
|
165 |
catch (Exception e) {
|
|
166 | 0 |
e.printStackTrace(); |
167 | 0 |
throw new BSFException(BSFException.REASON_EXECUTION_ERROR, "exception from Groovy: " + e, e); |
168 |
} |
|
169 |
} |
|
170 |
|
|
171 |
/**
|
|
172 |
* Initialize the engine.
|
|
173 |
*/
|
|
174 | 0 |
public void initialize(BSFManager mgr, String lang, Vector declaredBeans) throws BSFException { |
175 | 0 |
super.initialize(mgr, lang, declaredBeans);
|
176 |
|
|
177 |
// create a shell
|
|
178 | 0 |
shell = new GroovyShell(mgr.getClassLoader());
|
179 |
|
|
180 |
// register the mgr with object name "bsf"
|
|
181 | 0 |
shell.setVariable("bsf", new BSFFunctions(mgr, this)); |
182 |
|
|
183 | 0 |
int size = declaredBeans.size();
|
184 | 0 |
for (int i = 0; i < size; i++) { |
185 | 0 |
declareBean((BSFDeclaredBean) declaredBeans.elementAt(i)); |
186 |
} |
|
187 |
} |
|
188 |
|
|
189 |
/**
|
|
190 |
* Undeclare a previously declared bean.
|
|
191 |
*/
|
|
192 | 0 |
public void undeclareBean(BSFDeclaredBean bean) throws BSFException { |
193 | 0 |
shell.setVariable(bean.name, null);
|
194 |
} |
|
195 |
|
|
196 |
/**
|
|
197 |
* @return a newly created GroovyShell using the same variable scope but a new class loader
|
|
198 |
*/
|
|
199 | 0 |
protected GroovyShell getEvalShell() {
|
200 | 0 |
return new GroovyShell(shell); |
201 |
} |
|
202 |
} |
|
203 |
|
|