Rename remote auth interfaces
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / internal / kernel / CmsState.java
index 789312625c54219f66d24af46281dfeb1740e36d..219f2d53c059b58f4032ed11c22d3d9dc5c3812d 100644 (file)
@@ -1,45 +1,31 @@
 package org.argeo.cms.internal.kernel;
 
-import static bitronix.tm.TransactionManagerServices.getTransactionManager;
-import static bitronix.tm.TransactionManagerServices.getTransactionSynchronizationRegistry;
 import static java.util.Locale.ENGLISH;
 
-import java.io.File;
 import java.net.InetAddress;
 import java.net.UnknownHostException;
-import java.nio.file.spi.FileSystemProvider;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Locale;
-import java.util.UUID;
-
-import javax.jcr.RepositoryFactory;
-import javax.transaction.TransactionManager;
-import javax.transaction.TransactionSynchronizationRegistry;
-import javax.transaction.UserTransaction;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-import org.argeo.cms.CmsException;
-import org.argeo.cms.i18n.LocaleUtils;
-import org.argeo.node.NodeConstants;
-import org.argeo.node.NodeState;
-import org.argeo.transaction.simple.SimpleTransactionManager;
+import org.argeo.api.NodeConstants;
+import org.argeo.api.NodeState;
+import org.argeo.cms.LocaleUtils;
+import org.argeo.osgi.transaction.SimpleTransactionManager;
+import org.argeo.osgi.transaction.WorkControl;
+import org.argeo.osgi.transaction.WorkTransaction;
 import org.argeo.util.LangUtils;
-import org.osgi.framework.Bundle;
-import org.osgi.framework.BundleContext;
 import org.osgi.framework.Constants;
-import org.osgi.framework.FrameworkUtil;
-import org.osgi.framework.ServiceReference;
 import org.osgi.service.cm.ManagedServiceFactory;
 
