diff options
author | Arne Juul <arnej@yahoo-inc.com> | 2016-06-16 16:22:46 +0200 |
---|---|---|
committer | Arne Juul <arnej@yahoo-inc.com> | 2016-06-16 16:22:46 +0200 |
commit | 480d2e9bb5bd52cf08b3ad28e2dce5862b45e8d6 (patch) | |
tree | d4e817c24da77509b57c2e23ea40622a976da360 | |
parent | 88b8aa1f8ddb13994f3f1e356d2687e346909b11 (diff) |
add main() for running without jsvc.
* add StandaloneMain class
* move common code to new Main class
3 files changed, 117 insertions, 34 deletions
diff --git a/jdisc_core/src/main/java/com/yahoo/jdisc/core/BootstrapDaemon.java b/jdisc_core/src/main/java/com/yahoo/jdisc/core/BootstrapDaemon.java index 21c52d6047d..cc87abdd292 100644 --- a/jdisc_core/src/main/java/com/yahoo/jdisc/core/BootstrapDaemon.java +++ b/jdisc_core/src/main/java/com/yahoo/jdisc/core/BootstrapDaemon.java @@ -1,15 +1,10 @@ // Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.jdisc.core; -import com.google.inject.Module; -import com.yahoo.jdisc.application.ContainerBuilder; -import com.yahoo.jdisc.application.OsgiFramework; import org.apache.commons.daemon.Daemon; import org.apache.commons.daemon.DaemonContext; -import java.io.IOException; import java.util.Arrays; -import java.util.Collections; import java.util.logging.Logger; /** @@ -28,7 +23,7 @@ public class BootstrapDaemon implements Daemon { } public BootstrapDaemon() { - this(new ApplicationLoader(newOsgiFramework(), newConfigModule()), + this(new ApplicationLoader(Main.newOsgiFramework(), Main.newConfigModule()), Boolean.valueOf(System.getProperty("jdisc.privileged"))); } @@ -73,32 +68,4 @@ public class BootstrapDaemon implements Daemon { loader.destroy(); } - private static OsgiFramework newOsgiFramework() { - String cachePath = System.getProperty("jdisc.cache.path"); - if (cachePath == null) { - throw new IllegalStateException("System property 'jdisc.cache.path' not set."); - } - FelixParams params = new FelixParams() - .setCachePath(cachePath) - .setLoggerEnabled(Boolean.valueOf(System.getProperty("jdisc.logger.enabled", "true"))); - for (String str : ContainerBuilder.safeStringSplit(System.getProperty("jdisc.export.packages"), ",")) { - params.exportPackage(str); - } - return new FelixFramework(params); - } - - private static Iterable<Module> newConfigModule() { - String configFile = System.getProperty("jdisc.config.file"); - if (configFile == null) { - return Collections.emptyList(); - } - Module configModule; - try { - configModule = ApplicationConfigModule.newInstanceFromFile(configFile); - } catch (IOException e) { - throw new IllegalStateException("Exception thrown while reading config file '" + configFile + "'.", e); - } - return Arrays.asList(configModule); - } - } diff --git a/jdisc_core/src/main/java/com/yahoo/jdisc/core/Main.java b/jdisc_core/src/main/java/com/yahoo/jdisc/core/Main.java new file mode 100644 index 00000000000..1fd5cad04db --- /dev/null +++ b/jdisc_core/src/main/java/com/yahoo/jdisc/core/Main.java @@ -0,0 +1,45 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.jdisc.core; + +import com.google.inject.Module; +import com.yahoo.jdisc.application.ContainerBuilder; +import com.yahoo.jdisc.application.OsgiFramework; + +import java.io.IOException; +import java.util.Arrays; +import java.util.Collections; + +/** + * @author simon + */ +public class Main { + + static OsgiFramework newOsgiFramework() { + String cachePath = System.getProperty("jdisc.cache.path"); + if (cachePath == null) { + throw new IllegalStateException("System property 'jdisc.cache.path' not set."); + } + FelixParams params = new FelixParams() + .setCachePath(cachePath) + .setLoggerEnabled(Boolean.valueOf(System.getProperty("jdisc.logger.enabled", "true"))); + for (String str : ContainerBuilder.safeStringSplit(System.getProperty("jdisc.export.packages"), ",")) { + params.exportPackage(str); + } + return new FelixFramework(params); + } + + static Iterable<Module> newConfigModule() { + String configFile = System.getProperty("jdisc.config.file"); + if (configFile == null) { + return Collections.emptyList(); + } + Module configModule; + try { + configModule = ApplicationConfigModule.newInstanceFromFile(configFile); + } catch (IOException e) { + throw new IllegalStateException("Exception thrown while reading config file '" + configFile + "'.", e); + } + return Arrays.asList(configModule); + } + +} diff --git a/jdisc_core/src/main/java/com/yahoo/jdisc/core/StandaloneMain.java b/jdisc_core/src/main/java/com/yahoo/jdisc/core/StandaloneMain.java new file mode 100644 index 00000000000..552d423f8cb --- /dev/null +++ b/jdisc_core/src/main/java/com/yahoo/jdisc/core/StandaloneMain.java @@ -0,0 +1,71 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.jdisc.core; + +import com.yahoo.system.CatchSigTerm; + +import java.util.Arrays; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.logging.Level; +import java.util.logging.Logger; + + +/** + * @author arnej27959 + */ +public class StandaloneMain { + + private static final Logger log = Logger.getLogger(StandaloneMain.class.getName()); + private final BootstrapLoader loader; + + static { + // force load slf4j to avoid other logging frameworks from initializing before it + org.slf4j.LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME); + } + + public StandaloneMain() { + this.loader = new ApplicationLoader(Main.newOsgiFramework(), Main.newConfigModule()); + } + + public static void main(String [] args) { + if (args == null || args.length != 1 || args[0] == null) { + throw new IllegalArgumentException("Expected 1 argument, got " + Arrays.toString(args) + "."); + } + StandaloneMain me = new StandaloneMain(); + me.run(args[0]); + } + + void run(String bundleLocation) { + try { + log.info("Initializing application without privileges."); + loader.init(bundleLocation, false); + loader.start(); + setupSigTermHandler(); + waitForShutdown(); + // Event.stopping(APPNAME, "shutdown"); + loader.stop(); + // Event.stopped(APPNAME, 0, 0); + loader.destroy(); + // System.exit(0); + } catch (Exception e) { + log.log(Level.WARNING, "Unexpected: ", e); + System.exit(6); + } + } + + private final AtomicBoolean signalCaught = new AtomicBoolean(false); + private void setupSigTermHandler() { + CatchSigTerm.setup(signalCaught); // catch termination signal + } + private void waitForShutdown() { + synchronized (signalCaught) { + while (!signalCaught.get()) { + try { + signalCaught.wait(); + } catch (InterruptedException e) { + // empty + } + } + } + } + +} |