Switch to Jetty for embedded web serving needs.

This commit is contained in:
hns 2002-07-24 18:37:20 +00:00
parent 20ea4b874f
commit 53410917bd
2 changed files with 84 additions and 48 deletions

View file

@ -16,7 +16,11 @@ import helma.framework.core.*;
import helma.objectmodel.*; import helma.objectmodel.*;
import helma.servlet.*; import helma.servlet.*;
import helma.util.SystemProperties; import helma.util.SystemProperties;
import Acme.Serve.*; // import Acme.Serve.*;
import org.mortbay.http.*;
import org.mortbay.http.handler.*;
import org.mortbay.jetty.servlet.*;
import org.mortbay.util.*;
import javax.servlet.Servlet; import javax.servlet.Servlet;
@ -63,27 +67,29 @@ public class ApplicationManager {
// check if application has been removed and should be stopped // 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) { } else if (server.http != null) {
// check if application should be remounted at a // check if application should be remounted at a
// different location on embedded web server // different location on embedded web server
String oldMountpoint = mountpoints.getProperty (appName); String oldMountpoint = mountpoints.getProperty (appName);
String mountpoint = props.getProperty (appName+".mountpoint"); String mountpoint = getMountpoint (appName);
if (mountpoint == null || "".equals (mountpoint.trim())) String pattern = getPathPattern (mountpoint);
mountpoint = "/"+URLEncoder.encode(appName); if (!pattern.equals (oldMountpoint)) {
if (!mountpoint.equals (oldMountpoint)) { Server.getLogger().log("Moving application "+appName+" from "+oldMountpoint+" to "+pattern);
Server.getLogger().log("Moving application "+appName+" from "+oldMountpoint+" to "+mountpoint); HandlerContext oldContext = server.http.getContext (null, oldMountpoint);
if ("/".equals (oldMountpoint)) if (oldContext != null) {
server.websrv.removeDefaultServlet (); oldContext.stop ();
else oldContext.destroy ();
server.websrv.removeServlet (oldMountpoint+"/*"); }
Application app = (Application) applications.get (appName); Application app = (Application) applications.get (appName);
app.setBaseURI (mountpoint); app.setBaseURI (mountpoint);
EmbeddedServletClient servlet = new EmbeddedServletClient (appName, mountpoint); ServletHandlerContext context = new ServletHandlerContext (server.http, pattern);
if ("/".equals (mountpoint)) server.http.addContext (null, context);
server.websrv.setDefaultServlet (servlet); ServletHolder holder = context.addServlet (appName, "/*", "helma.servlet.EmbeddedServletClient");
else holder.setInitParameter ("application", appName);
server.websrv.addServlet (mountpoint+"/*", servlet); holder.setInitParameter ("mountpoint", mountpoint);
mountpoints.setProperty (appName, mountpoint); // holder.start ();
context.start ();
mountpoints.setProperty (appName, pattern);
} }
} }
} }
@ -112,16 +118,15 @@ public class ApplicationManager {
Server.getLogger().log ("Stopping application "+appName); Server.getLogger().log ("Stopping application "+appName);
try { try {
Application app = (Application) applications.get (appName); Application app = (Application) applications.get (appName);
if (server.websrv == null) { if (server.http == null) {
Naming.unbind ("//:"+port+"/"+appName); Naming.unbind ("//:"+port+"/"+appName);
} else { } else {
String mountpoint = mountpoints.getProperty (appName); String mountpoint = mountpoints.getProperty (appName);
if (mountpoint == null || "".equals (mountpoint.trim())) HandlerContext context = server.http.getContext (null, mountpoint);
mountpoint = "/"+URLEncoder.encode(appName); if (context != null) {
if ("/".equals (mountpoint)) context.stop ();
server.websrv.removeDefaultServlet (); context.destroy ();
else }
server.websrv.removeServlet (mountpoint+"/*");
} }
app.stop (); app.stop ();
Server.getLogger().log ("Unregistered application "+appName); Server.getLogger().log ("Unregistered application "+appName);
@ -135,27 +140,26 @@ public class ApplicationManager {
try { try {
Server.getLogger().log ("Binding application "+appName); Server.getLogger().log ("Binding application "+appName);
Application app = (Application) applications.get (appName); Application app = (Application) applications.get (appName);
if (server.websrv == null) { if (server.http == null) {
Naming.rebind ("//:"+port+"/"+appName, app); Naming.rebind ("//:"+port+"/"+appName, app);
} else { } else {
String mountpoint = props.getProperty (appName+".mountpoint"); String mountpoint = getMountpoint (appName);
if (mountpoint == null || "".equals (mountpoint.trim()))
mountpoint = "/"+URLEncoder.encode(appName);
// set application URL prefix // set application URL prefix
app.setBaseURI (mountpoint); app.setBaseURI (mountpoint);
// is the application mounted on the server root? String pattern = getPathPattern (mountpoint);
boolean isRoot = "/".equals (mountpoint); ServletHandlerContext context = new ServletHandlerContext (server.http, pattern);
EmbeddedServletClient servlet = new EmbeddedServletClient (appName, mountpoint); server.http.addContext (null, context);
if (isRoot) { ServletHolder holder = context.addServlet (appName, "/*", "helma.servlet.EmbeddedServletClient");
server.websrv.setDefaultServlet (servlet); holder.setInitParameter ("application", appName);
} else { holder.setInitParameter ("mountpoint", mountpoint);
server.websrv.addServlet (mountpoint+"/*", servlet); // holder.start ();
} context.start ();
mountpoints.setProperty (appName, mountpoint); mountpoints.setProperty (appName, pattern);
} }
app.start (); app.start ();
} catch (Exception x) { } catch (Exception x) {
Server.getLogger().log ("Couldn't register and start app: "+x); Server.getLogger().log ("Couldn't register and start app: "+x);
x.printStackTrace ();
} }
} }
@ -171,12 +175,14 @@ public class ApplicationManager {
if (appName.indexOf (".") == -1) if (appName.indexOf (".") == -1)
register (appName); register (appName);
} }
if (server.websrv != null) { if (server.http != null) {
// add handler for static files.
File staticContent = new File (server.getHopHome(), "static"); File staticContent = new File (server.getHopHome(), "static");
Server.getLogger().log("Serving static content from "+staticContent.getAbsolutePath()); Server.getLogger().log("Serving static content from "+staticContent.getAbsolutePath());
AcmeFileServlet fsrv = new AcmeFileServlet (staticContent); HandlerContext context = server.http.addContext ("/static/*");
server.websrv.addServlet ("/static/", fsrv); context.setResourceBase (staticContent.getAbsolutePath());
server.websrv.addServlet ("/static/*", fsrv); context.setServingResources (true);
context.start ();
} }
lastModified = System.currentTimeMillis (); lastModified = System.currentTimeMillis ();
} catch (Exception mx) { } catch (Exception mx) {
@ -199,4 +205,24 @@ public class ApplicationManager {
return (Application)applications.get(name); return (Application)applications.get(name);
} }
private String getMountpoint (String appName) {
String mountpoint = props.getProperty (appName+".mountpoint");
if (mountpoint == null)
return "/"+URLEncoder.encode(appName);
mountpoint = mountpoint.trim ();
if ("".equals (mountpoint))
return "/";
else if (!mountpoint.startsWith ("/"))
return "/"+mountpoint;
return mountpoint;
}
private String getPathPattern (String mountpoint) {
if ("/".equals (mountpoint))
return "/";
if (!mountpoint.endsWith ("/"))
return mountpoint+"/*";
return mountpoint+"*";
}
} }

