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
, Path cacheArea
) {
32 RuntimeManager
.loadDefaults(configuration
);
34 configuration
.put(InitConstants
.PROP_OSGI_USE_SYSTEM_PROPERTIES
, "false");
36 configuration
.put(InitConstants
.PROP_ARGEO_CONFIG_AREA
, configArea
.toString());
37 configuration
.put(InitConstants
.PROP_ARGEO_STATE_AREA
, stateArea
.toString());
38 configuration
.put(InitConstants
.PROP_ARGEO_CACHE_AREA
, cacheArea
.toString());
40 configuration
.put(InitConstants
.PROP_OSGI_SHARED_CONFIGURATION_AREA
, configArea
.toUri().toString());
41 configuration
.put(InitConstants
.PROP_OSGI_SHARED_CONFIGURATION_AREA_RO
, "true");
43 configuration
.put(InitConstants
.PROP_OSGI_CONFIGURATION_AREA
,
44 cacheArea
.resolve(RuntimeManager
.OSGI_STORAGE_DIRNAME
).toUri().toString());
45 configuration
.put(InitConstants
.PROP_OSGI_INSTANCE_AREA
,
46 stateArea
.resolve(RuntimeManager
.DATA
).toUri().toString());
48 // TODO find a cleaner way to configure Jackrabbit indexes
49 configuration
.put("argeo.node.repo.indexesBase", cacheArea
.resolve("indexes").toString());
51 logger
.log(Level
.TRACE
, () -> "Runtime manager configuration: " + configuration
);
55 OsgiRuntimeContext managerRuntimeContext
= new OsgiRuntimeContext(OsgiRuntimeContext
.loadFrameworkFactory(),
58 managerRuntimeContext
.run();
59 InternalState
.setMainRuntimeContext(managerRuntimeContext
);
62 Runtime
.getRuntime().addShutdownHook(new Thread(() -> shutdown(), "Runtime shutdown"));
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");
70 managerRuntimeContext
.waitForStop(0);
71 } catch (InterruptedException e
) {
78 protected void shutdown() {
79 // shutdown manager runtime
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());
85 } catch (Exception e
) {
87 Runtime
.getRuntime().halt(1);
91 public static void main(String
[] args
) {
92 ThinLoggerFinder
.reloadConfiguration();
93 logger
.log(Logger
.Level
.DEBUG
, () -> "Argeo Init starting with PID " + ProcessHandle
.current().pid());
95 Path writableArea
= getLocalPath(InitConstants
.PROP_ARGEO_STATE_AREA
, ENV_STATE_DIRECTORY
, null);
96 Path configArea
= getLocalPath(InitConstants
.PROP_ARGEO_CONFIG_AREA
, ENV_CONFIGURATION_DIRECTORY
, null);
97 Path cacheArea
= getLocalPath(InitConstants
.PROP_ARGEO_CACHE_AREA
, ENV_CACHE_DIRECTORY
, writableArea
);
98 RuntimeManagerMain runtimeManager
= new RuntimeManagerMain(configArea
, writableArea
, cacheArea
);
102 private static Path
getLocalPath(String systemProperty
, String environmentVariable
, Path defaultPath
) {
103 String prop
= System
.getProperty(systemProperty
);
105 return Paths
.get(prop
);
106 String env
= System
.getenv().get(environmentVariable
);
108 return Paths
.get(env
);
109 if (defaultPath
!= null)
111 throw new IllegalStateException("No local path set with system property " + systemProperty
112 + " or environment variable " + environmentVariable
);
113 // TODO allocate a temporary directory? or defaults based on working directory ?