X-Git-Url: http://git.argeo.org/?p=lgpl%2Fargeo-commons.git;a=blobdiff_plain;f=org.argeo.init%2Fsrc%2Forg%2Fargeo%2Fapi%2Finit%2FRuntimeManager.java;fp=org.argeo.init%2Fsrc%2Forg%2Fargeo%2Fapi%2Finit%2FRuntimeManager.java;h=cb8caeda9366c9c8d47c5ac19e29e49ae4bae6da;hp=0000000000000000000000000000000000000000;hb=b95462873703848193e56fcbe997693630db6121;hpb=55d88fba80cec198a0f11ba7545e19878c51fc5e diff --git a/org.argeo.init/src/org/argeo/api/init/RuntimeManager.java b/org.argeo.init/src/org/argeo/api/init/RuntimeManager.java new file mode 100644 index 000000000..cb8caeda9 --- /dev/null +++ b/org.argeo.init/src/org/argeo/api/init/RuntimeManager.java @@ -0,0 +1,72 @@ +package org.argeo.api.init; + +import java.io.IOException; +import java.io.InputStream; +import java.io.UncheckedIOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Map; +import java.util.Properties; +import java.util.function.Consumer; + +/** Dynamically manages multiple runtimes within a single JVM. */ +public interface RuntimeManager { + String JVM_ARGS = "jvm.args"; + String STATE = "state"; + String DATA = "data"; + + public void startRuntime(String relPath, Consumer> configCallback); + + public void closeRuntime(String relPath, boolean async); + + /** + * Load configs recursively starting with the parent directories, until a + * jvm.args file is found. + */ + static void loadConfig(Path dir, Map config) { + try { + Path jvmArgsPath = dir.resolve(RuntimeManager.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")) { + try (InputStream in = Files.newInputStream(p)) { + loadConfig(in, config); + } + } + } catch (IOException e) { + throw new UncheckedIOException("Cannot load configuration from " + dir, e); + } + } + + /** + * Load config from a {@link Properties} formatted stream. If a property value + * starts with a '+' character, itis expected that the last character is a + * separator and it will be prepended to the existing value. + */ + static void loadConfig(InputStream in, Map config) throws IOException { + Properties props = new Properties(); + props.load(in); + for (Object k : props.keySet()) { + String key = k.toString(); + String value = props.getProperty(key); + if (value.length() > 1 && '+' == value.charAt(0)) { + String currentValue = config.get(key); + if (currentValue == null || "".equals(currentValue)) { + // remove the + and the trailing separator + value = value.substring(1, value.length() - 1); + config.put(key, value); + } else { + // remove the + but keep the trailing separator + value = value.substring(1); + config.put(key, value + currentValue); + } + } else { + config.put(key, value); + } + } + } +}