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
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 {
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();
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
}
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<>();
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);
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));