Adapt to MS Windows
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / internal / runtime / CmsStateImpl.java
index cf203e5e61bacf4de33bd5c1c4f182682d06611e..c1f92deb40b3ea7743098bcb0c8e4bbdacc028af 100644 (file)
 package org.argeo.cms.internal.runtime;
 
+import java.io.BufferedInputStream;
+import java.io.IOException;
+import java.io.Reader;
 import java.net.InetAddress;
 import java.net.URL;
 import java.net.UnknownHostException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.attribute.PosixFilePermission;
+import java.security.KeyStore;
 import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+import java.util.StringJoiner;
+import java.util.UUID;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ForkJoinPool;
+import java.util.concurrent.ForkJoinTask;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
 
 import javax.security.auth.login.Configuration;
 
+import org.argeo.api.cms.CmsConstants;
 import org.argeo.api.cms.CmsLog;
 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.cms.internal.osgi.CmsShutdown;
-import org.osgi.framework.Constants;
+import org.argeo.cms.util.FsUtils;
+import org.argeo.cms.util.OS;
 
 /**
  * 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());
+       private final Map<CmsDeployProperty, String> deployPropertyDefaults;
+
+       public CmsStateImpl() {
+               this.deployPropertyDefaults = Collections.unmodifiableMap(createDeployPropertiesDefaults());
+       }
+
+       protected Map<CmsDeployProperty, String> createDeployPropertiesDefaults() {
+               Map<CmsDeployProperty, String> deployPropertyDefaults = new HashMap<>();
+               deployPropertyDefaults.put(CmsDeployProperty.NODE_INIT, "../../init");
+               deployPropertyDefaults.put(CmsDeployProperty.LOCALE, Locale.getDefault().toString());
+
+               // certificates
+               deployPropertyDefaults.put(CmsDeployProperty.SSL_KEYSTORETYPE, KernelConstants.PKCS12);
+               deployPropertyDefaults.put(CmsDeployProperty.SSL_PASSWORD, KernelConstants.DEFAULT_KEYSTORE_PASSWORD);
+               Path keyStorePath = getDataPath(KernelConstants.DEFAULT_KEYSTORE_PATH);
+               if (keyStorePath != null) {
+                       deployPropertyDefaults.put(CmsDeployProperty.SSL_KEYSTORE, keyStorePath.toAbsolutePath().toString());
+               }
+
+               Path trustStorePath = getDataPath(KernelConstants.DEFAULT_TRUSTSTORE_PATH);
+               if (trustStorePath != null) {
+                       deployPropertyDefaults.put(CmsDeployProperty.SSL_TRUSTSTORE, trustStorePath.toAbsolutePath().toString());
+               }
+               deployPropertyDefaults.put(CmsDeployProperty.SSL_TRUSTSTORETYPE, KernelConstants.PKCS12);
+               deployPropertyDefaults.put(CmsDeployProperty.SSL_TRUSTSTOREPASSWORD, KernelConstants.DEFAULT_KEYSTORE_PASSWORD);
+
+               // SSH
+               Path authorizedKeysPath = getDataPath(KernelConstants.NODE_SSHD_AUTHORIZED_KEYS_PATH);
+               if (authorizedKeysPath != null) {
+                       deployPropertyDefaults.put(CmsDeployProperty.SSHD_AUTHORIZEDKEYS,
+                                       authorizedKeysPath.toAbsolutePath().toString());
+               }
+               return deployPropertyDefaults;
+       }
 
+       public void start() {
                try {
+                       // First init check
+                       Path privateBase = getDataPath(KernelConstants.DIR_PRIVATE);
+                       if (privateBase != null && !Files.exists(privateBase)) {// first init
+                               firstInit();
+                               Files.createDirectories(privateBase);
+                       }
+
                        initSecurity();
 //                     initArgeoLogger();
-//                     initNode();
 
                        if (log.isTraceEnabled())
                                log.trace("CMS State started");
 
-                       this.stateUuid = 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);
+                       this.uuid = uuidFactory.timeUUID();
+
+                       // hostname
+                       this.hostname = getDeployProperty(CmsDeployProperty.HOST);
+                       // TODO verify we have access to the IP address
+                       if (hostname == null) {
+                               final String LOCALHOST_IP = "::1";
+                               ForkJoinTask<String> hostnameFJT = ForkJoinPool.commonPool().submit(() -> {
+                                       try {
+                                               String hostname = InetAddress.getLocalHost().getHostName();
+                                               return hostname;
+                                       } catch (UnknownHostException e) {
+                                               throw new IllegalStateException("Cannot get local hostname", e);
+                                       }
+                               });
+                               try {
+                                       this.hostname = hostnameFJT.get(5, TimeUnit.SECONDS);
+                               } catch (InterruptedException | ExecutionException | TimeoutException e) {
+                                       this.hostname = LOCALHOST_IP;
+                                       log.warn("Could not get local hostname, using " + this.hostname);
+                               }
                        }
 
                        availableSince = System.currentTimeMillis();
-                       if (log.isDebugEnabled())
+                       if (log.isDebugEnabled()) {
                                // log.debug("## CMS starting... stateUuid=" + this.stateUuid + (cleanState ? "
                                // (clean state) " : " "));
-                               log.debug("## CMS starting... (" + stateUuid + ")");
-
-//             initI18n();
-//             initServices();
+                               StringJoiner sb = new StringJoiner("\n");
+                               CmsDeployProperty[] deployProperties = CmsDeployProperty.values();
+                               Arrays.sort(deployProperties, (o1, o2) -> o1.name().compareTo(o2.name()));
+                               for (CmsDeployProperty deployProperty : deployProperties) {
+                                       List<String> values = getDeployProperties(deployProperty);
+                                       for (int i = 0; i < values.size(); i++) {
+                                               String value = values.get(i);
+                                               if (value != null) {
+                                                       boolean isDefault = deployPropertyDefaults.containsKey(deployProperty)
+                                                                       && value.equals(deployPropertyDefaults.get(deployProperty));
+                                                       String line = deployProperty.getProperty() + (i == 0 ? "" : "." + i) + "=" + value
+                                                                       + (isDefault ? " (default)" : "");
+                                                       sb.add(line);
+                                               }
+                                       }
+                               }
+                               log.debug("## CMS starting... (" + uuid + ")\n" + sb + "\n");
+                       }
 
-               } catch (RuntimeException e) {
-                       log.error("## FATAL: CMS activator failed", e);
+               } catch (RuntimeException | IOException e) {
+                       log.error("## FATAL: CMS state failed", e);
                }
        }
 
        private void initSecurity() {
-               if (System.getProperty(KernelConstants.JAAS_CONFIG_PROP) == null) {
+               // private directory permissions
+               Path privateDir = getDataPath(KernelConstants.DIR_PRIVATE);
+               if (privateDir != null) {
+                       // TODO rather check whether we can read and write
+                       Set<PosixFilePermission> posixPermissions = new HashSet<>();
+                       posixPermissions.add(PosixFilePermission.OWNER_READ);
+                       posixPermissions.add(PosixFilePermission.OWNER_WRITE);
+                       posixPermissions.add(PosixFilePermission.OWNER_EXECUTE);
+                       try {
+                               if (!Files.exists(privateDir))
+                                       Files.createDirectories(privateDir);
+                               if (!OS.LOCAL.isMSWindows())
+                                       Files.setPosixFilePermissions(privateDir, posixPermissions);
+                       } catch (IOException e) {
+                               log.error("Cannot set permissions on " + privateDir, e);
+                       }
+               }
+
+               if (getDeployProperty(CmsDeployProperty.JAVA_LOGIN_CONFIG) == null) {
                        String jaasConfig = KernelConstants.JAAS_CONFIG;
                        URL url = getClass().getResource(jaasConfig);
                        // System.setProperty(KernelConstants.JAAS_CONFIG_PROP,
@@ -81,140 +185,193 @@ public class CmsStateImpl implements CmsState {
                }
                // explicitly load JAAS configuration
                Configuration.getConfiguration();
+
+               boolean initSsl = getDeployProperty(CmsDeployProperty.HTTPS_PORT) != null;
+               if (initSsl) {
+                       initCertificates();
+               }
        }
 
-//     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() {
-               if (log.isDebugEnabled())
-                       log.debug("CMS stopping...  (" + this.stateUuid + ")");
+       private void initCertificates() {
+               // server certificate
+               Path keyStorePath = Paths.get(getDeployProperty(CmsDeployProperty.SSL_KEYSTORE));
+               Path pemKeyPath = getDataPath(KernelConstants.DEFAULT_PEM_KEY_PATH);
+               Path pemCertPath = getDataPath(KernelConstants.DEFAULT_PEM_CERT_PATH);
+               char[] keyStorePassword = getDeployProperty(CmsDeployProperty.SSL_PASSWORD).toCharArray();
+
+               // Keystore
+               // if PEM files both exists, update the PKCS12 file
+               if (Files.exists(pemCertPath) && Files.exists(pemKeyPath)) {
+                       // TODO check certificate update time? monitor changes?
+                       KeyStore keyStore = PkiUtils.getKeyStore(keyStorePath, keyStorePassword,
+                                       getDeployProperty(CmsDeployProperty.SSL_KEYSTORETYPE));
+                       try (Reader key = Files.newBufferedReader(pemKeyPath, StandardCharsets.US_ASCII);
+                                       BufferedInputStream cert = new BufferedInputStream(Files.newInputStream(pemCertPath));) {
+                               PkiUtils.loadPrivateCertificatePem(keyStore, CmsConstants.NODE, key, keyStorePassword, cert);
+                               Files.createDirectories(keyStorePath.getParent());
+                               PkiUtils.saveKeyStore(keyStorePath, keyStorePassword, keyStore);
+                               if (log.isDebugEnabled())
+                                       log.debug("PEM certificate stored in " + keyStorePath);
+                       } catch (IOException e) {
+                               log.error("Cannot read PEM files " + pemKeyPath + " and " + pemCertPath, e);
+                       }
+               }
 
-               // 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
+               // Truststore
+               Path trustStorePath = Paths.get(getDeployProperty(CmsDeployProperty.SSL_TRUSTSTORE));
+               char[] trustStorePassword = getDeployProperty(CmsDeployProperty.SSL_TRUSTSTOREPASSWORD).toCharArray();
+
+               // IPA CA
+               Path ipaCaCertPath = Paths.get(KernelConstants.IPA_PEM_CA_CERT_PATH);
+               if (Files.exists(ipaCaCertPath)) {
+                       KeyStore trustStore = PkiUtils.getKeyStore(trustStorePath, trustStorePassword,
+                                       getDeployProperty(CmsDeployProperty.SSL_TRUSTSTORETYPE));
+                       try (BufferedInputStream cert = new BufferedInputStream(Files.newInputStream(ipaCaCertPath));) {
+                               PkiUtils.loadTrustedCertificatePem(trustStore, trustStorePassword, cert);
+                               Files.createDirectories(keyStorePath.getParent());
+                               PkiUtils.saveKeyStore(trustStorePath, trustStorePassword, trustStore);
+                               if (log.isDebugEnabled())
+                                       log.debug("IPA CA certificate stored in " + trustStorePath);
+                       } catch (IOException e) {
+                               log.error("Cannot trust CA certificate", e);
+                       }
                }
 
-//             internalExecutorService.shutdown();
+//             if (!Files.exists(keyStorePath))
+//                     PkiUtils.createSelfSignedKeyStore(keyStorePath, keyStorePassword, PkiUtils.PKCS12);
+       }
+
+       public void stop() {
+               if (log.isDebugEnabled())
+                       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);
+       private void firstInit() throws IOException {
+               log.info("## FIRST INIT ##");
+               List<String> nodeInits = getDeployProperties(CmsDeployProperty.NODE_INIT);
+//             if (nodeInits == null)
+//                     nodeInits = "../../init";
+               CmsStateImpl.prepareFirstInitInstanceArea(nodeInits);
+       }
+
+       @Override
+       public String getDeployProperty(String property) {
+               CmsDeployProperty deployProperty = CmsDeployProperty.find(property);
+               if (deployProperty == null) {
+                       // legacy
+                       if (property.startsWith("argeo.node.")) {
+                               return doGetDeployProperty(property);
+                       }
+                       if (property.equals("argeo.i18n.locales")) {
+                               String value = doGetDeployProperty(property);
+                               if (value != null) {
+                                       log.warn("Property " + property + " was ignored (value=" + value + ")");
+
+                               }
+                               return null;
                        }
+                       throw new IllegalArgumentException("Unsupported deploy property " + property);
                }
-               // Clean hanging Gogo shell thread
-               new GogoShellKiller().start();
+               int index = CmsDeployProperty.getPropertyIndex(property);
+               return getDeployProperty(deployProperty, index);
+       }
+
+       @Override
+       public List<String> getDeployProperties(String property) {
+               CmsDeployProperty deployProperty = CmsDeployProperty.find(property);
+               if (deployProperty == null)
+                       return new ArrayList<>();
+               return getDeployProperties(deployProperty);
+       }
+
+       public static List<String> getDeployProperties(CmsState cmsState, CmsDeployProperty deployProperty) {
+               return ((CmsStateImpl) cmsState).getDeployProperties(deployProperty);
+       }
+
+       public List<String> getDeployProperties(CmsDeployProperty deployProperty) {
+               List<String> res = new ArrayList<>(deployProperty.getMaxCount());
+               for (int i = 0; i < deployProperty.getMaxCount(); i++) {
+                       // String propertyName = i == 0 ? deployProperty.getProperty() :
+                       // deployProperty.getProperty() + "." + i;
+                       String value = getDeployProperty(deployProperty, i);
+                       res.add(i, value);
+               }
+               return res;
+       }
 
-//             instance = null;
+       public static String getDeployProperty(CmsState cmsState, CmsDeployProperty deployProperty) {
+               return ((CmsStateImpl) cmsState).getDeployProperty(deployProperty);
        }
 
-//     @Override
-//     public boolean isClean() {
-//             return cleanState;
-//     }
+       public String getDeployProperty(CmsDeployProperty deployProperty) {
+               String value = getDeployProperty(deployProperty, 0);
+               return value;
+       }
+
+       public String getDeployProperty(CmsDeployProperty deployProperty, int index) {
+               String propertyName = deployProperty.getProperty() + (index == 0 ? "" : "." + index);
+               String value = doGetDeployProperty(propertyName);
+               if (value == null && index == 0) {
+                       // try defaults
+                       if (deployPropertyDefaults.containsKey(deployProperty)) {
+                               value = deployPropertyDefaults.get(deployProperty);
+                               if (deployProperty.isSystemPropertyOnly())
+                                       System.setProperty(deployProperty.getProperty(), value);
+                       }
+
+                       if (value == null) {
+                               // try legacy properties
+                               String legacyProperty = switch (deployProperty) {
+                               case DIRECTORY -> "argeo.node.useradmin.uris";
+                               case DB_URL -> "argeo.node.dburl";
+                               case DB_USER -> "argeo.node.dbuser";
+                               case DB_PASSWORD -> "argeo.node.dbpassword";
+                               case HTTP_PORT -> "org.osgi.service.http.port";
+                               case HTTPS_PORT -> "org.osgi.service.http.port.secure";
+                               case HOST -> "org.eclipse.equinox.http.jetty.http.host";
+                               case LOCALE -> "argeo.i18n.defaultLocale";
+
+                               default -> null;
+                               };
+                               if (legacyProperty != null) {
+                                       value = doGetDeployProperty(legacyProperty);
+                                       if (value != null) {
+                                               log.warn("Retrieved deploy property " + deployProperty.getProperty()
+                                                               + " through deprecated property " + legacyProperty);
+                                       }
+                               }
+                       }
+               }
+               if (index == 0 && deployProperty.isSystemPropertyOnly()) {
+                       String systemPropertyValue = System.getProperty(deployProperty.getProperty());
+                       if (!Objects.equals(value, systemPropertyValue))
+                               throw new IllegalStateException(
+                                               "Property " + deployProperty + " must be a ssystem property, but its value is " + value
+                                                               + ", while the system property value is " + systemPropertyValue);
+               }
+               return value != null ? value.toString() : null;
+       }
+
+       protected String getLegacyProperty(String legacyProperty, CmsDeployProperty deployProperty) {
+               String value = doGetDeployProperty(legacyProperty);
+               if (value != null) {
+                       log.warn("Retrieved deploy property " + deployProperty.getProperty() + " through deprecated property "
+                                       + legacyProperty + ".");
+               }
+               return value;
+       }
+
+       protected String doGetDeployProperty(String property) {
+               return KernelUtils.getFrameworkProp(property);
+       }
+
+       @Override
+       public Path getDataPath(String relativePath) {
+               return KernelUtils.getOsgiInstancePath(relativePath);
+       }
 
        @Override
        public Long getAvailableSince() {
@@ -224,10 +381,50 @@ public class CmsStateImpl implements CmsState {
        /*
         * ACCESSORS
         */
