com.sun.grizzly.jruby.rack.RubyRuntimeFactory Maven / Gradle / Ivy
package com.sun.grizzly.jruby.rack;
import org.jruby.Ruby;
import org.jruby.RubyInstanceConfig;
import org.jruby.javasupport.JavaEmbedUtils;
import org.jruby.util.ClassCache;
import java.util.ArrayList;
import com.sun.grizzly.jruby.RubyAdapter;
/**
* @author Jacob Kessler
*/
class RubyRuntimeFactory {
private int created = 1;
private final boolean isJRubyInstanceProvided;
public RubyRuntimeFactory() {
isJRubyInstanceProvided = System.getProperties().get("jruby.runtime.instance") != null;
}
public Ruby makeRubyRuntime() {
Ruby rt;
if(isJRubyInstanceProvided && created == 1){
rt = (Ruby) System.getProperties().get("jruby.runtime.instance");
}else{
RubyInstanceConfig config = new RubyInstanceConfig();
ArrayList libs = new ArrayList();
libs.add("META-INF/jruby.home/lib/ruby/site_ruby/1.8");
try { // try to set jruby home to jar file path
String binjruby = RubyInstanceConfig.class.getResource(
"/META-INF/jruby.home/bin/jruby").getFile();
config.setJRubyHome(binjruby.substring(0, binjruby.length() - 10));
} catch (Exception e) {
}
ClassCache cache = new ClassCache(RubyAdapter.class.getClassLoader()); // All of these methods are JRuby helper methods
config.setLoader(RubyRuntimeFactory.class.getClassLoader());
config.setClassCache(cache);
rt = JavaEmbedUtils.initialize(libs, config);
}
rt.defineReadonlyVariable("$runtimeNumber", JavaEmbedUtils.javaToRuby(rt, created));
created += 1;
return rt;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy