* Minor code cleanup.

This commit is contained in:
hns 2008-04-04 11:40:25 +00:00
parent 02b00926ff
commit 35ed7a58f8

View file

@ -21,6 +21,7 @@ import helma.framework.repository.Repository;
import helma.framework.repository.FileRepository; import helma.framework.repository.FileRepository;
import helma.util.StringUtils; import helma.util.StringUtils;
import org.apache.xmlrpc.XmlRpcHandler; import org.apache.xmlrpc.XmlRpcHandler;
import org.apache.commons.logging.Log;
import org.mortbay.http.*; import org.mortbay.http.*;
import org.mortbay.http.handler.*; import org.mortbay.http.handler.*;
import org.mortbay.jetty.servlet.*; import org.mortbay.jetty.servlet.*;
@ -28,6 +29,7 @@ import java.io.*;
import java.rmi.*; import java.rmi.*;
import java.util.*; import java.util.*;
import helma.util.ResourceProperties; import helma.util.ResourceProperties;
import helma.util.Logging;
/** /**
* This class is responsible for starting and stopping Helma applications. * This class is responsible for starting and stopping Helma applications.
@ -40,6 +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;
/** /**
* Creates a new ApplicationManager object. * Creates a new ApplicationManager object.
@ -57,6 +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;
} }
/** /**
@ -87,15 +91,15 @@ public class ApplicationManager implements XmlRpcHandler {
} else if (server.http != null) { } else if (server.http != 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.
appDesc.unbind();
AppDescriptor ndesc = new AppDescriptor(appDesc.appName); AppDescriptor ndesc = new AppDescriptor(appDesc.appName);
ndesc.app = appDesc.app; ndesc.app = appDesc.app;
appDesc.unbind();
ndesc.bind(); ndesc.bind();
descriptors.put(ndesc.appName, ndesc); descriptors.put(ndesc.appName, ndesc);
} }
} }
} catch (Exception mx) { } catch (Exception mx) {
server.getLogger().error("Error checking applications", mx); getLogger().error("Error checking applications", mx);
} }
lastModified = System.currentTimeMillis(); lastModified = System.currentTimeMillis();
@ -159,7 +163,7 @@ public class ApplicationManager implements XmlRpcHandler {
lastModified = System.currentTimeMillis(); lastModified = System.currentTimeMillis();
} catch (Exception mx) { } catch (Exception mx) {
server.getLogger().error("Error starting applications", mx); getLogger().error("Error starting applications", mx);
mx.printStackTrace(); mx.printStackTrace();
} }
} }
@ -273,6 +277,10 @@ public class ApplicationManager implements XmlRpcHandler {
} }
} }
private Log getLogger() {
return server.getLogger();
}
/** /**
* Inner class that describes an application and its start settings. * Inner class that describes an application and its start settings.
*/ */
@ -307,6 +315,7 @@ public class ApplicationManager implements XmlRpcHandler {
/** /**
* Creates an AppDescriptor from the properties. * Creates an AppDescriptor from the properties.
* @param name the application name
*/ */
AppDescriptor(String name) { AppDescriptor(String name) {
ResourceProperties conf = props.getSubProperties(name + '.'); ResourceProperties conf = props.getSubProperties(name + '.');
@ -341,7 +350,7 @@ public class ApplicationManager implements XmlRpcHandler {
ignoreDirs = conf.getProperty("ignore"); ignoreDirs = conf.getProperty("ignore");
// read and configure app repositories // read and configure app repositories
ArrayList repositoryList = new ArrayList(); ArrayList<Repository> repositoryList = new ArrayList<Repository>();
Class[] parameters = { String.class }; Class[] parameters = { String.class };
for (int i = 0; true; i++) { for (int i = 0; true; i++) {
String repositoryArgs = conf.getProperty("repository." + i); String repositoryArgs = conf.getProperty("repository." + i);
@ -364,10 +373,10 @@ public class ApplicationManager implements XmlRpcHandler {
try { try {
Repository newRepository = (Repository) Class.forName(repositoryImpl) Repository newRepository = (Repository) Class.forName(repositoryImpl)
.getConstructor(parameters) .getConstructor(parameters)
.newInstance(new Object[] { repositoryArgs }); .newInstance(repositoryArgs);
repositoryList.add(newRepository); repositoryList.add(newRepository);
} catch (Exception ex) { } catch (Exception ex) {
server.getLogger().error("Adding repository " + repositoryArgs + " failed. " + getLogger().error("Adding repository " + repositoryArgs + " failed. " +
"Will not use that repository. Check your initArgs!", ex); "Will not use that repository. Check your initArgs!", ex);
} }
} else { } else {
@ -388,12 +397,12 @@ public class ApplicationManager implements XmlRpcHandler {
new File(server.getAppsHome(), appName))); new File(server.getAppsHome(), appName)));
} }
repositories = new Repository[repositoryList.size()]; repositories = new Repository[repositoryList.size()];
repositoryList.toArray(repositories); repositories = repositoryList.toArray(repositories);
} }
void start() { void start() {
server.getLogger().info("Building application " + appName); getLogger().info("Building application " + appName);
try { try {
// create the application instance // create the application instance
@ -413,13 +422,13 @@ public class ApplicationManager implements XmlRpcHandler {
app.start(); app.start();
} catch (Exception x) { } catch (Exception x) {
server.getLogger().error("Error creating application " + appName, x); getLogger().error("Error creating application " + appName, x);
x.printStackTrace(); x.printStackTrace();
} }
} }
void stop() { void stop() {
server.getLogger().info("Stopping application " + appName); getLogger().info("Stopping application " + appName);
// unbind application // unbind application
unbind(); unbind();
@ -427,9 +436,9 @@ public class ApplicationManager implements XmlRpcHandler {
// stop application // stop application
try { try {
app.stop(); app.stop();
server.getLogger().info("Stopped application " + appName); getLogger().info("Stopped application " + appName);
} catch (Exception x) { } catch (Exception x) {
server.getLogger().error("Couldn't stop app", x); getLogger().error("Couldn't stop app", x);
} }
descriptors.remove(appName); descriptors.remove(appName);
@ -438,7 +447,7 @@ public class ApplicationManager implements XmlRpcHandler {
void bind() { void bind() {
try { try {
server.getLogger().info("Binding application " + appName); getLogger().info("Binding application " + appName + " :: " + app.hashCode() + " :: " + this.hashCode());
// bind to RMI server // bind to RMI server
if (rmiPort > 0) { if (rmiPort > 0) {
@ -451,14 +460,14 @@ public class ApplicationManager implements XmlRpcHandler {
} }
// bind to Jetty HTTP server // bind to Jetty HTTP server
if (server.http != null) { if (httpServer != null) {
HttpContext context = server.http.addContext(pathPattern); HttpContext context = httpServer.addContext(pathPattern);
if (encode) { if (encode) {
// FIXME: ContentEncodingHandler is broken/removed in Jetty 4.2 // FIXME: ContentEncodingHandler is broken/removed in Jetty 4.2
// context.addHandler(new ContentEncodingHandler()); // context.addHandler(new ContentEncodingHandler());
server.getLogger().warn("Warning: disabling response encoding for Jetty 4.2 compatibility"); getLogger().warn("Warning: disabling response encoding for Jetty 4.2 compatibility");
} }
ServletHandler handler = new ServletHandler(); ServletHandler handler = new ServletHandler();
@ -498,7 +507,7 @@ public class ApplicationManager implements XmlRpcHandler {
if (protectedStaticDir != null) { if (protectedStaticDir != null) {
File protectedContent = getAbsoluteFile(protectedStaticDir); File protectedContent = getAbsoluteFile(protectedStaticDir);
context.setResourceBase(protectedContent.getPath()); context.setResourceBase(protectedContent.getPath());
server.getLogger().info("Serving protected static from " + getLogger().info("Serving protected static from " +
protectedContent.getPath()); protectedContent.getPath());
} }
@ -509,12 +518,12 @@ public class ApplicationManager implements XmlRpcHandler {
File staticContent = getAbsoluteFile(staticDir); File staticContent = getAbsoluteFile(staticDir);
server.getLogger().info("Serving static from " + getLogger().info("Serving static from " +
staticContent.getPath()); staticContent.getPath());
server.getLogger().info("Mounting static at " + getLogger().info("Mounting static at " +
staticMountpoint); staticMountpoint);
context = server.http.addContext(staticMountpoint); context = httpServer.addContext(staticMountpoint);
context.setWelcomeFiles(staticHome); context.setWelcomeFiles(staticHome);
context.setResourceBase(staticContent.getPath()); context.setResourceBase(staticContent.getPath());
@ -531,13 +540,13 @@ public class ApplicationManager implements XmlRpcHandler {
xmlrpcHandlers.put(xmlrpcHandlerName, app); xmlrpcHandlers.put(xmlrpcHandlerName, app);
// app.start(); // app.start();
} catch (Exception x) { } catch (Exception x) {
server.getLogger().error("Couldn't bind app", x); getLogger().error("Couldn't bind app", x);
x.printStackTrace(); x.printStackTrace();
} }
} }
void unbind() { void unbind() {
server.getLogger().info("Unbinding application " + appName); getLogger().info("Unbinding application " + appName);
try { try {
// unbind from RMI server // unbind from RMI server
@ -546,8 +555,8 @@ public class ApplicationManager implements XmlRpcHandler {
} }
// unbind from Jetty HTTP server // unbind from Jetty HTTP server
if (server.http != null) { if (httpServer != null) {
HttpContext context = server.http.getContext(null, pathPattern); HttpContext context = httpServer.getContext(null, pathPattern);
if (context != null) { if (context != null) {
context.stop(); context.stop();
@ -555,7 +564,7 @@ public class ApplicationManager implements XmlRpcHandler {
} }
if (staticDir != null) { if (staticDir != null) {
context = server.http.getContext(null, staticMountpoint); context = httpServer.getContext(null, staticMountpoint);
if (context != null) { if (context != null) {
context.stop(); context.stop();
@ -569,7 +578,7 @@ public class ApplicationManager implements XmlRpcHandler {
xmlrpcHandlers.remove(xmlrpcHandlerName); xmlrpcHandlers.remove(xmlrpcHandlerName);
} }
} catch (Exception x) { } catch (Exception x) {
server.getLogger().error("Couldn't unbind app", x); getLogger().error("Couldn't unbind app", x);
} }
} }