Bindings bindings = null;
Reader reader = null;
try {
bindings = verifySlingBindings(scriptName, props);
ScriptContext ctx = new SimpleScriptContext();
ctx.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
ctx.setReader((Reader) bindings.get(READER));
ctx.setWriter((Writer) bindings.get(OUT));
ctx.setErrorWriter(new LogWriter((Logger) bindings.get(LOG)));
reader = getScriptReader();
if ( method != null && !(this.scriptEngine instanceof Invocable)) {
reader = getWrapperReader(reader, method, args);
}
// evaluate the script
final Object result = scriptEngine.eval(reader, ctx);
// call method - if supplied and script engine supports direct invocation
if ( method != null && (this.scriptEngine instanceof Invocable)) {
try {
((Invocable)scriptEngine).invokeFunction(method, Arrays.asList(args).toArray());
} catch (NoSuchMethodException e) {
throw new ScriptEvaluationException(scriptName, "Method " + method + " not found in script.", e);
}
}
// optionall flush the output channel
Object flushObject = bindings.get(SlingBindings.FLUSH);
if (flushObject instanceof Boolean && (Boolean) flushObject) {
ctx.getWriter().flush();
}
// allways flush the error channel
ctx.getErrorWriter().flush();
return result;
} catch (IOException ioe) {
throw new ScriptEvaluationException(scriptName, ioe.getMessage(),