All Downloads are FREE. Search and download functionalities are using the official Maven repository.

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;

/**
 * @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(String jrubyHome) {
        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) {
                config.setJRubyHome(jrubyHome);
            }

            ClassCache cache = new ClassCache(RubyRuntimeFactory.class.getClassLoader()); // All of these methods are JRuby helper methods
            config.setLoader(RubyRuntimeFactory.class.getClassLoader());
            
            //enable jruby to report deeper stack trace for debugging purpose
            if(System.getProperty("jruby.debug") !=null){
                config.processArguments(new String[]{"-d"});
            }

            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