+       @Override
+       public UUID getUuid() {
+               return uuid;
+       }
+
+       public void setUuidFactory(UuidFactory uuidFactory) {
+               this.uuidFactory = uuidFactory;
+       }
+
        public String getHostname() {
                return hostname;
        }
 
+       /**
+        * Called before node initialisation, in order populate OSGi instance are with
+        * some files (typically LDIF, etc).
+        */
+       public static void prepareFirstInitInstanceArea(List<String> nodeInits) {
+
+               for (String nodeInit : nodeInits) {
+                       if (nodeInit == null)
+                               continue;
+
+                       if (nodeInit.startsWith("http")) {
+                               // TODO reconnect it
+                               // registerRemoteInit(nodeInit);
+                       } else {
+
+                               // TODO use java.nio.file
+                               Path initDir;
+                               if (nodeInit.startsWith("."))
+                                       initDir = KernelUtils.getExecutionDir(nodeInit);
+                               else
+                                       initDir = Paths.get(nodeInit);
+                               // TODO also uncompress archives
+                               if (Files.exists(initDir)) {
+                                       Path dataPath = KernelUtils.getOsgiInstancePath("");
+                                       FsUtils.copyDirectory(initDir, dataPath);
+                                       log.info("CMS initialized from " + initDir);
+                               }
+                       }
+               }
+       }
+
        /*
         * STATIC
         */