Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Extract RackConfig and implementations

  • Loading branch information...
commit 3a1889ef5badc571f9d2ac410a693fee68d9bee5 1 parent b4db74d
@nicksieger authored
Showing with 571 additions and 359 deletions.
  1. +1 −0  .gitignore
  2. +2 −1  Rakefile
  3. +5 −8 src/main/java/org/jruby/rack/DefaultRackApplication.java
  4. +17 −29 src/main/java/org/jruby/rack/DefaultRackApplicationFactory.java
  5. +188 −0 src/main/java/org/jruby/rack/DefaultRackConfig.java
  6. +2 −2 src/main/java/org/jruby/rack/DefaultRackDispatcher.java
  7. +5 −36 src/main/java/org/jruby/rack/PoolingRackApplicationFactory.java
  8. +3 −0  src/main/java/org/jruby/rack/RackApplicationFactory.java
  9. +66 −0 src/main/java/org/jruby/rack/RackConfig.java
  10. +3 −2 src/main/java/org/jruby/rack/RackContext.java
  11. +1 −3 src/main/java/org/jruby/rack/RackDispatcher.java
  12. +6 −7 src/main/java/org/jruby/rack/RackFilter.java
  13. +2 −1  src/main/java/org/jruby/rack/RackInitializationException.java
  14. +6 −7 src/main/java/org/jruby/rack/RackServlet.java
  15. +12 −7 src/main/java/org/jruby/rack/RackServletContextListener.java
  16. +6 −4 src/main/java/org/jruby/rack/RackTag.java
  17. +3 −2 src/main/java/org/jruby/rack/input/RackBaseInput.java
  18. +3 −2 src/main/java/org/jruby/rack/input/RackNonRewindableInput.java
  19. +24 −13 src/main/java/org/jruby/rack/input/RackRewindableInput.java
  20. +11 −16 src/main/java/org/jruby/rack/jms/DefaultQueueManager.java
  21. +6 −2 src/main/java/org/jruby/rack/jms/QueueContextListener.java
  22. +2 −1  src/main/java/org/jruby/rack/jms/QueueManager.java
  23. +0 −4 src/main/java/org/jruby/rack/logging/CommonsLoggingLogger.java
  24. +0 −70 src/main/java/org/jruby/rack/logging/RackLoggerFactory.java
  25. +2 −2 src/main/java/org/jruby/rack/logging/ServletContextLogger.java
  26. +1 −5 src/main/java/org/jruby/rack/logging/Slf4jLogger.java
  27. +5 −2 src/main/java/org/jruby/rack/logging/StandardOutLogger.java
  28. +2 −3 src/main/java/org/jruby/rack/merb/MerbServletContextListener.java
  29. +4 −20 src/main/java/org/jruby/rack/rails/RailsServletContextListener.java
  30. +56 −0 src/main/java/org/jruby/rack/servlet/ServletRackConfig.java
  31. +21 −19 src/main/java/org/jruby/rack/servlet/ServletRackContext.java
  32. +5 −3 src/main/java/org/jruby/rack/servlet/ServletRackEnvironment.java
  33. +5 −3 src/main/java/org/jruby/rack/servlet/ServletRackResponseEnvironment.java
  34. +14 −29 src/spec/ruby/rack/application_spec.rb
  35. +63 −0 src/spec/ruby/rack/config_spec.rb
  36. +2 −1  src/spec/ruby/rack/filter_spec.rb
  37. +0 −46 src/spec/ruby/rack/logging_spec.rb
  38. +8 −5 src/spec/ruby/rack/servlet_context_listener_spec.rb
  39. +2 −1  src/spec/ruby/rack/servlet_spec.rb
  40. +2 −1  src/spec/ruby/rack/tag_spec.rb
  41. +5 −2 src/spec/ruby/spec_helper.rb
