Search in sources :

Example 56 with Script

use of groovy.lang.Script in project hale by halestudio.

the class CustomGroovyTransformation method evaluate.

@Override
protected Object evaluate(String transformationIdentifier, TransformationEngine engine, ListMultimap<String, PropertyValue> variables, String resultName, PropertyEntityDefinition resultProperty, Map<String, String> executionParameters, TransformationLog log) throws TransformationException, NoResultException {
    // store script as parameter
    if (!CustomPropertyFunctionType.GROOVY.equals(customFunction.getFunctionType())) {
        throw new TransformationException("Custom function is not of type groovy");
    }
    Value scriptValue = customFunction.getFunctionDefinition();
    if (scriptValue.isEmpty()) {
        throw new NoResultException("Script not defined");
    }
    ListMultimap<String, ParameterValue> params = ArrayListMultimap.create();
    params.put(GroovyTransformation.PARAMETER_SCRIPT, new ParameterValue(scriptValue));
    setParameters(params);
    // instance builder
    InstanceBuilder builder = GroovyTransformation.createBuilder(resultProperty);
    // create the script binding
    Binding binding = createGroovyBinding(variables, getCell(), getTypeCell(), builder, log, getExecutionContext(), resultProperty.getDefinition().getPropertyType());
    Object result;
    try {
        GroovyService service = getExecutionContext().getService(GroovyService.class);
        Script groovyScript = GroovyUtil.getScript(this, binding, service, true);
        // evaluate the script
        result = GroovyTransformation.evaluate(groovyScript, builder, resultProperty.getDefinition().getPropertyType(), service, log);
    } catch (TransformationException | NoResultException e) {
        throw e;
    } catch (Throwable e) {
        throw new TransformationException("Error evaluating the custom function script", e);
    }
    if (result == null) {
        throw new NoResultException();
    }
    return result;
}
Also used : Binding(groovy.lang.Binding) ParameterBinding(eu.esdihumboldt.hale.common.align.model.impl.mdexpl.ParameterBinding) Script(groovy.lang.Script) TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) NoResultException(eu.esdihumboldt.hale.common.align.transformation.function.impl.NoResultException) GroovyService(eu.esdihumboldt.util.groovy.sandbox.GroovyService) PropertyValue(eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue) Value(eu.esdihumboldt.hale.common.core.io.Value) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) InstanceBuilder(eu.esdihumboldt.hale.common.instance.groovy.InstanceBuilder)

Example 57 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) InvocationTargetException(java.lang.reflect.InvocationTargetException) ResourceException(groovy.util.ResourceException) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) ScriptException(groovy.util.ScriptException)

Example 58 with Script

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

the class DbUpdaterEngine method executeGroovyScript.

protected boolean executeGroovyScript(ScriptResource file) {
    try {
        ClassLoader classLoader = getClass().getClassLoader();
        CompilerConfiguration cc = new CompilerConfiguration();
        cc.setRecompileGroovySource(true);
        Binding bind = new Binding();
        bind.setProperty("ds", getDataSource());
        bind.setProperty("log", LoggerFactory.getLogger(String.format("%s$%s", DbUpdaterEngine.class.getName(), StringUtils.removeEndIgnoreCase(file.getName(), ".groovy"))));
        if (!StringUtils.endsWithIgnoreCase(file.getName(), "." + UPGRADE_GROOVY_EXTENSION)) {
            bind.setProperty("postUpdate", new PostUpdateScripts() {

                @Override
                public void add(Closure closure) {
                    super.add(closure);
                    log.warn("Added post update action will be ignored for data store [{}]", storeNameToString(storeName));
                }
            });
        }
        GroovyShell shell = new GroovyShell(classLoader, bind, cc);
        Script script = shell.parse(file.getContent());
        script.run();
    } catch (Exception e) {
        throw new RuntimeException(String.format("%sError executing Groovy script %s\n%s", ERROR, file.name, e.getMessage()), e);
    }
    return true;
}
Also used : Binding(groovy.lang.Binding) Script(groovy.lang.Script) PostUpdateScripts(com.haulmont.cuba.core.sys.PostUpdateScripts) Closure(groovy.lang.Closure) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) GroovyShell(groovy.lang.GroovyShell) DbInitializationException(com.haulmont.cuba.core.sys.DbInitializationException) IOException(java.io.IOException)

Example 59 with Script

use of groovy.lang.Script in project cucumber-jvm by cucumber.

the class GroovyBackend method loadGlue.

@Override
public void loadGlue(Glue glue, List<String> gluePaths) {
    this.glue = glue;
    final Binding context = shell.getContext();
    for (String gluePath : gluePaths) {
        // Load sources
        for (Resource resource : resourceLoader.resources(gluePath, ".groovy")) {
            Script script = parse(resource);
            runIfScript(context, script);
        }
        // Load compiled scripts
        for (Class<? extends Script> glueClass : classFinder.getDescendants(Script.class, packageName(gluePath))) {
            try {
                Script script = glueClass.getConstructor(Binding.class).newInstance(context);
                runIfScript(context, script);
            } catch (Exception e) {
                throw new CucumberException(e);
            }
        }
    }
}
Also used : Binding(groovy.lang.Binding) Script(groovy.lang.Script) Resource(cucumber.runtime.io.Resource) CucumberException(cucumber.runtime.CucumberException) InvokerInvocationException(org.codehaus.groovy.runtime.InvokerInvocationException) CucumberException(cucumber.runtime.CucumberException) IOException(java.io.IOException)

Example 60 with Script

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

the class DefaultUrlMappingEvaluatorTests method testResourceMappingsWithVersionAndNamespace.

public void testResourceMappingsWithVersionAndNamespace() throws Exception {
    GroovyShell shell = new GroovyShell();
    Binding binding = new Binding();
    // Resource Entry: "/api/foo"(resources:"foo", version:'1.0', namespace:'v1')
    Script script = shell.parse("mappings = {\n" + "\"/api/foo\"(resources: 'foo', version: '1.0', namespace: 'v1')\n" + "}");
    script.setBinding(binding);
    script.run();
    Closure closure = (Closure) binding.getVariable("mappings");
    List<UrlMapping> mappings = evaluator.evaluateMappings(closure);
    assertTrue(mappings.size() > 0);
    //Check that version and namespace are correct for each mapping
    for (UrlMapping mapping : mappings) {
        assertEquals("1.0", mapping.getVersion());
        assertEquals("v1", mapping.getNamespace());
    }
}
Also used : Binding(groovy.lang.Binding) UrlMapping(grails.web.mapping.UrlMapping) Script(groovy.lang.Script) Closure(groovy.lang.Closure) GroovyShell(groovy.lang.GroovyShell)

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