com.sun.grizzly.jruby.rack.RackApplicationPoolAdapter Maven / Gradle / Ivy
package com.sun.grizzly.jruby.rack;
import com.sun.grizzly.pool.PoolAdapter;
import com.sun.grizzly.http.SelectorThread;
import com.sun.grizzly.jruby.GrizzlyContext;
import com.sun.grizzly.jruby.RailsAdapter;
import org.jruby.javasupport.JavaEmbedUtils;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.Ruby;
import java.util.logging.Level;
/**
* Adapter to allow RackApplications to be pooled
*/
class RackApplicationPoolAdapter implements PoolAdapter {
private final RackApplicationFactory factory;
private final RubyRuntimeFactory rubyFactory = new RubyRuntimeFactory();
private final RailsAdapter adapter;
public RackApplicationPoolAdapter(RackApplicationFactory f, RailsAdapter adapter) {
this.factory = f;
this.adapter = adapter;
}
public RackApplication initializeObject() {
try {
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()));
RackApplication app = factory.newApplication(runtime);
return app;
} catch (RackInitializationException e) {
SelectorThread.logger().severe("Error initializing rack application!");
e.printStackTrace();
throw new IllegalStateException("Rack initialization failed!");
}
}
// 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();
}
public void dispose(RackApplication dead) {
dead.destroy();
}
public boolean validate(RackApplication toCheck) {
return true;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy