Introduce OSGi sub framework with shared class loaders
[lgpl/argeo-commons.git] / org.argeo.init / src / org / argeo / init / RuntimeManagerMain.java
index 72b673b68abd18f2fcf335179beb2cecdaadf099..f4ed507c07b37229cd73b8ccbd376deadcef8f22 100644 (file)
@@ -2,19 +2,14 @@ package org.argeo.init;
 
 import static org.argeo.api.init.InitConstants.SYMBOLIC_NAME_INIT;
 
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.UncheckedIOException;
 import java.lang.System.Logger;
 import java.lang.System.Logger.Level;
-import java.nio.file.Files;
 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.Properties;
 import java.util.TreeMap;
 import java.util.function.Consumer;
 
@@ -22,11 +17,12 @@ 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.Bundle;
 import org.osgi.framework.BundleContext;
-import org.osgi.framework.BundleException;
+import org.osgi.framework.FrameworkEvent;
+import org.osgi.framework.launch.Framework;
 
 /**
  * Dynamically configures and launches multiple runtimes, coordinated by a main
@@ -41,22 +37,24 @@ public class RuntimeManagerMain implements RuntimeManager {
 
        private final static long RUNTIME_SHUTDOWN_TIMEOUT = 60 * 1000;
 
-       private final static String JVM_ARGS = "jvm.args";
-
        private Path baseConfigArea;
-       private Path baseStateArea;
+       private Path baseWritableArea;
        private Map<String, String> configuration = new HashMap<>();
 
        private Map<String, OsgiRuntimeContext> runtimeContexts = new TreeMap<>();
 
        RuntimeManagerMain(Path configArea, Path stateArea) {
-               loadConfig(configArea, configuration);
-               configuration.put(InitConstants.PROP_OSGI_CONFIGURATION_AREA, stateArea.toUri().toString());
+               RuntimeManager.loadConfig(configArea, configuration);
+               configuration.put(InitConstants.PROP_OSGI_CONFIGURATION_AREA, stateArea.resolve(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(DATA).toUri().toString());
                this.baseConfigArea = configArea.getParent();
-               this.baseStateArea = stateArea.getParent();
+               this.baseWritableArea = stateArea.getParent();
 
                logger.log(Level.TRACE, () -> "Runtime manager configuration: " + configuration);
 
+//             System.out.println("java.library.path=" + System.getProperty("java.library.path"));
        }
 
        public void run() {
@@ -70,16 +68,12 @@ public class RuntimeManagerMain implements RuntimeManager {
 
                        BundleContext bc = managerRuntimeContext.getFramework().getBundleContext();
                        // uninstall init as a bundle since it will be available via OSGi system
-                       for (Bundle b : bc.getBundles()) {
-                               if (b.getSymbolicName().equals(SYMBOLIC_NAME_INIT)) {
-                                       b.uninstall();
-                               }
-                       }
+                       OsgiBoot.uninstallBundles(bc, SYMBOLIC_NAME_INIT);
                        bc.registerService(RuntimeManager.class, this, new Hashtable<>(configuration));
                        logger.log(Level.DEBUG, "Registered runtime manager");
 
                        managerRuntimeContext.waitForStop(0);
-               } catch (InterruptedException | BundleException e) {
+               } catch (InterruptedException e) {
                        e.printStackTrace();
                        System.exit(1);
                }
@@ -91,7 +85,7 @@ public class RuntimeManagerMain implements RuntimeManager {
                Map<String, RuntimeContext> shutdowning = new HashMap<>(runtimeContexts);
                for (String id : new HashSet<>(runtimeContexts.keySet())) {
                        logger.log(Logger.Level.DEBUG, "Shutting down runtime " + id + " ...");
-                       stopRuntime(id, true);
+                       closeRuntime(id, true);
                }
                for (String id : shutdowning.keySet())
                        try {
@@ -99,8 +93,9 @@ public class RuntimeManagerMain implements RuntimeManager {
                                runtimeContext.waitForStop(RUNTIME_SHUTDOWN_TIMEOUT);
                        } catch (InterruptedException e) {
                                // silent
+                       } catch (Exception e) {
+                               logger.log(Logger.Level.DEBUG, "Cannot wait for " + id + " to shutdown", e);
                        }
-
                // shutdown manager runtime
                try {
                        InternalState.getMainRuntimeContext().close();
@@ -113,44 +108,20 @@ public class RuntimeManagerMain implements RuntimeManager {
                }
        }
 
-       public static void loadConfig(Path dir, Map<String, String> config) {
-               try {
-                       System.out.println("Load from " + dir);
-                       Path jvmArgsPath = dir.resolve(JVM_ARGS);
-                       if (!Files.exists(jvmArgsPath)) {
-                               // load from parent directory
-                               loadConfig(dir.getParent(), config);
-                       }
-
-                       if (Files.exists(dir))
-                               for (Path p : Files.newDirectoryStream(dir, "*.ini")) {
-                                       Properties props = new Properties();
-                                       try (InputStream in = Files.newInputStream(p)) {
-                                               props.load(in);
-                                       }
-                                       for (Object key : props.keySet()) {
-                                               config.put(key.toString(), props.getProperty(key.toString()));
-                                       }
-                               }
-               } catch (IOException e) {
-                       throw new UncheckedIOException("Cannot load configuration from " + dir, e);
-               }
-       }
-
        OsgiRuntimeContext loadRuntime(String relPath, Consumer<Map<String, String>> configCallback) {
-               stopRuntime(relPath, false);
-               Path stateArea = baseStateArea.resolve(relPath);
+               closeRuntime(relPath, false);
+               Path writableArea = baseWritableArea.resolve(relPath);
                Path configArea = baseConfigArea.resolve(relPath);
                Map<String, String> config = new HashMap<>();
-               loadConfig(configArea, config);
-               config.put(InitConstants.PROP_OSGI_CONFIGURATION_AREA, stateArea.toUri().toString());
+               RuntimeManager.loadConfig(configArea, config);
+               config.put(InitConstants.PROP_OSGI_CONFIGURATION_AREA, writableArea.resolve(STATE).toUri().toString());
 
                if (configCallback != null)
                        configCallback.accept(config);
 
                // use config area if instance area is not set
                if (!config.containsKey(InitConstants.PROP_OSGI_INSTANCE_AREA))
-                       config.put(InitConstants.PROP_OSGI_INSTANCE_AREA, config.get(InitConstants.PROP_OSGI_CONFIGURATION_AREA));
+                       config.put(InitConstants.PROP_OSGI_INSTANCE_AREA, writableArea.resolve(DATA).toUri().toString());
 
                OsgiRuntimeContext runtimeContext = new OsgiRuntimeContext(config);
                runtimeContexts.put(relPath, runtimeContext);
@@ -160,9 +131,20 @@ public class RuntimeManagerMain implements RuntimeManager {
        public void startRuntime(String relPath, Consumer<Map<String, String>> configCallback) {
                OsgiRuntimeContext runtimeContext = loadRuntime(relPath, configCallback);
                runtimeContext.run();
+               Framework framework = runtimeContext.getFramework();
+               if (framework != null) {// in case the framework has closed very quickly after run
+                       framework.getBundleContext().addFrameworkListener((e) -> {
+                               if (e.getType() >= FrameworkEvent.STOPPED) {
+                                       logger.log(Level.DEBUG, "Externally stopped runtime " + relPath + ". Unregistering...", e);
+                                       runtimeContexts.remove(relPath);
+                               }
+                       });
+               } else {
+                       closeRuntime(relPath, false);
+               }
        }
 
-       public void stopRuntime(String relPath, boolean async) {
+       public void closeRuntime(String relPath, boolean async) {
                if (!runtimeContexts.containsKey(relPath))
                        return;
                RuntimeContext runtimeContext = runtimeContexts.get(relPath);
@@ -182,7 +164,7 @@ public class RuntimeManagerMain implements RuntimeManager {
 
        public static void main(String[] args) {
                ThinLoggerFinder.reloadConfiguration();
-               logger.log(Logger.Level.INFO, () -> "Argeo Init starting with PID " + ProcessHandle.current().pid());
+               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));