private IRubyObject[] convertToRuby(Object[] javaArgs) { if (javaArgs == null || javaArgs.length == 0) { return new IRubyObject[0]; } IRubyObject[] rubyArgs = new IRubyObject[javaArgs.length]; for (int i = 0; i < javaArgs.length; ++i) { rubyArgs[i] = JavaEmbedUtils.javaToRuby(this.ruby, javaArgs[i]); } return rubyArgs; }
/** * Initializes an instance of the {@link org.jruby.Ruby} runtime. */ @SuppressWarnings("unchecked") private static Ruby initializeRuntime() { return JavaEmbedUtils.initialize(Collections.EMPTY_LIST); }
private Object convertRubyValue(Object rubyValue) { if (rubyValue == null) { return null; } else if (rubyValue instanceof IRubyObject) { return JavaEmbedUtils.rubyToJava((IRubyObject) rubyValue); } else { return rubyValue; } }
Ruby ruby = JavaEmbedUtils.initialize(Collections.EMPTY_LIST, rubyConfig); ruby.getLoadService().require("com/picocontainer/script/jruby/scriptedbuilder"); ruby.defineReadonlyVariable("$parent", JavaEmbedUtils.javaToRuby(ruby, parentContainer), Scope.GLOBAL); ruby.defineReadonlyVariable("$assembly_scope", JavaEmbedUtils.javaToRuby(ruby, assemblyScope), Scope.GLOBAL); IRubyObject result = JavaEmbedUtils.newRuntimeAdapter().eval(ruby, script); return (PicoContainer) JavaEmbedUtils.rubyToJava(ruby, result, PicoContainer.class); } catch (RaiseException re) { if (re.getCause() instanceof ScriptedPicoContainerMarkupException) { JavaEmbedUtils.terminate(ruby);
@Inject public RackServlet(RackServletConfig config) throws IOException { Preconditions.checkNotNull(config); File rackScriptFile = new File(config.getRackConfigPath()); Preconditions.checkArgument(rackScriptFile.canRead(), "Could not find rack script specified by [" + config.getRackConfigPath() + "] and resolved to [" + rackScriptFile.getAbsolutePath() + "]"); runtime = JavaEmbedUtils.initialize(ImmutableList.of(rackScriptFile.getParentFile().getCanonicalPath()), createRuntimeConfig()); // don't inherit system settings for gems RubyHash env = runtime.evalScriptlet("ENV").convertToHash(); env.remove("GEM_HOME"); env.remove("GEM_PATH"); InputStream stream = Resources.getResource("proofpoint/rack.rb").openStream(); try { runtime.loadFile("rack.rb", stream, false); } finally { stream.close(); } IRubyObject builder = runtime.evalScriptlet("Proofpoint::RackServer::Builder.new"); rackApplication = adapter.callMethod(builder, "build", new IRubyObject[] { javaToRuby(runtime, rackScriptFile.getCanonicalPath()) }); }
/** * @see org.apache.bsf.BSFEngine#terminate() */ @Override public void terminate() { JavaEmbedUtils.terminate(runtime); runtime = null; super.terminate(); } }
public Object call(Object recv, String method, Object[] args) throws BSFException { try { return JavaEmbedUtils.invokeMethod(runtime, recv, method, args, Object.class); } catch (Exception excptn) { throw new BSFException(BSFException.REASON_EXECUTION_ERROR, excptn.getMessage(), excptn); } }
private RubyInstanceConfig createRuntimeConfig() { RubyInstanceConfig config = new RubyInstanceConfig(); ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); if (classLoader == null) { classLoader = ClassLoader.getSystemClassLoader(); } config.setClassCache(JavaEmbedUtils.createClassCache(classLoader)); URL resource = RubyInstanceConfig.class.getResource("/META-INF/jruby.home"); if (resource != null && resource.getProtocol().equals("jar")) { try { // http://weblogs.java.net/blog/2007/04/25/how-convert-javaneturl-javaiofile config.setJRubyHome(resource.toURI().getSchemeSpecificPart()); } catch (URISyntaxException e) { config.setJRubyHome(resource.getPath()); } } return config; }
@Inject public RackServlet(RackServletConfig config) throws IOException { Preconditions.checkNotNull(config); File rackScriptFile = new File(config.getRackConfigPath()); Preconditions.checkArgument(rackScriptFile.canRead(), "Could not find rack script specified by [" + config.getRackConfigPath() + "] and resolved to [" + rackScriptFile.getAbsolutePath() + "]"); runtime = JavaEmbedUtils.initialize(ImmutableList.of(rackScriptFile.getParentFile().getCanonicalPath()), createRuntimeConfig()); // don't inherit system settings for gems RubyHash env = runtime.evalScriptlet("ENV").convertToHash(); env.remove("GEM_HOME"); env.remove("GEM_PATH"); InputStream stream = Resources.getResource("io/airlift/rack.rb").openStream(); try { runtime.loadFile("rack.rb", stream, false); } finally { stream.close(); } IRubyObject builder = runtime.evalScriptlet("Airlift::RackServer::Builder.new"); rackApplication = adapter.callMethod(builder, "build", new IRubyObject[] { javaToRuby(runtime, rackScriptFile.getCanonicalPath()) }); }
/** * @see org.apache.bsf.BSFEngine#terminate() */ @Override public void terminate() { JavaEmbedUtils.terminate(runtime); runtime = null; super.terminate(); } }
public Object call(Object recv, String method, Object[] args) throws BSFException { try { return JavaEmbedUtils.invokeMethod(runtime, recv, method, args, Object.class); } catch (Exception excptn) { throw new BSFException(BSFException.REASON_EXECUTION_ERROR, excptn.getMessage(), excptn); } }
private RubyInstanceConfig createRuntimeConfig() { RubyInstanceConfig config = new RubyInstanceConfig(); ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); if (classLoader == null) { classLoader = ClassLoader.getSystemClassLoader(); } config.setClassCache(JavaEmbedUtils.createClassCache(classLoader)); URL resource = RubyInstanceConfig.class.getResource("/META-INF/jruby.home"); if (resource != null && resource.getProtocol().equals("jar")) { try { // http://weblogs.java.net/blog/2007/04/25/how-convert-javaneturl-javaiofile config.setJRubyHome(resource.toURI().getSchemeSpecificPart()); } catch (URISyntaxException e) { config.setJRubyHome(resource.getPath()); } } return config; }
public static IRubyObject javaToRuby(Ruby runtime, boolean value) { return javaToRuby(runtime, value ? Boolean.TRUE : Boolean.FALSE); }
private Object convertRubyValue(Object rubyValue) { if (rubyValue == null) { return null; } else if (rubyValue instanceof IRubyObject) { return JavaEmbedUtils.rubyToJava((IRubyObject) rubyValue); } else { return rubyValue; } }
/** * Get an instance of a JRuby runtime. Provide any loadpaths you want used at startup. * * @param loadPaths to specify where to look for Ruby modules. * @return an instance */ public static Ruby initialize(List<String> loadPaths) { return initialize(loadPaths, new RubyInstanceConfig()); }
@Inject public RackServlet(RackServletConfig config) throws IOException { Preconditions.checkNotNull(config); File rackScriptFile = new File(config.getRackConfigPath()); Preconditions.checkArgument(rackScriptFile.canRead(), "Could not find rack script specified by [" + config.getRackConfigPath() + "] and resolved to [" + rackScriptFile.getAbsolutePath() + "]"); runtime = JavaEmbedUtils.initialize(ImmutableList.of(rackScriptFile.getParentFile().getCanonicalPath()), createRuntimeConfig()); // don't inherit system settings for gems RubyHash env = runtime.evalScriptlet("ENV").convertToHash(); env.remove("GEM_HOME"); env.remove("GEM_PATH"); InputStream stream = Resources.getResource("io/airlift/rack.rb").openStream(); try { runtime.loadFile("rack.rb", stream, false); } finally { stream.close(); } IRubyObject builder = runtime.evalScriptlet("Airlift::RackServer::Builder.new"); rackApplication = adapter.callMethod(builder, "build", new IRubyObject[] { javaToRuby(runtime, rackScriptFile.getCanonicalPath()) }); }
public void destroy() { JavaEmbedUtils.terminate(_runtime); _runtime = null; super.destroy(); }
public Object call(Object recv, String method, Object[] args) throws BSFException { try { return JavaEmbedUtils.invokeMethod(runtime, recv, method, args, Object.class); } catch (Exception excptn) { throw new BSFException(BSFException.REASON_EXECUTION_ERROR, excptn.getMessage(), excptn); } }
private RubyInstanceConfig createRuntimeConfig() { RubyInstanceConfig config = new RubyInstanceConfig(); config.setCompatVersion(CompatVersion.RUBY1_8); ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); if (classLoader == null) { classLoader = ClassLoader.getSystemClassLoader(); } config.setClassCache(JavaEmbedUtils.createClassCache(classLoader)); URL resource = RubyInstanceConfig.class.getResource("/META-INF/jruby.home"); if (resource != null && resource.getProtocol().equals("jar")) { try { // http://weblogs.java.net/blog/2007/04/25/how-convert-javaneturl-javaiofile config.setJRubyHome(resource.toURI().getSchemeSpecificPart()); } catch (URISyntaxException e) { config.setJRubyHome(resource.getPath()); } } return config; }
public static IRubyObject javaToRuby(Ruby runtime, short value) { return javaToRuby(runtime, Short.valueOf(value)); } }