Factor out jetty server into JettyServer wrapper class and duplicate

InetAddrPort as InetEndpoint to avoid direct dependency of helma.main.Server 
on Jetty 4, which may be problematic when running Helma apps through 
helma.servlet.StandaloneServletClient within a servlet container.
This commit is contained in:
hns 2008-11-05 12:49:06 +00:00
parent 72341e832e
commit 57b5275705
4 changed files with 189 additions and 91 deletions

View file

@ -42,7 +42,7 @@ public class ApplicationManager implements XmlRpcHandler {
private ResourceProperties props; private ResourceProperties props;
private Server server; private Server server;
private long lastModified; private long lastModified;
private HttpServer httpServer = null; private JettyServer jetty = null;
/** /**
* Creates a new ApplicationManager object. * Creates a new ApplicationManager object.
@ -60,7 +60,7 @@ public class ApplicationManager implements XmlRpcHandler {
applications = new Hashtable(); applications = new Hashtable();
xmlrpcHandlers = new Hashtable(); xmlrpcHandlers = new Hashtable();
lastModified = 0; lastModified = 0;
httpServer = server.http; jetty = server.jetty;
} }
/** /**
@ -88,7 +88,7 @@ public class ApplicationManager implements XmlRpcHandler {
// check if application has been removed and should be stopped // check if application has been removed and should be stopped
if (!props.containsKey(appDesc.appName)) { if (!props.containsKey(appDesc.appName)) {
appDesc.stop(); appDesc.stop();
} else if (server.http != null) { } else if (server.jetty != null) {
// If application continues to run, remount // If application continues to run, remount
// as the mounting options may have changed. // as the mounting options may have changed.
AppDescriptor ndesc = new AppDescriptor(appDesc.appName); AppDescriptor ndesc = new AppDescriptor(appDesc.appName);
@ -471,9 +471,9 @@ public class ApplicationManager implements XmlRpcHandler {
} }
// bind to Jetty HTTP server // bind to Jetty HTTP server
if (httpServer != null) { if (jetty != null) {
HttpContext context = httpServer.addContext(pathPattern); HttpContext context = jetty.addContext(pathPattern);
if (encode) { if (encode) {
// FIXME: ContentEncodingHandler is broken/removed in Jetty 4.2 // FIXME: ContentEncodingHandler is broken/removed in Jetty 4.2
@ -534,7 +534,7 @@ public class ApplicationManager implements XmlRpcHandler {
getLogger().info("Mounting static at " + getLogger().info("Mounting static at " +
staticMountpoint); staticMountpoint);
context = httpServer.addContext(staticMountpoint); context = jetty.addContext(staticMountpoint);
context.setWelcomeFiles(staticHome); context.setWelcomeFiles(staticHome);
context.setResourceBase(staticContent.getPath()); context.setResourceBase(staticContent.getPath());
@ -566,8 +566,8 @@ public class ApplicationManager implements XmlRpcHandler {
} }
// unbind from Jetty HTTP server // unbind from Jetty HTTP server
if (httpServer != null) { if (jetty != null) {
HttpContext context = httpServer.getContext(null, pathPattern); HttpContext context = jetty.getContext(pathPattern);
if (context != null) { if (context != null) {
context.stop(); context.stop();
@ -575,7 +575,7 @@ public class ApplicationManager implements XmlRpcHandler {
} }
if (staticDir != null) { if (staticDir != null) {
context = httpServer.getContext(null, staticMountpoint); context = jetty.getContext(staticMountpoint);
if (context != null) { if (context != null) {
context.stop(); context.stop();

View file

@ -0,0 +1,116 @@
/*
* Helma License Notice
*
* The contents of this file are subject to the Helma License
* Version 2.0 (the "License"). You may not use this file except in
* compliance with the License. A copy of the License is available at
* http://adele.helma.org/download/helma/license.txt
*
* Copyright 1998-2003 Helma Software. All Rights Reserved.
*
* $RCSfile$
* $Author$
* $Revision$
* $Date$
*/
package helma.main;
import org.mortbay.http.HttpServer;
import org.mortbay.http.HttpContext;
import org.mortbay.http.ajp.AJP13Listener;
import org.mortbay.util.InetAddrPort;
import java.util.StringTokenizer;
import java.net.URL;
import java.net.MalformedURLException;
import java.io.IOException;
public class JettyServer {
// the embedded web server
protected HttpServer http;
// the AJP13 Listener, used for connecting from external webserver to servlet via JK
protected AJP13Listener ajp13;
public static JettyServer init(Server server)
throws MalformedURLException, IOException {
if (server.configFile != null && server.configFile.exists()) {
return new JettyServer(server.configFile.toURI().toURL());
} else if (server.websrvPort != null || server.ajp13Port != null) {
return new JettyServer(server.websrvPort, server.ajp13Port, server);
}
return null;
}
private JettyServer(URL url) throws IOException {
http = new org.mortbay.jetty.Server(url);
}
private JettyServer(InetEndpoint webPort, InetEndpoint ajpPort, Server server)
throws IOException {
http = new HttpServer();
// start embedded web server if port is specified
if (webPort != null) {
http.addListener(new InetAddrPort(webPort.getInetAddress(), webPort.getPort()));
}
// activate the ajp13-listener
if (ajpPort != null) {
// create AJP13Listener
ajp13 = new AJP13Listener(new InetAddrPort(ajpPort.getInetAddress(), ajpPort.getPort()));
ajp13.setHttpServer(http);
String jkallow = server.sysProps.getProperty("allowAJP13");
// by default the AJP13-connection just accepts requests from 127.0.0.1
if (jkallow == null) {
jkallow = "127.0.0.1";
}
StringTokenizer st = new StringTokenizer(jkallow, " ,;");
String[] jkallowarr = new String[st.countTokens()];
int cnt = 0;
while (st.hasMoreTokens()) {
jkallowarr[cnt] = st.nextToken();
cnt++;
}
ajp13.setRemoteServers(jkallowarr);
server.getLogger().info("Starting AJP13-Listener on port " + (ajpPort));
}
}
public HttpServer getHttpServer() {
return http;
}
public HttpContext getContext(String contextPath) {
return http.getContext(contextPath);
}
public HttpContext addContext(String contextPath) {
return http.addContext(contextPath);
}
public void start() throws Exception {
http.start();
if (ajp13 != null) {
ajp13.start();
}
}
public void stop() throws InterruptedException {
http.stop();
if (ajp13 != null) {
ajp13.stop();
}
}
public void destroy() {
http.destroy();
}
}

View file

@ -24,9 +24,6 @@ import helma.util.*;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.apache.xmlrpc.*; import org.apache.xmlrpc.*;
import org.mortbay.http.*;
import org.mortbay.http.ajp.*;
import org.mortbay.util.InetAddrPort;
import java.io.*; import java.io.*;
import java.rmi.registry.*; import java.rmi.registry.*;
@ -72,10 +69,10 @@ public class Server implements Runnable {
private Thread mainThread; private Thread mainThread;
// server ports // server ports
InetAddrPort rmiPort = null; InetEndpoint rmiPort = null;
InetAddrPort xmlrpcPort = null; InetEndpoint xmlrpcPort = null;
InetAddrPort websrvPort = null; InetEndpoint websrvPort = null;
InetAddrPort ajp13Port = null; InetEndpoint ajp13Port = null;
// Jetty configuration file // Jetty configuration file
File configFile = null; File configFile = null;
@ -85,10 +82,7 @@ public class Server implements Runnable {
// the embedded web server // the embedded web server
// protected Serve websrv; // protected Serve websrv;
protected HttpServer http; protected JettyServer jetty;
// the AJP13 Listener, used for connecting from external webserver to servlet via JK
protected AJP13Listener ajp13;
// the XML-RPC server // the XML-RPC server
protected WebServer xmlrpc; protected WebServer xmlrpc;
@ -195,7 +189,7 @@ public class Server implements Runnable {
// check if there's a property setting for those ports not specified via command line // check if there's a property setting for those ports not specified via command line
if (!config.hasWebsrvPort() && sysProps.getProperty("webPort") != null) { if (!config.hasWebsrvPort() && sysProps.getProperty("webPort") != null) {
try { try {
config.setWebsrvPort(new InetAddrPort(sysProps.getProperty("webPort"))); config.setWebsrvPort(new InetEndpoint(sysProps.getProperty("webPort")));
} catch (Exception portx) { } catch (Exception portx) {
throw new Exception("Error parsing web server port property from server.properties: " + portx); throw new Exception("Error parsing web server port property from server.properties: " + portx);
} }
@ -203,7 +197,7 @@ public class Server implements Runnable {
if (!config.hasAjp13Port() && sysProps.getProperty("ajp13Port") != null) { if (!config.hasAjp13Port() && sysProps.getProperty("ajp13Port") != null) {
try { try {
config.setAjp13Port(new InetAddrPort(sysProps.getProperty("ajp13Port"))); config.setAjp13Port(new InetEndpoint(sysProps.getProperty("ajp13Port")));
} catch (Exception portx) { } catch (Exception portx) {
throw new Exception("Error parsing AJP1.3 server port property from server.properties: " + portx); throw new Exception("Error parsing AJP1.3 server port property from server.properties: " + portx);
} }
@ -211,7 +205,7 @@ public class Server implements Runnable {
if (!config.hasRmiPort() && sysProps.getProperty("rmiPort") != null) { if (!config.hasRmiPort() && sysProps.getProperty("rmiPort") != null) {
try { try {
config.setRmiPort(new InetAddrPort(sysProps.getProperty("rmiPort"))); config.setRmiPort(new InetEndpoint(sysProps.getProperty("rmiPort")));
} catch (Exception portx) { } catch (Exception portx) {
throw new Exception("Error parsing RMI server port property from server.properties: " + portx); throw new Exception("Error parsing RMI server port property from server.properties: " + portx);
} }
@ -219,7 +213,7 @@ public class Server implements Runnable {
if (!config.hasXmlrpcPort() && sysProps.getProperty("xmlrpcPort") != null) { if (!config.hasXmlrpcPort() && sysProps.getProperty("xmlrpcPort") != null) {
try { try {
config.setXmlrpcPort(new InetAddrPort(sysProps.getProperty("xmlrpcPort"))); config.setXmlrpcPort(new InetEndpoint(sysProps.getProperty("xmlrpcPort")));
} catch (Exception portx) { } catch (Exception portx) {
throw new Exception("Error parsing XML-RPC server port property from server.properties: " + portx); throw new Exception("Error parsing XML-RPC server port property from server.properties: " + portx);
} }
@ -242,25 +236,25 @@ public class Server implements Runnable {
config.setPropFile(new File(args[++i])); config.setPropFile(new File(args[++i]));
} else if (args[i].equals("-p") && ((i + 1) < args.length)) { } else if (args[i].equals("-p") && ((i + 1) < args.length)) {
try { try {
config.setRmiPort(new InetAddrPort(args[++i])); config.setRmiPort(new InetEndpoint(args[++i]));
} catch (Exception portx) { } catch (Exception portx) {
throw new Exception("Error parsing RMI server port property: " + portx); throw new Exception("Error parsing RMI server port property: " + portx);
} }
} else if (args[i].equals("-x") && ((i + 1) < args.length)) { } else if (args[i].equals("-x") && ((i + 1) < args.length)) {
try { try {
config.setXmlrpcPort(new InetAddrPort(args[++i])); config.setXmlrpcPort(new InetEndpoint(args[++i]));
} catch (Exception portx) { } catch (Exception portx) {
throw new Exception("Error parsing XML-RPC server port property: " + portx); throw new Exception("Error parsing XML-RPC server port property: " + portx);
} }
} else if (args[i].equals("-w") && ((i + 1) < args.length)) { } else if (args[i].equals("-w") && ((i + 1) < args.length)) {
try { try {
config.setWebsrvPort(new InetAddrPort(args[++i])); config.setWebsrvPort(new InetEndpoint(args[++i]));
} catch (Exception portx) { } catch (Exception portx) {
throw new Exception("Error parsing web server port property: " + portx); throw new Exception("Error parsing web server port property: " + portx);
} }
} else if (args[i].equals("-jk") && ((i + 1) < args.length)) { } else if (args[i].equals("-jk") && ((i + 1) < args.length)) {
try { try {
config.setAjp13Port(new InetAddrPort(args[++i])); config.setAjp13Port(new InetEndpoint(args[++i]));
} catch (Exception portx) { } catch (Exception portx) {
throw new Exception("Error parsing AJP1.3 server port property: " + portx); throw new Exception("Error parsing AJP1.3 server port property: " + portx);
} }
@ -382,7 +376,7 @@ public class Server implements Runnable {
/** /**
* Check whether a server port is available by trying to open a server socket * Check whether a server port is available by trying to open a server socket
*/ */
private static void checkPort(InetAddrPort addrPort) throws Exception { private static void checkPort(InetEndpoint addrPort) throws Exception {
InetAddress addr = addrPort.getInetAddress(); InetAddress addr = addrPort.getInetAddress();
int port = addrPort.getPort(); int port = addrPort.getPort();
if (addr == null) { if (addr == null) {
@ -524,10 +518,10 @@ public class Server implements Runnable {
appManager.stopAll(); appManager.stopAll();
if (http != null) { if (jetty != null) {
try { try {
http.stop(); jetty.stop();
http.destroy(); jetty.destroy();
} catch (InterruptedException irx) { } catch (InterruptedException irx) {
// http.stop() interrupted by another thread. ignore. // http.stop() interrupted by another thread. ignore.
} }
@ -567,42 +561,7 @@ public class Server implements Runnable {
*/ */
public void run() { public void run() {
try { try {
if (configFile != null && configFile.exists()) { jetty = JettyServer.init(this);
http = new org.mortbay.jetty.Server(configFile.toURI().toURL());
} else if ((websrvPort != null) || (ajp13Port != null)) {
http = new HttpServer();
// start embedded web server if port is specified
if (websrvPort != null) {
http.addListener(websrvPort);
}
// activate the ajp13-listener
if (ajp13Port != null) {
// create AJP13Listener
ajp13 = new AJP13Listener(ajp13Port);
ajp13.setHttpServer(http);
String jkallow = sysProps.getProperty("allowAJP13");
// by default the AJP13-connection just accepts requests from 127.0.0.1
if (jkallow == null) {
jkallow = "127.0.0.1";
}
StringTokenizer st = new StringTokenizer(jkallow, " ,;");
String[] jkallowarr = new String[st.countTokens()];
int cnt = 0;
while (st.hasMoreTokens()) {
jkallowarr[cnt] = st.nextToken();
cnt++;
}
ajp13.setRemoteServers(jkallowarr);
logger.info("Starting AJP13-Listener on port " + (ajp13Port));
}
}
if (xmlrpcPort != null) { if (xmlrpcPort != null) {
String xmlparser = sysProps.getProperty("xmlparser"); String xmlparser = sysProps.getProperty("xmlparser");
@ -689,22 +648,14 @@ public class Server implements Runnable {
} }
// start embedded web server // start embedded web server
if (http != null) { if (jetty != null) {
try { try {
http.start(); jetty.start();
} catch (Exception m) { } catch (Exception m) {
throw new RuntimeException("Error starting embedded web server", m); throw new RuntimeException("Error starting embedded web server", m);
} }
} }
if (ajp13 != null) {
try {
ajp13.start();
} catch (Exception m) {
throw new RuntimeException("Error starting AJP13 listener: " + m);
}
}
// start applications // start applications
appManager.startAll(); appManager.startAll();
@ -894,4 +845,36 @@ public class Server implements Runnable {
} }
} }
class InetEndpoint {
InetAddress addr;
int port;
public InetEndpoint(String inetAddrPort)
throws java.net.UnknownHostException {
int c = inetAddrPort.indexOf(':');
if (c >= 0) {
String addr = inetAddrPort.substring(0, c);
if (addr.indexOf('/') > 0)
addr = addr.substring(addr.indexOf('/') + 1);
inetAddrPort = inetAddrPort.substring(c + 1);
if (addr.length() > 0 && !"0.0.0.0".equals(addr)) {
this.addr = InetAddress.getByName(addr);
}
}
this.port = Integer.parseInt(inetAddrPort);
}
public InetAddress getInetAddress() {
return addr;
}
public int getPort() {
return port;
}
}

View file

@ -16,7 +16,6 @@
package helma.main; package helma.main;
import org.mortbay.util.InetAddrPort;
import java.io.File; import java.io.File;
/** /**
@ -25,10 +24,10 @@ import java.io.File;
public class ServerConfig { public class ServerConfig {
private InetAddrPort rmiPort = null; private InetEndpoint rmiPort = null;
private InetAddrPort xmlrpcPort = null; private InetEndpoint xmlrpcPort = null;
private InetAddrPort websrvPort = null; private InetEndpoint websrvPort = null;
private InetAddrPort ajp13Port = null; private InetEndpoint ajp13Port = null;
private File propFile = null; private File propFile = null;
private File homeDir = null; private File homeDir = null;
private File configFile = null; private File configFile = null;
@ -57,35 +56,35 @@ public class ServerConfig {
return (ajp13Port != null); return (ajp13Port != null);
} }
public InetAddrPort getRmiPort() { public InetEndpoint getRmiPort() {
return rmiPort; return rmiPort;
} }
public void setRmiPort(InetAddrPort rmiPort) { public void setRmiPort(InetEndpoint rmiPort) {
this.rmiPort = rmiPort; this.rmiPort = rmiPort;
} }
public InetAddrPort getXmlrpcPort() { public InetEndpoint getXmlrpcPort() {
return xmlrpcPort; return xmlrpcPort;
} }
public void setXmlrpcPort(InetAddrPort xmlrpcPort) { public void setXmlrpcPort(InetEndpoint xmlrpcPort) {
this.xmlrpcPort = xmlrpcPort; this.xmlrpcPort = xmlrpcPort;
} }
public InetAddrPort getWebsrvPort() { public InetEndpoint getWebsrvPort() {
return websrvPort; return websrvPort;
} }
public void setWebsrvPort(InetAddrPort websrvPort) { public void setWebsrvPort(InetEndpoint websrvPort) {
this.websrvPort = websrvPort; this.websrvPort = websrvPort;
} }
public InetAddrPort getAjp13Port() { public InetEndpoint getAjp13Port() {
return ajp13Port; return ajp13Port;
} }
public void setAjp13Port(InetAddrPort ajp13Port) { public void setAjp13Port(InetEndpoint ajp13Port) {
this.ajp13Port = ajp13Port; this.ajp13Port = ajp13Port;
} }