Stabilise deployment
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / internal / kernel / CmsState.java
index f8073c46ff984b9ed5c3735baf512b8ff24ae3f0..23d2f266d89325c71dc99bc168498340049b37ab 100644 (file)
@@ -4,145 +4,80 @@ import static bitronix.tm.TransactionManagerServices.getTransactionManager;
 import static bitronix.tm.TransactionManagerServices.getTransactionSynchronizationRegistry;
 import static java.util.Locale.ENGLISH;
 import static org.argeo.cms.internal.auth.LocaleChoice.asLocaleList;
-import static org.argeo.cms.internal.kernel.KernelUtils.getFrameworkProp;
 
 import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
 import java.net.InetAddress;
-import java.net.URI;
 import java.net.UnknownHostException;
-import java.nio.file.Files;
-import java.nio.file.Path;
 import java.util.ArrayList;
 import java.util.Dictionary;
-import java.util.Hashtable;
 import java.util.List;
 import java.util.Locale;
-import java.util.SortedMap;
 import java.util.UUID;
 
 import javax.jcr.RepositoryFactory;
-import javax.naming.InvalidNameException;
-import javax.naming.directory.Attributes;
-import javax.naming.ldap.LdapName;
-import javax.naming.ldap.Rdn;
 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.apache.jackrabbit.core.RepositoryContext;
-import org.argeo.cms.CmsException;
 import org.argeo.cms.maintenance.MaintenanceUi;
-import org.argeo.jcr.ArgeoJcrConstants;
 import org.argeo.node.NodeConstants;
-import org.argeo.node.NodeDeployment;
 import org.argeo.node.NodeState;
-import org.argeo.node.RepoConf;
 import org.argeo.util.LangUtils;
-import org.argeo.util.naming.AttributesDictionary;
-import org.argeo.util.naming.LdifParser;
-import org.eclipse.equinox.http.jetty.JettyConfigurator;
-import org.eclipse.equinox.http.jetty.JettyConstants;
 import org.eclipse.rap.rwt.application.ApplicationConfiguration;
 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.Configuration;
-import org.osgi.service.cm.ConfigurationAdmin;
-import org.osgi.service.cm.ConfigurationException;
-import org.osgi.service.cm.ManagedService;
 import org.osgi.service.cm.ManagedServiceFactory;
-import org.osgi.service.http.HttpService;
-import org.osgi.service.metatype.MetaTypeProvider;
 import org.osgi.service.useradmin.UserAdmin;
-import org.osgi.util.tracker.ServiceTracker;
-import org.osgi.util.tracker.ServiceTrackerCustomizer;
 
 import bitronix.tm.BitronixTransactionManager;
 import bitronix.tm.BitronixTransactionSynchronizationRegistry;
 import bitronix.tm.TransactionManagerServices;
 
