Massive Argeo APIs refactoring
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / internal / kernel / CmsShutdown.java
index 43295ae3f2681f47eee52d03c573a7bd68ba5652..eb7657edc22380484e866d3b9d516a5f67b83443 100644 (file)
@@ -1,7 +1,6 @@
 package org.argeo.cms.internal.kernel;
 
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
+import org.argeo.api.cms.CmsLog;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.FrameworkEvent;
 import org.osgi.framework.FrameworkUtil;
@@ -14,7 +13,7 @@ class CmsShutdown extends Thread {
        public final int EXIT_TIMEOUT = 2;
        public final int EXIT_UNKNOWN = 3;
 
-       private final Log log = LogFactory.getLog(CmsShutdown.class);
+       private final CmsLog log = CmsLog.getLog(CmsShutdown.class);
        // private final BundleContext bc =
        // FrameworkUtil.getBundle(CmsShutdown.class).getBundleContext();
        private final Framework framework;
@@ -24,38 +23,42 @@ class CmsShutdown extends Thread {
 
        public CmsShutdown() {
                super("CMS Shutdown Hook");
-               framework = (Framework) FrameworkUtil.getBundle(CmsShutdown.class).getBundleContext().getBundle(0);
+               framework = FrameworkUtil.getBundle(CmsShutdown.class) != null
+                               ? (Framework) FrameworkUtil.getBundle(CmsShutdown.class).getBundleContext().getBundle(0)
+                               : null;
        }
 
        @Override
        public void run() {
-               if (framework.getState() != Bundle.ACTIVE) {
+               if (framework != null && framework.getState() != Bundle.ACTIVE) {
                        return;
                }
-               
+
                if (log.isDebugEnabled())
                        log.debug("Shutting down OSGi framework...");
                try {
-                       // shutdown framework
-                       framework.stop();
-                       // wait for shutdown
-                       FrameworkEvent shutdownEvent = framework.waitForStop(timeout);
-                       int stoppedType = shutdownEvent.getType();
-                       Runtime runtime = Runtime.getRuntime();
-                       if (stoppedType == FrameworkEvent.STOPPED) {
-                               // close VM
-                               //System.exit(EXIT_OK);
-                       } else if (stoppedType == FrameworkEvent.ERROR) {
-                               log.error("The OSGi framework stopped with an error");
-                               runtime.halt(EXIT_ERROR);
-                       } else if (stoppedType == FrameworkEvent.WAIT_TIMEDOUT) {
-                               log.error("The OSGi framework hasn't stopped after " + timeout + "ms."
-                                               + " Forcibly terminating the JVM...");
-                               runtime.halt(EXIT_TIMEOUT);
-                       } else {
-                               log.error("Unknown state of OSGi framework after " + timeout + "ms."
-                                               + " Forcibly terminating the JVM... (" + shutdownEvent + ")");
-                               runtime.halt(EXIT_UNKNOWN);
+                       if (framework != null) {
+                               // shutdown framework
+                               framework.stop();
+                               // wait for shutdown
+                               FrameworkEvent shutdownEvent = framework.waitForStop(timeout);
+                               int stoppedType = shutdownEvent.getType();
+                               Runtime runtime = Runtime.getRuntime();
+                               if (stoppedType == FrameworkEvent.STOPPED) {
+                                       // close VM
+                                       // System.exit(EXIT_OK);
+                               } else if (stoppedType == FrameworkEvent.ERROR) {
+                                       log.error("The OSGi framework stopped with an error");
+                                       runtime.halt(EXIT_ERROR);
+                               } else if (stoppedType == FrameworkEvent.WAIT_TIMEDOUT) {
+                                       log.error("The OSGi framework hasn't stopped after " + timeout + "ms."
+                                                       + " Forcibly terminating the JVM...");
+                                       runtime.halt(EXIT_TIMEOUT);
+                               } else {
+                                       log.error("Unknown state of OSGi framework after " + timeout + "ms."
+                                                       + " Forcibly terminating the JVM... (" + shutdownEvent + ")");
+                                       runtime.halt(EXIT_UNKNOWN);
+                               }
                        }
                } catch (Exception e) {
                        e.printStackTrace();