]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - org.argeo.init/src/org/argeo/init/RuntimeManagerMain.java
Fix IPA initialisation
[lgpl/argeo-commons.git] / org.argeo.init / src / org / argeo / init / RuntimeManagerMain.java
index d092242f7d6e41736df802c11b0bef411930b398..27778427c2df7638e8e91d593913a4d3be50947c 100644 (file)
@@ -1,28 +1,17 @@
 package org.argeo.init;
 
-import static org.argeo.api.init.InitConstants.SYMBOLIC_NAME_INIT;
-
 import java.lang.System.Logger;
 import java.lang.System.Logger.Level;
 import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Hashtable;
 import java.util.Map;
-import java.util.TreeMap;
-import java.util.function.Consumer;
 
 import org.argeo.api.init.InitConstants;
-import org.argeo.api.init.RuntimeContext;
 import org.argeo.api.init.RuntimeManager;
 import org.argeo.init.logging.ThinLoggerFinder;
-import org.argeo.init.osgi.OsgiBoot;
 import org.argeo.init.osgi.OsgiRuntimeContext;
 import org.argeo.internal.init.InternalState;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.FrameworkEvent;
-import org.osgi.framework.launch.Framework;
 
 /**
  * Dynamically configures and launches multiple runtimes, coordinated by a main
@@ -32,36 +21,39 @@ public class RuntimeManagerMain {
        private final static Logger logger = System.getLogger(RuntimeManagerMain.class.getName());
 
        private final static String ENV_STATE_DIRECTORY = "STATE_DIRECTORY";
-//     private final static String ENV_CONFIGURATION_DIRECTORY = "CONFIGURATION_DIRECTORY";
-//     private final static String ENV_CACHE_DIRECTORY = "CACHE_DIRECTORY";
+       private final static String ENV_CONFIGURATION_DIRECTORY = "CONFIGURATION_DIRECTORY";
+       private final static String ENV_CACHE_DIRECTORY = "CACHE_DIRECTORY";
 
        private final static long RUNTIME_SHUTDOWN_TIMEOUT = 60 * 1000;
 
-       private Path baseConfigArea;
-       private Path baseWritableArea;
        private Map<String, String> configuration = new HashMap<>();
 
-       RuntimeManagerMain(Path configArea, Path stateArea) {
-               RuntimeManager.loadConfig(configArea, configuration);
-               
-               // integration with OSGi runtime; this will be read by the init bundle
-               configuration.put(ServiceMain.PROP_ARGEO_INIT_MAIN, "true");
+       RuntimeManagerMain(Path configArea, Path stateArea, Path cacheArea) {
+               RuntimeManager.loadDefaults(configuration);
+
+               configuration.put(InitConstants.PROP_OSGI_USE_SYSTEM_PROPERTIES, "false");
+
+               configuration.put(InitConstants.PROP_ARGEO_CONFIG_AREA, configArea.toString());
+               configuration.put(InitConstants.PROP_ARGEO_STATE_AREA, stateArea.toString());
+               configuration.put(InitConstants.PROP_ARGEO_CACHE_AREA, cacheArea.toString());
+
                configuration.put(InitConstants.PROP_OSGI_SHARED_CONFIGURATION_AREA, configArea.toUri().toString());
-               
-               configuration.put(InitConstants.PROP_OSGI_CONFIGURATION_AREA, stateArea.resolve(RuntimeManager.STATE).toUri().toString());
-               // use config area if instance area is not set
-               if (!configuration.containsKey(InitConstants.PROP_OSGI_INSTANCE_AREA))
-                       configuration.put(InitConstants.PROP_OSGI_INSTANCE_AREA, stateArea.resolve(RuntimeManager.DATA).toUri().toString());
-               this.baseConfigArea = configArea.getParent();
-               this.baseWritableArea = stateArea.getParent();
+               configuration.put(InitConstants.PROP_OSGI_SHARED_CONFIGURATION_AREA_RO, "true");
 
-               logger.log(Level.TRACE, () -> "Runtime manager configuration: " + configuration);
+               configuration.put(InitConstants.PROP_OSGI_CONFIGURATION_AREA,
+                               cacheArea.resolve(RuntimeManager.OSGI_STORAGE_DIRNAME).toUri().toString());
+               configuration.put(InitConstants.PROP_OSGI_INSTANCE_AREA,
+                               stateArea.resolve(RuntimeManager.DATA).toUri().toString());
 
-//             System.out.println("java.library.path=" + System.getProperty("java.library.path"));
+               // TODO find a cleaner way to configure Jackrabbit indexes
+               configuration.put("argeo.node.repo.indexesBase", cacheArea.resolve("indexes").toString());
+
+               logger.log(Level.TRACE, () -> "Runtime manager configuration: " + configuration);
        }
 
        public void run() {
-               OsgiRuntimeContext managerRuntimeContext = new OsgiRuntimeContext(configuration);
+               OsgiRuntimeContext managerRuntimeContext = new OsgiRuntimeContext(OsgiRuntimeContext.loadFrameworkFactory(),
+                               configuration);
                try {
                        managerRuntimeContext.run();
                        InternalState.setMainRuntimeContext(managerRuntimeContext);
@@ -99,21 +91,25 @@ public class RuntimeManagerMain {
        public static void main(String[] args) {
                ThinLoggerFinder.reloadConfiguration();
                logger.log(Logger.Level.DEBUG, () -> "Argeo Init starting with PID " + ProcessHandle.current().pid());
-               Map<String, String> env = System.getenv();
-//             for (String envName : new TreeSet<>(env.keySet())) {
-//                     System.out.format("%s=%s%n", envName, env.get(envName));
-//             }
-               if (args.length < 1)
-                       throw new IllegalArgumentException("A relative configuration directory must be specified");
-               Path configArea = Paths.get(System.getProperty("user.dir"), args[0]);
-
-               // System.out.println("## Start with PID " + ProcessHandle.current().pid());
-               // System.out.println("user.dir=" + System.getProperty("user.dir"));
 
-               Path stateArea = Paths.get(env.get(ENV_STATE_DIRECTORY));
-
-               RuntimeManagerMain runtimeManager = new RuntimeManagerMain(configArea, stateArea);
+               Path writableArea = getLocalPath(InitConstants.PROP_ARGEO_STATE_AREA, ENV_STATE_DIRECTORY, null);
+               Path configArea = getLocalPath(InitConstants.PROP_ARGEO_CONFIG_AREA, ENV_CONFIGURATION_DIRECTORY, null);
+               Path cacheArea = getLocalPath(InitConstants.PROP_ARGEO_CACHE_AREA, ENV_CACHE_DIRECTORY, writableArea);
+               RuntimeManagerMain runtimeManager = new RuntimeManagerMain(configArea, writableArea, cacheArea);
                runtimeManager.run();
        }
 
+       private static Path getLocalPath(String systemProperty, String environmentVariable, Path defaultPath) {
+               String prop = System.getProperty(systemProperty);
+               if (prop != null)
+                       return Paths.get(prop);
+               String env = System.getenv().get(environmentVariable);
+               if (env != null)
+                       return Paths.get(env);
+               if (defaultPath != null)
+                       return defaultPath;
+               throw new IllegalStateException("No local path set with system property " + systemProperty
+                               + " or environment variable " + environmentVariable);
+               // TODO allocate a temporary directory? or defaults based on working directory ?
+       }
 }