org.codehaus.groovy.bsf.CachingGroovyEngine Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of groovy-all-minimal Show documentation
Show all versions of groovy-all-minimal Show documentation
Groovy: A powerful, dynamic language for the JVM
/*
* Copyright 2003-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.codehaus.groovy.bsf;
import groovy.lang.Binding;
import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyShell;
import groovy.lang.Script;
import org.apache.bsf.BSFDeclaredBean;
import org.apache.bsf.BSFException;
import org.apache.bsf.BSFManager;
import org.apache.bsf.util.BSFFunctions;
import org.codehaus.groovy.control.CompilerConfiguration;
import org.codehaus.groovy.runtime.InvokerHelper;
import java.io.ByteArrayInputStream;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;
import java.util.logging.Logger;
import java.util.logging.Level;
/**
* A Caching implementation of the GroovyEngine
*
* @author James Birchfield
*/
public class CachingGroovyEngine extends GroovyEngine {
private static final Logger LOG = Logger.getLogger(CachingGroovyEngine.class.getName());
private static final Object[] EMPTY_ARGS = new Object[]{new String[]{}};
private Map evalScripts;
private Map execScripts;
private Binding context;
private GroovyClassLoader loader;
/**
* Evaluate an expression.
*/
public Object eval(String source, int lineNo, int columnNo, Object script) throws BSFException {
try {
Class scriptClass = (Class) evalScripts.get(script);
if (scriptClass == null) {
scriptClass = loader.parseClass(new ByteArrayInputStream(script.toString().getBytes()), source);
evalScripts.put(script, scriptClass);
} else {
LOG.fine("eval() - Using cached script...");
}
//can't cache the script because the context may be different.
//but don't bother loading parsing the class again
Script s = InvokerHelper.createScript(scriptClass, context);
return s.run();
} catch (Exception e) {
throw new BSFException(BSFException.REASON_EXECUTION_ERROR, "exception from Groovy: " + e, e);
}
}
/**
* Execute a script.
*/
public void exec(String source, int lineNo, int columnNo, Object script) throws BSFException {
try {
// shell.run(script.toString(), source, EMPTY_ARGS);
Class scriptClass = (Class) execScripts.get(script);
if (scriptClass == null) {
scriptClass = loader.parseClass(new ByteArrayInputStream(script.toString().getBytes()), source);
execScripts.put(script, scriptClass);
} else {
LOG.fine("exec() - Using cached version of class...");
}
InvokerHelper.invokeMethod(scriptClass, "main", EMPTY_ARGS);
} catch (Exception e) {
LOG.log(Level.WARNING, "BSF trace", e);
throw new BSFException(BSFException.REASON_EXECUTION_ERROR, "exception from Groovy: " + e, e);
}
}
/**
* Initialize the engine.
*/
public void initialize(final BSFManager mgr, String lang, Vector declaredBeans) throws BSFException {
super.initialize(mgr, lang, declaredBeans);
ClassLoader parent = mgr.getClassLoader();
if (parent == null)
parent = GroovyShell.class.getClassLoader();
final ClassLoader finalParent = parent;
this.loader =
(GroovyClassLoader) AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
CompilerConfiguration configuration = new CompilerConfiguration();
configuration.setClasspath(mgr.getClassPath());
return new GroovyClassLoader(finalParent, configuration);
}
});
execScripts = new HashMap();
evalScripts = new HashMap();
context = shell.getContext();
// create a shell
// register the mgr with object name "bsf"
context.setVariable("bsf", new BSFFunctions(mgr, this));
int size = declaredBeans.size();
for (int i = 0; i < size; i++) {
declareBean((BSFDeclaredBean) declaredBeans.elementAt(i));
}
}
}