View
1  .gitignore
@@ -10,3 +10,4 @@ examples/rails3/rails3.war
examples/camping/camping.war
examples/sinatra/sinatra.war
nbproject/private/*
+/.rakeTasks
View
3  Rakefile
@@ -32,7 +32,8 @@ directory 'target/classes'
desc "Compile java classes"
task :compile => "target/classes" do |t|
- sh 'javac -Xlint:deprecation -Xlint:unchecked -classpath "' + compile_classpath.join(File::PATH_SEPARATOR) + '" -source 1.5 ' +
+ debug = ENV['DEBUG'] ? '-g' : ''
+ sh "javac -Xlint:deprecation -Xlint:unchecked #{debug} -classpath \"" + compile_classpath.join(File::PATH_SEPARATOR) + '" -source 1.5 ' +
'-target 1.5 -d ' + t.prerequisites.first + ' ' + Dir["src/main/java/**/*.java"].join(" ")
end
View
13 src/main/java/org/jruby/rack/DefaultRackApplication.java
@@ -7,29 +7,26 @@
package org.jruby.rack;
-import java.io.IOException;
import org.jruby.Ruby;
+import org.jruby.RubyObjectAdapter;
import org.jruby.exceptions.RaiseException;
import org.jruby.javasupport.JavaEmbedUtils;
-import org.jruby.RubyObjectAdapter;
-import org.jruby.runtime.builtin.IRubyObject;
-import org.jruby.util.SafePropertyAccessor;
-
import org.jruby.rack.input.RackBaseInput;
import org.jruby.rack.input.RackNonRewindableInput;
import org.jruby.rack.input.RackRewindableInput;
+import org.jruby.runtime.builtin.IRubyObject;
+
+import java.io.IOException;
/**
*
* @author nicksieger
*/
public class DefaultRackApplication implements RackApplication {
- private final boolean rewindable;
private final RubyObjectAdapter adapter = JavaEmbedUtils.newObjectAdapter();
private IRubyObject application;
public DefaultRackApplication() {
- rewindable = SafePropertyAccessor.getBoolean("jruby.rack.input.rewindable", true);
}
public RackResponse call(final RackEnvironment env) {
@@ -69,7 +66,7 @@ public IRubyObject __call(final IRubyObject env) {
}
private RackBaseInput createRackInput(Ruby runtime, RackEnvironment env) throws IOException {
- if (rewindable) {
+ if (env.getContext().getConfig().isRewindable()) {
return new RackRewindableInput(runtime, env);
} else {
return new RackNonRewindableInput(runtime, env);
View
46 src/main/java/org/jruby/rack/DefaultRackApplicationFactory.java
@@ -7,29 +7,23 @@
package org.jruby.rack;
+import org.jruby.Ruby;
+import org.jruby.RubyInstanceConfig;
+import org.jruby.exceptions.RaiseException;
+import org.jruby.javasupport.JavaEmbedUtils;
+import org.jruby.runtime.builtin.IRubyObject;
+import org.jruby.util.ClassCache;
+
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
-import java.net.URL;
import java.net.URISyntaxException;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Iterator;
-import java.util.Map;
-import java.util.Set;
+import java.net.URL;
+import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
-import org.jruby.CompatVersion;
-import org.jruby.Ruby;
-import org.jruby.RubyInstanceConfig;
-import org.jruby.exceptions.RaiseException;
-import org.jruby.javasupport.JavaEmbedUtils;
-import org.jruby.runtime.builtin.IRubyObject;
-import org.jruby.util.ClassCache;
-import java.net.URLDecoder;
-
/**
*
* @author nicksieger
@@ -77,6 +71,10 @@ public void destroy() {
errorApplication = null;
}
+ public RackContext getRackContext() {
+ return rackContext;
+ }
+
public Ruby newRuntime() throws RackInitializationException {
try {
Ruby runtime = (Ruby) rackContext.getAttribute("jruby.runtime");
@@ -103,21 +101,11 @@ private void setupJRubyManagement() {
}
}
- private static final Pattern COMPAT_VERSION = Pattern.compile("1[._]([89])");
-
private RubyInstanceConfig createRuntimeConfig() {
RubyInstanceConfig config = new RubyInstanceConfig();
config.setClassCache(classCache);
- if (rackContext.getInitParameter("jruby.compat.version") != null) {
- Matcher matcher = COMPAT_VERSION.matcher(rackContext.getInitParameter("jruby.compat.version"));
- if (matcher.find()) {
- String version = matcher.group(1);
- if (version.equals("8")) {
- config.setCompatVersion(CompatVersion.RUBY1_8);
- } else if (version.equals("9")) {
- config.setCompatVersion(CompatVersion.RUBY1_9);
- }
- }
+ if (rackContext.getConfig().getCompatVersion() != null) {
+ config.setCompatVersion(rackContext.getConfig().getCompatVersion());
}
try { // try to set jruby home to jar file path
@@ -282,12 +270,12 @@ private String inputStreamToString(InputStream stream) {
private String findRackupScript() {
rackupLocation = "<web.xml>";
- String rackup = rackContext.getInitParameter("rackup");
+ String rackup = rackContext.getConfig().getRackup();
if (rackup != null) {
return rackup;
}
- rackup = rackContext.getInitParameter("rackup.path");
+ rackup = rackContext.getConfig().getRackupPath();
if (rackup == null) {
rackup = findConfigRuPathInSubDirectories("/WEB-INF/", 1);
View
188 src/main/java/org/jruby/rack/DefaultRackConfig.java
@@ -0,0 +1,188 @@
+package org.jruby.rack;
+
+import org.jruby.CompatVersion;
+import org.jruby.rack.input.RackRewindableInput;
+import org.jruby.rack.logging.StandardOutLogger;
+import org.jruby.util.SafePropertyAccessor;
+
+import java.lang.reflect.Constructor;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * Base implementation of RackConfig that retrieves settings from system properties.
+ */
+public class DefaultRackConfig implements RackConfig {
+ private static final Pattern COMPAT_VERSION = Pattern.compile("1[._]([89])");
+ private static final Map<String,String> loggerTypes = Collections.unmodifiableMap(new HashMap<String, String>() {{
+ put("commons_logging", "org.jruby.rack.logging.CommonsLoggingLogger");
+ put("clogging", "org.jruby.rack.logging.CommonsLoggingLogger");
+ put("slf4j", "org.jruby.rack.logging.Slf4jLogger");
+ put("servlet_context", "org.jruby.rack.logging.ServletContextLogger");
+ put("stdout", "org.jruby.rack.logging.StandardOutLogger");
+ }});
+
+ private RackLogger logger;
+ private boolean quiet = false;
+
+ public CompatVersion getCompatVersion() {
+ String versionString = getProperty("jruby.compat.version");
+ if (versionString != null) {
+ Matcher matcher = COMPAT_VERSION.matcher(versionString);
+ if (matcher.find()) {
+ String version = matcher.group(1);
+ if (version.equals("8")) {
+ return CompatVersion.RUBY1_8;
+ } else if (version.equals("9")) {
+ return CompatVersion.RUBY1_9;
+ }
+ }
+ }
+ return null;
+ }
+
+ public String getRackup() {
+ return getProperty("rackup");
+ }
+
+ public String getRackupPath() {
+ return getProperty("rackup.path");
+ }
+
+ public Integer getRuntimeTimeoutSeconds() {
+ return getPositiveInteger("jruby.runtime.timeout.sec");
+ }
+
+ public Integer getNumInitializerThreads() {
+ return getPositiveInteger("jruby.runtime.initializer.threads");
+ }
+
+ public RackLogger getLogger() {
+ if (logger == null) {
+ String loggerClass = getLoggerClassName();
+ if (loggerTypes.containsKey(loggerClass)) {
+ loggerClass = loggerTypes.get(loggerClass);
+ }
+
+ logger = createLogger(loggerClass);
+ }
+ return logger;
+ }
+
+ protected RackLogger createLogger(String loggerClass) {
+ try {
+ Class<?> c = Class.forName(loggerClass);
+ try {
+ Constructor<?> ctor = c.getConstructor(new Class<?>[] {String.class});
+ return (RackLogger) ctor.newInstance(new Object[] {getLoggerName()});
+ } catch (Exception tryAgain) {
+ return (RackLogger) c.newInstance();
+ }
+ } catch (Exception e) {
+ if (!quiet) {
+ System.err.println("Error loading logger: " + loggerClass);
+ e.printStackTrace(System.err);
+ }
+ return new StandardOutLogger(null);
+ }
+ }
+
+ public boolean isSlashIndex() {
+ return getBoolean("jruby.rack.slash.index", false);
+ }
+
+ public boolean isBackgroundSpooling() {
+ return getBoolean("jruby.rack.background.spool", false);
+ }
+
+ public String getJmsConnectionFactory() {
+ return getProperty("jms.connection.factory");
+ }
+
+ public String getJmsJndiProperties() {
+ return getProperty("jms.jndi.properties");
+ }
+
+ public int getMemoryBufferSize() {
+ Integer i = getPositiveInteger("jruby.rack.request.size.threshold.bytes");
+ if (i == null) {
+ i = RackRewindableInput.getDefaultThreshold();
+ }
+ return i;
+ }
+
+ public boolean isSerialInitialization() {
+ return getBoolean("jruby.init.serial", false);
+ }
+
+ public String getLoggerName() {
+ return getProperty("jruby.rack.logging.name", "jruby.rack");
+ }
+
+ public String getLoggerClassName() {
+ return getProperty("jruby.rack.logging", "servlet_context");
+ }
+
+ public Integer getInitialRuntimes() {
+ return getRangeValue("min", "minIdle");
+ }
+
+ public Integer getMaximumRuntimes() {
+ return getRangeValue("max", "maxActive");
+ }
+
+ public boolean isRewindable() {
+ return getBoolean("jruby.rack.input.rewindable", true);
+ }
+
+ public String getProperty(String key) {
+ return getProperty(key, null);
+ }
+
+ public String getProperty(String key, String defaultValue) {
+ return SafePropertyAccessor.getProperty(key, defaultValue);
+ }
+
+ public boolean isQuiet() {
+ return quiet;
+ }
+
+ public void setQuiet(boolean quiet) {
+ this.quiet = quiet;
+ }
+
+ private Integer getRangeValue(String end, String gsValue) {
+ Integer v = getPositiveInteger("jruby." + end + ".runtimes");
+ if (v == null) {
+ v = getPositiveInteger("jruby.pool." + gsValue);
+ }
+ if (v == null) {
+ getLogger().log("Warning: no " + end + " runtimes specified.");
+ } else {
+ getLogger().log("Info: received " + end + " runtimes = " + v);
+ }
+ return v;
+ }
+
+ private Integer getPositiveInteger(String string) {
+ try {
+ int i = Integer.parseInt(getProperty(string));
+ if (i > 0) {
+ return new Integer(i);
+ }
+ } catch (Exception e) {
+ }
+ return null;
+ }
+
+ private boolean getBoolean(String key, boolean defValue) {
+ try {
+ return Boolean.parseBoolean(getProperty(key));
+ } catch (Exception e) {
+ }
+ return defValue;
+ }
+}
View
4 src/main/java/org/jruby/rack/DefaultRackDispatcher.java
@@ -16,8 +16,8 @@
public class DefaultRackDispatcher implements RackDispatcher {
private RackContext context;
- public DefaultRackDispatcher(RackContext servletContext) {
- this.context = servletContext;
+ public DefaultRackDispatcher(RackContext rackContext) {
+ this.context = rackContext;
}
public void process(RackEnvironment request, RackResponseEnvironment response)
View
41 src/main/java/org/jruby/rack/PoolingRackApplicationFactory.java
@@ -49,14 +49,15 @@ public void init(final RackContext rackContext) throws RackInitializationExcepti
this.rackContext = rackContext;
realFactory.init(rackContext);
- Integer specifiedTimeout = getPositiveInteger("jruby.runtime.timeout.sec");
+ RackConfig config = rackContext.getConfig();
+ Integer specifiedTimeout = config.getRuntimeTimeoutSeconds();
if (specifiedTimeout != null) {
timeout = specifiedTimeout.longValue();
}
rackContext.log("Info: using runtime pool timeout of " + timeout + " seconds");
- initial = getInitial();
- maximum = getMaximum();
+ initial = config.getInitialRuntimes();
+ maximum = config.getMaximumRuntimes();
if (maximum != null) {
if (initial != null && initial > maximum) {
maximum = initial;
@@ -139,7 +140,7 @@ protected void fillInitialPool() throws RackInitializationException {
}
protected void launchInitializerThreads(final Queue<RackApplication> apps) {
- Integer numThreads = getPositiveInteger("jruby.runtime.initializer.threads");
+ Integer numThreads = rackContext.getConfig().getNumInitializerThreads();
if (numThreads == null) {
numThreads = 4;
}
@@ -192,36 +193,4 @@ public void waitForNextAvailable(long timeout) {
} catch (InterruptedException ex) {
}
}
-
- private Integer getInitial() {
- return getRangeValue("min", "minIdle");
- }
-
- private Integer getMaximum() {
- return getRangeValue("max", "maxActive");
- }
-
- private Integer getRangeValue(String end, String gsValue) {
- Integer v = getPositiveInteger("jruby." + end + ".runtimes");
- if (v == null) {
- v = getPositiveInteger("jruby.pool." + gsValue);
- }
- if (v == null) {
- rackContext.log("Warning: no " + end + " runtimes specified.");
- } else {
- rackContext.log("Info: received " + end + " runtimes = " + v);
- }
- return v;
- }
-
- private Integer getPositiveInteger(String string) {
- try {
- int i = Integer.parseInt(rackContext.getInitParameter(string));
- if (i > 0) {
- return new Integer(i);
- }
- } catch (Exception e) {
- }
- return null;
- }
}
View
3  src/main/java/org/jruby/rack/RackApplicationFactory.java
@@ -12,6 +12,9 @@
* @author nicksieger
*/
public interface RackApplicationFactory {
+ String RACK_CONTEXT = "rack.context";
+ String FACTORY = "rack.factory";
+
/** Initialize the factory. */
void init(RackContext rackContext) throws RackInitializationException;
/** Create a new, uninitialized application. The resulting object must
View
66 src/main/java/org/jruby/rack/RackConfig.java
@@ -0,0 +1,66 @@
+/*
+ * Copyright (c) 2010-2011 Engine Yard, Inc.
+ * Copyright (c) 2007-2009 Sun Microsystems, Inc.
+ * This source code is available under the MIT license.
+ * See the file LICENSE.txt for details.
+ */
+
+package org.jruby.rack;
+
+import org.jruby.CompatVersion;
+
+/**
+ * Centralized interface for configuration options used by JRuby-Rack. JRuby-Rack can either be configured by setting the key-value pairs as init parameters in the servlet context or as VM-wide system properties.
+ */
+public interface RackConfig {
+ /** Return the Ruby version that JRuby should run. */
+ CompatVersion getCompatVersion();
+
+ /** Return the rackup Ruby script to be used to launch the application. */
+ String getRackup();
+
+ /** Return the path to the Rackup script to be used to launch the application. */
+ String getRackupPath();
+
+ /** Return the configured amount of time before runtime acquisition times out (in seconds). */
+ Integer getRuntimeTimeoutSeconds();
+
+ /** Get the number of initial runtimes, or null if unspecified. */
+ Integer getInitialRuntimes();
+
+ /** Get the number of maximum runtimes, or null if unspecified. */
+ Integer getMaximumRuntimes();
+
+ /** Get the number of initializer threads, or null if unspecified. */
+ Integer getNumInitializerThreads();
+
+ /** Create a logger based on the configuration. */
+ RackLogger getLogger();
+
+ /** Return true if "slash" should be mapped to the index or index.html URI. */
+ boolean isSlashIndex();
+
+ /** Return true if large request bodies should be spooled in the background. */
+ boolean isBackgroundSpooling();
+
+ /** Return the JNDI name of the JMS connection factory.*/
+ String getJmsConnectionFactory();
+
+ /** Return the JNDI properties for */
+ String getJmsJndiProperties();
+
+ /** Return the threshold/size of the in-memory buffer used for request bodies. */
+ int getMemoryBufferSize();
+
+ /** Return true if the runtimes should be initialized in serial (e.g, if threads cannot be created). */
+ boolean isSerialInitialization();
+
+ /** Return true if the request body is rewindable. */
+ boolean isRewindable();
+
+ /** General property retrieval for custom configuration values. */
+ String getProperty(String key);
+
+ /** General property retrieval for custom configuration values. */
+ String getProperty(String key, String defaultValue);
+}
View
5 src/main/java/org/jruby/rack/RackContext.java
@@ -7,10 +7,10 @@
package org.jruby.rack;
-import java.util.Set;
-import java.net.URL;
import java.io.InputStream;
import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Set;
/**
* Abstraction of an application context to make parts of the library
@@ -25,4 +25,5 @@
URL getResource(String path) throws MalformedURLException;
InputStream getResourceAsStream(String path);
Object getAttribute(String name);
+ RackConfig getConfig();
}
View
4 src/main/java/org/jruby/rack/RackDispatcher.java
@@ -7,10 +7,8 @@
package org.jruby.rack;
-import java.io.IOException;
import javax.servlet.ServletException;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
/**
*
View
13 src/main/java/org/jruby/rack/RackFilter.java
@@ -7,16 +7,15 @@
package org.jruby.rack;
-import java.io.IOException;
+import org.jruby.rack.servlet.ServletRackEnvironment;
+import org.jruby.rack.servlet.ServletRackResponseEnvironment;
+
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
-
-import org.jruby.rack.servlet.ServletRackContext;
-import org.jruby.rack.servlet.ServletRackEnvironment;
-import org.jruby.rack.servlet.ServletRackResponseEnvironment;
+import java.io.IOException;
/**
*
@@ -39,13 +38,13 @@ public RackFilter(RackDispatcher dispatcher, RackContext context) {
/** Construct a new dispatcher with the servlet context */
public void init(FilterConfig config) throws ServletException {
- this.context = new ServletRackContext(config.getServletContext());
+ this.context = (RackContext) config.getServletContext().getAttribute(RackApplicationFactory.RACK_CONTEXT);
this.dispatcher = new DefaultRackDispatcher(this.context);
this.convertSlashToIndex = shouldConvertSlashToIndex(config.getServletContext());
}
private boolean shouldConvertSlashToIndex(ServletContext context) {
- return this.context.getInitParameter("jruby.rack.slash.index") != null
+ return this.context.getConfig().isSlashIndex()
|| context.getServerInfo().contains("jetty"); // JRUBY_RACK-35
}
View
3  src/main/java/org/jruby/rack/RackInitializationException.java
@@ -7,9 +7,10 @@
package org.jruby.rack;
+import org.jruby.exceptions.RaiseException;
+
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
-import org.jruby.exceptions.RaiseException;
public class RackInitializationException extends Exception {
public RackInitializationException(RaiseException re) {
View
13 src/main/java/org/jruby/rack/RackServlet.java
@@ -7,7 +7,9 @@
package org.jruby.rack;
-import java.io.IOException;
+import org.jruby.rack.servlet.ServletRackEnvironment;
+import org.jruby.rack.servlet.ServletRackResponseEnvironment;
+
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
@@ -15,10 +17,7 @@
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-
-import org.jruby.rack.servlet.ServletRackContext;
-import org.jruby.rack.servlet.ServletRackEnvironment;
-import org.jruby.rack.servlet.ServletRackResponseEnvironment;
+import java.io.IOException;
/**
*
@@ -26,7 +25,7 @@
*/
public class RackServlet extends HttpServlet {
private RackDispatcher dispatcher;
- private ServletRackContext rackContext;
+ private RackContext rackContext;
/** Default ctor, used by servlet container */
public RackServlet() {
@@ -40,7 +39,7 @@ public RackServlet(RackDispatcher dispatcher) {
@Override
public void init(ServletConfig config) {
if (dispatcher == null) {
- rackContext = new ServletRackContext(config.getServletContext());
+ rackContext = (RackContext) config.getServletContext().getAttribute(RackApplicationFactory.RACK_CONTEXT);
dispatcher = new DefaultRackDispatcher(rackContext);
}
}
View
19 src/main/java/org/jruby/rack/RackServletContextListener.java
@@ -7,7 +7,9 @@
package org.jruby.rack;
+import org.jruby.rack.servlet.ServletRackConfig;
import org.jruby.rack.servlet.ServletRackContext;
+
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
@@ -17,7 +19,6 @@
* @author nicksieger
*/
public class RackServletContextListener implements ServletContextListener {
- public static final String FACTORY_KEY = "rack.factory";
private final RackApplicationFactory factory;
public RackServletContextListener() {
@@ -34,10 +35,13 @@ public RackServletContextListener(RackApplicationFactory factoryForTest) {
public void contextInitialized(ServletContextEvent ctxEvent) {
ServletContext ctx = ctxEvent.getServletContext();
- final RackApplicationFactory fac = newApplicationFactory(ctx);
- ctx.setAttribute(FACTORY_KEY, fac);
+ ServletRackConfig config = new ServletRackConfig(ctx);
+ final RackApplicationFactory fac = newApplicationFactory(config);
+ ctx.setAttribute(RackApplicationFactory.FACTORY, fac);
+ ServletRackContext rackContext = new ServletRackContext(config);
+ ctx.setAttribute(RackApplicationFactory.RACK_CONTEXT, rackContext);
try {
- fac.init(new ServletRackContext(ctx));
+ fac.init(rackContext);
} catch (Exception ex) {
ctx.log("Error: application initialization failed", ex);
}
@@ -46,14 +50,15 @@ public void contextInitialized(ServletContextEvent ctxEvent) {
public void contextDestroyed(ServletContextEvent ctxEvent) {
ServletContext ctx = ctxEvent.getServletContext();
final RackApplicationFactory fac =
- (RackApplicationFactory) ctx.getAttribute(FACTORY_KEY);
+ (RackApplicationFactory) ctx.getAttribute(RackApplicationFactory.FACTORY);
if (fac != null) {
fac.destroy();
- ctx.removeAttribute(FACTORY_KEY);
+ ctx.removeAttribute(RackApplicationFactory.FACTORY);
+ ctx.removeAttribute(RackApplicationFactory.RACK_CONTEXT);
}
}
- protected RackApplicationFactory newApplicationFactory(ServletContext context) {
+ protected RackApplicationFactory newApplicationFactory(RackConfig config) {
if (factory != null) {
return factory;
}
View
10 src/main/java/org/jruby/rack/RackTag.java
@@ -7,12 +7,12 @@
package org.jruby.rack;
-import org.jruby.rack.servlet.ServletRackContext;
import org.jruby.rack.servlet.ServletRackEnvironment;
+
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
-import javax.servlet.jsp.tagext.TagSupport;
import javax.servlet.jsp.JspException;
+import javax.servlet.jsp.tagext.TagSupport;
public class RackTag extends TagSupport {
private String path;
@@ -30,7 +30,9 @@ public void setParams(String params) {
public int doEndTag() throws JspException {
try {
RackApplicationFactory factory = (RackApplicationFactory)
- pageContext.getServletContext().getAttribute(RackServletContextListener.FACTORY_KEY);
+ pageContext.getServletContext().getAttribute(RackApplicationFactory.FACTORY);
+ RackContext context = (RackContext)
+ pageContext.getServletContext().getAttribute(RackApplicationFactory.RACK_CONTEXT);
RackApplication app = factory.getApplication();
try {
final HttpServletRequest request =
@@ -41,7 +43,7 @@ public int doEndTag() throws JspException {
@Override public String getQueryString() { return params; }
@Override public String getServletPath() { return ""; }
};
- RackResponse result = app.call(new ServletRackEnvironment(request, new ServletRackContext(pageContext.getServletContext())));
+ RackResponse result = app.call(new ServletRackEnvironment(request, context));
pageContext.getOut().write(result.getBody());
} finally {
factory.finishedWithApplication(app);
View
5 src/main/java/org/jruby/rack/input/RackBaseInput.java
@@ -7,8 +7,6 @@
package org.jruby.rack.input;
-import java.io.IOException;
-import java.io.InputStream;
import org.jruby.Ruby;
import org.jruby.RubyClass;
import org.jruby.RubyModule;
@@ -23,6 +21,9 @@
import org.jruby.runtime.Visibility;
import org.jruby.runtime.builtin.IRubyObject;
+import java.io.IOException;
+import java.io.InputStream;
+
/**
*
* @author nicksieger
View
5 src/main/java/org/jruby/rack/input/RackNonRewindableInput.java
@@ -7,8 +7,6 @@
package org.jruby.rack.input;
-import java.io.FilterInputStream;
-import java.io.IOException;
import org.jruby.Ruby;
import org.jruby.RubyClass;
import org.jruby.RubyIO;
@@ -17,6 +15,9 @@
import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.builtin.IRubyObject;
+import java.io.FilterInputStream;
+import java.io.IOException;
+
/**
*
* @author nicksieger
View
37 src/main/java/org/jruby/rack/input/RackRewindableInput.java
@@ -7,25 +7,32 @@
package org.jruby.rack.input;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.nio.ByteBuffer;
-import java.nio.channels.*;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-import java.util.concurrent.Future;
-import java.util.concurrent.ThreadFactory;
-
-import org.jruby.*;
+import org.jruby.Ruby;
+import org.jruby.RubyClass;
+import org.jruby.RubyString;
+import org.jruby.RubyTempfile;
import org.jruby.anno.JRubyMethod;
+import org.jruby.rack.RackConfig;
import org.jruby.rack.RackEnvironment;
+import org.jruby.rack.RackInput;
import org.jruby.runtime.Block;
import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.Visibility;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.ByteList;
-import org.jruby.rack.RackInput;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.channels.Channels;
+import java.nio.channels.ClosedChannelException;
+import java.nio.channels.FileChannel;
+import java.nio.channels.ReadableByteChannel;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.ThreadFactory;
/**
* Suitable env['rack.input'] object for servlet environments, allowing to rewind the
@@ -74,8 +81,12 @@ public RackRewindableInput(Ruby runtime, RubyClass klass) {
public RackRewindableInput(Ruby runtime, RackEnvironment environment) throws IOException {
super(runtime, getRackRewindableInputClass(runtime), environment);
- if (environment != null && environment.getContext().getInitParameter("jruby.rack.background.spool") != null) {
- spooler = backgroundSpooler;
+ if (environment != null) {
+ RackConfig config = environment.getContext().getConfig();
+ if (config.isBackgroundSpooling()) {
+ spooler = backgroundSpooler;
+ }
+ threshold = config.getMemoryBufferSize();
}
}
View
27 src/main/java/org/jruby/rack/jms/DefaultQueueManager.java
@@ -7,20 +7,6 @@
package org.jruby.rack.jms;
-import java.io.ByteArrayInputStream;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.Map;
-import java.util.Properties;
-import javax.jms.Message;
-import javax.jms.MessageListener;
-import javax.jms.Connection;
-import javax.jms.ConnectionFactory;
-import javax.jms.Destination;
-import javax.jms.MessageConsumer;
-import javax.jms.Session;
-import javax.naming.Context;
-import javax.naming.InitialContext;
import org.jruby.Ruby;
import org.jruby.RubyModule;
import org.jruby.RubyObjectAdapter;
@@ -31,6 +17,15 @@
import org.jruby.rack.RackContext;
import org.jruby.runtime.builtin.IRubyObject;
+import javax.jms.*;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import java.io.ByteArrayInputStream;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Properties;
+
/**
*
* @author nicksieger
@@ -53,10 +48,10 @@ public DefaultQueueManager(ConnectionFactory qcf, Context ctx) {
public void init(RackContext context) throws Exception {
this.context = context;
- String jndiName = context.getInitParameter("jms.connection.factory");
+ String jndiName = context.getConfig().getJmsConnectionFactory();
if (jndiName != null && connectionFactory == null) {
Properties properties = new Properties();
- String jndiProperties = context.getInitParameter("jms.jndi.properties");
+ String jndiProperties = context.getConfig().getJmsJndiProperties();
if (jndiProperties != null) {
properties.load(new ByteArrayInputStream(jndiProperties.getBytes("UTF-8")));
}
View
8 src/main/java/org/jruby/rack/jms/QueueContextListener.java
@@ -7,10 +7,12 @@
package org.jruby.rack.jms;
+import org.jruby.rack.RackApplicationFactory;
+import org.jruby.rack.RackContext;
+
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
-import org.jruby.rack.servlet.ServletRackContext;
/**
*
@@ -29,9 +31,11 @@ public QueueContextListener(QueueManagerFactory qmf) {
public void contextInitialized(ServletContextEvent event) {
final ServletContext servletContext = event.getServletContext();
+ RackContext rackContext = (RackContext) servletContext.getAttribute(RackApplicationFactory.RACK_CONTEXT);
+
try {
QueueManager qm = newQueueManagerFactory().newQueueManager();
- qm.init(new ServletRackContext(servletContext));
+ qm.init(rackContext);
servletContext.setAttribute(QueueManager.MGR_KEY, qm);
} catch (Exception e) {
servletContext.log("Error initializing queue manager:" + e.getMessage(), e);
View
3  src/main/java/org/jruby/rack/jms/QueueManager.java
@@ -7,9 +7,10 @@
package org.jruby.rack.jms;
-import javax.jms.ConnectionFactory;
import org.jruby.rack.RackContext;
+import javax.jms.ConnectionFactory;
+
/**
*
* @author nicksieger
View
4 src/main/java/org/jruby/rack/logging/CommonsLoggingLogger.java
@@ -15,10 +15,6 @@
public class CommonsLoggingLogger implements RackLogger {
private Log logger;
- public CommonsLoggingLogger() {
- this(RackLoggerFactory.defaultLogName());
- }
-
public CommonsLoggingLogger(String loggerName) {
setLoggerName(loggerName);
}
View
70 src/main/java/org/jruby/rack/logging/RackLoggerFactory.java
@@ -1,70 +0,0 @@
-/*
- * Copyright (c) 2010-2011 Engine Yard, Inc.
- * Copyright (c) 2007-2009 Sun Microsystems, Inc.
- * This source code is available under the MIT license.
- * See the file LICENSE.txt for details.
- */
-
-package org.jruby.rack.logging;
-
-import javax.servlet.ServletContext;
-
-import org.jruby.rack.RackLogger;
-import org.jruby.util.SafePropertyAccessor;
-import java.util.Map;
-import java.util.HashMap;
-import java.util.Collections;
-
-public class RackLoggerFactory {
- private static final String LOGGING_KEY = "jruby.rack.logging";
- private static final Map<String,String> loggerTypes = Collections.unmodifiableMap(new HashMap<String,String>() {{
- put("commons_logging", "org.jruby.rack.logging.CommonsLoggingLogger");
- put("clogging", "org.jruby.rack.logging.CommonsLoggingLogger");
- put("slf4j", "org.jruby.rack.logging.Slf4jLogger");
- put("servlet_context", "org.jruby.rack.logging.ServletContextLogger");
- put("stdout", "org.jruby.rack.logging.StandardOutLogger");
- }});
-
- private final boolean quiet;
-
- public RackLoggerFactory(boolean quiet) {
- this.quiet = quiet;
- }
-
- public RackLoggerFactory() {
- this(false);
- }
-
- public static final String defaultLogName() {
- return SafePropertyAccessor.getProperty(LOGGING_KEY + ".name", "jruby.rack");
- }
-
- public RackLogger getLogger(ServletContext context) {
- String loggerClass = context.getInitParameter(LOGGING_KEY);
-
- if (loggerClass == null) {
- loggerClass = SafePropertyAccessor.getProperty(LOGGING_KEY, "servlet_context");
- }
-
- if (loggerTypes.containsKey(loggerClass)) {
- loggerClass = loggerTypes.get(loggerClass);
- }
-
- RackLogger logger;
- try {
- Class c = Class.forName(loggerClass);
- if (c.equals(ServletContextLogger.class)) {
- logger = new ServletContextLogger(context);
- } else {
- logger = (RackLogger) c.newInstance();
- }
- } catch (Exception e) {
- if (!quiet) {
- System.err.println("Error loading logger: " + loggerClass);
- e.printStackTrace(System.err);
- }
- logger = new StandardOutLogger();
- }
- return logger;
- }
-}
View
4 src/main/java/org/jruby/rack/logging/ServletContextLogger.java
@@ -7,10 +7,10 @@
package org.jruby.rack.logging;
-import javax.servlet.ServletContext;
-
import org.jruby.rack.RackLogger;
+import javax.servlet.ServletContext;
+
public class ServletContextLogger implements RackLogger {
private ServletContext context;
View
6 src/main/java/org/jruby/rack/logging/Slf4jLogger.java
@@ -7,17 +7,13 @@
package org.jruby.rack.logging;
+import org.jruby.rack.RackLogger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import org.jruby.rack.RackLogger;
public class Slf4jLogger implements RackLogger {
private Logger logger;
- public Slf4jLogger() {
- this(RackLoggerFactory.defaultLogName());
- }
-
public Slf4jLogger(String loggerName) {
setLoggerName(loggerName);
}
View
7 src/main/java/org/jruby/rack/logging/StandardOutLogger.java
@@ -7,11 +7,14 @@
package org.jruby.rack.logging;
-import static java.lang.System.out;
-
import org.jruby.rack.RackLogger;
+import static java.lang.System.out;
+
public class StandardOutLogger implements RackLogger {
+ public StandardOutLogger(String ignored) {
+ }
+
public void log(String message) {
out.println(message);
out.flush();
View
5 src/main/java/org/jruby/rack/merb/MerbServletContextListener.java
@@ -7,9 +7,8 @@
package org.jruby.rack.merb;
-import javax.servlet.ServletContext;
-
import org.jruby.rack.RackApplicationFactory;
+import org.jruby.rack.RackConfig;
import org.jruby.rack.RackServletContextListener;
import org.jruby.rack.SharedRackApplicationFactory;
@@ -19,7 +18,7 @@
*/
public class MerbServletContextListener extends RackServletContextListener {
@Override
- protected RackApplicationFactory newApplicationFactory(ServletContext context) {
+ protected RackApplicationFactory newApplicationFactory(RackConfig config) {
return new SharedRackApplicationFactory(
new MerbRackApplicationFactory()
);
View
24 src/main/java/org/jruby/rack/rails/RailsServletContextListener.java
@@ -7,13 +7,7 @@
package org.jruby.rack.rails;
-import javax.servlet.ServletContext;
-
-import org.jruby.rack.PoolingRackApplicationFactory;
-import org.jruby.rack.RackApplicationFactory;
-import org.jruby.rack.RackServletContextListener;
-import org.jruby.rack.SharedRackApplicationFactory;
-import org.jruby.rack.SerialPoolingRackApplicationFactory;
+import org.jruby.rack.*;
/**
*
@@ -21,22 +15,12 @@
*/
public class RailsServletContextListener extends RackServletContextListener {
@Override
- protected RackApplicationFactory newApplicationFactory(ServletContext context) {
- Integer maxRuntimes = null;
- try {
- maxRuntimes = Integer.parseInt(context.getInitParameter("jruby.max.runtimes").toString());
- } catch (Exception e) {
- }
+ protected RackApplicationFactory newApplicationFactory(RackConfig config) {
+ Integer maxRuntimes = config.getMaximumRuntimes();
if (maxRuntimes != null && maxRuntimes == 1) {
return new SharedRackApplicationFactory(new RailsRackApplicationFactory());
} else {
- boolean serial = false;
- try {
- serial = Boolean.parseBoolean(context.getInitParameter("jruby.init.serial").toString());
- } catch (Exception e) {
- }
-
- if(serial) {
+ if (config.isSerialInitialization()) {
return new SerialPoolingRackApplicationFactory(new RailsRackApplicationFactory());
} else {
return new PoolingRackApplicationFactory(new RailsRackApplicationFactory());
View
56 src/main/java/org/jruby/rack/servlet/ServletRackConfig.java
@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2010-2011 Engine Yard, Inc.
+ * Copyright (c) 2007-2009 Sun Microsystems, Inc.
+ * This source code is available under the MIT license.
+ * See the file LICENSE.txt for details.
+ */
+
+package org.jruby.rack.servlet;
+
+import org.jruby.rack.DefaultRackConfig;
+import org.jruby.rack.RackLogger;
+import org.jruby.rack.logging.ServletContextLogger;
+
+import javax.servlet.ServletContext;
+
+/**
+ * Servlet environment version of RackConfig.
+ */
+public class ServletRackConfig extends DefaultRackConfig {
+
+ private ServletContext context;
+
+ public ServletRackConfig(ServletContext context) {
+ this.context = context;
+ }
+
+ @Override
+ public String getProperty(String key) {
+ String value;
+ if ((value = context.getInitParameter(key)) != null) {
+ return value;
+ }
+ return super.getProperty(key);
+ }
+
+ @Override
+ public String getProperty(String key, String defaultValue) {
+ String value;
+ if ((value = context.getInitParameter(key)) != null) {
+ return value;
+ }
+ return super.getProperty(key, defaultValue);
+ }
+
+ @Override
+ protected RackLogger createLogger(String loggerClass) {
+ if (loggerClass == null || loggerClass.equals(ServletContextLogger.class.getName())) {
+ return new ServletContextLogger(context);
+ }
+ return super.createLogger(loggerClass);
+ }
+
+ public ServletContext getServletContext() {
+ return context;
+ }
+}
View
40 src/main/java/org/jruby/rack/servlet/ServletRackContext.java
@@ -7,40 +7,38 @@
package org.jruby.rack.servlet;
-import org.jruby.rack.input.RackRewindableInput;
-import org.jruby.rack.logging.RackLoggerFactory;
-import org.jruby.rack.*;
+import org.jruby.rack.RackApplicationFactory;
+import org.jruby.rack.RackConfig;
+import org.jruby.rack.RackContext;
+import org.jruby.rack.RackLogger;
+
+import javax.servlet.RequestDispatcher;
+import javax.servlet.Servlet;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Enumeration;
import java.util.Set;
-import javax.servlet.RequestDispatcher;
-import javax.servlet.Servlet;
-import javax.servlet.ServletContext;
-import javax.servlet.ServletException;
-
-import org.jruby.util.SafePropertyAccessor;
-import static java.lang.System.out;
/**
*
* @author nicksieger
*/
public class ServletRackContext implements RackContext, ServletContext {
+ private RackConfig config;
private ServletContext context;
private RackLogger logger;
- public ServletRackContext(ServletContext context) {
- this.context = context;
- this.logger = new RackLoggerFactory().getLogger(context);
- RackRewindableInput.setDefaultThreshold(
- SafePropertyAccessor.getInt("jruby.rack.request.size.threshold.bytes",
- RackRewindableInput.getDefaultThreshold()));
+ public ServletRackContext(ServletRackConfig config) {
+ this.config = config;
+ this.context = config.getServletContext();
+ this.logger = config.getLogger();
}
public String getInitParameter(String key) {
- return context.getInitParameter(key);
+ return config.getProperty(key);
}
public void log(String message) {
@@ -66,7 +64,7 @@ public String getRealPath(String path) {
}
public RackApplicationFactory getRackFactory() {
- return (RackApplicationFactory) context.getAttribute(RackServletContextListener.FACTORY_KEY);
+ return (RackApplicationFactory) context.getAttribute(RackApplicationFactory.FACTORY);
}
public ServletContext getContext(String path) {
@@ -126,7 +124,7 @@ public Enumeration getServletNames() {
@Deprecated
public void log(Exception ex, String msg) {
- context.log(ex, msg);
+ logger.log(msg, ex);
}
public String getServerInfo() {
@@ -141,6 +139,10 @@ public Object getAttribute(String key) {
return context.getAttribute(key);
}
+ public RackConfig getConfig() {
+ return config;
+ }
+
public Enumeration getAttributeNames() {
return context.getAttributeNames();
}
View
8 src/main/java/org/jruby/rack/servlet/ServletRackEnvironment.java
@@ -7,11 +7,13 @@
package org.jruby.rack.servlet;
-import org.jruby.rack.*;
-import java.io.IOException;
-import java.io.InputStream;
+import org.jruby.rack.RackContext;
+import org.jruby.rack.RackEnvironment;
+
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
+import java.io.IOException;
+import java.io.InputStream;
/**
* Implementation of RackEnvironment for the servlet environment.
View
8 src/main/java/org/jruby/rack/servlet/ServletRackResponseEnvironment.java
@@ -7,12 +7,14 @@
package org.jruby.rack.servlet;
-import org.jruby.rack.*;
+import org.jruby.rack.RackResponse;
+import org.jruby.rack.RackResponseEnvironment;
+
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpServletResponseWrapper;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map.Entry;
-import javax.servlet.http.HttpServletResponse;
-import javax.servlet.http.HttpServletResponseWrapper;
/**
*
View
43 src/spec/ruby/rack/application_spec.rb
@@ -14,6 +14,7 @@
describe DefaultRackApplication, "call" do
it "should invoke the call method on the ruby object and return the rack response" do
server_request = mock("server request")
+ server_request.stub!(:getContext).and_return @rack_context
server_request.stub!(:getInput).and_return(StubInputStream.new("hello"))
server_request.stub!(:getContentLength).and_return(-1)
rack_response = org.jruby.rack.RackResponse.impl {}
@@ -38,13 +39,13 @@
end
it "should receive a rackup script via the 'rackup' parameter" do
- @rack_context.should_receive(:getInitParameter).with('rackup').and_return 'run MyRackApp'
+ @rack_config.should_receive(:getRackup).and_return 'run MyRackApp'
@app_factory.init @rack_context
@app_factory.rackup_script.should == 'run MyRackApp'
end
it "should look for a rackup script via the 'rackup.path' parameter" do
- @rack_context.should_receive(:getInitParameter).with('rackup.path').and_return '/WEB-INF/hello.ru'
+ @rack_config.should_receive(:getRackupPath).and_return '/WEB-INF/hello.ru'
@rack_context.should_receive(:getResourceAsStream).with('/WEB-INF/hello.ru').and_return StubInputStream.new("run MyRackApp")
@app_factory.init @rack_context
@app_factory.rackup_script.should == 'run MyRackApp'
@@ -70,7 +71,7 @@
end
it "should handle config.ru files with a coding: pragma" do
- @rack_context.should_receive(:getInitParameter).with('rackup.path').and_return '/WEB-INF/hello.ru'
+ @rack_config.should_receive(:getRackupPath).and_return '/WEB-INF/hello.ru'
@rack_context.should_receive(:getResourceAsStream).with('/WEB-INF/hello.ru').and_return StubInputStream.new("# coding: us-ascii\nrun MyRackApp")
@app_factory.init @rack_context
@app_factory.rackup_script.should == "# coding: us-ascii\nrun MyRackApp"
@@ -106,7 +107,7 @@
end
it "should handle jruby.compat.version == '1.9' and start up in 1.9 mode" do
- @rack_context.stub!(:getInitParameter).with("jruby.compat.version").and_return "1.9"
+ @rack_config.stub!(:getCompatVersion).and_return org.jruby.CompatVersion::RUBY1_9
runtime = app_factory.newRuntime
runtime.instance_config.compat_version.should == org.jruby.CompatVersion::RUBY1_9
end
@@ -120,14 +121,14 @@
end
it "should create a Ruby object from the script snippet given" do
- @rack_context.should_receive(:getInitParameter).with('rackup').and_return("require 'rack/lobster'; Rack::Lobster.new")
+ @rack_config.should_receive(:getRackup).and_return("require 'rack/lobster'; Rack::Lobster.new")
@app_factory.init @rack_context
object = @app_factory.newApplication
object.respond_to?(:call).should == true
end
it "should raise an exception if creation failed" do
- @rack_context.should_receive(:getInitParameter).with('rackup').and_return("raise 'something went wrong'")
+ @rack_config.should_receive(:getRackup).and_return("raise 'something went wrong'")
@app_factory.init @rack_context
object = @app_factory.newApplication
lambda { object.init }.should raise_error
@@ -136,7 +137,7 @@
describe "getApplication" do
it "should create an application and initialize it" do
- @rack_context.should_receive(:getInitParameter).with('rackup').and_return("raise 'init was called'")
+ @rack_config.should_receive(:getRackup).and_return("raise 'init was called'")
@app_factory.init @rack_context
lambda { @app_factory.getApplication }.should raise_error
end
@@ -209,15 +210,14 @@
app.should_receive(:init)
app
end
- @rack_context.should_receive(:getInitParameter).with("jruby.min.runtimes").and_return "1"
+ @rack_config.should_receive(:getInitialRuntimes).and_return 1
@pool.init(@rack_context)
@pool.getApplicationPool.size.should == 1
end
- it "should not create any new applications beyond the maximum specified
- by the jruby.max.runtimes context parameter" do
+ it "should not create any new applications beyond the maximum specified by the jruby.max.runtimes context parameter" do
@factory.should_receive(:init).with(@rack_context)
- @rack_context.should_receive(:getInitParameter).with("jruby.max.runtimes").and_return "1"
+ @rack_config.should_receive(:getMaximumRuntimes).and_return 1
@pool.init(@rack_context)
@pool.finishedWithApplication mock("app1")
@pool.finishedWithApplication mock("app2")
@@ -226,7 +226,7 @@
it "should not add an application back into the pool if it already exists" do
@factory.should_receive(:init).with(@rack_context)
- @rack_context.should_receive(:getInitParameter).with("jruby.max.runtimes").and_return "4"
+ @rack_config.should_receive(:getMaximumRuntimes).and_return 4
@pool.init(@rack_context)
rack_application_1 = mock("app1")
@pool.finishedWithApplication rack_application_1
@@ -234,21 +234,6 @@
@pool.getApplicationPool.size.should == 1
end
- it "should also recognize the jruby.pool.minIdle and jruby.pool.maxActive parameters from Goldspike" do
- @factory.should_receive(:init).with(@rack_context)
- @factory.stub!(:newApplication).and_return do
- app = mock "app"
- app.should_receive(:init)
- app
- end
- @rack_context.should_receive(:getInitParameter).with("jruby.pool.minIdle").and_return "1"
- @rack_context.should_receive(:getInitParameter).with("jruby.pool.maxActive").and_return "2"
- @pool.init(@rack_context)
- @pool.getApplicationPool.size.should == 1
- @pool.finishedWithApplication mock("app")
- @pool.getApplicationPool.size.should == 2
- end
-
it "should force the maximum size to be greater or equal to the initial size" do
@factory.should_receive(:init).with(@rack_context)
@factory.stub!(:newApplication).and_return do
@@ -256,8 +241,8 @@
app.should_receive(:init)
app
end
- @rack_context.should_receive(:getInitParameter).with("jruby.min.runtimes").and_return "2"
- @rack_context.should_receive(:getInitParameter).with("jruby.max.runtimes").and_return "1"
+ @rack_config.should_receive(:getInitialRuntimes).and_return 2
+ @rack_config.should_receive(:getMaximumRuntimes).and_return 1
@pool.init(@rack_context)
@pool.waitForNextAvailable(30)
@pool.getApplicationPool.size.should == 2
View
63 src/spec/ruby/rack/config_spec.rb
@@ -0,0 +1,63 @@
+#--
+# Copyright (c) 2010-2011 Engine Yard, Inc.
+# Copyright (c) 2007-2009 Sun Microsystems, Inc.
+# This source code is available under the MIT license.
+# See the file LICENSE.txt for details.
+#++
+
+require 'spec_helper'
+
+import org.jruby.rack.servlet.ServletRackConfig
+
+describe ServletRackConfig do
+ let(:config) { ServletRackConfig.new(@servlet_context).tap {|c| c.quiet = true } }
+
+ describe "getLogger" do
+ let(:logger) { config.getLogger }
+
+ after :each do
+ java.lang.System.clearProperty("jruby.rack.logging")
+ end
+
+ it "should construct a logger from the context init params over system properties" do
+ @servlet_context.should_receive(:getInitParameter).with("jruby.rack.logging").and_return "clogging"
+ java.lang.System.setProperty("jruby.rack.logging", "stdout")
+ logger.should be_kind_of(org.jruby.rack.logging.CommonsLoggingLogger)
+ end
+
+ it "should construct a standard out logger when the logging attribute is unrecognized" do
+ java.lang.System.setProperty("jruby.rack.logging", "other")
+ logger.should be_kind_of(org.jruby.rack.logging.StandardOutLogger)
+ end
+
+ it "should constct a standard out logger when the logger can't be instantiated" do
+ java.lang.System.setProperty("jruby.rack.logging", "java.lang.String")
+ logger.should be_kind_of(org.jruby.rack.logging.StandardOutLogger)
+ end
+
+ it "should construct a servlet context logger by default" do
+ logger.should be_kind_of(org.jruby.rack.logging.ServletContextLogger)
+ end
+
+ it "should allow specifying a class name in the logging attribute" do
+ java.lang.System.setProperty("jruby.rack.logging", "org.jruby.rack.logging.CommonsLoggingLogger")
+ logger.should be_kind_of(org.jruby.rack.logging.CommonsLoggingLogger)
+ end
+ end
+
+ describe "runtime counts" do
+ it "should retrieve the minimum and maximum counts from jruby.min and jruby.max.runtimes" do
+ @servlet_context.should_receive(:getInitParameter).with("jruby.min.runtimes").and_return "1"
+ @servlet_context.should_receive(:getInitParameter).with("jruby.max.runtimes").and_return "2"
+ config.initial_runtimes.should == 1
+ config.maximum_runtimes.should == 2
+ end
+
+ it "should recognize the jruby.pool.minIdle and jruby.pool.maxActive parameters from Goldspike" do
+ @servlet_context.should_receive(:getInitParameter).with("jruby.pool.minIdle").and_return "1"
+ @servlet_context.should_receive(:getInitParameter).with("jruby.pool.maxActive").and_return "2"
+ config.initial_runtimes.should == 1
+ config.maximum_runtimes.should == 2
+ end
+ end
+end
View
3  src/spec/ruby/rack/filter_spec.rb
@@ -121,7 +121,8 @@ def stub_request(path_info)
config = mock "filter config"
config.stub!(:getServletContext).and_return servlet_context
servlet_context.stub!(:getInitParameter).and_return nil
- servlet_context.stub!(:getInitParameter).with("jruby.rack.slash.index").and_return "true"
+ servlet_context.should_receive(:getAttribute).with('rack.context').and_return @rack_context
+ @rack_config.stub!(:isSlashIndex).and_return true
@filter.init(config)
stub_request("/")
@chain.should_receive(:doFilter).ordered.and_return do |req,resp|
View
46 src/spec/ruby/rack/logging_spec.rb
@@ -1,46 +0,0 @@
-#--
-# Copyright (c) 2010-2011 Engine Yard, Inc.
-# Copyright (c) 2007-2009 Sun Microsystems, Inc.
-# This source code is available under the MIT license.
-# See the file LICENSE.txt for details.
-#++
-
-require 'spec_helper'
-
-import org.jruby.rack.logging.RackLoggerFactory
-
-describe RackLoggerFactory, "getLogger" do
- after :each do
- java.lang.System.clearProperty("jruby.rack.logging")
- end
-
- def logger(context = @servlet_context)
- RackLoggerFactory.new(true).getLogger(context)
- end
-
- it "should construct a logger from the context init params over system properties" do
- context = mock "context"
- context.should_receive(:getInitParameter).with("jruby.rack.logging").and_return "clogging"
- java.lang.System.setProperty("jruby.rack.logging", "stdout")
- logger(context).should be_kind_of(org.jruby.rack.logging.CommonsLoggingLogger)
- end
-
- it "should construct a standard out logger when the logging attribute is unrecognized" do
- java.lang.System.setProperty("jruby.rack.logging", "other")
- logger.should be_kind_of(org.jruby.rack.logging.StandardOutLogger)
- end
-
- it "should constct a standard out logger when the logger can't be instantiated" do
- java.lang.System.setProperty("jruby.rack.logging", "java.lang.String")
- logger.should be_kind_of(org.jruby.rack.logging.StandardOutLogger)
- end
-
- it "should construct a servlet context logger by default" do
- logger.should be_kind_of(org.jruby.rack.logging.ServletContextLogger)
- end
-
- it "should allow specifying a class name in the logging attribute" do
- java.lang.System.setProperty("jruby.rack.logging", "org.jruby.rack.logging.CommonsLoggingLogger")
- logger.should be_kind_of(org.jruby.rack.logging.CommonsLoggingLogger)
- end
-end
View
13 src/spec/ruby/rack/servlet_context_listener_spec.rb
@@ -19,7 +19,8 @@
describe "contextInitialized" do
it "should create a Rack application factory and store it in the context" do
- @servlet_context.should_receive(:setAttribute).with(RackServletContextListener::FACTORY_KEY, @factory)
+ @servlet_context.should_receive(:setAttribute).with(RackApplicationFactory::FACTORY, @factory)
+ @servlet_context.should_receive(:setAttribute).with(RackApplicationFactory::RACK_CONTEXT, anything())
@factory.stub!(:init)
@listener.contextInitialized @servlet_context_event
end
@@ -41,16 +42,18 @@
describe "contextDestroyed" do
it "should remove the application factory from the servlet context" do
@servlet_context.should_receive(:getAttribute).with(
- RackServletContextListener::FACTORY_KEY).and_return @factory
+ RackApplicationFactory::FACTORY).and_return @factory
@servlet_context.should_receive(:removeAttribute).with(
- RackServletContextListener::FACTORY_KEY)
+ RackApplicationFactory::FACTORY)
+ @servlet_context.should_receive(:removeAttribute).with(
+ RackApplicationFactory::RACK_CONTEXT)
@factory.stub!(:destroy)
@listener.contextDestroyed @servlet_context_event
end
it "should destroy it" do
@servlet_context.should_receive(:getAttribute).with(
- RackServletContextListener::FACTORY_KEY).and_return @factory
+ RackApplicationFactory::FACTORY).and_return @factory
@servlet_context.stub!(:removeAttribute)
@factory.should_receive(:destroy)
@listener.contextDestroyed @servlet_context_event
@@ -58,7 +61,7 @@
it "should do nothing if no application is found in the context" do
@servlet_context.should_receive(:getAttribute).with(
- RackServletContextListener::FACTORY_KEY).and_return nil
+ RackApplicationFactory::FACTORY).and_return nil
@listener.contextDestroyed @servlet_context_event
end
end
View
3  src/spec/ruby/rack/servlet_spec.rb
@@ -9,6 +9,7 @@
import org.jruby.rack.RackServlet
import org.jruby.rack.servlet.ServletRackContext
+import org.jruby.rack.servlet.ServletRackConfig
describe RackServlet, "service" do
it "should delegate to process" do
@@ -23,7 +24,7 @@
describe ServletRackContext, "getRealPath" do
before :each do
- @context = ServletRackContext.new(@servlet_context)
+ @context = ServletRackContext.new(ServletRackConfig.new(@servlet_context))
end
it "should use getResource when getRealPath returns nil" do
View
3  src/spec/ruby/rack/tag_spec.rb
@@ -31,7 +31,8 @@ def call(request)
@rack_factory.stub!(:getApplication).and_return @application
@rack_factory.stub!(:finishedWithApplication)
- @servlet_context.stub!(:getAttribute).and_return @rack_factory
+ @servlet_context.stub!(:getAttribute).with('rack.factory').and_return @rack_factory
+ @servlet_context.stub!(:getAttribute).with('rack.context').and_return @rack_context
@servlet_request = mock("Servlet Request")
@servlet_request.stub!(:getContextPath).and_return ""
@servlet_response = mock("Servlet Response")
View
7 src/spec/ruby/spec_helper.rb
@@ -14,11 +14,13 @@
WD_START = Dir.getwd
java_import org.jruby.rack.RackContext
+java_import org.jruby.rack.RackConfig
java_import org.jruby.rack.RackServletContextListener
java_import javax.servlet.ServletContext
Spec::Runner.configure do |config|
def mock_servlet_context
+ @rack_config ||= RackConfig.impl {}
@rack_context ||= RackContext.impl {}
@servlet_context ||= ServletContext.impl {}
[@rack_context, @servlet_context].each do |context|
@@ -28,9 +30,10 @@ def mock_servlet_context
context.stub!(:getResource).and_return nil
context.stub!(:getContextPath).and_return "/"
end
+ @rack_context.stub!(:getConfig).and_return @rack_config
@servlet_config ||= mock("servlet config")
- @servlet_config.stub!(:getServletName).and_return("A Servlet")
- @servlet_config.stub!(:getServletContext).and_return(@servlet_context)
+ @servlet_config.stub!(:getServletName).and_return "A Servlet"
+ @servlet_config.stub!(:getServletContext).and_return @servlet_context
end
def create_booter(booter_class = JRuby::Rack::Booter)
Please sign in to comment.
Something went wrong with that request. Please try again.