-import bitronix.tm.BitronixTransactionManager;
-import bitronix.tm.BitronixTransactionSynchronizationRegistry;
-import bitronix.tm.TransactionManagerServices;
-
+/**
+ * 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();
+//     private final BundleContext bc = FrameworkUtil.getBundle(CmsState.class).getBundleContext();
 
        // REFERENCES
        private Long availableSince;
@@ -49,17 +35,16 @@ public class CmsState implements NodeState {
        private List<Locale> locales = null;
 
        private ThreadGroup threadGroup = new ThreadGroup("CMS");
-       private KernelThread kernelThread;
        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) {
@@ -68,15 +53,13 @@ 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();
 
-               // kernel thread
-               kernelThread = new KernelThread(threadGroup, "Kernel Thread");
-               kernelThread.setContextClassLoader(getClass().getClassLoader());
-               kernelThread.start();
        }
 
        private void initI18n() {
@@ -93,9 +76,11 @@ public class CmsState implements NodeState {
                if (NodeConstants.TRANSACTION_MANAGER_SIMPLE.equals(tmType)) {
                        initSimpleTransactionManager();
                } else if (NodeConstants.TRANSACTION_MANAGER_BITRONIX.equals(tmType)) {
-                       initBitronixTransactionManager();
+//                     initBitronixTransactionManager();
+                       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
@@ -112,86 +97,77 @@ public class CmsState implements NodeState {
 //             ocrParser.setLanguage("ara");
 //             bc.registerService(Parser.class, ocrParser, new Hashtable());
 
-               // JCR
-               RepositoryServiceFactory repositoryServiceFactory = new RepositoryServiceFactory();
-               stopHooks.add(() -> repositoryServiceFactory.shutdown());
-               bc.registerService(ManagedServiceFactory.class, repositoryServiceFactory,
-                               LangUtils.dico(Constants.SERVICE_PID, NodeConstants.NODE_REPOS_FACTORY_PID));
-
-               NodeRepositoryFactory repositoryFactory = new NodeRepositoryFactory();
-               bc.registerService(RepositoryFactory.class, repositoryFactory, null);
+//             // JCR
+//             RepositoryServiceFactory repositoryServiceFactory = new RepositoryServiceFactory();
+//             stopHooks.add(() -> repositoryServiceFactory.shutdown());
+//             Activator.registerService(ManagedServiceFactory.class, repositoryServiceFactory,
+//                             LangUtils.dict(Constants.SERVICE_PID, NodeConstants.NODE_REPOS_FACTORY_PID));
+//
+//             NodeRepositoryFactory repositoryFactory = new NodeRepositoryFactory();
+//             Activator.registerService(RepositoryFactory.class, repositoryFactory, null);
 
                // Security
-               NodeUserAdmin userAdmin = new NodeUserAdmin(NodeConstants.ROLES_BASEDN);
+               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));
-
-               // File System
-               CmsFsProvider cmsFsProvider = new CmsFsProvider();
-//             ServiceLoader<FileSystemProvider> fspSl = ServiceLoader.load(FileSystemProvider.class);
-//             for (FileSystemProvider fsp : fspSl) {
-//                     log.debug("FileSystemProvider " + fsp);
-//                     if (fsp instanceof CmsFsProvider) {
-//                             cmsFsProvider = (CmsFsProvider) fsp;
-//                     }
-//             }
-//             for (FileSystemProvider fsp : FileSystemProvider.installedProviders()) {
-//                     log.debug("Installed FileSystemProvider " + fsp);
-//             }
-               bc.registerService(FileSystemProvider.class, cmsFsProvider,
-                               LangUtils.dico(Constants.SERVICE_PID, NodeConstants.NODE_FS_PROVIDER_PID));
+               Activator.registerService(ManagedServiceFactory.class, userAdmin,
+                               LangUtils.dict(Constants.SERVICE_PID, NodeConstants.NODE_USER_ADMIN_PID));
+
        }
 
        private void initSimpleTransactionManager() {
                SimpleTransactionManager transactionManager = new SimpleTransactionManager();
-               bc.registerService(TransactionManager.class, transactionManager, null);
-               bc.registerService(UserTransaction.class, transactionManager, null);
+               Activator.registerService(WorkControl.class, transactionManager, null);
+               Activator.registerService(WorkTransaction.class, transactionManager, null);
+//             Activator.registerService(TransactionManager.class, transactionManager, null);
+//             Activator.registerService(UserTransaction.class, transactionManager, null);
                // TODO TransactionSynchronizationRegistry
        }
 
-       private void initBitronixTransactionManager() {
-               // TODO manage it in a managed service, as startup could be long
-               ServiceReference<TransactionManager> existingTm = bc.getServiceReference(TransactionManager.class);
-               if (existingTm != null) {
-                       if (log.isDebugEnabled())
-                               log.debug("Using provided transaction manager " + existingTm);
-                       return;
-               }
-
-               if (!TransactionManagerServices.isTransactionManagerRunning()) {
-                       bitronix.tm.Configuration tmConf = TransactionManagerServices.getConfiguration();
-                       tmConf.setServerId(UUID.randomUUID().toString());
-
-                       Bundle bitronixBundle = FrameworkUtil.getBundle(bitronix.tm.Configuration.class);
-                       File tmBaseDir = bitronixBundle.getDataFile(KernelConstants.DIR_TRANSACTIONS);
-                       File tmDir1 = new File(tmBaseDir, "btm1");
-                       tmDir1.mkdirs();
-                       tmConf.setLogPart1Filename(new File(tmDir1, tmDir1.getName() + ".tlog").getAbsolutePath());
-                       File tmDir2 = new File(tmBaseDir, "btm2");
-                       tmDir2.mkdirs();
-                       tmConf.setLogPart2Filename(new File(tmDir2, tmDir2.getName() + ".tlog").getAbsolutePath());
-               }
-               BitronixTransactionManager transactionManager = getTransactionManager();
-               stopHooks.add(() -> transactionManager.shutdown());
-               BitronixTransactionSynchronizationRegistry transactionSynchronizationRegistry = getTransactionSynchronizationRegistry();
-               // register
-               bc.registerService(TransactionManager.class, transactionManager, null);
-               bc.registerService(UserTransaction.class, transactionManager, null);
-               bc.registerService(TransactionSynchronizationRegistry.class, transactionSynchronizationRegistry, null);
-               if (log.isDebugEnabled())
-                       log.debug("Initialised default Bitronix transaction manager");
-       }
+//     private void initBitronixTransactionManager() {
+//             // TODO manage it in a managed service, as startup could be long
+//             ServiceReference<TransactionManager> existingTm = bc.getServiceReference(TransactionManager.class);
+//             if (existingTm != null) {
+//                     if (log.isDebugEnabled())
+//                             log.debug("Using provided transaction manager " + existingTm);
+//                     return;
+//             }
+//
+//             if (!TransactionManagerServices.isTransactionManagerRunning()) {
+//                     bitronix.tm.Configuration tmConf = TransactionManagerServices.getConfiguration();
+//                     tmConf.setServerId(UUID.randomUUID().toString());
+//
+//                     Bundle bitronixBundle = FrameworkUtil.getBundle(bitronix.tm.Configuration.class);
+//                     File tmBaseDir = bitronixBundle.getDataFile(KernelConstants.DIR_TRANSACTIONS);
+//                     File tmDir1 = new File(tmBaseDir, "btm1");
+//                     tmDir1.mkdirs();
+//                     tmConf.setLogPart1Filename(new File(tmDir1, tmDir1.getName() + ".tlog").getAbsolutePath());
+//                     File tmDir2 = new File(tmBaseDir, "btm2");
+//                     tmDir2.mkdirs();
+//                     tmConf.setLogPart2Filename(new File(tmDir2, tmDir2.getName() + ".tlog").getAbsolutePath());
+//             }
+//             BitronixTransactionManager transactionManager = getTransactionManager();
+//             stopHooks.add(() -> transactionManager.shutdown());
+//             BitronixTransactionSynchronizationRegistry transactionSynchronizationRegistry = getTransactionSynchronizationRegistry();
+//             // register
+//             bc.registerService(TransactionManager.class, transactionManager, null);
+//             bc.registerService(UserTransaction.class, transactionManager, null);
+//             bc.registerService(TransactionSynchronizationRegistry.class, transactionSynchronizationRegistry, null);
+//             if (log.isDebugEnabled())
+//                     log.debug("Initialised default Bitronix transaction manager");
+//     }
 
        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 ##");
@@ -210,10 +186,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() {