Search in sources :

Example 1 with Script

use of groovy.lang.Script in project intellij-community by JetBrains.

the class ScriptSupport method checkValidScript.

public static String checkValidScript(String scriptText) {
    try {
        final File scriptFile = new File(scriptText);
        final GroovyShell shell = new GroovyShell();
        final Script script = scriptFile.exists() ? shell.parse(scriptFile) : shell.parse(scriptText);
        return null;
    } catch (IOException e) {
        return e.getMessage();
    } catch (MultipleCompilationErrorsException e) {
        final ErrorCollector errorCollector = e.getErrorCollector();
        final List<Message> errors = errorCollector.getErrors();
        for (Message error : errors) {
            if (error instanceof SyntaxErrorMessage) {
                final SyntaxErrorMessage errorMessage = (SyntaxErrorMessage) error;
                final SyntaxException cause = errorMessage.getCause();
                return cause.getMessage();
            }
        }
        return e.getMessage();
    } catch (CompilationFailedException ex) {
        return ex.getLocalizedMessage();
    }
}
Also used : Script(groovy.lang.Script) Message(org.codehaus.groovy.control.messages.Message) SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) SyntaxException(org.codehaus.groovy.syntax.SyntaxException) ErrorCollector(org.codehaus.groovy.control.ErrorCollector) ArrayList(java.util.ArrayList) List(java.util.List) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) IOException(java.io.IOException) MultipleCompilationErrorsException(org.codehaus.groovy.control.MultipleCompilationErrorsException) File(java.io.File) GroovyShell(groovy.lang.GroovyShell)

Example 2 with Script

use of groovy.lang.Script in project grails-core by grails.

the class DefaultUrlMappingEvaluatorTests method testRedirectMappings.

public void testRedirectMappings() throws Exception {
    GroovyShell shell = new GroovyShell();
    Binding binding = new Binding();
    Script script = shell.parse("mappings = {\n" + "\"/first\"(redirect:[controller: 'foo', action: 'bar'])\n" + "\"/second\"(redirect: '/bing/bang')\n" + "}");
    script.setBinding(binding);
    script.run();
    Closure closure = (Closure) binding.getVariable("mappings");
    List<UrlMapping> mappings = evaluator.evaluateMappings(closure);
    assertEquals(2, mappings.size());
    Object redirectInfo = mappings.get(0).getRedirectInfo();
    assertTrue(redirectInfo instanceof Map);
    Map redirectMap = (Map) redirectInfo;
    assertEquals(2, redirectMap.size());
    assertEquals("foo", redirectMap.get("controller"));
    assertEquals("bar", redirectMap.get("action"));
    assertEquals("/bing/bang", mappings.get(1).getRedirectInfo());
}
Also used : Binding(groovy.lang.Binding) UrlMapping(grails.web.mapping.UrlMapping) Script(groovy.lang.Script) Closure(groovy.lang.Closure) Map(java.util.Map) GroovyShell(groovy.lang.GroovyShell)

Example 3 with Script

use of groovy.lang.Script in project groovy by apache.

the class CachingGroovyEngine method eval.

/**
     * Evaluate an expression.
     */
public Object eval(String source, int lineNo, int columnNo, Object script) throws BSFException {
    try {
        Class scriptClass = evalScripts.get(script);
        if (scriptClass == null) {
            scriptClass = loader.parseClass(script.toString(), 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);
    }
}
Also used : Script(groovy.lang.Script) BSFException(org.apache.bsf.BSFException) BSFException(org.apache.bsf.BSFException)

Example 4 with Script

use of groovy.lang.Script 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 5 with Script

use of groovy.lang.Script in project cuba by cuba-platform.

the class AbstractScripting method evaluateGroovy.

@Override
public <T> T evaluateGroovy(String text, Binding binding, ScriptExecutionPolicy... policies) {
    boolean useCompilationCache = policies == null || !Arrays.asList(policies).contains(ScriptExecutionPolicy.DO_NOT_USE_COMPILE_CACHE);
    Script script = null;
    Object result;
    try {
        script = useCompilationCache ? getPool().borrowObject(text) : createScript(text);
        script.setBinding(binding);
        result = script.run();
    } catch (Exception e) {
        if (script != null && useCompilationCache) {
            try {
                getPool().invalidateObject(text, script);
            } catch (Exception e1) {
                log.warn("Error invalidating object in the pool", e1);
            }
        }
        if (e instanceof RuntimeException)
            throw ((RuntimeException) e);
        else
            throw new RuntimeException("Error evaluating Groovy expression", e);
    }
    if (useCompilationCache) {
        try {
            // free memory
            script.setBinding(null);
            getPool().returnObject(text, script);
        } catch (Exception e) {
            log.warn("Error returning object into the pool", e);
        }
    }
    // noinspection unchecked
    return (T) result;
}
Also used : Script(groovy.lang.Script) PooledObject(org.apache.commons.pool2.PooledObject) DefaultPooledObject(org.apache.commons.pool2.impl.DefaultPooledObject) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ResourceException(groovy.util.ResourceException) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) ScriptException(groovy.util.ScriptException)

Aggregations

Script (groovy.lang.Script)117 Binding (groovy.lang.Binding)55 GroovyShell (groovy.lang.GroovyShell)23 IOException (java.io.IOException)21 Test (org.junit.Test)16 ArrayList (java.util.ArrayList)13 File (java.io.File)12 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)12 HashMap (java.util.HashMap)11 GroovyService (eu.esdihumboldt.util.groovy.sandbox.GroovyService)10 Map (java.util.Map)10 InstanceBuilder (eu.esdihumboldt.hale.common.instance.groovy.InstanceBuilder)9 GroovityClassLoader (com.disney.groovity.compile.GroovityClassLoader)8 PrintWriter (java.io.PrintWriter)8 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)8 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)8 CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)8 Closure (groovy.lang.Closure)7 MissingMethodException (groovy.lang.MissingMethodException)7 InvocationTargetException (java.lang.reflect.InvocationTargetException)7