Make launcher Main class compatible with jsvc (apache commons daemon) and adapt Server lifecycle API.

This commit is contained in:
hns 2008-12-13 01:39:10 +00:00
parent 0cb7e2a1aa
commit 256335adde
4 changed files with 78 additions and 18 deletions

View file

@ -107,6 +107,7 @@ public class CommandlineRunner {
// stop the application and server // stop the application and server
server.stop(); server.stop();
server.shutdown();
} }

View file

@ -34,6 +34,7 @@ public class HelmaShutdownHook extends Thread {
Server server = Server.getServer(); Server server = Server.getServer();
if (server != null) { if (server != null) {
server.stop(); server.stop();
server.shutdown();
} }
} }
} }

View file

@ -92,6 +92,8 @@ public class Server implements Runnable {
/** /**
* Constructs a new Server instance with an array of command line options. * 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) { public Server(ServerConfig config) {
server = this; 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) { 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(); checkJavaVersion();
ServerConfig config = null; ServerConfig config = null;
@ -130,12 +146,7 @@ public class Server implements Runnable {
// create new server instance // create new server instance
server = new Server(config); server = new Server(config);
return server;
// parse properties files etc
server.init();
// start the server main thread
server.start();
} }
@ -513,10 +524,11 @@ public class Server implements Runnable {
public void stop() { public void stop() {
mainThread = null; mainThread = null;
getLogger().info("Shutting down Helma");
appManager.stopAll(); appManager.stopAll();
}
public void shutdown() {
getLogger().info("Shutting down Helma");
if (jetty != null) { if (jetty != null) {
try { try {

View file

@ -44,6 +44,12 @@ public class Main {
"tagsoup.jar" "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 * Helma boot method. This retrieves the Helma home directory, creates the
* classpath and invokes main() in helma.main.Server. * classpath and invokes main() in helma.main.Server.
@ -52,27 +58,67 @@ public class Main {
* *
*/ */
public static void main(String[] args) { public static void main(String[] args) {
Main main = new Main();
main.init(args);
main.start();
}
public void init(String[] args) {
try { try {
String installDir = getInstallDir(args); String installDir = getInstallDir(args);
ClassLoader loader = createClassLoader(installDir); ClassLoader loader = createClassLoader(installDir);
// get the main server class // get the main server class
Class clazz = loader.loadClass("helma.main.Server"); serverClass = loader.loadClass("helma.main.Server");
Class[] cargs = new Class[]{args.getClass()}; Class[] cargs = new Class[]{args.getClass()};
Method main = clazz.getMethod("main", cargs); Method loadServer = serverClass.getMethod("loadServer", cargs);
Object[] nargs = new Object[]{args}; Object[] nargs = new Object[]{args};
// and invoke the static loadServer(String[]) method
// and invoke the static main(String, String[]) method server = loadServer.invoke(null, nargs);
main.invoke(null, nargs); Method init = serverClass.getMethod("init", EMPTY_CLASS_ARRAY);
init.invoke(server, EMPTY_OBJECT_ARRAY);
} catch (Exception x) { } catch (Exception x) {
// unable to get Helma installation dir from launcher jar // 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(); x.printStackTrace();
System.exit(2); 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. * Create a server-wide ClassLoader from our install directory.