X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.init%2Fsrc%2Forg%2Fargeo%2Finit%2FRuntimeManagerMain.java;h=9c8692c4b3ca401634a4d365e0d9dd4c14daa46a;hb=6bfcb81dd21d8fdd9094610c00f5f8ad9c6a36a7;hp=72b673b68abd18f2fcf335179beb2cecdaadf099;hpb=e90495f6155a3996fa8b7009a5f7dc7f33a2ecb8;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.init/src/org/argeo/init/RuntimeManagerMain.java b/org.argeo.init/src/org/argeo/init/RuntimeManagerMain.java index 72b673b68..9c8692c4b 100644 --- a/org.argeo.init/src/org/argeo/init/RuntimeManagerMain.java +++ b/org.argeo.init/src/org/argeo/init/RuntimeManagerMain.java @@ -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 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 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> configCallback) { - stopRuntime(relPath, false); + closeRuntime(relPath, false); Path stateArea = baseStateArea.resolve(relPath); Path configArea = baseConfigArea.resolve(relPath); Map config = new HashMap<>(); @@ -160,9 +163,20 @@ public class RuntimeManagerMain implements RuntimeManager { public void startRuntime(String relPath, Consumer> 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 env = System.getenv(); // for (String envName : new TreeSet<>(env.keySet())) { // System.out.format("%s=%s%n", envName, env.get(envName));