Use OSGi framework UUID as CMS State UUID
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / internal / runtime / CmsStateImpl.java
index 6921de77abc4b07954907ebde4d2d7f679a2dc31..ee7f06340d521a76642c0bd9424436c4244e3629 100644 (file)
@@ -6,6 +6,7 @@ import java.io.Reader;
 import java.net.InetAddress;
 import java.net.URL;
 import java.net.UnknownHostException;
+import java.nio.charset.Charset;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.nio.file.Path;
@@ -23,6 +24,7 @@ import java.util.Map;
 import java.util.Objects;
 import java.util.Set;
 import java.util.StringJoiner;
+import java.util.TreeMap;
 import java.util.UUID;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ForkJoinPool;
@@ -38,7 +40,8 @@ import org.argeo.api.cms.CmsState;
 import org.argeo.api.uuid.UuidFactory;
 import org.argeo.cms.CmsDeployProperty;
 import org.argeo.cms.auth.ident.IdentClient;
-import org.argeo.util.FsUtils;
+import org.argeo.cms.util.FsUtils;
+import org.argeo.cms.util.OS;
 
 /**
  * Implementation of a {@link CmsState}, initialising the required services.
@@ -91,19 +94,25 @@ public class CmsStateImpl implements CmsState {
        }
 
        public void start() {
-//             Runtime.getRuntime().addShutdownHook(new CmsShutdown());
-
+               Charset defaultCharset = Charset.defaultCharset();
+               if (!StandardCharsets.UTF_8.equals(defaultCharset))
+                       log.error("Default JVM charset is " + defaultCharset + " and not " + StandardCharsets.UTF_8);
                try {
+                       // First init check
+                       Path privateBase = getDataPath(KernelConstants.DIR_PRIVATE);
+                       if (privateBase != null && !Files.exists(privateBase)) {// first init
+                               firstInit();
+                               Files.createDirectories(privateBase);
+                       }
+
                        initSecurity();
 //                     initArgeoLogger();
 
                        if (log.isTraceEnabled())
                                log.trace("CMS State started");
 
-//                     String stateUuidStr = KernelUtils.getFrameworkProp(Constants.FRAMEWORK_UUID);
-//                     this.uuid = UUID.fromString(stateUuidStr);
-                       this.uuid = uuidFactory.timeUUID();
-//             this.cleanState = stateUuid.equals(frameworkUuid);
+                       String frameworkUuid = KernelUtils.getFrameworkProp(KernelUtils.OSGI_FRAMEWORK_UUID);
+                       this.uuid = frameworkUuid != null ? UUID.fromString(frameworkUuid) : uuidFactory.timeUUID();
 
                        // hostname
                        this.hostname = getDeployProperty(CmsDeployProperty.HOST);
@@ -149,10 +158,14 @@ public class CmsStateImpl implements CmsState {
                                log.debug("## CMS starting... (" + uuid + ")\n" + sb + "\n");
                        }
 
-                       Path privateBase = getDataPath(KernelConstants.DIR_PRIVATE);
-                       if (privateBase != null && !Files.exists(privateBase)) {// first init
-                               firstInit();
-                               Files.createDirectories(privateBase);
+                       if (log.isTraceEnabled()) {
+                               // print system properties
+                               StringJoiner sb = new StringJoiner("\n");
+                               for (Object key : new TreeMap<>(System.getProperties()).keySet()) {
+                                       sb.add(key + "=" + System.getProperty(key.toString()));
+                               }
+                               log.trace("System properties:\n" + sb + "\n");
+
                        }
 
                } catch (RuntimeException | IOException e) {
@@ -162,7 +175,7 @@ public class CmsStateImpl implements CmsState {
 
        private void initSecurity() {
                // private directory permissions
-               Path privateDir = KernelUtils.getOsgiInstancePath(KernelConstants.DIR_PRIVATE);
+               Path privateDir = getDataPath(KernelConstants.DIR_PRIVATE);
                if (privateDir != null) {
                        // TODO rather check whether we can read and write
                        Set<PosixFilePermission> posixPermissions = new HashSet<>();
@@ -172,7 +185,8 @@ public class CmsStateImpl implements CmsState {
                        try {
                                if (!Files.exists(privateDir))
                                        Files.createDirectories(privateDir);
-                               Files.setPosixFilePermissions(privateDir, posixPermissions);
+                               if (!OS.LOCAL.isMSWindows())
+                                       Files.setPosixFilePermissions(privateDir, posixPermissions);
                        } catch (IOException e) {
                                log.error("Cannot set permissions on " + privateDir, e);
                        }
@@ -188,8 +202,9 @@ public class CmsStateImpl implements CmsState {
                // explicitly load JAAS configuration
                Configuration.getConfiguration();
 
-               boolean initSsl = getDeployProperty(CmsDeployProperty.HTTPS_PORT) != null;
-               if (initSsl) {
+               boolean initCertificates = (getDeployProperty(CmsDeployProperty.HTTPS_PORT) != null)
+                               || (getDeployProperty(CmsDeployProperty.SSHD_PORT) != null);
+               if (initCertificates) {
                        initCertificates();
                }
        }
@@ -238,9 +253,6 @@ public class CmsStateImpl implements CmsState {
                                log.error("Cannot trust CA certificate", e);
                        }
                }
-
-//             if (!Files.exists(keyStorePath))
-//                     PkiUtils.createSelfSignedKeyStore(keyStorePath, keyStorePassword, PkiUtils.PKCS12);
        }
 
        public void stop() {
@@ -248,7 +260,8 @@ public class CmsStateImpl implements CmsState {
                        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 ##");
+               log.info("## ARGEO CMS " + uuid + " STOPPED after " + (duration / 60) + "h " + (duration % 60)
+                               + "min uptime ##");
        }
 
        private void firstInit() throws IOException {
@@ -375,6 +388,11 @@ public class CmsStateImpl implements CmsState {
                return KernelUtils.getOsgiInstancePath(relativePath);
        }
 
+       @Override
+       public Path getStatePath(String relativePath) {
+               return KernelUtils.getOsgiConfigurationPath(relativePath);
+       }
+
        @Override
        public Long getAvailableSince() {
                return availableSince;