1 package org
.argeo
.cms
.internal
.kernel
;
3 import java
.io
.IOException
;
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
;
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;
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
;
25 /** Shutdown timeout in ms */
26 private long timeout
= 10 * 60 * 1000;
28 public CmsShutdown() {
29 super("CMS Shutdown Hook");
30 framework
= (Framework
) FrameworkUtil
.getBundle(CmsShutdown
.class).getBundleContext().getBundle(0);
35 if (framework
.getState() != Bundle
.ACTIVE
) {
39 if (log
.isDebugEnabled())
40 log
.debug("Shutting down OSGi framework...");
45 FrameworkEvent shutdownEvent
= framework
.waitForStop(timeout
);
46 int stoppedType
= shutdownEvent
.getType();
47 Runtime runtime
= Runtime
.getRuntime();
48 if (stoppedType
== FrameworkEvent
.STOPPED
) {
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
);
59 log
.error("Unknown state of OSGi framework after " + timeout
+ "ms."
60 + " Forcibly terminating the JVM... (" + shutdownEvent
+ ")");
61 runtime
.halt(EXIT_UNKNOWN
);
63 } catch (Exception e
) {
65 log
.error("Unexpected exception " + e
+ " in shutdown hook. " + " Forcibly terminating the JVM...");
66 Runtime
.getRuntime().halt(EXIT_UNKNOWN
);