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;
}
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;
}
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;
}
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);
}
}
}
}
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());
}
}
Aggregations