Search in sources :

Example 1 with GroovyShell

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);
    }
}
Also used : Script(groovy.lang.Script) RuntimeCamelException(org.apache.camel.RuntimeCamelException) GroovyShell(groovy.lang.GroovyShell)

Example 2 with GroovyShell

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));
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) CamelContext(org.apache.camel.CamelContext) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) DefaultExchange(org.apache.camel.impl.DefaultExchange) SimpleRegistry(org.apache.camel.impl.SimpleRegistry) GroovyShell(groovy.lang.GroovyShell) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) Test(org.junit.Test)

Example 3 with GroovyShell

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);
}
Also used : Binding(groovy.lang.Binding) Commandline(org.apache.tools.ant.types.Commandline) AntBuilder(groovy.util.AntBuilder) File(java.io.File) GroovyShell(groovy.lang.GroovyShell)

Example 4 with GroovyShell

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();
    }
}
Also used : File(java.io.File) GroovyShell(groovy.lang.GroovyShell)

Example 5 with GroovyShell

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));
    }
}
Also used : GroovyShell(groovy.lang.GroovyShell) BSFFunctions(org.apache.bsf.util.BSFFunctions)

Aggregations

GroovyShell (groovy.lang.GroovyShell)86 Binding (groovy.lang.Binding)43 File (java.io.File)15 CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)14 Script (groovy.lang.Script)10 ImportCustomizer (org.codehaus.groovy.control.customizers.ImportCustomizer)10 Closure (groovy.lang.Closure)9 IOException (java.io.IOException)8 List (java.util.List)6 GroovyClassLoader (groovy.lang.GroovyClassLoader)5 GroovyObject (groovy.lang.GroovyObject)4 GroovyRuntimeException (groovy.lang.GroovyRuntimeException)4 AntBuilder (groovy.util.AntBuilder)4 InputStreamReader (java.io.InputStreamReader)4 StringWriter (java.io.StringWriter)4 BuildException (org.apache.tools.ant.BuildException)4 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)4 UrlMapping (grails.web.mapping.UrlMapping)3 InputStream (java.io.InputStream)3 PrintWriter (java.io.PrintWriter)3