com.sun.grizzly.jruby.rack.RackApplicationChooser Maven / Gradle / Ivy
package com.sun.grizzly.jruby.rack;
import com.sun.grizzly.http.SelectorThread;
import com.sun.grizzly.jruby.RackGrizzlyAdapter;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.logging.Logger;
import org.glassfish.scripting.jruby.common.config.JRubyConfig;
/**
* @author Jacob Kessler
* Attempts to determine the RackApplicationFactory most suited to handle
* the given application directory
*/
public class RackApplicationChooser {
public static RackAdapter getFactory(String appPath, RackGrizzlyAdapter adapter) {
Logger theLog = adapter.getLogger();
JRubyConfig.Framework framework = adapter.config.framework();
String appType = framework.type();
RackAdapter rack;
rack = handleFramework(adapter, appPath, appType, theLog);
if (rack != null) {
return rack;
} else {
rack = handleStartupScript(framework.initScript().getAbsolutePath(), adapter, theLog);
if (rack != null) {
return rack;
}
}
SelectorThread.logger().severe("Framework autodetection failed! Please set jruby.applicationType deployment property to the path to a script that will start your framework");
throw new IllegalStateException("No framework to start!");
}
private static String inhale (File f, Logger theLog) {
String app = "";
if (f.exists()) {
try {
BufferedReader in = new BufferedReader(new FileReader(f));
String s;
while ((s = in.readLine()) != null) {
// read the file
app = app + s + "\n";
}
} catch (IOException e) {
theLog.severe("IO Exception determining framework for " + f.getAbsolutePath() + ": " + e);
}
} else {
theLog.severe("Was told to read nonexistant file " + f.getAbsolutePath());
}
return app;
}
private static RackAdapter handleFramework(RackGrizzlyAdapter adapter, String appPath, String appType, Logger theLog) {
// User has told us what to look for, so we don't need to guess
System.out.println("App type is " + appType);
if (appType.equalsIgnoreCase("rails")) {
RackApplicationFactory myFactory = new RailsApplicationFactory(adapter);
if (adapter.config.isMTSafe()) {
theLog.info("User has specified rails in threadsafe mode, initializing multithreaded rails");
return new MultiThreadedRackAdapter(myFactory, adapter);
} else {
// Check rails thread safety
boolean multithreaded = false;
String environment = "";
if ("development".compareTo(adapter.config.environment()) == 0) {
// Grab the development environment file
environment = inhale(new File(appPath+"/config/environments/development.rb"), theLog);
} else if ("production".compareTo(adapter.config.environment()) == 0) {
// if we are in production mode
environment = inhale(new File(appPath+"/config/environments/production.rb"), theLog);
} else if ("testing".compareTo(adapter.config.environment()) == 0) {
environment = inhale(new File(appPath+"/config/environments/testing.rb"), theLog);
}
if (environment.matches("(?s)(?m).*^[^#]*config\\.threadsafe!.*")) { // If there is a non-commented line with config.threadsafe! in it
multithreaded = true;
}
if (multithreaded) {
theLog.info("config.threadsafe sighted! All hands prepare for multithread mode!");
return new MultiThreadedRackAdapter(myFactory, adapter);
} else {
theLog.info("Rails not in thread-safe mode, starting in single-thread mode");
return new SingleThreadedRackAdapter(myFactory, adapter);
}
}
} else if (appType.equalsIgnoreCase("merb")) {
theLog.info("Initializing merb");
RackApplicationFactory myFactory = new MerbApplicationFactory(adapter);
return new MultiThreadedRackAdapter(myFactory, adapter);
} else if (appType.equalsIgnoreCase("sinatra")) {
theLog.info("Initializing Sinatra");
File script = adapter.config.framework().initScript();
if(script != null){
RackApplicationFactory myFactory = new SinatraApplicationFactory(adapter, script.getAbsolutePath());
return new MultiThreadedRackAdapter(myFactory, adapter);
}
}
return null;
}
private static RackAdapter handleStartupScript(String rackUp, RackGrizzlyAdapter adapter, Logger theLog) {
// User has specified a file to run.
RackApplicationFactory myFactory = new GenericApplicationFactory(rackUp, adapter);
if (adapter.config.isMTSafe()) {
theLog.info("Running a user-provided rackup script in thread-safe mode");
return new MultiThreadedRackAdapter(myFactory, adapter);
} else {
theLog.info("Running a user-provided rackup script in pooled mode");
return new SingleThreadedRackAdapter(myFactory, adapter);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy