Specific script engine able to reload modified scripts as well as dealing properly with dependent scripts.
| Method from groovy.util.GroovyScriptEngine Detail: |
public Script createScript(String scriptName,
Binding binding) throws ResourceException, ScriptException {
ScriptCacheEntry entry = updateCacheEntry(scriptName);
scriptName = scriptName.intern();
return InvokerHelper.createScript(entry.scriptClass, binding);
}
Creates a Script with a given scriptName and binding. |
public GroovyClassLoader getGroovyClassLoader() {
return groovyLoader;
}
Returns the GroovyClassLoader associated with this script engine instance.
Useful if you need to pass the class loader to another library. |
public ClassLoader getParentClassLoader() {
return groovyLoader.getParent();
}
Get the ClassLoader that will serve as the parent ClassLoader of the
GroovyClassLoader in which scripts will be executed. By default, this is the
ClassLoader that loaded the GroovyScriptEngine class. |
public URLConnection getResourceConnection(String resourceName) throws ResourceException {
// Get the URLConnection
URLConnection groovyScriptConn = null;
ResourceException se = null;
for (URL root : roots) {
URL scriptURL = null;
try {
scriptURL = new URL(root, resourceName);
groovyScriptConn = scriptURL.openConnection();
// Make sure we can open it, if we can't it doesn't exist.
// Could be very slow if there are any non-file:// URLs in there
groovyScriptConn.getInputStream();
break; // Now this is a bit unusual
} catch (MalformedURLException e) {
String message = "Malformed URL: " + root + ", " + resourceName;
if (se == null) {
se = new ResourceException(message);
} else {
se = new ResourceException(message, se);
}
} catch (IOException e1) {
String message = "Cannot open URL: " + scriptURL;
if (se == null) {
se = new ResourceException(message);
} else {
se = new ResourceException(message, se);
}
}
}
// If we didn't find anything, report on all the exceptions that occurred.
if (groovyScriptConn == null) {
throw se;
}
return groovyScriptConn;
}
Get a resource connection as a URLConnection to retrieve a script
from the ResourceConnector. |
public Class loadScriptByName(String scriptName) throws ResourceException, ScriptException {
scriptName = scriptName.replace('.', File.separatorChar) + ".groovy";
ScriptCacheEntry entry = updateCacheEntry(scriptName);
return entry.scriptClass;
}
Get the class of the scriptName in question, so that you can instantiate Groovy objects with caching and reloading. |
public Class loadScriptByName(String scriptName,
ClassLoader parentClassLoader) throws ResourceException, ScriptException {
initGroovyLoader(parentClassLoader);
return loadScriptByName(scriptName);
} Deprecated!
Get the class of the scriptName in question, so that you can instantiate Groovy objects with caching and reloading. |
public static void main(String[] urls) throws Exception {
GroovyScriptEngine gse = new GroovyScriptEngine(urls);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line;
while (true) {
System.out.print("groovy > ");
if ((line = br.readLine()) == null || line.equals("quit"))
break;
try {
System.out.println(gse.run(line, new Binding()));
} catch (Exception e) {
e.printStackTrace();
}
}
}
Simple testing harness for the GSE. Enter script roots as arguments and
then input script names to run them. |
public String run(String scriptName,
String argument) throws ResourceException, ScriptException {
Binding binding = new Binding();
binding.setVariable("arg", argument);
Object result = run(scriptName, binding);
return result == null ? "" : result.toString();
}
Run a script identified by name with a single argument. |
public Object run(String scriptName,
Binding binding) throws ResourceException, ScriptException {
return createScript(scriptName, binding).run();
}
Run a script identified by name with a given binding. |
public void setParentClassLoader(ClassLoader parentClassLoader) {
if (parentClassLoader == null) {
throw new IllegalArgumentException("The parent class loader must not be null.");
}
initGroovyLoader(parentClassLoader);
} Deprecated!
|