]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - org.argeo.cms/src/org/argeo/cms/internal/runtime/CmsStateImpl.java
Improve ACR.
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / internal / runtime / CmsStateImpl.java
index c071283949b3d688d7738fb4ee79cc9482348e10..41e32653432cc7b4e621109c0c9d67bfc92b9dc6 100644 (file)
@@ -3,51 +3,43 @@ package org.argeo.cms.internal.runtime;
 import java.net.InetAddress;
 import java.net.URL;
 import java.net.UnknownHostException;
+import java.util.UUID;
 
 import javax.security.auth.login.Configuration;
 
 import org.argeo.api.cms.CmsLog;
 import org.argeo.api.cms.CmsState;
+import org.argeo.api.uuid.UuidFactory;
 import org.argeo.cms.auth.ident.IdentClient;
-import org.argeo.cms.internal.osgi.CmsShutdown;
-import org.osgi.framework.Constants;
 
 /**
  * Implementation of a {@link CmsState}, initialising the required services.
  */
 public class CmsStateImpl implements CmsState {
        private final static CmsLog log = CmsLog.getLog(CmsStateImpl.class);
-//     private final BundleContext bc = FrameworkUtil.getBundle(CmsState.class).getBundleContext();
-
-//     private static CmsStateImpl instance;
-
-//     private ExecutorService internalExecutorService;
 
        // REFERENCES
        private Long availableSince;
 
-//     private ThreadGroup threadGroup = new ThreadGroup("CMS");
-//     private List<Runnable> stopHooks = new ArrayList<>();
-
-       private String stateUuid;
+       private UUID uuid;
 //     private final boolean cleanState;
        private String hostname;
 
-       public void init() {
-//             instance = this;
+       private UuidFactory uuidFactory;
 
-               Runtime.getRuntime().addShutdownHook(new CmsShutdown());
-//             this.internalExecutorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
+       public void start() {
+//             Runtime.getRuntime().addShutdownHook(new CmsShutdown());
 
                try {
                        initSecurity();
 //                     initArgeoLogger();
-//                     initNode();
 
                        if (log.isTraceEnabled())
                                log.trace("CMS State started");
 
-                       this.stateUuid = KernelUtils.getFrameworkProp(Constants.FRAMEWORK_UUID);
+//                     String stateUuidStr = KernelUtils.getFrameworkProp(Constants.FRAMEWORK_UUID);
+//                     this.uuid = UUID.fromString(stateUuidStr);
+                       this.uuid = uuidFactory.timeUUID();
 //             this.cleanState = stateUuid.equals(frameworkUuid);
                        try {
                                this.hostname = InetAddress.getLocalHost().getHostName();
@@ -59,7 +51,7 @@ public class CmsStateImpl implements CmsState {
                        if (log.isDebugEnabled())
                                // log.debug("## CMS starting... stateUuid=" + this.stateUuid + (cleanState ? "
                                // (clean state) " : " "));
-                               log.debug("## CMS starting... (" + stateUuid + ")");
+                               log.debug("## CMS starting... (" + uuid + ")");
 
 //             initI18n();
 //             initServices();
@@ -81,140 +73,14 @@ public class CmsStateImpl implements CmsState {
                Configuration.getConfiguration();
        }
 
-//     private void initI18n() {
-//             Object defaultLocaleValue = KernelUtils.getFrameworkProp(CmsConstants.I18N_DEFAULT_LOCALE);
-//             defaultLocale = defaultLocaleValue != null ? new Locale(defaultLocaleValue.toString())
-//                             : new Locale(ENGLISH.getLanguage());
-//             locales = LocaleUtils.asLocaleList(KernelUtils.getFrameworkProp(CmsConstants.I18N_LOCALES));
-//     }
-
-       private void initServices() {
-               // JTA
-//             String tmType = KernelUtils.getFrameworkProp(CmsConstants.TRANSACTION_MANAGER,
-//                             CmsConstants.TRANSACTION_MANAGER_SIMPLE);
-//             if (CmsConstants.TRANSACTION_MANAGER_SIMPLE.equals(tmType)) {
-//                     initSimpleTransactionManager();
-//             } else if (CmsConstants.TRANSACTION_MANAGER_BITRONIX.equals(tmType)) {
-////                   initBitronixTransactionManager();
-//                     throw new UnsupportedOperationException(
-//                                     "Bitronix is not supported anymore, but could be again if there is enough interest.");
-//             } else {
-//                     throw new IllegalArgumentException("Usupported transaction manager type " + tmType);
-//             }
-
-               // POI
-//             POIXMLTypeLoader.setClassLoader(CTConnection.class.getClassLoader());
-
-               // Tika
-//             OpenDocumentParser odfParser = new OpenDocumentParser();
-//             bc.registerService(Parser.class, odfParser, new Hashtable());
-//             PDFParser pdfParser = new PDFParser();
-//             bc.registerService(Parser.class, pdfParser, new Hashtable());
-//             OOXMLParser ooxmlParser = new OOXMLParser();
-//             bc.registerService(Parser.class, ooxmlParser, new Hashtable());
-//             TesseractOCRParser ocrParser = new TesseractOCRParser();
-//             ocrParser.setLanguage("ara");
-//             bc.registerService(Parser.class, ocrParser, new Hashtable());
-
-//             // 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(CmsConstants.ROLES_BASEDN, CmsConstants.TOKENS_BASEDN);
-//             stopHooks.add(() -> userAdmin.destroy());
-//             Activator.registerService(ManagedServiceFactory.class, userAdmin,
-//                             LangUtils.dict(Constants.SERVICE_PID, CmsConstants.NODE_USER_ADMIN_PID));
-
-       }
-
-//     private void initSimpleTransactionManager() {
-//             SimpleTransactionManager transactionManager = new SimpleTransactionManager();
-//             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");
-//     }
-
-       public void destroy() {
+       public void stop() {
                if (log.isDebugEnabled())
-                       log.debug("CMS stopping...  (" + this.stateUuid + ")");
-//             new GogoShellKiller().start();
-
-               // 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
-//             }
-
-//             internalExecutorService.shutdown();
+                       log.debug("CMS stopping...  (" + this.uuid + ")");
 
                long duration = ((System.currentTimeMillis() - availableSince) / 1000) / 60;
                log.info("## ARGEO CMS STOPPED after " + (duration / 60) + "h " + (duration % 60) + "min uptime ##");
        }
 
-       /** Apply shutdown hoos in reverse order. */
-//     private void applyStopHooks() {
-////           for (int i = stopHooks.size() - 1; i >= 0; i--) {
-////                   try {
-////                           stopHooks.get(i).run();
-////                   } catch (Exception e) {
-////                           log.error("Could not run shutdown hook #" + i);
-////                   }
-////           }
-//             // Clean hanging Gogo shell thread
-//             new GogoShellKiller().start();
-//
-////           instance = null;
-//     }
-
-//     @Override
-//     public boolean isClean() {
-//             return cleanState;
-//     }
-
        @Override
        public Long getAvailableSince() {
                return availableSince;
@@ -227,6 +93,15 @@ public class CmsStateImpl implements CmsState {
                return hostname;
        }
 
+       @Override
+       public UUID getUuid() {
+               return uuid;
+       }
+
+       public void setUuidFactory(UuidFactory uuidFactory) {
+               this.uuidFactory = uuidFactory;
+       }
+
        /*
         * STATIC
         */