diff --git a/src/helma/main/HelmaShutdownHook.java b/src/helma/main/HelmaShutdownHook.java index d5ec8542..3c4d2731 100644 --- a/src/helma/main/HelmaShutdownHook.java +++ b/src/helma/main/HelmaShutdownHook.java @@ -23,16 +23,7 @@ import org.apache.commons.logging.LogFactory; * ShutdownHook that shuts down all running Helma applications on exit. */ public class HelmaShutdownHook extends Thread { - ApplicationManager appmgr; - /** - * Creates a new HelmaShutdownHook object. - * - * @param appmgr ... - */ - public HelmaShutdownHook(ApplicationManager appmgr) { - this.appmgr = appmgr; - } /** * @@ -40,12 +31,9 @@ public class HelmaShutdownHook extends Thread { public void run() { System.err.println("Shutting down Helma - please stand by..."); - Server.getServer().getLogger().info("Shutting down Helma"); - - appmgr.stopAll(); - - if (LogFactory.getFactory() instanceof Logging) { - Logging.shutdown(); + Server server = Server.getServer(); + if (server != null) { + server.stop(); } } } diff --git a/src/helma/main/Server.java b/src/helma/main/Server.java index 8e82a64c..a7313e94 100644 --- a/src/helma/main/Server.java +++ b/src/helma/main/Server.java @@ -87,6 +87,8 @@ public class Server implements IPathElement, Runnable { // the XML-RPC server protected WebServer xmlrpc; + + Thread shutdownhook; /** @@ -503,15 +505,40 @@ public class Server implements IPathElement, Runnable { - protected void start() { + public void start() { // Start running, finishing setup and then entering a loop to check changes // in the apps.properties file. mainThread = new Thread(this); mainThread.start(); } - protected void stop() { + public void stop() { mainThread = null; + + getLogger().info("Shutting down Helma"); + + appManager.stopAll(); + + if (http != null) { + try { + http.stop(); + http.destroy(); + } catch (InterruptedException irx) { + // http.stop() interrupted by another thread. ignore. + } + } + + if (helmaLogging) { + Logging.shutdown(); + } + + try { + Runtime.getRuntime().removeShutdownHook(shutdownhook); + } catch (Exception x) { + // invalid shutdown hook or already shutting down. ignore. + } + + server = null; } /** @@ -646,7 +673,8 @@ public class Server implements IPathElement, Runnable { } // add shutdown hook to close running apps and servers on exit - Runtime.getRuntime().addShutdownHook(new HelmaShutdownHook(appManager)); + shutdownhook = new HelmaShutdownHook(); + Runtime.getRuntime().addShutdownHook(shutdownhook); } catch (Exception gx) { logger.error("Error initializing embedded database: " + gx); gx.printStackTrace();