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.util.logging.Logger;
import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

/**
 *  @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 {
    String creationString = "";

    public GenericApplicationFactory(String config, Logger logger, String root) {
        super(logger, root);
        try {
            File configScript = new File(config);
            if (configScript.exists()) {
                BufferedReader in = new BufferedReader(new FileReader(configScript));
                String s;
                while ((s = in.readLine()) != null) {
                    // read the file
                    creationString = creationString + s;
                }
            } else {
                logger.severe("Specified Rack configuration script could not be found, aborting");
            }
        } catch (IOException e) {
            logger.severe("IO error reading the configuration script!");
            e.printStackTrace();
        }
    }

    public IRubyObject createApplicationObject(Ruby runtime) {
        runtime.evalScriptlet("require 'rack/handler/grizzly'");
        IRubyObject app = runtime.evalScriptlet("creationString");
        runtime.defineReadonlyVariable("$glassfish_app", app);
        return runtime.evalScriptlet(
                "Rack::Handler::Grizzly.new($glassfish_app)");
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy