X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.init%2Fsrc%2Forg%2Fargeo%2Finit%2FRuntimeManagerMain.java;h=41102243c476cfdb7855ef8dd7d149e96afe9c30;hb=faeeb8f0ba92b640942d88782ca6f5cfc332597e;hp=7a3f781391f9fd72e02424fb807ff63a5ae4a025;hpb=f92ba77466d18fcd0e7a9386ae0f16b530337f9f;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 7a3f78139..41102243c 100644 --- a/org.argeo.init/src/org/argeo/init/RuntimeManagerMain.java +++ b/org.argeo.init/src/org/argeo/init/RuntimeManagerMain.java @@ -1,35 +1,23 @@ 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.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 * one. */ -public class RuntimeManagerMain implements RuntimeManager { +public class RuntimeManagerMain { private final static Logger logger = System.getLogger(RuntimeManagerMain.class.getName()); private final static String ENV_STATE_DIRECTORY = "STATE_DIRECTORY"; @@ -38,20 +26,21 @@ public class RuntimeManagerMain implements RuntimeManager { private final static long RUNTIME_SHUTDOWN_TIMEOUT = 60 * 1000; - private Path baseConfigArea; - private Path baseWritableArea; private Map configuration = new HashMap<>(); - private Map runtimeContexts = new TreeMap<>(); - RuntimeManagerMain(Path configArea, Path stateArea) { RuntimeManager.loadConfig(configArea, configuration); - configuration.put(InitConstants.PROP_OSGI_CONFIGURATION_AREA, stateArea.resolve(STATE).toUri().toString()); + + // integration with OSGi runtime; this will be read by the init bundle + configuration.put(ServiceMain.PROP_ARGEO_INIT_MAIN, "true"); + 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(DATA).toUri().toString()); - this.baseConfigArea = configArea.getParent(); - this.baseWritableArea = stateArea.getParent(); + configuration.put(InitConstants.PROP_OSGI_INSTANCE_AREA, + stateArea.resolve(RuntimeManager.DATA).toUri().toString()); logger.log(Level.TRACE, () -> "Runtime manager configuration: " + configuration); @@ -59,19 +48,8 @@ public class RuntimeManagerMain implements RuntimeManager { } public void run() { -// try { -// for (Path p : Files.newDirectoryStream(Paths.get("/usr/local/lib/a2"), "*.so")) { -// try { -// System.load(p.toString()); -// } catch (UnsatisfiedLinkError e) { -// e.printStackTrace(); -// } -// } -// } catch (IOException e) { -// e.printStackTrace(); -// } - - OsgiRuntimeContext managerRuntimeContext = new OsgiRuntimeContext(configuration); + OsgiRuntimeContext managerRuntimeContext = new OsgiRuntimeContext(OsgiRuntimeContext.loadFrameworkFactory(), + configuration); try { managerRuntimeContext.run(); InternalState.setMainRuntimeContext(managerRuntimeContext); @@ -79,18 +57,14 @@ public class RuntimeManagerMain implements RuntimeManager { // shutdown on exit Runtime.getRuntime().addShutdownHook(new Thread(() -> shutdown(), "Runtime shutdown")); - 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(); - } - } - bc.registerService(RuntimeManager.class, this, new Hashtable<>(configuration)); +// BundleContext bc = managerRuntimeContext.getFramework().getBundleContext(); +// // uninstall init as a bundle since it will be available via OSGi system +// 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); } @@ -98,21 +72,6 @@ public class RuntimeManagerMain implements RuntimeManager { } protected void shutdown() { - // shutdowm runtimes - Map shutdowning = new HashMap<>(runtimeContexts); - for (String id : new HashSet<>(runtimeContexts.keySet())) { - logger.log(Logger.Level.DEBUG, "Shutting down runtime " + id + " ..."); - closeRuntime(id, true); - } - for (String id : shutdowning.keySet()) - try { - RuntimeContext runtimeContext = shutdowning.get(id); - 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(); @@ -125,75 +84,6 @@ public class RuntimeManagerMain implements RuntimeManager { } } - OsgiRuntimeContext loadRuntime(String relPath, Consumer> configCallback) { - closeRuntime(relPath, false); - Path writableArea = baseWritableArea.resolve(relPath); - Path configArea = baseConfigArea.resolve(relPath); - Map config = new HashMap<>(); - 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, writableArea.resolve(DATA).toUri().toString()); - - OsgiRuntimeContext runtimeContext = new OsgiRuntimeContext(config); - runtimeContexts.put(relPath, runtimeContext); - return runtimeContext; - } - - public void startRuntime(String relPath, Consumer> configCallback) { - OsgiRuntimeContext runtimeContext = loadRuntime(relPath, configCallback); - runtimeContext.run(); - Framework framework = runtimeContext.getFramework(); - -// for (Bundle b : framework.getBundleContext().getBundles()) { -// try { -//// if (b.getSymbolicName().startsWith("org.eclipse.swt.gtk")) { -//// b.uninstall(); -//// } -//// else if (b.getSymbolicName().startsWith("org.eclipse.jface")) { -//// b.uninstall(); -//// } -// } catch (Exception e) { -// // TODO Auto-generated catch block -// e.printStackTrace(); -// } -// } - - 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 closeRuntime(String relPath, boolean async) { - if (!runtimeContexts.containsKey(relPath)) - return; - RuntimeContext runtimeContext = runtimeContexts.get(relPath); - try { - runtimeContext.close(); - if (!async) { - runtimeContext.waitForStop(RUNTIME_SHUTDOWN_TIMEOUT); - System.gc(); - } - } catch (Exception e) { - logger.log(Level.ERROR, "Cannot close runtime context " + relPath, e); - } finally { - runtimeContexts.remove(relPath); - } - - } - public static void main(String[] args) { ThinLoggerFinder.reloadConfiguration(); logger.log(Logger.Level.DEBUG, () -> "Argeo Init starting with PID " + ProcessHandle.current().pid());