-public class CmsState implements NodeState, ManagedService {
+public class CmsState implements NodeState {
        private final Log log = LogFactory.getLog(CmsState.class);
        private final BundleContext bc = FrameworkUtil.getBundle(CmsState.class).getBundleContext();
 
-       // avoid dependency to RWT OSGi
-       private final static String PROPERTY_CONTEXT_NAME = "contextName";
-
        // REFERENCES
-       private ConfigurationAdmin configurationAdmin;
+       private Long availableSince;
 
        // i18n
        private Locale defaultLocale;
        private List<Locale> locales = null;
 
-       // private BitronixTransactionManager transactionManager;
-       // private BitronixTransactionSynchronizationRegistry
-       // transactionSynchronizationRegistry;
-       // private NodeRepositoryFactory repositoryFactory;
-       // private NodeUserAdmin userAdmin;
-       // private RepositoryServiceFactory repositoryServiceFactory;
-       // private RepositoryService repositoryService;
-
-       // Deployment
-       // private final CmsDeployment nodeDeployment = new CmsDeployment();
-
-       private boolean cleanState = false;
-       private URI nodeRepoUri = null;
-
        private ThreadGroup threadGroup = new ThreadGroup("CMS");
        private KernelThread kernelThread;
        private List<Runnable> shutdownHooks = new ArrayList<>();
 
+       private final String stateUuid;
+       private final boolean cleanState;
        private String hostname;
 
-       public CmsState() {
+       public CmsState(String stateUuid) {
+               this.stateUuid = stateUuid;
+               String frameworkUuid = KernelUtils.getFrameworkProp(Constants.FRAMEWORK_UUID);
+               this.cleanState = stateUuid.equals(frameworkUuid);
                try {
                        this.hostname = InetAddress.getLocalHost().getHostName();
                } catch (UnknownHostException e) {
                        log.error("Cannot set hostname", e);
                }
-       }
 
-       @Override
-       public void updated(Dictionary<String, ?> properties) throws ConfigurationException {
-               if (properties == null) {
-                       // // TODO this should not happen anymore
-                       // this.cleanState = true;
-                       // if (log.isTraceEnabled())
-                       // log.trace("Clean state");
-                       return;
-               }
-               String stateUuid = properties.get(NodeConstants.CN).toString();
-               String frameworkUuid = KernelUtils.getFrameworkProp(Constants.FRAMEWORK_UUID);
-               this.cleanState = stateUuid.equals(frameworkUuid);
+               availableSince = System.currentTimeMillis();
+               if (log.isDebugEnabled())
+                       log.debug("## CMS STARTED " + this.stateUuid + (cleanState ? " (clean state) " : " "));
 
-               try {
-                       if (log.isDebugEnabled())
-                               log.debug("## CMS STARTED " + stateUuid + (cleanState ? " (clean state) " : " ")
-                                               + LangUtils.toJson(properties, true));
-                       configurationAdmin = bc.getService(bc.getServiceReference(ConfigurationAdmin.class));
-
-                       nodeRepoUri = KernelUtils.getOsgiInstanceUri("repos/node");
-
-                       initI18n();
-                       initServices();
-//                     initDeployConfigs();
-                       // initWebServer();
-                       // initNodeDeployment();
-
-                       // kernel thread
-                       kernelThread = new KernelThread(threadGroup, "Kernel Thread");
-                       kernelThread.setContextClassLoader(getClass().getClassLoader());
-                       kernelThread.start();
-               } catch (Exception e) {
-                       throw new CmsException("Cannot get configuration", e);
-               }
+               initI18n();
+               initServices();
+
+               // kernel thread
+               kernelThread = new KernelThread(threadGroup, "Kernel Thread");
+               kernelThread.setContextClassLoader(getClass().getClassLoader());
+               kernelThread.start();
        }
 
        private void initI18n() {
@@ -153,9 +88,6 @@ public class CmsState implements NodeState, ManagedService {
        }
 
        private void initServices() {
-               // trackers
-               new ServiceTracker<HttpService, HttpService>(bc, HttpService.class, new PrepareHttpStc()).open();
-
                initTransactionManager();
 
                // JCR
@@ -167,12 +99,6 @@ public class CmsState implements NodeState, ManagedService {
                NodeRepositoryFactory repositoryFactory = new NodeRepositoryFactory();
                bc.registerService(RepositoryFactory.class, repositoryFactory, null);
 
-               // RepositoryService repositoryService = new RepositoryService();
-               // shutdownHooks.add(() -> repositoryService.shutdown());
-               // bc.registerService(LangUtils.names(ManagedService.class,
-               // MetaTypeProvider.class), repositoryService,
-               // LangUtils.init(Constants.SERVICE_PID, NodeConstants.NODE_REPO_PID));
-
                // Security
                NodeUserAdmin userAdmin = new NodeUserAdmin();
                shutdownHooks.add(() -> userAdmin.destroy());
@@ -182,17 +108,9 @@ public class CmsState implements NodeState, ManagedService {
 
                // UI
                bc.registerService(ApplicationConfiguration.class, new MaintenanceUi(),
-                               LangUtils.init(PROPERTY_CONTEXT_NAME, "system"));
-               bc.registerService(ApplicationConfiguration.class, new UserUi(), LangUtils.init(PROPERTY_CONTEXT_NAME, "user"));
+                               LangUtils.init(KernelConstants.CONTEXT_NAME_PROP, "system"));
+               bc.registerService(ApplicationConfiguration.class, new UserUi(), LangUtils.init(KernelConstants.CONTEXT_NAME_PROP, "user"));
        }
-       // private void initUserAdmin() {
-       // userAdmin = new NodeUserAdmin();
-       // // register
-       // Dictionary<String, Object> props = userAdmin.currentState();
-       // props.put(Constants.SERVICE_PID, NodeConstants.NODE_USER_ADMIN_PID);
-       // // TODO use ManagedService
-       // bc.registerService(UserAdmin.class, userAdmin, props);
-       // }
 
        private void initTransactionManager() {
                // TODO manage it in a managed service, as startup could be long
@@ -224,49 +142,7 @@ public class CmsState implements NodeState, ManagedService {
                        log.debug("Initialised default Bitronix transaction manager");
        }
 
-       // private void initRepositoryFactory() {
-       // // TODO rationalise RepositoryFactory
-       // repositoryFactory = new NodeRepositoryFactory();
-       // // register
-       // bc.registerService(RepositoryFactory.class, repositoryFactory, null);
-       // }
-
-       // private void initUi() {
-       // bc.registerService(ApplicationConfiguration.class, new MaintenanceUi(),
-       // LangUtils.init(PROPERTY_CONTEXT_NAME, "system"));
-       // bc.registerService(ApplicationConfiguration.class, new UserUi(),
-       // LangUtils.init(PROPERTY_CONTEXT_NAME, "user"));
-       // }
-
-       // private void initRepositories(Dictionary<String, ?> stateProps) throws
-       // IOException {
-       // // register
-       // repositoryServiceFactory = new RepositoryServiceFactory();
-       // bc.registerService(ManagedServiceFactory.class, repositoryServiceFactory,
-       // LangUtils.init(Constants.SERVICE_PID,
-       // NodeConstants.JACKRABBIT_FACTORY_PID));
-       //
-       // repositoryService = new RepositoryService();
-       // Dictionary<String, Object> regProps =
-       // LangUtils.init(Constants.SERVICE_PID, NodeConstants.NODE_REPO_PID);
-       // bc.registerService(LangUtils.names(ManagedService.class,
-       // MetaTypeProvider.class), repositoryService, regProps);
-       // }
-
-       // private void initNodeDeployment() throws IOException {
-       // Configuration nodeDeploymentConf =
-       // configurationAdmin.getConfiguration(NodeConstants.NODE_DEPLOYMENT_PID);
-       // nodeDeploymentConf.update(new Hashtable<>());
-       // }
-
        void shutdown() {
-               // if (transactionManager != null)
-               // transactionManager.shutdown();
-               // if (userAdmin != null)
-               // userAdmin.destroy();
-               // if (repositoryServiceFactory != null)
-               // repositoryServiceFactory.shutdown();
-
                applyShutdownHooks();
 
                if (kernelThread != null)
@@ -280,8 +156,6 @@ public class CmsState implements NodeState, ManagedService {
        private void applyShutdownHooks() {
                for (int i = shutdownHooks.size() - 1; i >= 0; i--) {
                        try {
-                               // new Thread(shutdownHooks.get(i), "CMS Shutdown Hook #" +
-                               // i).start();
                                shutdownHooks.get(i).run();
                        } catch (Exception e) {
                                log.error("Could not run shutdown hook #" + i);
@@ -291,49 +165,16 @@ public class CmsState implements NodeState, ManagedService {
                new GogoShellKiller().start();
        }
 
-       private class PrepareHttpStc implements ServiceTrackerCustomizer<HttpService, HttpService> {
-               private DataHttp dataHttp;
-               private NodeHttp nodeHttp;
-
-               @Override
-               public HttpService addingService(ServiceReference<HttpService> reference) {
-                       HttpService httpService = addHttpService(reference);
-                       return httpService;
-               }
-
-               @Override
-               public void modifiedService(ServiceReference<HttpService> reference, HttpService service) {
-               }
-
-               @Override
-               public void removedService(ServiceReference<HttpService> reference, HttpService service) {
-                       if (dataHttp != null)
-                               dataHttp.destroy();
-                       dataHttp = null;
-                       if (nodeHttp != null)
-                               nodeHttp.destroy();
-                       nodeHttp = null;
-               }
-
-               private HttpService addHttpService(ServiceReference<HttpService> sr) {
-                       HttpService httpService = bc.getService(sr);
-                       // TODO find constants
-                       Object httpPort = sr.getProperty("http.port");
-                       Object httpsPort = sr.getProperty("https.port");
-                       dataHttp = new DataHttp(httpService);
-                       nodeHttp = new NodeHttp(httpService, bc);
-                       if (log.isDebugEnabled())
-                               log.debug(httpPortsMsg(httpPort, httpsPort));
-                       return httpService;
-               }
-
-       }
-
        @Override
        public boolean isClean() {
                return cleanState;
        }
 
+       @Override
+       public Long getAvailableSince() {
+               return availableSince;
+       }
+
        /*
         * ACCESSORS
         */
@@ -349,13 +190,6 @@ public class CmsState implements NodeState, ManagedService {
                return hostname;
        }
 
-       /*
-        * STATIC
-        */
-       private static String httpPortsMsg(Object httpPort, Object httpsPort) {
-               return "HTTP " + httpPort + (httpsPort != null ? " - HTTPS " + httpsPort : "");
-       }
-
        /** Workaround for blocking Gogo shell by system shutdown. */
        private class GogoShellKiller extends Thread {