throws ScriptException {
Bindings bindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
SlingScriptHelper helper = (SlingScriptHelper) bindings.get(SlingBindings.SLING);
if (helper == null) {
throw new ScriptException("SlingScriptHelper missing from bindings");
}
// ensure GET request
if (helper.getRequest() != null && !"GET".equals(helper.getRequest().getMethod())) {
throw new ScriptException(
"JRuby scripting only supports GET requests");
}
final ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
StringBuffer scriptString = new StringBuffer();
BufferedReader bufferedScript = new BufferedReader(script);
String nextLine = bufferedScript.readLine();
while (nextLine != null) {
scriptString.append(nextLine);
scriptString.append("\n");
nextLine = bufferedScript.readLine();
}
IRubyObject scriptRubyString = JavaEmbedUtils.javaToRuby(runtime,
scriptString.toString());
IRubyObject erb = (IRubyObject) JavaEmbedUtils.invokeMethod(
runtime, erbModule, "new", new Object[] { scriptRubyString },
IRubyObject.class);
JavaEmbedUtils.invokeMethod(runtime, erb, "set_props",
new Object[] { JavaEmbedUtils.javaToRuby(runtime, bindings) },
IRubyObject.class);
IRubyObject binding = (IRubyObject) JavaEmbedUtils.invokeMethod(
runtime, erb, "send", new Object[] { bindingSym },
IRubyObject.class);
scriptContext.getWriter().write(
(String) JavaEmbedUtils.invokeMethod(runtime, erb, "result",
new Object[] { binding }, String.class));
} catch (Throwable t) {
final ScriptException ex = new ScriptException("Failure running Ruby script:" + t);
ex.initCause(t);
throw ex;
} finally {
Thread.currentThread().setContextClassLoader(oldClassLoader);
}
return null;