# HG changeset patch # User Sascha L. Teichmann # Date 1311774690 0 # Node ID 40b64b4aafce23f0ece6e041df5834e4ce7ec6dc # Parent 190aa68ae7a8a2b6cad825e768194b5a394697e2 Added lifetime listeners to be called when system is up and is going down. artifacts/trunk@2410 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r 190aa68ae7a8 -r 40b64b4aafce ChangeLog --- a/ChangeLog Wed Jul 27 09:32:26 2011 +0000 +++ b/ChangeLog Wed Jul 27 13:51:30 2011 +0000 @@ -1,3 +1,19 @@ +2011-07-27 Sascha L. Teichmann + + * artifact-database/src/main/java/de/intevation/artifactdatabase/LifetimeListener.java: + New. Interface instances of are called when system is up and is going down. + + * artifact-database/src/main/java/de/intevation/artifactdatabase/FactoryBootstrap.java: + Load lifetime listeners from configuration. XPATH + /artifact-database/lifetime-listeners/listeners/text() + + * artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java: + Call the listeners after start up and before shutdown. + + * artifact-database/src/main/java/de/intevation/artifactdatabase/App.java: + Trigger the start of the artifact database explicitly when the boot process + is finished. + 2011-07-27 Sascha L. Teichmann * artifacts/src/main/java/de/intevation/artifacts/ArtifactDatabase.java: diff -r 190aa68ae7a8 -r 40b64b4aafce artifact-database/src/main/java/de/intevation/artifactdatabase/App.java --- a/artifact-database/src/main/java/de/intevation/artifactdatabase/App.java Wed Jul 27 09:32:26 2011 +0000 +++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/App.java Wed Jul 27 13:51:30 2011 +0000 @@ -82,6 +82,8 @@ cleaner.start(); + db.start(); + httpServer.startAsServer(db); } } diff -r 190aa68ae7a8 -r 40b64b4aafce artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java --- a/artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java Wed Jul 27 09:32:26 2011 +0000 +++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java Wed Jul 27 13:51:30 2011 +0000 @@ -435,6 +435,8 @@ */ protected List postAdvanceHooks; + protected List lifetimeListeners; + /** * Default constructor. */ @@ -468,6 +470,7 @@ setupUserFactory(bootstrap); setupCallContextListener(bootstrap); setupHooks(bootstrap); + setupLifetimeListeners(bootstrap); context = bootstrap.getContext(); context.put(GLOBAL_CONTEXT_KEY, this); @@ -545,6 +548,10 @@ setPostAdvanceHook(bootstrap.getPostAdvanceHooks()); } + protected void setupLifetimeListeners(FactoryBootstrap bootstrap) { + this.lifetimeListeners = bootstrap.getLifetimeListeners(); + } + /** * Used to extract the user factory from the bootstrap. */ @@ -1719,5 +1726,24 @@ throw new ArtifactDatabaseException(INTERNAL_ERROR); } } + + public void start() { + if (lifetimeListeners == null || lifetimeListeners.isEmpty()) { + return; + } + + for (LifetimeListener ltl: lifetimeListeners) { + ltl.systemUp(context); + } + + Runtime.getRuntime().addShutdownHook(new Thread() { + @Override + public void run() { + for (LifetimeListener ltl: lifetimeListeners) { + ltl.systemDown(context); + } + } + }); + } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : diff -r 190aa68ae7a8 -r 40b64b4aafce artifact-database/src/main/java/de/intevation/artifactdatabase/FactoryBootstrap.java --- a/artifact-database/src/main/java/de/intevation/artifactdatabase/FactoryBootstrap.java Wed Jul 27 09:32:26 2011 +0000 +++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/FactoryBootstrap.java Wed Jul 27 13:51:30 2011 +0000 @@ -120,6 +120,9 @@ public static final String DEFAULT_HTTP_SERVER = "de.intevation.artifactdatabase.rest.Standalone"; + public static final String LIFETIME_LISTENERS = + "/artifact-database/lifetime-listeners/listener"; + /** * Default export signing secret. * PLEASE CHANGE THE SECRET VIA THE XPATH EXPORT_SECRET @@ -164,6 +167,8 @@ protected List postAdvanceHooks; + protected List lifetimeListeners; + /** * byte array holding the export signing secret. */ @@ -441,6 +446,51 @@ } } + protected void loadLifetimeListeners() { + logger.info("loading lifetime listeners"); + + NodeList nodes = Config.getNodeSetXPath(LIFETIME_LISTENERS); + + if (nodes == null) { + logger.debug("no lifetime listeners configure"); + return; + } + + List ltls = new ArrayList(); + + for (int i = 0, N = nodes.getLength(); i < N; ++i) { + Node node = nodes.item(i); + String className = node.getTextContent(); + if (className == null + || (className = className.trim()).length() == 0) { + continue; + } + try { + Class clazz = Class.forName(className); + LifetimeListener listener = + (LifetimeListener)clazz.newInstance(); + + listener.setup(Config.getConfig()); + + lifetimeListeners.add(listener); + } + catch (ClassNotFoundException cnfe) { + logger.error(cnfe.getLocalizedMessage(), cnfe); + } + catch (InstantiationException ie) { + logger.error(ie.getLocalizedMessage(), ie); + } + catch (ClassCastException cce) { + logger.error(cce.getLocalizedMessage(), cce); + } + catch (IllegalAccessException iae) { + logger.error(iae.getLocalizedMessage(), iae); + } + } + + lifetimeListeners = ltls; + } + protected void loadHooks() { logger.info("loading hooks"); @@ -533,6 +583,7 @@ loadCallContextListener(); loadHTTPServer(); loadHooks(); + loadLifetimeListeners(); } /** @@ -606,5 +657,9 @@ public HTTPServer getHTTPServer() { return httpServer; } + + public List getLifetimeListeners() { + return lifetimeListeners; + } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : diff -r 190aa68ae7a8 -r 40b64b4aafce artifact-database/src/main/java/de/intevation/artifactdatabase/LifetimeListener.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/LifetimeListener.java Wed Jul 27 13:51:30 2011 +0000 @@ -0,0 +1,15 @@ +package de.intevation.artifactdatabase; + +import de.intevation.artifacts.GlobalContext; + +import org.w3c.dom.Document; + +public interface LifetimeListener +{ + void setup(Document document); + + void systemUp(GlobalContext globalContext); + + void systemDown(GlobalContext globalContext); +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :