diff --git a/src/helma/main/ApplicationManager.java b/src/helma/main/ApplicationManager.java index 1dd3e99f..bb2c9d7d 100644 --- a/src/helma/main/ApplicationManager.java +++ b/src/helma/main/ApplicationManager.java @@ -5,6 +5,7 @@ package helma.main; import java.util.Hashtable; import java.util.Enumeration; +import java.util.Properties; import java.io.*; import java.lang.reflect.*; import java.rmi.*; @@ -26,6 +27,7 @@ import javax.servlet.Servlet; public class ApplicationManager { private Hashtable applications; + private Properties mountpoints; private int port; private File hopHome; private SystemProperties props; @@ -39,14 +41,8 @@ public class ApplicationManager { this.props = props; this.server = server; applications = new Hashtable (); + mountpoints = new Properties (); lastModified = 0; - /* tomcat = new EmbeddedTomcat(); - tomcat.setPath("/Users/hannes/Desktop/jakarta-tomcat-4.0.3/test"); - try { - tomcat.startTomcat(); - } catch (Exception x) { - System.err.println ("Error starting Tomcat: "+x); - } */ } @@ -64,12 +60,34 @@ public class ApplicationManager { // then stop deleted ones for (Enumeration e = applications.keys(); e.hasMoreElements (); ) { String appName = (String) e.nextElement (); + // check if application has been removed and should be stopped if (!props.containsKey (appName)) { stop (appName); + } else if (server.websrv != null) { + // check if application should be remounted at a + // different location on embedded web server + String oldMountpoint = mountpoints.getProperty (appName); + String mountpoint = props.getProperty (appName+".mountpoint"); + if (mountpoint == null || "".equals (mountpoint.trim())) + mountpoint = "/"+URLEncoder.encode(appName); + if (!mountpoint.equals (oldMountpoint)) { + if ("/".equals (oldMountpoint)) + server.websrv.removeDefaultServlet (); + else + server.websrv.removeServlet (oldMountpoint+"/*"); + Application app = (Application) applications.get (appName); + app.setBaseURI (mountpoint); + EmbeddedServletClient servlet = new EmbeddedServletClient (appName, mountpoint); + if ("/".equals (mountpoint)) + server.websrv.setDefaultServlet (servlet); + else + server.websrv.addServlet (mountpoint+"/*", servlet); + mountpoints.setProperty (appName, mountpoint); + } } } } catch (Exception mx) { - Server.getLogger().log ("Error starting applications: "+mx); + Server.getLogger().log ("Error checking applications: "+mx); } lastModified = System.currentTimeMillis (); @@ -79,13 +97,8 @@ public class ApplicationManager { void start (String appName) { Server.getLogger().log ("Building application "+appName); try { - String mountpoint = props.getProperty (appName+".mountpoint", - "/"+URLEncoder.encode(appName)); Application app = new Application (appName, hopHome, Server.sysProps, Server.dbProps); applications.put (appName, app); - // if we're running with the embedded web server, set app base uri to /appname - if (server.websrv != null) - app.setBaseURI (mountpoint); // the application is started later in the register method, when it's bound app.init (); } catch (Exception x) { @@ -101,9 +114,9 @@ public class ApplicationManager { if (server.websrv == null) { Naming.unbind ("//:"+port+"/"+appName); } else { - String mountpoint = props.getProperty (appName+".mountpoint", - "/"+URLEncoder.encode(appName)); - // server.websrv.removeServlet ("/"+appName+"/"); + String mountpoint = mountpoints.getProperty (appName); + if (mountpoint == null || "".equals (mountpoint.trim())) + mountpoint = "/"+URLEncoder.encode(appName); if ("/".equals (mountpoint)) server.websrv.removeDefaultServlet (); else @@ -124,16 +137,20 @@ public class ApplicationManager { if (server.websrv == null) { Naming.rebind ("//:"+port+"/"+appName, app); } else { - String mountpoint = props.getProperty (appName+".mountpoint", - "/"+URLEncoder.encode(appName)); + String mountpoint = props.getProperty (appName+".mountpoint"); + if (mountpoint == null || "".equals (mountpoint.trim())) + mountpoint = "/"+URLEncoder.encode(appName); + // set application URL prefix + app.setBaseURI (mountpoint); + // is the application mounted on the server root? boolean isRoot = "/".equals (mountpoint); - EmbeddedServletClient servlet = new EmbeddedServletClient (appName, isRoot); - if (isRoot) + EmbeddedServletClient servlet = new EmbeddedServletClient (appName, mountpoint); + if (isRoot) { server.websrv.setDefaultServlet (servlet); - else { + } else { server.websrv.addServlet (mountpoint+"/*", servlet); } - // tomcat.addApplication (appName); + mountpoints.setProperty (appName, mountpoint); } app.start (); } catch (Exception x) { diff --git a/src/helma/servlet/EmbeddedServletClient.java b/src/helma/servlet/EmbeddedServletClient.java index e8d2a5e2..f46ab8e0 100644 --- a/src/helma/servlet/EmbeddedServletClient.java +++ b/src/helma/servlet/EmbeddedServletClient.java @@ -21,17 +21,16 @@ public final class EmbeddedServletClient extends AbstractServletClient { private Application app = null; private String appName; - // tells us whether the application is mounted as root or by its name - // depending on this we know whether we have to transform the request path - boolean root; + // The path where this servlet is mounted + String servletPath; public EmbeddedServletClient () { super (); } - public EmbeddedServletClient (String appName, boolean isRoot) { + public EmbeddedServletClient (String appName, String servletPath) { this.appName = appName; - this.root = isRoot; + this.servletPath = servletPath; } public void init (ServletConfig init) throws ServletException { @@ -59,11 +58,9 @@ public final class EmbeddedServletClient extends AbstractServletClient { String getRequestPath (String path) { if (path == null) return ""; - if (root) - return trim (path); - int appInPath = path.indexOf (appName); - if (appInPath > 0) - return trim (path.substring (appInPath+appName.length())); + int pathMatch = path.indexOf (servletPath); + if (pathMatch > -1) + return trim (path.substring (pathMatch+servletPath.length())); else return trim (path); }