]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.init/src/org/argeo/init/RuntimeManagerMain.java
Improve runtime manager
[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 stateArea, Path cacheArea) {
32 RuntimeManager.loadDefaults(configuration);
33
34 configuration.put(InitConstants.PROP_OSGI_SHARED_CONFIGURATION_AREA, configArea.toUri().toString());
35 configuration.put(InitConstants.PROP_OSGI_SHARED_CONFIGURATION_AREA_RO, "true");
36 // configuration.put(InitConstants.PROP_OSGI_USE_SYSTEM_PROPERTIES, "false");
37
38 configuration.put(InitConstants.PROP_OSGI_CONFIGURATION_AREA,
39 cacheArea.resolve(RuntimeManager.OSGI_STORAGE_DIRNAME).toUri().toString());
40 configuration.put(InitConstants.PROP_OSGI_INSTANCE_AREA,
41 stateArea.resolve(RuntimeManager.DATA).toUri().toString());
42
43 logger.log(Level.TRACE, () -> "Runtime manager configuration: " + configuration);
44 }
45
46 public void run() {
47 OsgiRuntimeContext managerRuntimeContext = new OsgiRuntimeContext(OsgiRuntimeContext.loadFrameworkFactory(),
48 configuration);
49 try {
50 managerRuntimeContext.run();
51 InternalState.setMainRuntimeContext(managerRuntimeContext);
52
53 // shutdown on exit
54 Runtime.getRuntime().addShutdownHook(new Thread(() -> shutdown(), "Runtime shutdown"));
55
56 // BundleContext bc = managerRuntimeContext.getFramework().getBundleContext();
57 // // uninstall init as a bundle since it will be available via OSGi system
58 // OsgiBoot.uninstallBundles(bc, SYMBOLIC_NAME_INIT);
59 // bc.registerService(RuntimeManager.class, this, new Hashtable<>(configuration));
60 logger.log(Level.DEBUG, "Registered runtime manager");
61
62 managerRuntimeContext.waitForStop(0);
63 } catch (InterruptedException e) {
64 e.printStackTrace();
65 System.exit(1);
66 }
67
68 }
69
70 protected void shutdown() {
71 // shutdown manager runtime
72 try {
73 InternalState.getMainRuntimeContext().close();
74 InternalState.getMainRuntimeContext().waitForStop(RUNTIME_SHUTDOWN_TIMEOUT);
75 // logger.log(Logger.Level.INFO, "Argeo Init stopped with PID " + ProcessHandle.current().pid());
76 System.out.flush();
77 } catch (Exception e) {
78 e.printStackTrace();
79 Runtime.getRuntime().halt(1);
80 }
81 }
82
83 public static void main(String[] args) {
84 ThinLoggerFinder.reloadConfiguration();
85 logger.log(Logger.Level.DEBUG, () -> "Argeo Init starting with PID " + ProcessHandle.current().pid());
86
87 Path writableArea = getLocalPath(InitConstants.PROP_ARGEO_STATE_AREA, ENV_STATE_DIRECTORY);
88 Path configArea =getLocalPath(InitConstants.PROP_ARGEO_CONFIG_AREA, ENV_CONFIGURATION_DIRECTORY);
89 Path cacheArea = getLocalPath(InitConstants.PROP_ARGEO_CACHE_AREA, ENV_CACHE_DIRECTORY);
90 RuntimeManagerMain runtimeManager = new RuntimeManagerMain(configArea, writableArea, cacheArea);
91 runtimeManager.run();
92 }
93
94 private static Path getLocalPath(String systemProperty, String environmentVariable) {
95 String prop = System.getProperty(systemProperty);
96 if (prop != null)
97 return Paths.get(prop);
98 String env = System.getenv().get(environmentVariable);
99 if (env != null)
100 return Paths.get(env);
101 throw new IllegalStateException("No local path set with system property " + systemProperty
102 + " or environment variable " + environmentVariable);
103 // TODO allocate a temporary directory? or defaults based on working directory ?
104 }
105 }