]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.init/src/org/argeo/init/RuntimeManagerMain.java
Improve nested OSGi runtimes
[lgpl/argeo-commons.git] / org.argeo.init / src / org / argeo / init / RuntimeManagerMain.java
1 package org.argeo.init;
2
3 import java.lang.System.Logger;
4 import java.lang.System.Logger.Level;
5 import java.nio.file.Path;
6 import java.nio.file.Paths;
7 import java.util.HashMap;
8 import java.util.Map;
9
10 import org.argeo.api.init.InitConstants;
11 import org.argeo.api.init.RuntimeManager;
12 import org.argeo.init.logging.ThinLoggerFinder;
13 import org.argeo.init.osgi.OsgiRuntimeContext;
14 import org.argeo.internal.init.InternalState;
15
16 /**
17 * Dynamically configures and launches multiple runtimes, coordinated by a main
18 * one.
19 */
20 public class RuntimeManagerMain {
21 private final static Logger logger = System.getLogger(RuntimeManagerMain.class.getName());
22
23 private final static String ENV_STATE_DIRECTORY = "STATE_DIRECTORY";
24 private final static String ENV_CONFIGURATION_DIRECTORY = "CONFIGURATION_DIRECTORY";
25 // private final static String ENV_CACHE_DIRECTORY = "CACHE_DIRECTORY";
26
27 private final static long RUNTIME_SHUTDOWN_TIMEOUT = 60 * 1000;
28
29 private Map<String, String> configuration = new HashMap<>();
30
31 RuntimeManagerMain(Path configArea, Path writableArea) {
32 // RuntimeManager.loadConfig(configArea, configuration);
33
34 // integration with OSGi runtime; this will be read by the init bundle
35 // configuration.put(ServiceMain.PROP_ARGEO_INIT_MAIN, "true");
36 RuntimeManager.loadDefaults(configuration);
37
38 configuration.put(InitConstants.PROP_OSGI_SHARED_CONFIGURATION_AREA, configArea.toUri().toString());
39 configuration.put(InitConstants.PROP_OSGI_SHARED_CONFIGURATION_AREA_RO, "true");
40 configuration.put(InitConstants.PROP_OSGI_USE_SYSTEM_PROPERTIES, "false");
41
42 configuration.put(InitConstants.PROP_OSGI_CONFIGURATION_AREA,
43 writableArea.resolve(RuntimeManager.STATE).toUri().toString());
44 // use config area if instance area is not set
45 if (!configuration.containsKey(InitConstants.PROP_OSGI_INSTANCE_AREA))
46 configuration.put(InitConstants.PROP_OSGI_INSTANCE_AREA,
47 writableArea.resolve(RuntimeManager.DATA).toUri().toString());
48
49 logger.log(Level.TRACE, () -> "Runtime manager configuration: " + configuration);
50
51 // System.out.println("java.library.path=" + System.getProperty("java.library.path"));
52 }
53
54 public void run() {
55 OsgiRuntimeContext managerRuntimeContext = new OsgiRuntimeContext(OsgiRuntimeContext.loadFrameworkFactory(),
56 configuration);
57 try {
58 managerRuntimeContext.run();
59 InternalState.setMainRuntimeContext(managerRuntimeContext);
60
61 // shutdown on exit
62 Runtime.getRuntime().addShutdownHook(new Thread(() -> shutdown(), "Runtime shutdown"));
63
64 // BundleContext bc = managerRuntimeContext.getFramework().getBundleContext();
65 // // uninstall init as a bundle since it will be available via OSGi system
66 // OsgiBoot.uninstallBundles(bc, SYMBOLIC_NAME_INIT);
67 // bc.registerService(RuntimeManager.class, this, new Hashtable<>(configuration));
68 logger.log(Level.DEBUG, "Registered runtime manager");
69
70 managerRuntimeContext.waitForStop(0);
71 } catch (InterruptedException e) {
72 e.printStackTrace();
73 System.exit(1);
74 }
75
76 }
77
78 protected void shutdown() {
79 // shutdown manager runtime
80 try {
81 InternalState.getMainRuntimeContext().close();
82 InternalState.getMainRuntimeContext().waitForStop(RUNTIME_SHUTDOWN_TIMEOUT);
83 // logger.log(Logger.Level.INFO, "Argeo Init stopped with PID " + ProcessHandle.current().pid());
84 System.out.flush();
85 } catch (Exception e) {
86 e.printStackTrace();
87 Runtime.getRuntime().halt(1);
88 }
89 }
90
91 public static void main(String[] args) {
92 ThinLoggerFinder.reloadConfiguration();
93 logger.log(Logger.Level.DEBUG, () -> "Argeo Init starting with PID " + ProcessHandle.current().pid());
94 Map<String, String> env = System.getenv();
95
96 // if (args.length < 1)
97 // throw new IllegalArgumentException("A relative configuration directory must be specified");
98 // Path configArea = Paths.get(System.getProperty("user.dir"), args[0]);
99
100 // System.out.println("## Start with PID " + ProcessHandle.current().pid());
101 // System.out.println("user.dir=" + System.getProperty("user.dir"));
102
103 Path writableArea = Paths.get(env.get(ENV_STATE_DIRECTORY));
104 Path configArea = Paths.get(env.get(ENV_CONFIGURATION_DIRECTORY));
105 RuntimeManagerMain runtimeManager = new RuntimeManagerMain(configArea, writableArea);
106 runtimeManager.run();
107 }
108
109 }