]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/internal/kernel/CmsShutdown.java
Make remote default workspace configurable via System properties.
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / internal / kernel / CmsShutdown.java
1 package org.argeo.cms.internal.kernel;
2
3 import org.apache.commons.logging.Log;
4 import org.apache.commons.logging.LogFactory;
5 import org.osgi.framework.Bundle;
6 import org.osgi.framework.FrameworkEvent;
7 import org.osgi.framework.FrameworkUtil;
8 import org.osgi.framework.launch.Framework;
9
10 /** Shutdowns the OSGi framework */
11 class CmsShutdown extends Thread {
12 public final int EXIT_OK = 0;
13 public final int EXIT_ERROR = 1;
14 public final int EXIT_TIMEOUT = 2;
15 public final int EXIT_UNKNOWN = 3;
16
17 private final Log log = LogFactory.getLog(CmsShutdown.class);
18 // private final BundleContext bc =
19 // FrameworkUtil.getBundle(CmsShutdown.class).getBundleContext();
20 private final Framework framework;
21
22 /** Shutdown timeout in ms */
23 private long timeout = 10 * 60 * 1000;
24
25 public CmsShutdown() {
26 super("CMS Shutdown Hook");
27 framework = FrameworkUtil.getBundle(CmsShutdown.class) != null
28 ? (Framework) FrameworkUtil.getBundle(CmsShutdown.class).getBundleContext().getBundle(0)
29 : null;
30 }
31
32 @Override
33 public void run() {
34 if (framework != null && framework.getState() != Bundle.ACTIVE) {
35 return;
36 }
37
38 if (log.isDebugEnabled())
39 log.debug("Shutting down OSGi framework...");
40 try {
41 if (framework != null) {
42 // shutdown framework
43 framework.stop();
44 // wait for shutdown
45 FrameworkEvent shutdownEvent = framework.waitForStop(timeout);
46 int stoppedType = shutdownEvent.getType();
47 Runtime runtime = Runtime.getRuntime();
48 if (stoppedType == FrameworkEvent.STOPPED) {
49 // close VM
50 // System.exit(EXIT_OK);
51 } else if (stoppedType == FrameworkEvent.ERROR) {
52 log.error("The OSGi framework stopped with an error");
53 runtime.halt(EXIT_ERROR);
54 } else if (stoppedType == FrameworkEvent.WAIT_TIMEDOUT) {
55 log.error("The OSGi framework hasn't stopped after " + timeout + "ms."
56 + " Forcibly terminating the JVM...");
57 runtime.halt(EXIT_TIMEOUT);
58 } else {
59 log.error("Unknown state of OSGi framework after " + timeout + "ms."
60 + " Forcibly terminating the JVM... (" + shutdownEvent + ")");
61 runtime.halt(EXIT_UNKNOWN);
62 }
63 }
64 } catch (Exception e) {
65 e.printStackTrace();
66 log.error("Unexpected exception " + e + " in shutdown hook. " + " Forcibly terminating the JVM...");
67 Runtime.getRuntime().halt(EXIT_UNKNOWN);
68 }
69 }
70
71 }