1 package org
.argeo
.init
;
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
;
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
;
17 * Dynamically configures and launches multiple runtimes, coordinated by a main
20 public class RuntimeManagerMain
{
21 private final static Logger logger
= System
.getLogger(RuntimeManagerMain
.class.getName());
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";
27 private final static long RUNTIME_SHUTDOWN_TIMEOUT
= 60 * 1000;
29 private Map
<String
, String
> configuration
= new HashMap
<>();
31 RuntimeManagerMain(Path configArea
, Path stateArea
) {
32 RuntimeManager
.loadConfig(configArea
, configuration
);
34 // integration with OSGi runtime; this will be read by the init bundle
35 configuration
.put(ServiceMain
.PROP_ARGEO_INIT_MAIN
, "true");
36 configuration
.put(InitConstants
.PROP_OSGI_SHARED_CONFIGURATION_AREA
, configArea
.toUri().toString());
38 configuration
.put(InitConstants
.PROP_OSGI_CONFIGURATION_AREA
,
39 stateArea
.resolve(RuntimeManager
.STATE
).toUri().toString());
40 // use config area if instance area is not set
41 if (!configuration
.containsKey(InitConstants
.PROP_OSGI_INSTANCE_AREA
))
42 configuration
.put(InitConstants
.PROP_OSGI_INSTANCE_AREA
,
43 stateArea
.resolve(RuntimeManager
.DATA
).toUri().toString());
45 logger
.log(Level
.TRACE
, () -> "Runtime manager configuration: " + configuration
);
47 // System.out.println("java.library.path=" + System.getProperty("java.library.path"));
51 OsgiRuntimeContext managerRuntimeContext
= new OsgiRuntimeContext(OsgiRuntimeContext
.loadFrameworkFactory(),
54 managerRuntimeContext
.run();
55 InternalState
.setMainRuntimeContext(managerRuntimeContext
);
58 Runtime
.getRuntime().addShutdownHook(new Thread(() -> shutdown(), "Runtime shutdown"));
60 // BundleContext bc = managerRuntimeContext.getFramework().getBundleContext();
61 // // uninstall init as a bundle since it will be available via OSGi system
62 // OsgiBoot.uninstallBundles(bc, SYMBOLIC_NAME_INIT);
63 // bc.registerService(RuntimeManager.class, this, new Hashtable<>(configuration));
64 logger
.log(Level
.DEBUG
, "Registered runtime manager");
66 managerRuntimeContext
.waitForStop(0);
67 } catch (InterruptedException e
) {
74 protected void shutdown() {
75 // shutdown manager runtime
77 InternalState
.getMainRuntimeContext().close();
78 InternalState
.getMainRuntimeContext().waitForStop(RUNTIME_SHUTDOWN_TIMEOUT
);
79 // logger.log(Logger.Level.INFO, "Argeo Init stopped with PID " + ProcessHandle.current().pid());
81 } catch (Exception e
) {
83 Runtime
.getRuntime().halt(1);
87 public static void main(String
[] args
) {
88 ThinLoggerFinder
.reloadConfiguration();
89 logger
.log(Logger
.Level
.DEBUG
, () -> "Argeo Init starting with PID " + ProcessHandle
.current().pid());
90 Map
<String
, String
> env
= System
.getenv();
91 // for (String envName : new TreeSet<>(env.keySet())) {
92 // System.out.format("%s=%s%n", envName, env.get(envName));
95 throw new IllegalArgumentException("A relative configuration directory must be specified");
96 Path configArea
= Paths
.get(System
.getProperty("user.dir"), args
[0]);
98 // System.out.println("## Start with PID " + ProcessHandle.current().pid());
99 // System.out.println("user.dir=" + System.getProperty("user.dir"));
101 Path stateArea
= Paths
.get(env
.get(ENV_STATE_DIRECTORY
));
103 RuntimeManagerMain runtimeManager
= new RuntimeManagerMain(configArea
, stateArea
);
104 runtimeManager
.run();