View file

@ -15,7 +15,9 @@ import helma.framework.*;
import helma.framework.core.*; import helma.framework.core.*;
import helma.xmlrpc.*; import helma.xmlrpc.*;
import helma.util.*; import helma.util.*;
import Acme.Serve.Serve; // import Acme.Serve.Serve;
import org.mortbay.http.*;
import org.mortbay.util.*;
/** /**
@ -59,7 +61,8 @@ import Acme.Serve.Serve;
private static Logger logger; private static Logger logger;
// the embedded web server // the embedded web server
protected Serve websrv; // protected Serve websrv;
protected HttpServer http;
// the XML-RPC server // the XML-RPC server
protected WebServer xmlrpc; protected WebServer xmlrpc;
@ -242,7 +245,9 @@ import Acme.Serve.Serve;
// start embedded web server if port is specified // start embedded web server if port is specified
if (websrvPort > 0) { if (websrvPort > 0) {
websrv = new Acme.Serve.Serve (websrvPort, sysProps); // websrv = new Acme.Serve.Serve (websrvPort, sysProps);
http = new HttpServer ();
http.addListener (new InetAddrPort (websrvPort));
} }
String xmlparser = sysProps.getProperty ("xmlparser"); String xmlparser = sysProps.getProperty ("xmlparser");
@ -274,7 +279,7 @@ import Acme.Serve.Serve;
RMISocketFactory.setSocketFactory (factory); RMISocketFactory.setSocketFactory (factory);
} }
if (websrv == null) { if (http == null) {
getLogger().log ("Starting RMI server on port "+rmiPort); getLogger().log ("Starting RMI server on port "+rmiPort);
LocateRegistry.createRegistry (rmiPort); LocateRegistry.createRegistry (rmiPort);
} }
@ -294,9 +299,14 @@ import Acme.Serve.Serve;
appManager.startAll (); appManager.startAll ();
// start embedded web server // start embedded web server
if (websrv != null) { /* if (websrv != null) {
Thread webthread = new Thread (websrv, "WebServer"); Thread webthread = new Thread (websrv, "WebServer");
webthread.start (); webthread.start ();
} */
if (http != null) try {
http.start ();
} catch (MultiException m) {
getLogger().log ("Error starting embedded web server: "+m);
} }
int count = 0; int count = 0;