Specifying the mountpoint for applications on the embedded web server

now actually works. (Servlet path translation was broken).
Application manager is now able to move applications on the embedded web server,
i.e. if appname.mountpoint is modified, the application is re-mounted at the new
location.
This commit is contained in:
hns 2002-07-12 18:57:07 +00:00
parent 17a0a7a93c
commit 6978b08841
2 changed files with 46 additions and 32 deletions

View file

@ -5,6 +5,7 @@ package helma.main;
import java.util.Hashtable; import java.util.Hashtable;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.Properties;
import java.io.*; import java.io.*;
import java.lang.reflect.*; import java.lang.reflect.*;
import java.rmi.*; import java.rmi.*;
@ -26,6 +27,7 @@ import javax.servlet.Servlet;
public class ApplicationManager { public class ApplicationManager {
private Hashtable applications; private Hashtable applications;
private Properties mountpoints;
private int port; private int port;
private File hopHome; private File hopHome;
private SystemProperties props; private SystemProperties props;
@ -39,14 +41,8 @@ public class ApplicationManager {
this.props = props; this.props = props;
this.server = server; this.server = server;
applications = new Hashtable (); applications = new Hashtable ();
mountpoints = new Properties ();
lastModified = 0; 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 // then stop deleted ones
for (Enumeration e = applications.keys(); e.hasMoreElements (); ) { for (Enumeration e = applications.keys(); e.hasMoreElements (); ) {
String appName = (String) e.nextElement (); String appName = (String) e.nextElement ();
// check if application has been removed and should be stopped
if (!props.containsKey (appName)) { if (!props.containsKey (appName)) {
stop (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) { } catch (Exception mx) {
Server.getLogger().log ("Error starting applications: "+mx); Server.getLogger().log ("Error checking applications: "+mx);
} }
lastModified = System.currentTimeMillis (); lastModified = System.currentTimeMillis ();
@ -79,13 +97,8 @@ public class ApplicationManager {
void start (String appName) { void start (String appName) {
Server.getLogger().log ("Building application "+appName); Server.getLogger().log ("Building application "+appName);
try { try {
String mountpoint = props.getProperty (appName+".mountpoint",
"/"+URLEncoder.encode(appName));
Application app = new Application (appName, hopHome, Server.sysProps, Server.dbProps); Application app = new Application (appName, hopHome, Server.sysProps, Server.dbProps);
applications.put (appName, app); 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 // the application is started later in the register method, when it's bound
app.init (); app.init ();
} catch (Exception x) { } catch (Exception x) {
@ -101,9 +114,9 @@ public class ApplicationManager {
if (server.websrv == null) { if (server.websrv == null) {
Naming.unbind ("//:"+port+"/"+appName); Naming.unbind ("//:"+port+"/"+appName);
} else { } else {
String mountpoint = props.getProperty (appName+".mountpoint", String mountpoint = mountpoints.getProperty (appName);
"/"+URLEncoder.encode(appName)); if (mountpoint == null || "".equals (mountpoint.trim()))
// server.websrv.removeServlet ("/"+appName+"/"); mountpoint = "/"+URLEncoder.encode(appName);
if ("/".equals (mountpoint)) if ("/".equals (mountpoint))
server.websrv.removeDefaultServlet (); server.websrv.removeDefaultServlet ();
else else
@ -124,16 +137,20 @@ public class ApplicationManager {
if (server.websrv == null) { if (server.websrv == null) {
Naming.rebind ("//:"+port+"/"+appName, app); Naming.rebind ("//:"+port+"/"+appName, app);
} else { } else {
String mountpoint = props.getProperty (appName+".mountpoint", String mountpoint = props.getProperty (appName+".mountpoint");
"/"+URLEncoder.encode(appName)); 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); boolean isRoot = "/".equals (mountpoint);
EmbeddedServletClient servlet = new EmbeddedServletClient (appName, isRoot); EmbeddedServletClient servlet = new EmbeddedServletClient (appName, mountpoint);
if (isRoot) if (isRoot) {
server.websrv.setDefaultServlet (servlet); server.websrv.setDefaultServlet (servlet);
else { } else {
server.websrv.addServlet (mountpoint+"/*", servlet); server.websrv.addServlet (mountpoint+"/*", servlet);
} }
// tomcat.addApplication (appName); mountpoints.setProperty (appName, mountpoint);
} }
app.start (); app.start ();
} catch (Exception x) { } catch (Exception x) {

View file

@ -21,17 +21,16 @@ public final class EmbeddedServletClient extends AbstractServletClient {
private Application app = null; private Application app = null;
private String appName; private String appName;
// tells us whether the application is mounted as root or by its name // The path where this servlet is mounted
// depending on this we know whether we have to transform the request path String servletPath;
boolean root;
public EmbeddedServletClient () { public EmbeddedServletClient () {
super (); super ();
} }
public EmbeddedServletClient (String appName, boolean isRoot) { public EmbeddedServletClient (String appName, String servletPath) {
this.appName = appName; this.appName = appName;
this.root = isRoot; this.servletPath = servletPath;
} }
public void init (ServletConfig init) throws ServletException { public void init (ServletConfig init) throws ServletException {
@ -59,11 +58,9 @@ public final class EmbeddedServletClient extends AbstractServletClient {
String getRequestPath (String path) { String getRequestPath (String path) {
if (path == null) if (path == null)
return ""; return "";
if (root) int pathMatch = path.indexOf (servletPath);
return trim (path); if (pathMatch > -1)
int appInPath = path.indexOf (appName); return trim (path.substring (pathMatch+servletPath.length()));
if (appInPath > 0)
return trim (path.substring (appInPath+appName.length()));
else else
return trim (path); return trim (path);
} }