X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=org.argeo.init%2Fsrc%2Forg%2Fargeo%2Finit%2FService.java;h=c63fdcd376daffa6ac0492f78ae38bef28ff5fd9;hb=c692c089327ab02a19dc5dae90bb003cdf75956d;hp=9a01cffed555f88eaf081cd7bcb3d86245f61f68;hpb=b7d8618ce593bbeca7e311d32a4d98988e27f877;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.init/src/org/argeo/init/Service.java b/org.argeo.init/src/org/argeo/init/Service.java index 9a01cffed..c63fdcd37 100644 --- a/org.argeo.init/src/org/argeo/init/Service.java +++ b/org.argeo.init/src/org/argeo/init/Service.java @@ -1,59 +1,139 @@ package org.argeo.init; +import java.io.IOException; +import java.io.InputStream; import java.lang.System.Logger; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.HashMap; import java.util.Map; +import java.util.Objects; +import java.util.Properties; +import java.util.TreeMap; +import org.argeo.init.logging.ThinLoggerFinder; +import org.argeo.init.osgi.OsgiBoot; import org.argeo.init.osgi.OsgiRuntimeContext; -public class Service implements Runnable, AutoCloseable { - private final static Logger log = System.getLogger(Service.class.getName()); +/** Configure and launch an Argeo service. */ +public class Service { + private final static Logger logger = System.getLogger(Service.class.getName()); - private static RuntimeContext runtimeContext = null; + final static String FILE_SYSTEM_PROPERTIES = "system.properties"; - protected Service(String[] args) { - } + public final static String PROP_ARGEO_INIT_MAIN = "argeo.init.main"; - @Override - public void run() { - } + private static RuntimeContext runtimeContext = null; - @Override - public void close() throws Exception { + protected Service(String[] args) { } public static void main(String[] args) { - long pid = ProcessHandle.current().pid(); - log.log(Logger.Level.DEBUG, "Starting with PID " + pid); + final long pid = ProcessHandle.current().pid(); + logger.log(Logger.Level.DEBUG, () -> "Argeo Init starting with PID " + pid); // shutdown on exit Runtime.getRuntime().addShutdownHook(new Thread(() -> { try { - if (Service.runtimeContext != null) + if (Service.runtimeContext != null) { +// System.out.println("Argeo Init stopping with PID " + pid); Service.runtimeContext.close(); + Service.runtimeContext.waitForStop(0); + } } catch (Exception e) { e.printStackTrace(); - System.exit(1); + Runtime.getRuntime().halt(1); } }, "Runtime shutdown")); + // TODO use args as well + String dataArea = System.getProperty(OsgiBoot.PROP_OSGI_INSTANCE_AREA); + String stateArea = System.getProperty(OsgiBoot.PROP_OSGI_CONFIGURATION_AREA); + String configArea = System.getProperty(OsgiBoot.PROP_OSGI_SHARED_CONFIGURATION_AREA); + + if (configArea != null) { + Path configAreaPath = Paths.get(configArea); + Path additionalSystemPropertiesPath = configAreaPath.resolve(FILE_SYSTEM_PROPERTIES); + if (Files.exists(additionalSystemPropertiesPath)) { + Properties properties = new Properties(); + try (InputStream in = Files.newInputStream(additionalSystemPropertiesPath)) { + properties.load(in); + } catch (IOException e) { + logger.log(Logger.Level.ERROR, + "Cannot load additional system properties " + additionalSystemPropertiesPath, e); + } + + for (Object key : properties.keySet()) { + String currentValue = System.getProperty(key.toString()); + String value = properties.getProperty(key.toString()); + if (currentValue != null) { + if (!Objects.equals(value, currentValue)) + logger.log(Logger.Level.WARNING, "System property " + key + " already set with value " + + currentValue + " instead of " + value + ". Ignoring new value."); + } else { + System.setProperty(key.toString(), value); + logger.log(Logger.Level.TRACE, () -> "Added " + key + "=" + value + + " to system properties, from " + additionalSystemPropertiesPath.getFileName()); + } + } + ThinLoggerFinder.reloadConfiguration(); + } + } + Map config = new HashMap<>(); + config.put(PROP_ARGEO_INIT_MAIN, "true"); + + // add OSGi system properties to the configuration + sysprops: for (Object key : new TreeMap<>(System.getProperties()).keySet()) { + String keyStr = key.toString(); + switch (keyStr) { + case OsgiBoot.PROP_OSGI_CONFIGURATION_AREA: + case OsgiBoot.PROP_OSGI_SHARED_CONFIGURATION_AREA: + case OsgiBoot.PROP_OSGI_INSTANCE_AREA: + // we should already have dealt with those + continue sysprops; + default: + } + + if (keyStr.startsWith("osgi.") || keyStr.startsWith("org.osgi.") || keyStr.startsWith("eclipse.") + || keyStr.startsWith("org.eclipse.equinox.") || keyStr.startsWith("felix.")) { + String value = System.getProperty(keyStr); + config.put(keyStr, value); + logger.log(Logger.Level.TRACE, + () -> "Added " + key + "=" + value + " to configuration, from system properties"); + } + } + try { - try (OsgiRuntimeContext osgiRuntimeContext = new OsgiRuntimeContext(config)) { + try { + if (stateArea != null) + config.put(OsgiBoot.PROP_OSGI_CONFIGURATION_AREA, stateArea); + if (configArea != null) + config.put(OsgiBoot.PROP_OSGI_SHARED_CONFIGURATION_AREA, configArea); + if (dataArea != null) + config.put(OsgiBoot.PROP_OSGI_INSTANCE_AREA, dataArea); + // config.put(OsgiBoot.PROP_OSGI_USE_SYSTEM_PROPERTIES, "true"); + + OsgiRuntimeContext osgiRuntimeContext = new OsgiRuntimeContext(config); osgiRuntimeContext.run(); Service.runtimeContext = osgiRuntimeContext; Service.runtimeContext.waitForStop(0); } catch (NoClassDefFoundError e) { - try (StaticRuntimeContext staticRuntimeContext = new StaticRuntimeContext(config)) { - staticRuntimeContext.run(); - Service.runtimeContext = staticRuntimeContext; - Service.runtimeContext.waitForStop(0); - } + StaticRuntimeContext staticRuntimeContext = new StaticRuntimeContext((Map) config); + staticRuntimeContext.run(); + Service.runtimeContext = staticRuntimeContext; + Service.runtimeContext.waitForStop(0); } } catch (Exception e) { e.printStackTrace(); System.exit(1); } + logger.log(Logger.Level.DEBUG, "Argeo Init stopped with PID " + pid); } + /** The root runtime context in this JVM. */ + public static RuntimeContext getRuntimeContext() { + return runtimeContext; + } }