Search in sources :

Example 66 with Script

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

the class GroovyExpression method evaluate.

public <T> T evaluate(Exchange exchange, Class<T> type) {
    Script script = instantiateScript(exchange);
    script.setBinding(createBinding(exchange));
    Object value = script.run();
    return exchange.getContext().getTypeConverter().convertTo(type, value);
}
Also used : Script(groovy.lang.Script)

Example 67 with Script

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

the class AbstractScripting method getPool.

private synchronized GenericKeyedObjectPool<String, Script> getPool() {
    if (pool == null) {
        GenericKeyedObjectPoolConfig poolConfig = new GenericKeyedObjectPoolConfig();
        poolConfig.setMaxTotalPerKey(-1);
        poolConfig.setMaxIdlePerKey(globalConfig.getGroovyEvaluationPoolMaxIdle());
        pool = new GenericKeyedObjectPool<>(new BaseKeyedPooledObjectFactory<String, Script>() {

            @Override
            public Script create(String key) throws Exception {
                return createScript(key);
            }

            @Override
            public PooledObject<Script> wrap(Script value) {
                return new DefaultPooledObject<>(value);
            }
        }, poolConfig);
    }
    return pool;
}
Also used : GenericKeyedObjectPoolConfig(org.apache.commons.pool2.impl.GenericKeyedObjectPoolConfig) Script(groovy.lang.Script) DefaultPooledObject(org.apache.commons.pool2.impl.DefaultPooledObject) BaseKeyedPooledObjectFactory(org.apache.commons.pool2.BaseKeyedPooledObjectFactory)

Example 68 with Script

use of groovy.lang.Script in project pom-manipulation-ext by release-engineering.

the class GroovyManipulator method applyChanges.

/**
 * Apply the groovy script changes to the top level pom.
 */
@Override
public Set<Project> applyChanges(final List<Project> projects) throws ManipulationException {
    final GroovyState state = session.getState(GroovyState.class);
    if (!session.isEnabled() || !state.isEnabled()) {
        logger.debug(getClass().getSimpleName() + ": Nothing to do!");
        return Collections.emptySet();
    }
    final Set<Project> changed = new HashSet<>();
    for (File groovyScript : parseGroovyScripts(state.getGroovyScripts())) {
        GroovyShell shell = new GroovyShell();
        Script script;
        for (final Project project : projects) {
            if (project.isExecutionRoot()) {
                logger.info("Executing {} on {}", groovyScript, project);
                try {
                    script = shell.parse(groovyScript);
                    if (script instanceof BaseScript) {
                        ((BaseScript) script).setValues(session.getUserProperties(), projects, project);
                    } else {
                        throw new ManipulationException("Cannot cast " + groovyScript + " to a BaseScript to set values.");
                    }
                } catch (MissingMethodException e) {
                    try {
                        logger.debug("Failure when injecting into script {} ", FileUtils.readFileToString(groovyScript), e);
                    } catch (IOException e1) {
                        logger.debug("Unable to read script file {} for debugging! {} ", groovyScript, e1);
                    }
                    throw new ManipulationException("Unable to inject values into base script", e);
                } catch (CompilationFailedException e) {
                    try {
                        logger.debug("Failure when parsing script {} ", FileUtils.readFileToString(groovyScript), e);
                    } catch (IOException e1) {
                        logger.debug("Unable to read script file {} for debugging! {} ", groovyScript, e1);
                    }
                    throw new ManipulationException("Unable to parse script", e);
                } catch (IOException e) {
                    throw new ManipulationException("Unable to parse script", e);
                }
                try {
                    script.run();
                } catch (Exception e) {
                    throw new ManipulationException("Unable to parse script", e);
                }
                changed.add(project);
            }
        }
    }
    return changed;
}
Also used : Script(groovy.lang.Script) BaseScript(org.commonjava.maven.ext.core.groovy.BaseScript) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) IOException(java.io.IOException) GroovyShell(groovy.lang.GroovyShell) MissingMethodException(groovy.lang.MissingMethodException) IOException(java.io.IOException) ManipulationException(org.commonjava.maven.ext.common.ManipulationException) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) GroovyState(org.commonjava.maven.ext.core.state.GroovyState) Project(org.commonjava.maven.ext.common.model.Project) MissingMethodException(groovy.lang.MissingMethodException) BaseScript(org.commonjava.maven.ext.core.groovy.BaseScript) ManipulationException(org.commonjava.maven.ext.common.ManipulationException) File(java.io.File) HashSet(java.util.HashSet)

