From 256335adde5fdd30091892f63e195b453618800b Mon Sep 17 00:00:00 2001 From: hns Date: Sat, 13 Dec 2008 01:39:10 +0000 Subject: [PATCH] Make launcher Main class compatible with jsvc (apache commons daemon) and adapt Server lifecycle API. --- src/helma/main/CommandlineRunner.java | 1 + src/helma/main/HelmaShutdownHook.java | 1 + src/helma/main/Server.java | 32 +++++++++----- src/helma/main/launcher/Main.java | 62 +++++++++++++++++++++++---- 4 files changed, 78 insertions(+), 18 deletions(-) diff --git a/src/helma/main/CommandlineRunner.java b/src/helma/main/CommandlineRunner.java index 31bcce20..aeaaee1f 100644 --- a/src/helma/main/CommandlineRunner.java +++ b/src/helma/main/CommandlineRunner.java @@ -107,6 +107,7 @@ public class CommandlineRunner { // stop the application and server server.stop(); + server.shutdown(); } diff --git a/src/helma/main/HelmaShutdownHook.java b/src/helma/main/HelmaShutdownHook.java index 3c4d2731..f3eff903 100644 --- a/src/helma/main/HelmaShutdownHook.java +++ b/src/helma/main/HelmaShutdownHook.java @@ -34,6 +34,7 @@ public class HelmaShutdownHook extends Thread { Server server = Server.getServer(); if (server != null) { server.stop(); + server.shutdown(); } } } diff --git a/src/helma/main/Server.java b/src/helma/main/Server.java index 0107f092..de77d59a 100644 --- a/src/helma/main/Server.java +++ b/src/helma/main/Server.java @@ -92,6 +92,8 @@ public class Server implements Runnable { /** * Constructs a new Server instance with an array of command line options. + * TODO make this a singleton + * @param config the configuration */ public Server(ServerConfig config) { server = this; @@ -113,9 +115,23 @@ public class Server implements Runnable { /** - * static main entry point. + * Static main entry point. + * @param args the command line arguments */ public static void main(String[] args) { + loadServer(args); + // parse properties files etc + server.init(); + // start the server main thread + server.start(); + } + + /** + * Entry point used by launcher.jar to load a server instance + * @param args the command line arguments + * @return the server instance + */ + public static Server loadServer(String[] args) { checkJavaVersion(); ServerConfig config = null; @@ -130,12 +146,7 @@ public class Server implements Runnable { // create new server instance server = new Server(config); - - // parse properties files etc - server.init(); - - // start the server main thread - server.start(); + return server; } @@ -513,10 +524,11 @@ public class Server implements Runnable { public void stop() { mainThread = null; - - getLogger().info("Shutting down Helma"); - appManager.stopAll(); + } + + public void shutdown() { + getLogger().info("Shutting down Helma"); if (jetty != null) { try { diff --git a/src/helma/main/launcher/Main.java b/src/helma/main/launcher/Main.java index 539358f3..9e1b0b22 100644 --- a/src/helma/main/launcher/Main.java +++ b/src/helma/main/launcher/Main.java @@ -44,6 +44,12 @@ public class Main { "tagsoup.jar" }; + private Class serverClass; + private Object server; + + private static final Class[] EMPTY_CLASS_ARRAY = new Class[0]; + private static final Object[] EMPTY_OBJECT_ARRAY = new Object[0]; + /** * Helma boot method. This retrieves the Helma home directory, creates the * classpath and invokes main() in helma.main.Server. @@ -52,27 +58,67 @@ public class Main { * */ public static void main(String[] args) { + Main main = new Main(); + main.init(args); + main.start(); + } + + public void init(String[] args) { try { String installDir = getInstallDir(args); - ClassLoader loader = createClassLoader(installDir); - // get the main server class - Class clazz = loader.loadClass("helma.main.Server"); + serverClass = loader.loadClass("helma.main.Server"); Class[] cargs = new Class[]{args.getClass()}; - Method main = clazz.getMethod("main", cargs); + Method loadServer = serverClass.getMethod("loadServer", cargs); Object[] nargs = new Object[]{args}; - - // and invoke the static main(String, String[]) method - main.invoke(null, nargs); + // and invoke the static loadServer(String[]) method + server = loadServer.invoke(null, nargs); + Method init = serverClass.getMethod("init", EMPTY_CLASS_ARRAY); + init.invoke(server, EMPTY_OBJECT_ARRAY); } catch (Exception x) { // unable to get Helma installation dir from launcher jar - System.err.println("Unable to get Helma installation directory: "); + System.err.println("Unable to load Helma: "); x.printStackTrace(); System.exit(2); } } + public void start() { + try { + Method start = serverClass.getMethod("start", EMPTY_CLASS_ARRAY); + start.invoke(server, EMPTY_OBJECT_ARRAY); + } catch (Exception x) { + // unable to get Helma installation dir from launcher jar + System.err.println("Unable to start Helma: "); + x.printStackTrace(); + System.exit(2); + } + } + + public void stop() { + try { + Method start = serverClass.getMethod("stop", EMPTY_CLASS_ARRAY); + start.invoke(server, EMPTY_OBJECT_ARRAY); + } catch (Exception x) { + // unable to get Helma installation dir from launcher jar + System.err.println("Unable to stop Helma: "); + x.printStackTrace(); + System.exit(2); + } + } + + public void destroy() { + try { + Method start = serverClass.getMethod("shutdown", EMPTY_CLASS_ARRAY); + start.invoke(server, EMPTY_OBJECT_ARRAY); + } catch (Exception x) { + // unable to get Helma installation dir from launcher jar + System.err.println("Unable to shutdown Helma: "); + x.printStackTrace(); + System.exit(2); + } + } /** * Create a server-wide ClassLoader from our install directory.