Improve CmsView capabilities.
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / internal / kernel / CmsState.java
index ca469f0f5b08116c78658fdc8d15154a282aa7b4..53a3de8ef6edb660eded8add8c259db2d637c649 100644 (file)
@@ -17,7 +17,6 @@ import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.argeo.api.NodeConstants;
 import org.argeo.api.NodeState;
-import org.argeo.cms.CmsException;
 import org.argeo.cms.LocaleUtils;
 import org.argeo.transaction.simple.SimpleTransactionManager;
 import org.argeo.util.LangUtils;
@@ -26,6 +25,9 @@ import org.osgi.framework.Constants;
 import org.osgi.framework.FrameworkUtil;
 import org.osgi.service.cm.ManagedServiceFactory;
 
+/**
+ * Implementation of a {@link NodeState}, initialising the required services.
+ */
 public class CmsState implements NodeState {
        private final static Log log = LogFactory.getLog(CmsState.class);
        private final BundleContext bc = FrameworkUtil.getBundle(CmsState.class).getBundleContext();
@@ -42,13 +44,13 @@ public class CmsState implements NodeState {
        private List<Runnable> stopHooks = new ArrayList<>();
 
        private final String stateUuid;
-       private final boolean cleanState;
+//     private final boolean cleanState;
        private String hostname;
 
-       public CmsState(String stateUuid) {
-               this.stateUuid = stateUuid;
-               String frameworkUuid = KernelUtils.getFrameworkProp(Constants.FRAMEWORK_UUID);
-               this.cleanState = stateUuid.equals(frameworkUuid);
+       public CmsState() {
+//             this.stateUuid = stateUuid;
+               this.stateUuid = KernelUtils.getFrameworkProp(Constants.FRAMEWORK_UUID);
+//             this.cleanState = stateUuid.equals(frameworkUuid);
                try {
                        this.hostname = InetAddress.getLocalHost().getHostName();
                } catch (UnknownHostException e) {
@@ -57,7 +59,9 @@ public class CmsState implements NodeState {
 
                availableSince = System.currentTimeMillis();
                if (log.isDebugEnabled())
-                       log.debug("## CMS starting... stateUuid=" + this.stateUuid + (cleanState ? " (clean state) " : " "));
+                       // log.debug("## CMS starting... stateUuid=" + this.stateUuid + (cleanState ? "
+                       // (clean state) " : " "));
+                       log.debug("## CMS starting... (" + stateUuid + ")");
 
                initI18n();
                initServices();
@@ -86,7 +90,7 @@ public class CmsState implements NodeState {
                        throw new UnsupportedOperationException(
                                        "Bitronix is not supported anymore, but could be again if there is enough interest.");
                } else {
-                       throw new CmsException("Usupported transaction manager type " + tmType);
+                       throw new IllegalArgumentException("Usupported transaction manager type " + tmType);
                }
 
                // POI
@@ -107,7 +111,7 @@ public class CmsState implements NodeState {
                RepositoryServiceFactory repositoryServiceFactory = new RepositoryServiceFactory();
                stopHooks.add(() -> repositoryServiceFactory.shutdown());
                bc.registerService(ManagedServiceFactory.class, repositoryServiceFactory,
-                               LangUtils.dico(Constants.SERVICE_PID, NodeConstants.NODE_REPOS_FACTORY_PID));
+                               LangUtils.dict(Constants.SERVICE_PID, NodeConstants.NODE_REPOS_FACTORY_PID));
 
                NodeRepositoryFactory repositoryFactory = new NodeRepositoryFactory();
                bc.registerService(RepositoryFactory.class, repositoryFactory, null);
@@ -116,7 +120,7 @@ public class CmsState implements NodeState {
                NodeUserAdmin userAdmin = new NodeUserAdmin(NodeConstants.ROLES_BASEDN, NodeConstants.TOKENS_BASEDN);
                stopHooks.add(() -> userAdmin.destroy());
                bc.registerService(ManagedServiceFactory.class, userAdmin,
-                               LangUtils.dico(Constants.SERVICE_PID, NodeConstants.NODE_USER_ADMIN_PID));
+                               LangUtils.dict(Constants.SERVICE_PID, NodeConstants.NODE_USER_ADMIN_PID));
 
                // File System
                CmsFsProvider cmsFsProvider = new CmsFsProvider();
@@ -131,7 +135,7 @@ public class CmsState implements NodeState {
 //                     log.debug("Installed FileSystemProvider " + fsp);
 //             }
                bc.registerService(FileSystemProvider.class, cmsFsProvider,
-                               LangUtils.dico(Constants.SERVICE_PID, NodeConstants.NODE_FS_PROVIDER_PID));
+                               LangUtils.dict(Constants.SERVICE_PID, NodeConstants.NODE_FS_PROVIDER_PID));
        }
 
        private void initSimpleTransactionManager() {
@@ -176,13 +180,18 @@ public class CmsState implements NodeState {
 
        void shutdown() {
                if (log.isDebugEnabled())
-                       log.debug("CMS stopping...  stateUuid=" + this.stateUuid + (cleanState ? " (clean state) " : " "));
+                       log.debug("CMS stopping...  (" + this.stateUuid + ")");
 
                if (kernelThread != null)
                        kernelThread.destroyAndJoin();
-               // In a different state in order to avois interruptions
-               new Thread(() -> applyStopHooks(), "Apply Argeo Stop Hooks").start();
-               // applyStopHooks();
+               // In a different thread in order to avoid interruptions
+               Thread stopHookThread = new Thread(() -> applyStopHooks(), "Apply Argeo Stop Hooks");
+               stopHookThread.start();
+               try {
+                       stopHookThread.join(10 * 60 * 1000);
+               } catch (InterruptedException e) {
+                       // silent
+               }
 
                long duration = ((System.currentTimeMillis() - availableSince) / 1000) / 60;
                log.info("## ARGEO CMS STOPPED after " + (duration / 60) + "h " + (duration % 60) + "min uptime ##");
@@ -201,10 +210,10 @@ public class CmsState implements NodeState {
                new GogoShellKiller().start();
        }
 
-       @Override
-       public boolean isClean() {
-               return cleanState;
-       }
+//     @Override
+//     public boolean isClean() {
+//             return cleanState;
+//     }
 
        @Override
        public Long getAvailableSince() {