Title: [705] trunk/rails-integration/src/main/java/org/jruby/webapp: Fix for WebLogic using getResource instead of getRealPath.
Revision
705
Author
tantalon
Date
2007-08-23 19:26:11 -0400 (Thu, 23 Aug 2007)

Log Message

Fix for WebLogic using getResource instead of getRealPath.
(WebLogic expands the wars, but doesn't implement getRealPath to the expanded location)

Modified Paths

Added Paths

Diff

Modified: trunk/rails-integration/src/main/java/org/jruby/webapp/FileServlet.java (704 => 705)


--- trunk/rails-integration/src/main/java/org/jruby/webapp/FileServlet.java	2007-08-23 20:29:33 UTC (rev 704)
+++ trunk/rails-integration/src/main/java/org/jruby/webapp/FileServlet.java	2007-08-23 23:26:11 UTC (rev 705)
@@ -1,5 +1,6 @@
 package org.jruby.webapp;
 
+import org.jruby.webapp.util.FileUtil;
 import javax.activation.FileTypeMap;
 import javax.servlet.RequestDispatcher;
 import javax.servlet.ServletContext;
@@ -91,10 +92,7 @@
 	protected File findRoot() throws ServletException {
 		String rootPath = getServletContext().getInitParameter("files.root");
 		if (rootPath == null) {
-			rootPath = getServletContext().getRealPath("/");
-			if (rootPath == null) {
-				throw new ServletException("Cannot find the real path of this webapp, probably using a non-extracted WAR");
-			}
+			rootPath = FileUtil.getPath(getServletContext(), "/");
 		}
 
 		File root = new File(rootPath);

Modified: trunk/rails-integration/src/main/java/org/jruby/webapp/RailsContextListener.java (704 => 705)


--- trunk/rails-integration/src/main/java/org/jruby/webapp/RailsContextListener.java	2007-08-23 20:29:33 UTC (rev 704)
+++ trunk/rails-integration/src/main/java/org/jruby/webapp/RailsContextListener.java	2007-08-23 23:26:11 UTC (rev 705)
@@ -1,10 +1,11 @@
 package org.jruby.webapp;
 import org.jruby.webapp.util.CustomObjectPool;
+import org.jruby.webapp.util.FileUtil;
 import org.apache.commons.pool.ObjectPool;
-import java.io.File;
 import javax.servlet.ServletContextListener;
 import javax.servlet.ServletContextEvent;
 import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
 /**
  * This manages the lifecycle of the Rails environment.
  *
@@ -34,7 +35,12 @@
 		// find the root of the web application
 		String railsRoot = context.getInitParameter("rails.root");
 		if (railsRoot == null || railsRoot.length() == 0) {
-			railsRoot = getPath("/");
+			try {
+				railsRoot = getPath("/");
+			} catch (ServletException e) {
+				log("ERROR: " + e.getMessage());
+				throw new RuntimeException(e);
+			}
 		}
 
 		// create the factory
@@ -69,7 +75,12 @@
 		if (isStandalone()) {
 			// look for a local copy to override the default
 			// returns null if the webapp doesn't include any gems
-			return getPath("/WEB-INF/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");
@@ -136,22 +147,8 @@
 	/**
 	 * Locate a relative webapp path on the file system.
 	 */
-	protected String getPath(String path) {
-		String realPath = getServletContext().getRealPath(path);
-		// make an attempt, as this is a fatal error
-		if (realPath == null) {
-			File realFile = new File("." + path);
-			if (realFile.exists()) {
-				realPath = realFile.getAbsolutePath();
-			}
-		}
-		// validate
-		if (realPath == null) {
-			log("Could not find resource " + path);
-		} else if (realPath.endsWith("/")) {
-			realPath = realPath.substring(0, realPath.length() - 1);
-		}
-		return realPath;
+	protected String getPath(String path) throws ServletException {
+		return FileUtil.getPath(getServletContext(), path);
 	}
 
 	/**

Added: trunk/rails-integration/src/main/java/org/jruby/webapp/util/FileUtil.java (0 => 705)


--- trunk/rails-integration/src/main/java/org/jruby/webapp/util/FileUtil.java	                        (rev 0)
+++ trunk/rails-integration/src/main/java/org/jruby/webapp/util/FileUtil.java	2007-08-23 23:26:11 UTC (rev 705)
@@ -0,0 +1,56 @@
+package org.jruby.webapp.util;
+import java.net.URL;
+import java.net.MalformedURLException;
+import java.io.File;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
+/**
+ * @author Robert Egglestone
+ */
+public class FileUtil {
+
+	/**
+	 * Locate a relative webapp path on the file system.
+	 */
+	public static String getPath(ServletContext context, String path) throws ServletException {
+		// the proper way of doing this
+		String realPath = context.getRealPath(path);
+
+		// WebLogic returns a file URL for getResource, but doesn't support getRealPath
+		if (realPath == null) {
+			try {
+				URL resourcePath = context.getResource(path);
+				if (resourcePath.getProtocol().equals("file")) {
+					realPath = resourcePath.getPath();
+				}
+			} catch (MalformedURLException e) {
+				// fallback to other mechanisms
+			}
+		}
+
+		// make a best effort attempt
+		if (realPath == null) {
+			// make an attempt, as this is a fatal error
+			File realFile = new File("." + path);
+			if (realFile.exists()) {
+				realPath = realFile.getAbsolutePath();
+			}
+		}
+
+		// fatal error
+		if (realPath == null) {
+			throw new ServletException("Cannot find the real path of this webapp, probably using a non-extracted WAR");
+		}
+
+		// normalize path
+		if (realPath.endsWith("/")) {
+			realPath = realPath.substring(0, realPath.length() - 1);
+		}
+
+		return realPath;
+	}
+
+	private FileUtil() {
+	}
+
+}
_______________________________________________
Jruby-extras-devel mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/jruby-extras-devel

Reply via email to