Clean up Argeo Init
[lgpl/argeo-commons.git] / org.argeo.init / src / org / argeo / init / RuntimeManagerMain.java
index 72b673b68abd18f2fcf335179beb2cecdaadf099..9c8692c4b3ca401634a4d365e0d9dd4c14daa46a 100644 (file)
@@ -27,6 +27,8 @@ 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
@@ -91,7 +93,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 +101,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();
@@ -115,7 +118,7 @@ public class RuntimeManagerMain implements RuntimeManager {
 
        public static void loadConfig(Path dir, Map<String, String> config) {
                try {
-                       System.out.println("Load from " + dir);
+//                     System.out.println("Load from " + dir);
                        Path jvmArgsPath = dir.resolve(JVM_ARGS);
                        if (!Files.exists(jvmArgsPath)) {
                                // load from parent directory
@@ -138,7 +141,7 @@ public class RuntimeManagerMain implements RuntimeManager {
        }
 
        OsgiRuntimeContext loadRuntime(String relPath, Consumer<Map<String, String>> configCallback) {
-               stopRuntime(relPath, false);
+               closeRuntime(relPath, false);
                Path stateArea = baseStateArea.resolve(relPath);
                Path configArea = baseConfigArea.resolve(relPath);
                Map<String, String> config = new HashMap<>();
@@ -160,9 +163,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 +196,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));