1 package org
.argeo
.cms
.internal
.kernel
;
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
;
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;
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
;
22 /** Shutdown timeout in ms */
23 private long timeout
= 10 * 60 * 1000;
25 public CmsShutdown() {
26 super("CMS Shutdown Hook");
27 framework
= FrameworkUtil
.getBundle(CmsShutdown
.class) != null
28 ?
(Framework
) FrameworkUtil
.getBundle(CmsShutdown
.class).getBundleContext().getBundle(0)
34 if (framework
!= null && framework
.getState() != Bundle
.ACTIVE
) {
38 if (log
.isDebugEnabled())
39 log
.debug("Shutting down OSGi framework...");
41 if (framework
!= null) {
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
);
64 } catch (Exception e
) {
66 log
.error("Unexpected exception " + e
+ " in shutdown hook. " + " Forcibly terminating the JVM...");
67 Runtime
.getRuntime().halt(EXIT_UNKNOWN
);