use of groovy.lang.GroovyShell in project camel by apache.
the class GroovyExpression method instantiateScript.
@SuppressWarnings("unchecked")
private Script instantiateScript(Exchange exchange) {
// Get the script from the cache, or create a new instance
GroovyLanguage language = (GroovyLanguage) exchange.getContext().resolveLanguage("groovy");
Class<Script> scriptClass = language.getScriptFromCache(text);
if (scriptClass == null) {
GroovyShell shell;
Set<GroovyShellFactory> shellFactories = exchange.getContext().getRegistry().findByType(GroovyShellFactory.class);
if (shellFactories.size() > 1) {
throw new IllegalStateException("Too many GroovyShellFactory instances found: " + shellFactories.size());
} else if (shellFactories.size() == 1) {
shell = shellFactories.iterator().next().createGroovyShell(exchange);
} else {
ClassLoader cl = exchange.getContext().getApplicationContextClassLoader();
shell = cl != null ? new GroovyShell(cl) : new GroovyShell();
}
scriptClass = shell.getClassLoader().parseClass(text);
language.addScriptToCache(text, scriptClass);
}
// New instance of the script
try {
return scriptClass.newInstance();
} catch (InstantiationException e) {
throw new RuntimeCamelException(e);
} catch (IllegalAccessException e) {
throw new RuntimeCamelException(e);
}
}
use of groovy.lang.GroovyShell in project camel by apache.
the class GroovyShellFactoryTest method testExpressionReturnsTheCorrectValue.
@Test
public void testExpressionReturnsTheCorrectValue() {
// Given
GroovyShellFactory groovyShellFactory = mock(GroovyShellFactory.class);
given(groovyShellFactory.createGroovyShell(any(Exchange.class))).willReturn(new GroovyShell());
SimpleRegistry registry = new SimpleRegistry();
registry.put("groovyShellFactory", groovyShellFactory);
CamelContext camelContext = new DefaultCamelContext(registry);
// When
assertExpression(GroovyLanguage.groovy("exchange.in.body"), new DefaultExchange(camelContext), null);
// Then
verify(groovyShellFactory).createGroovyShell(any(Exchange.class));
}
use of groovy.lang.GroovyShell in project groovy by apache.
the class Groovy method main.
public static void main(String[] args) {
final GroovyShell shell = new GroovyShell(new Binding());
final Groovy groovy = new Groovy();
for (int i = 1; i < args.length; i++) {
final Commandline.Argument argument = groovy.createArg();
argument.setValue(args[i]);
}
final AntBuilder builder = new AntBuilder();
groovy.setProject(builder.getProject());
groovy.parseAndRunScript(shell, null, null, null, new File(args[0]), builder);
}
use of groovy.lang.GroovyShell in project groovy by apache.
the class DocGeneratorMain method main.
public static void main(String[] args) {
try {
GroovyShell shell = new GroovyShell();
//shell.run("src/main/org/codehaus/groovy/tools/DocGenerator.groovy", "org.codehaus.groovy.tools.DocGenerator.groovy", args);
shell.run(new File("src/main/org/codehaus/groovy/tools/DocGenerator.groovy"), args);
} catch (Exception e) {
System.out.println("Failed: " + e);
e.printStackTrace();
}
}
use of groovy.lang.GroovyShell in project groovy by apache.
the class GroovyEngine method initialize.
/**
* Initialize the engine.
*/
public void initialize(BSFManager mgr, String lang, Vector declaredBeans) throws BSFException {
super.initialize(mgr, lang, declaredBeans);
// create a shell
shell = new GroovyShell(mgr.getClassLoader());
// register the mgr with object name "bsf"
shell.setVariable("bsf", new BSFFunctions(mgr, this));
int size = declaredBeans.size();
for (int i = 0; i < size; i++) {
declareBean((BSFDeclaredBean) declaredBeans.elementAt(i));
}
}
Aggregations