Example 69 with Script

use of groovy.lang.Script in project streamline by hortonworks.

the class GroovyTest method testGroovyShell.

@Test
public void testGroovyShell() throws Exception {
    GroovyShell groovyShell = new GroovyShell();
    final String s = "x  > 2  &&  y  > 1";
    Script script = groovyShell.parse(s);
    Binding binding = new Binding();
    binding.setProperty("x", 5);
    binding.setProperty("y", 3);
    script.setBinding(binding);
    Object result = script.run();
    Assert.assertEquals(true, result);
    log.debug("evaluating [{}] with (x,y)=({},{}) => {}\n", s, script.getBinding().getProperty("x"), script.getBinding().getProperty("y"), result);
    binding.setProperty("y", 0);
    result = script.run();
    Assert.assertEquals(false, result);
    log.debug("evaluating [{}] with (x,y)=({},{}) => {}\n", s, binding.getProperty("x"), binding.getProperty("y"), result);
}
Also used : Binding(groovy.lang.Binding) Script(groovy.lang.Script) GroovyShell(groovy.lang.GroovyShell) Test(org.junit.Test)

Example 70 with Script

use of groovy.lang.Script in project beakerx by twosigma.

the class GroovyCodeRunner method call.

@Override
public TryResult call() throws Exception {
    ClassLoader oldld = Thread.currentThread().getContextClassLoader();
    TryResult either;
    String scriptName = SCRIPT_NAME;
    try {
        Object result = null;
        theOutput.setOutputHandler();
        Thread.currentThread().setContextClassLoader(groovyEvaluator.getGroovyClassLoader());
        scriptName += System.currentTimeMillis();
        Class<?> parsedClass = groovyEvaluator.getGroovyClassLoader().parseClass(theCode, scriptName);
        Script instance = (Script) parsedClass.newInstance();
        if (GroovyEvaluator.LOCAL_DEV) {
            groovyEvaluator.getScriptBinding().setVariable(Evaluator.BEAKER_VARIABLE_NAME, new HashMap<String, Object>());
        } else {
            groovyEvaluator.getScriptBinding().setVariable(Evaluator.BEAKER_VARIABLE_NAME, NamespaceClient.getBeaker(groovyEvaluator.getSessionId()));
        }
        instance.setBinding(groovyEvaluator.getScriptBinding());
        InternalVariable.setValue(theOutput);
        result = instance.run();
        if (GroovyEvaluator.LOCAL_DEV) {
            logger.info("Result: {}", result);
            logger.info("Variables: {}", groovyEvaluator.getScriptBinding().getVariables());
        }
        either = TryResult.createResult(result);
    } catch (Throwable e) {
        if (GroovyEvaluator.LOCAL_DEV) {
            logger.warn(e.getMessage());
            e.printStackTrace();
        }
        // unwrap ITE
        if (e instanceof InvocationTargetException) {
            e = ((InvocationTargetException) e).getTargetException();
        }
        if (e instanceof InterruptedException || e instanceof InvocationTargetException || e instanceof ThreadDeath) {
            either = TryResult.createError(INTERUPTED_MSG);
        } else {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            StackTraceUtils.sanitize(e).printStackTrace(pw);
            String value = sw.toString();
            value = printStacktrace(scriptName, value);
            either = TryResult.createError(value);
        }
    } finally {
        theOutput.clrOutputHandler();
        Thread.currentThread().setContextClassLoader(oldld);
    }
    return either;
}
Also used : Script(groovy.lang.Script) TryResult(com.twosigma.beakerx.TryResult) InvocationTargetException(java.lang.reflect.InvocationTargetException) StringWriter(java.io.StringWriter) SimpleEvaluationObject(com.twosigma.beakerx.jvm.object.SimpleEvaluationObject) PrintWriter(java.io.PrintWriter)

Aggregations

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