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