Modified: trunk/rails-integration/src/main/java/org/jruby/webapp/RailsContextListener.java (777 => 778)
--- trunk/rails-integration/src/main/java/org/jruby/webapp/RailsContextListener.java 2007-10-25 17:11:03 UTC (rev 777)
+++ trunk/rails-integration/src/main/java/org/jruby/webapp/RailsContextListener.java 2007-10-25 20:47:03 UTC (rev 778)
@@ -1,176 +1,179 @@
-package org.jruby.webapp;
-import org.jruby.webapp.util.CustomObjectPool;
-import org.jruby.webapp.util.FileUtil;
-import org.apache.commons.pool.ObjectPool;
-import java.util.Enumeration;
-import javax.servlet.ServletContextListener;
-import javax.servlet.ServletContextEvent;
-import javax.servlet.ServletContext;
-import javax.servlet.ServletException;
-/**
- * This manages the lifecycle of the Rails environment.
- *
- * @author Robert Egglestone
- */
-public class RailsContextListener implements ServletContextListener {
-
- /** The attribute we make the runtime pool available to servlets with */
- public static final String RUNTIME_POOL_ATTRIBUTE = "org.jruby.webapp.RuntimePool";
-
- /** Store the servlet context */
- private ServletContext context;
-
- /** The pool of JRuby runtimes to process the requests */
- private ObjectPool runtimePool;
-
- public void contextInitialized(ServletContextEvent event) {
- context = event.getServletContext();
-
- // prepare JRuby home
- if (isStandalone()) {
- log("Ruby is running in standalone mode");
- } else {
- setJRubyHome();
- }
-
- // find the root of the web application
- String railsRoot = context.getInitParameter("rails.root");
- if (railsRoot == null || railsRoot.length() == 0) {
- railsRoot = "/";
- }
- try {
- railsRoot = getPath(railsRoot);
- } catch (ServletException e) {
- log("ERROR: " + e.getMessage());
- throw new RuntimeException(e);
- }
-
- // create the factory
- RailsFactory railsFactory = createRailsFactory();
- railsFactory.setRailsRoot(railsRoot);
- railsFactory.setRailsEnvironment(getDeploymentEnvironment());
- railsFactory.setServletContext(context);
-
- // add any extra initial parameters to the environment
- for(Enumeration e = context.getInitParameterNames(); e.hasMoreElements(); ) {
- String name = (String)e.nextElement();
- String value = context.getInitParameter(name);
- railsFactory.addEnvironmentOverride(name, value);
- }
-
- // add the gem configuration
- if (isStandalone()) {
- railsFactory.removeEnvironment("GEM_HOME");
- }
- railsFactory.setGemPath(findGemPath());
-
- // create the pool
- runtimePool = createObjectPool(railsFactory);
- context.setAttribute(RUNTIME_POOL_ATTRIBUTE, runtimePool);
- }
-
- public void contextDestroyed(ServletContextEvent event) {
- try {
- runtimePool.close();
- } catch (Exception e) {
- log("Failed to close runtime pool", e);
- }
- }
-
- protected RailsFactory createRailsFactory() {
- return new RailsFactory();
- }
-
- private String findGemPath() {
- if (isStandalone()) {
- // look for a local copy to override the default
- // returns null if the webapp doesn't include any gems
- try {
- return getPath("/WEB-INF/gems");
- } catch (ServletException e) {
- log("Could not find gem home");
- return null;
- }
- } else {
- // try other locations is this is not standalone
- String gemPath = System.getProperty("gem.path");
- if (gemPath != null && gemPath.length() > 0) return gemPath;
- gemPath = System.getProperty("gem.home");
- if (gemPath != null && gemPath.length() > 0) return gemPath;
- }
- return null;
- }
-
- private void setJRubyHome() {
- String jrubyHome = getServletContext().getInitParameter("jruby.home");
- if (jrubyHome != null && jrubyHome.length() != 0) {
- System.setProperty("jruby.home", jrubyHome);
- }
- }
-
- /**
- * Create the pool of JRuby runtimes.
- */
- protected ObjectPool createObjectPool(RailsFactory railsFactory) {
- int maxObjects = getIntegerProperty("jruby.pool.maxActive", 4);
- int minIdle = getIntegerProperty("jruby.pool.minIdle", 2);
- int checkInterval = getIntegerProperty("jruby.pool.checkInterval", 1000);
- CustomObjectPool pool = new CustomObjectPool(railsFactory, maxObjects, minIdle, checkInterval);
- pool.setMaxWait(getIntegerProperty("jruby.pool.maxWait", 30000));
- return pool;
- }
-
- /**
- * Is the environment explicitly set to be standalone?
- */
- public boolean isStandalone() {
- String standalone = getServletContext().getInitParameter("jruby.standalone");
- if (standalone == null) return false;
- return Boolean.valueOf(standalone).booleanValue();
- }
-
- protected String getDeploymentEnvironment() {
- // The servlet config isn't available at this stage,
- // and it shouldn't take precedence over the context.
- // - Robert
-// String railsEnv = getServletConfig().getInitParameter("rails.env");
-// if (railsEnv == null) {
-// railsEnv = getServletContext().getInitParameter("rails.env");
-// }
- return getServletContext().getInitParameter("rails.env");
- }
-
- public ServletContext getServletContext() {
- return context;
- }
-
- protected void log(String message) {
- if (context == null) return;
- context.log(message);
- }
-
- protected void log(String message, Throwable exception) {
- if (context == null) return;
- context.log(message, exception);
- }
-
- /**
- * Locate a relative webapp path on the file system.
- */
- protected String getPath(String path) throws ServletException {
- return FileUtil.getPath(getServletContext(), path);
- }
-
- /**
- * Gets an integer variable from the context.
- */
- protected int getIntegerProperty(String name, int defaultValue) {
- String value = getServletContext().getInitParameter(name);
- if (value == null) {
- return defaultValue;
- } else {
- return Integer.parseInt(value);
- }
- }
-
-}
+package org.jruby.webapp;
+import org.jruby.webapp.util.CustomObjectPool;
+import org.jruby.webapp.util.FileUtil;
+import org.apache.commons.pool.ObjectPool;
+import java.util.Enumeration;
+import javax.servlet.ServletContextListener;
+import javax.servlet.ServletContextEvent;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
+/**
+ * This manages the lifecycle of the Rails environment.
+ *
+ * @author Robert Egglestone
+ */
+public class RailsContextListener implements ServletContextListener {
+
+ /** The attribute we make the runtime pool available to servlets with */
+ public static final String RUNTIME_POOL_ATTRIBUTE = "org.jruby.webapp.RuntimePool";
+
+ /** Store the servlet context */
+ private ServletContext context;
+
+ /** The pool of JRuby runtimes to process the requests */
+ private ObjectPool runtimePool;
+
+ public void contextInitialized(ServletContextEvent event) {
+ context = event.getServletContext();
+
+ // prepare JRuby home
+ if (isStandalone()) {
+ log("Ruby is running in standalone mode");
+ } else {
+ setJRubyHome();
+ }
+
+ // find the root of the web application
+ String railsRoot = context.getInitParameter("rails.root");
+ if (railsRoot == null || railsRoot.length() == 0) {
+ railsRoot = "/";
+ }
+ try {
+ railsRoot = getPath(railsRoot);
+ } catch (ServletException e) {
+ log("ERROR: " + e.getMessage());
+ throw new RuntimeException(e);
+ }
+ if(System.getProperty("jruby.objectspace.enabled") == null) {
+ System.setProperty("jruby.objectspace.enabled", "false");
+ }
+
+ // create the factory
+ RailsFactory railsFactory = createRailsFactory();
+ railsFactory.setRailsRoot(railsRoot);
+ railsFactory.setRailsEnvironment(getDeploymentEnvironment());
+ railsFactory.setServletContext(context);
+
+ // add any extra initial parameters to the environment
+ for(Enumeration e = context.getInitParameterNames(); e.hasMoreElements(); ) {
+ String name = (String)e.nextElement();
+ String value = context.getInitParameter(name);
+ railsFactory.addEnvironmentOverride(name, value);
+ }
+
+ // add the gem configuration
+ if (isStandalone()) {
+ railsFactory.removeEnvironment("GEM_HOME");
+ }
+ railsFactory.setGemPath(findGemPath());
+
+ // create the pool
+ runtimePool = createObjectPool(railsFactory);
+ context.setAttribute(RUNTIME_POOL_ATTRIBUTE, runtimePool);
+ }
+
+ public void contextDestroyed(ServletContextEvent event) {
+ try {
+ runtimePool.close();
+ } catch (Exception e) {
+ log("Failed to close runtime pool", e);
+ }
+ }
+
+ protected RailsFactory createRailsFactory() {
+ return new RailsFactory();
+ }
+
+ private String findGemPath() {
+ if (isStandalone()) {
+ // look for a local copy to override the default
+ // returns null if the webapp doesn't include any gems
+ try {
+ return getPath("/WEB-INF/gems");
+ } catch (ServletException e) {
+ log("Could not find gem home");
+ return null;
+ }
+ } else {
+ // try other locations is this is not standalone
+ String gemPath = System.getProperty("gem.path");
+ if (gemPath != null && gemPath.length() > 0) return gemPath;
+ gemPath = System.getProperty("gem.home");
+ if (gemPath != null && gemPath.length() > 0) return gemPath;
+ }
+ return null;
+ }
+
+ private void setJRubyHome() {
+ String jrubyHome = getServletContext().getInitParameter("jruby.home");
+ if (jrubyHome != null && jrubyHome.length() != 0) {
+ System.setProperty("jruby.home", jrubyHome);
+ }
+ }
+
+ /**
+ * Create the pool of JRuby runtimes.
+ */
+ protected ObjectPool createObjectPool(RailsFactory railsFactory) {
+ int maxObjects = getIntegerProperty("jruby.pool.maxActive", 4);
+ int minIdle = getIntegerProperty("jruby.pool.minIdle", 2);
+ int checkInterval = getIntegerProperty("jruby.pool.checkInterval", 1000);
+ CustomObjectPool pool = new CustomObjectPool(railsFactory, maxObjects, minIdle, checkInterval);
+ pool.setMaxWait(getIntegerProperty("jruby.pool.maxWait", 30000));
+ return pool;
+ }
+
+ /**
+ * Is the environment explicitly set to be standalone?
+ */
+ public boolean isStandalone() {
+ String standalone = getServletContext().getInitParameter("jruby.standalone");
+ if (standalone == null) return false;
+ return Boolean.valueOf(standalone).booleanValue();
+ }
+
+ protected String getDeploymentEnvironment() {
+ // The servlet config isn't available at this stage,
+ // and it shouldn't take precedence over the context.
+ // - Robert
+// String railsEnv = getServletConfig().getInitParameter("rails.env");
+// if (railsEnv == null) {
+// railsEnv = getServletContext().getInitParameter("rails.env");
+// }
+ return getServletContext().getInitParameter("rails.env");
+ }
+
+ public ServletContext getServletContext() {
+ return context;
+ }
+
+ protected void log(String message) {
+ if (context == null) return;
+ context.log(message);
+ }
+
+ protected void log(String message, Throwable exception) {
+ if (context == null) return;
+ context.log(message, exception);
+ }
+
+ /**
+ * Locate a relative webapp path on the file system.
+ */
+ protected String getPath(String path) throws ServletException {
+ return FileUtil.getPath(getServletContext(), path);
+ }
+
+ /**
+ * Gets an integer variable from the context.
+ */
+ protected int getIntegerProperty(String name, int defaultValue) {
+ String value = getServletContext().getInitParameter(name);
+ if (value == null) {
+ return defaultValue;
+ } else {
+ return Integer.parseInt(value);
+ }
+ }
+
+}