]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/internal/kernel/CmsShutdown.java
Introduce CMS commands
[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 java.io.IOException;
4
5 import org.apache.commons.logging.Log;
6 import org.apache.commons.logging.LogFactory;
7 import org.osgi.framework.Bundle;
8 import org.osgi.framework.BundleContext;
9 import org.osgi.framework.FrameworkEvent;
10 import org.osgi.framework.FrameworkUtil;
11 import org.osgi.framework.launch.Framework;
12
13 /** Shutdowns the OSGi framework */
14 class CmsShutdown extends Thread {
15 public final int EXIT_OK = 0;
16 public final int EXIT_ERROR = 1;
17 public final int EXIT_TIMEOUT = 2;
18 public final int EXIT_UNKNOWN = 3;
19
20 private final Log log = LogFactory.getLog(CmsShutdown.class);
21 // private final BundleContext bc =
22 // FrameworkUtil.getBundle(CmsShutdown.class).getBundleContext();
23 private final Framework framework;
24
25 /** Shutdown timeout in ms */
26 private long timeout = 10 * 60 * 1000;
27
28 public CmsShutdown() {
29 super("CMS Shutdown Hook");
30 framework = (Framework) FrameworkUtil.getBundle(CmsShutdown.class).getBundleContext().getBundle(0);
31 }
32
33 @Override
34 public void run() {
35 if (framework.getState() != Bundle.ACTIVE) {
36 return;
37 }
38
39 if (log.isDebugEnabled())
40 log.debug("Shutting down OSGi framework...");
41 try {
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 } catch (Exception e) {
64 e.printStackTrace();
65 log.error("Unexpected exception " + e + " in shutdown hook. " + " Forcibly terminating the JVM...");
66 Runtime.getRuntime().halt(EXIT_UNKNOWN);
67 }
68 }
69
70 }