public Object eval(Reader script, ScriptContext scriptContext) 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; }
/** * Call Ruby's respond_to? to find out if the Ruby object responds to a method. * * @param rubyObject The ruby object in the Java side * @param methodName The method name * @return true if the ruby object responds to that method */ public boolean respondTo(Object rubyObject, String methodName) { Object res = JavaEmbedUtils.invokeMethod( runtime, rubyObject, "respond_to?", new Object[] {methodName}, Object.class); if (res instanceof Boolean) { return (Boolean) res; } throw new RuntimeException("Problem calling respond_to?!"); }
@Before public void setUp() { this.ruby = Ruby.newInstance(); this.ruby.getLoadService().require("org/torquebox/web/component/mock_app"); RubyModule mockAppClass = this.ruby.getClassFromPath("MockApp"); this.rackApp = (IRubyObject) JavaEmbedUtils.invokeMethod(this.ruby, mockAppClass, "new", null, IRubyObject.class); }
private Object callMethodImpl( Object rubyObject, String methodName, Object[] params, Class<? extends Object> returnType) { String[] s = methodName.split("\\."); if (s.length == 1) { return JavaEmbedUtils.invokeMethod(runtime, rubyObject, methodName, params, returnType); } else { String m1 = s[0]; // first method... String m2 = getMethods(s); // remaining methods... Object temp = JavaEmbedUtils.invokeMethod(runtime, rubyObject, m1, EMPTY, Object.class); return callMethodImpl(temp, m2, params, returnType); // recursive... } }
private Block createBlock(AbstractBlock parent, String context, Map<String, Object> options) { IRubyObject rubyClass = rubyRuntime.evalScriptlet("Asciidoctor::Block"); RubyHash convertMapToRubyHashWithSymbols = RubyHashUtil.convertMapToRubyHashWithSymbols(rubyRuntime, options); Object[] parameters = { parent.delegate(), RubyUtils.toSymbol(rubyRuntime, context), convertMapToRubyHashWithSymbols }; return (Block) JavaEmbedUtils.invokeMethod(rubyRuntime, rubyClass, "new", parameters, Block.class); }
@Close public void close() { try { JavaEmbedUtils.invokeMethod( _ruby, _transformerObject, "close", new Object[0], IRubyObject.class); } catch (Exception e) { logger.error("Failed to call close() on RubyTransformer", e); } try { JavaEmbedUtils.terminate(_ruby); } catch (Exception e) { logger.error("Failed to terminate JRuby instance", e); } }
@Initialize public void initialize() { _ruby = JavaEmbedUtils.initialize(Collections.EMPTY_LIST); _runtimeAdapter = JavaEmbedUtils.newRuntimeAdapter(); // load the class def _runtimeAdapter.eval(_ruby, _code); _transformerObject = _runtimeAdapter.eval(_ruby, "Transformer.new()"); logger.info("Evaluated Ruby code to: {}", _transformerObject); JavaEmbedUtils.invokeMethod( _ruby, _transformerObject, "init", new Object[0], IRubyObject.class); }
public Object[] transform(InputRow row) { Object[] inputValues = new Object[_inputValues.length]; for (int i = 0; i < inputValues.length; i++) { Object value = row.getValue(_inputValues[i]); inputValues[i] = value; } IRubyObject[] rubyInputValues = JavaUtil.convertJavaArrayToRuby(_ruby, inputValues); IRubyObject rubyOutputCollector = JavaEmbedUtils.javaToRuby(_ruby, _rowCollector); Object[] arguments = new Object[] {rubyInputValues, rubyOutputCollector}; JavaEmbedUtils.invokeMethod(_ruby, _transformerObject, "transform", arguments, Object.class); return null; }
@Test public void testCall() throws Exception { RackApplicationComponent component = new RackApplicationComponent(this.rackApp); RackEnvironment env = mock(RackEnvironment.class); RubyHash envHash = RubyHash.newHash(this.ruby); when(env.getEnv()).thenReturn(envHash); Object response = component.call(env); assertNotNull(response); RubyHash internalEnvHash = (RubyHash) JavaEmbedUtils.invokeMethod(this.ruby, this.rackApp, "env", null, RubyHash.class); assertSame(envHash, internalEnvHash); }
public void testExceptionsAcrossTheBridge() { final IRubyObject wrappedThrower = JavaEmbedUtils.javaToRuby(runtime, new ExceptionThrower()); boolean exceptionThrown = false; try { // call throwException via JRuby JavaEmbedUtils.invokeMethod( runtime, wrappedThrower, "throwException", new Object[] {}, Object.class); } catch (final RaiseException e) { exceptionThrown = true; final NativeException ne = (NativeException) e.getException(); final ExpectedException ee = (ExpectedException) ne.getCause(); assertEquals( "The unpacked exception we receive should be the one we threw.", ExceptionThrower.expectedException, ee); } finally { assertTrue( "Java Exception should have been thrown and wrapped as a RaiseException", exceptionThrown); } }
public Inline createInline( AbstractBlock parent, String context, String text, Map<String, Object> attributes, Map<String, Object> options) { options.put(Options.ATTRIBUTES, attributes); IRubyObject rubyClass = rubyRuntime.evalScriptlet("Asciidoctor::Inline"); RubyHash convertMapToRubyHashWithSymbols = RubyHashUtil.convertMapToRubyHashWithSymbols(rubyRuntime, options); Object[] parameters = { parent.delegate(), RubyUtils.toSymbol(rubyRuntime, context), text, convertMapToRubyHashWithSymbols }; return (Inline) JavaEmbedUtils.invokeMethod(rubyRuntime, rubyClass, "new", parameters, Inline.class); }