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