diff options
author | gjoranv <gv@verizonmedia.com> | 2019-10-03 13:28:45 +0200 |
---|---|---|
committer | gjoranv <gv@verizonmedia.com> | 2019-10-03 13:28:45 +0200 |
commit | 399e86e2374327f6cee7e6ee4cc5eeae7181dfd5 (patch) | |
tree | 8bd4d643835246b7eb0caec6f3323c7203e603b7 /jdisc_core/src | |
parent | adf22d3886ccd6de163278434a1a6d502584d0f9 (diff) |
Remove unused BootstrapDaemon
Diffstat (limited to 'jdisc_core/src')
-rw-r--r-- | jdisc_core/src/main/java/com/yahoo/jdisc/core/BootstrapDaemon.java | 137 | ||||
-rw-r--r-- | jdisc_core/src/test/java/com/yahoo/jdisc/core/BootstrapDaemonTestCase.java | 154 |
2 files changed, 0 insertions, 291 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 deleted file mode 100644 index de6d5c5073f..00000000000 --- a/jdisc_core/src/main/java/com/yahoo/jdisc/core/BootstrapDaemon.java +++ /dev/null @@ -1,137 +0,0 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.jdisc.core; - -import com.yahoo.protect.Process; -import org.apache.commons.daemon.Daemon; -import org.apache.commons.daemon.DaemonContext; - -import java.util.Arrays; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * @author Simon Thoresen Hult - */ -public class BootstrapDaemon implements Daemon { - - private static final Logger log = Logger.getLogger(BootstrapDaemon.class.getName()); - private final BootstrapLoader loader; - private final boolean privileged; - private String bundleLocation; - - static { - // force load slf4j to avoid other logging frameworks from initializing before it - org.slf4j.LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME); - } - - public BootstrapDaemon() { - this(new ApplicationLoader(Main.newOsgiFramework(), Main.newConfigModule()), - Boolean.valueOf(System.getProperty("jdisc.privileged"))); - } - - BootstrapDaemon(BootstrapLoader loader, boolean privileged) { - this.loader = loader; - this.privileged = privileged; - } - - BootstrapLoader loader() { - return loader; - } - - private static class WatchDog implements Runnable { - final String name; - final CountDownLatch complete; - final long timeout; - final TimeUnit timeUnit; - WatchDog(String name, CountDownLatch complete, long timeout, TimeUnit timeUnit) { - this.name = name; - this.complete = complete; - this.timeout = timeout; - this.timeUnit = timeUnit; - } - @Override - public void run() { - boolean dumpStack; - try { - dumpStack = !complete.await(timeout, timeUnit); - } catch (InterruptedException e) { - return; - } - if (dumpStack) { - log.warning("The watchdog for BootstrapDaemon." + name + " detected that it had not completed in " - + timeUnit.toMillis(timeout) + "ms. Dumping stack."); - Process.dumpThreads(); - } - } - } - private interface MyRunnable { - void run() throws Exception; - } - private void startWithWatchDog(String name, long timeout, TimeUnit timeUnit, MyRunnable task) throws Exception { - CountDownLatch complete = new CountDownLatch(1); - Thread thread = new Thread(new WatchDog(name, complete, timeout, timeUnit), name); - thread.setDaemon(true); - thread.start(); - try { - task.run(); - } catch (Exception e) { - log.log(Level.WARNING, "Exception caught during BootstrapDaemon." + name, e); - throw e; - } catch (Error e) { - log.log(Level.WARNING, "Error caught during BootstrapDaemon." + name, e); - throw e; - } catch (Throwable thrown) { - log.log(Level.WARNING, "Throwable caught during BootstrapDaemon." + name, thrown); - } finally { - complete.countDown(); - thread.join(); - } - } - - @Override - public void init(DaemonContext context) throws Exception { - String[] args = context.getArguments(); - if (args == null || args.length != 1 || args[0] == null) { - throw new IllegalArgumentException("Expected 1 argument, got " + Arrays.toString(args) + "."); - } - bundleLocation = args[0]; - if (privileged) { - log.finer("Initializing application with privileges."); - startWithWatchDog("init", 60, TimeUnit.SECONDS, () -> loader.init(bundleLocation, true)); - } - } - - @Override - public void start() throws Exception { - try { - if (!privileged) { - log.finer("Initializing application without privileges."); - startWithWatchDog("init", 60, TimeUnit.SECONDS, () -> loader.init(bundleLocation, false)); - } - startWithWatchDog("start", 60, TimeUnit.SECONDS, () -> loader.start()); - } catch (Exception e) { - try { - log.log(Level.SEVERE, "Failed starting container", e); - } - finally { - Runtime.getRuntime().halt(1); - } - } - } - - @Override - public void stop() throws Exception { - startWithWatchDog("stop", 60, TimeUnit.SECONDS, () -> loader.stop()); - } - - @Override - public void destroy() { - try { - startWithWatchDog("destroy", 60, TimeUnit.SECONDS, () -> loader.destroy()); - } catch (Exception e) { - } - } - -} diff --git a/jdisc_core/src/test/java/com/yahoo/jdisc/core/BootstrapDaemonTestCase.java b/jdisc_core/src/test/java/com/yahoo/jdisc/core/BootstrapDaemonTestCase.java deleted file mode 100644 index df8223a6d86..00000000000 --- a/jdisc_core/src/test/java/com/yahoo/jdisc/core/BootstrapDaemonTestCase.java +++ /dev/null @@ -1,154 +0,0 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.jdisc.core; - -import org.apache.commons.daemon.DaemonContext; -import org.apache.commons.daemon.DaemonController; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -/** - * @author Simon Thoresen Hult - */ -public class BootstrapDaemonTestCase { - - @Test - public void requireThatPrivilegedLifecycleWorks() throws Exception { - MyLoader loader = new MyLoader(); - BootstrapDaemon daemon = new BootstrapDaemon(loader, true); - daemon.init(new MyContext("foo")); - assertTrue(loader.hasState(true, false, false, false)); - assertTrue(loader.privileged); - daemon.start(); - assertTrue(loader.hasState(true, true, false, false)); - daemon.stop(); - assertTrue(loader.hasState(true, true, true, false)); - daemon.destroy(); - assertTrue(loader.hasState(true, true, true, true)); - } - - @Test - public void requireThatNonPrivilegedLifecycleWorks() throws Exception { - MyLoader loader = new MyLoader(); - BootstrapDaemon daemon = new BootstrapDaemon(loader, false); - daemon.init(new MyContext("foo")); - assertTrue(loader.hasState(false, false, false, false)); - daemon.start(); - assertTrue(loader.hasState(true, true, false, false)); - assertFalse(loader.privileged); - daemon.stop(); - assertTrue(loader.hasState(true, true, true, false)); - daemon.destroy(); - assertTrue(loader.hasState(true, true, true, true)); - } - - @Test - public void requireThatBundleLocationIsRequired() throws Exception { - MyLoader loader = new MyLoader(); - BootstrapDaemon daemon = new BootstrapDaemon(loader, true); - try { - daemon.init(new MyContext((String[])null)); - fail(); - } catch (IllegalArgumentException e) { - assertNull(loader.bundleLocation); - } - try { - daemon.init(new MyContext()); - fail(); - } catch (IllegalArgumentException e) { - assertNull(loader.bundleLocation); - } - try { - daemon.init(new MyContext((String)null)); - fail(); - } catch (IllegalArgumentException e) { - assertNull(loader.bundleLocation); - } - try { - daemon.init(new MyContext("foo", "bar")); - fail(); - } catch (IllegalArgumentException e) { - assertNull(loader.bundleLocation); - } - - daemon.init(new MyContext("foo")); - daemon.start(); - - assertNotNull(loader.bundleLocation); - assertEquals("foo", loader.bundleLocation); - - daemon.stop(); - daemon.destroy(); - } - - @Test - public void requireThatEnvironmentIsRequired() { - try { - new BootstrapDaemon(); - fail(); - } catch (IllegalStateException e) { - - } - } - - private static class MyLoader implements BootstrapLoader { - - String bundleLocation = null; - boolean privileged = false; - boolean initCalled = false; - boolean startCalled = false; - boolean stopCalled = false; - boolean destroyCalled = false; - - boolean hasState(boolean initCalled, boolean startCalled, boolean stopCalled, boolean destroyCalled) { - return this.initCalled == initCalled && this.startCalled == startCalled && - this.stopCalled == stopCalled && this.destroyCalled == destroyCalled; - } - - @Override - public void init(String bundleLocation, boolean privileged) throws Exception { - this.bundleLocation = bundleLocation; - this.privileged = privileged; - initCalled = true; - } - - @Override - public void start() throws Exception { - startCalled = true; - } - - @Override - public void stop() throws Exception { - stopCalled = true; - } - - @Override - public void destroy() { - destroyCalled = true; - } - } - - private static class MyContext implements DaemonContext { - - final String[] args; - - MyContext(String... args) { - this.args = args; - } - - @Override - public DaemonController getController() { - return null; - } - - @Override - public String[] getArguments() { - return args; - } - } -} |