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

com.sun.grizzly.jruby.rack.MultiThreadedRackAdapter Maven / Gradle / Ivy

package com.sun.grizzly.jruby.rack;

import com.sun.grizzly.jruby.GrizzlyContext;
import com.sun.grizzly.jruby.RailsAdapter;
import org.jruby.Ruby;
import org.jruby.javasupport.JavaEmbedUtils;
import org.jruby.runtime.builtin.IRubyObject;

import java.util.logging.Level;

public class MultiThreadedRackAdapter implements RackAdapter {
    private RackApplication theApp;
    private final RackApplicationFactory myFactory;
    private final RailsAdapter adapter;

    // Starts this adapter up
    public MultiThreadedRackAdapter(RackApplicationFactory f, RailsAdapter adapter) {
        this.myFactory = f;
        this.adapter = adapter;
        RubyRuntimeFactory rubyfactory = new RubyRuntimeFactory();

        Ruby runtime = rubyfactory.makeRubyRuntime();
        //setup GrizzlyContext
        GrizzlyContext context = new GrizzlyContext(adapter);
        runtime.defineReadonlyVariable("$grizzly_context",
                JavaEmbedUtils.javaToRuby(runtime, context));
        IRubyObject loggerObj = JavaEmbedUtils.javaToRuby(runtime, context.getLogger());
        runtime.defineReadonlyVariable("$logger", loggerObj);
        String log_level = getEffectiveLogLevel();
        IRubyObject GF_log_level = JavaEmbedUtils.javaToRuby(runtime, log_level);
        runtime.defineReadonlyVariable("$glassfish_log_level", GF_log_level);
        runtime.defineReadonlyVariable("$root", JavaEmbedUtils.javaToRuby(runtime, adapter.getContextRoot()));


        try {
            theApp = myFactory.newApplication(runtime);
        } catch(RackInitializationException e) {
            theApp = myFactory.getErrorApplication(runtime);
            // Log something somewhere?
        }
    }

    // This method should be removed when glassfish/java.util.logger actually implements this
    private String getEffectiveLogLevel() {
        Level myLevel;
        java.util.logging.Logger pLog = adapter.getLogger();
        myLevel = pLog.getLevel();
        while (myLevel == null) {
            pLog = pLog.getParent();
            myLevel = pLog.getLevel();
        }
        return myLevel.getName();
    }

    // Shuts the adapter down
    public void shutdown() {
        // Nothing by default
    }// Returns an application suitable for processing a request
    public RackApplication getApp() {
        return theApp;
    }// Indicates that the app is done processing the request
    public void returnApp(RackApplication returned) {
        // Nothing by default
    }
    public RackApplicationFactory getFactory() {
        return myFactory;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy