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

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

package com.sun.grizzly.jruby.rack;

import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.Ruby;

import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import com.sun.grizzly.jruby.RackGrizzlyAdapter;

/**
 *  @author Jacob Kessler
 *  Rack Application Factory that will run a user-created script to generate its output
 * The user script MUST return an initialized rack application object 
 */
public class GenericApplicationFactory extends DefaultRackApplicationFactory {
    private final File configScript;

    public GenericApplicationFactory(String config, RackGrizzlyAdapter adapter) {
        super(adapter);
        configScript = new File(config);

        if (!configScript.exists()) {
            String msg = "Specified Rack configuration script "+ configScript.getAbsolutePath() + "could not be found, aborting";
            logger.severe(msg);
            throw new IllegalArgumentException(msg);
        }
    }

    public IRubyObject createApplicationObject(Ruby runtime) {
        // Setup some imports
        runtime.evalScriptlet("require 'rubygems'");
        runtime.evalScriptlet("gem 'rack'");
        runtime.evalScriptlet("require 'jruby/rack'");
//        runtime.evalScriptlet("require 'jruby/rack/grizzly_helper'");
        runtime.evalScriptlet("require 'rack/handler/grizzly'");

        String path;
        try {
            path = configScript.getCanonicalPath();
        } catch (IOException e) {
            path = configScript.getAbsolutePath();
        }
        //IRubyObject app = runtime.executeScript(path, configScript.getName());
        IRubyObject app = runtime.evalScriptlet("load '"+path+"'");

        runtime.defineReadonlyVariable("$glassfish_app", app);
        return runtime.evalScriptlet(
                "Rack::Handler::Grizzly.new(Rack::Builder.new { $glassfish_app }.to_app))");
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy