- public static void loadConfig(Path dir, Map<String, String> config) {
- try {
-// System.out.println("Load from " + dir);
- Path jvmArgsPath = dir.resolve(JVM_ARGS);
- if (!Files.exists(jvmArgsPath)) {
- // load from parent directory
- loadConfig(dir.getParent(), config);
- }
-
- if (Files.exists(dir))
- for (Path p : Files.newDirectoryStream(dir, "*.ini")) {
- Properties props = new Properties();
- try (InputStream in = Files.newInputStream(p)) {
- props.load(in);
- }
- for (Object key : props.keySet()) {
- config.put(key.toString(), props.getProperty(key.toString()));
- }
- }
- } catch (IOException e) {
- throw new UncheckedIOException("Cannot load configuration from " + dir, e);
- }
- }
-
- OsgiRuntimeContext loadRuntime(String relPath, Consumer<Map<String, String>> configCallback) {
- closeRuntime(relPath, false);
- Path stateArea = baseStateArea.resolve(relPath);
- Path configArea = baseConfigArea.resolve(relPath);
- Map<String, String> config = new HashMap<>();
- loadConfig(configArea, config);
- config.put(InitConstants.PROP_OSGI_CONFIGURATION_AREA, stateArea.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, config.get(InitConstants.PROP_OSGI_CONFIGURATION_AREA));
-
- OsgiRuntimeContext runtimeContext = new OsgiRuntimeContext(config);
- runtimeContexts.put(relPath, runtimeContext);
- return runtimeContext;
- }
-
- public void startRuntime(String relPath, Consumer<Map<String, String>> configCallback) {
- OsgiRuntimeContext runtimeContext = loadRuntime(relPath, configCallback);
- runtimeContext.run();
- Framework framework = runtimeContext.getFramework();
- 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);
- }
-
- }
-