summaryrefslogtreecommitdiffstats
path: root/yolean
diff options
context:
space:
mode:
authorArne Juul <arnej@yahoo-inc.com>2016-06-21 14:00:06 +0200
committerArne Juul <arnej@yahoo-inc.com>2016-06-21 14:00:12 +0200
commit40b650e0d9a2e007ebe4f8fabdef14864d6c24c7 (patch)
treec99cbfb8eee3444dd40548853f5e4e3b6ba1cfa3 /yolean
parent5d76f522d0c9060beee044541faf98e00ed90c70 (diff)
copy utility from vespajlib
* this class will be used by jdisc_core but we want to avoid the classloader problems we got when jdisc_core used vespajlib directly, so copy it to this library that has fewer transitive dependencies.
Diffstat (limited to 'yolean')
-rw-r--r--yolean/src/main/java/com/yahoo/yolean/system/CatchSigTerm.java69
-rw-r--r--yolean/src/test/java/com/yahoo/yolean/system/CatchSigTermTestCase.java19
2 files changed, 88 insertions, 0 deletions
diff --git a/yolean/src/main/java/com/yahoo/yolean/system/CatchSigTerm.java b/yolean/src/main/java/com/yahoo/yolean/system/CatchSigTerm.java
new file mode 100644
index 00000000000..204dcaacf28
--- /dev/null
+++ b/yolean/src/main/java/com/yahoo/yolean/system/CatchSigTerm.java
@@ -0,0 +1,69 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.yolean.system;
+
+import java.lang.reflect.*;
+
+// import sun.misc.Signal;
+// import sun.misc.SignalHandler;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+
+public class CatchSigTerm {
+ /**
+ * Sets up a signal handler for SIGTERM, where a given AtomicBoolean
+ * gets a true value when the TERM signal is caught.
+ *
+ * Callers basically have two options for acting on the TERM signal:
+ *
+ * They may choose to synchronize and wait() on this variable,
+ * and they will be notified when it changes state to true. To avoid
+ * problems with spurious wakeups, use a while loop and wait()
+ * again if the state is still false. As soon as the caller has been
+ * woken up and the state is true, the application should exit as
+ * soon as possible.
+ *
+ * They may also choose to poll the state of this variable. As soon
+ * as its state becomes true, the signal has been received, and the
+ * application should exit as soon as possible.
+ *
+ * @param signalCaught set to false initially, will be set to true when SIGTERM is caught.
+ */
+ @SuppressWarnings("rawtypes")
+ public static void setup(final AtomicBoolean signalCaught) {
+ signalCaught.set(false);
+ try {
+ Class shc = Class.forName("sun.misc.SignalHandler");
+ Class ssc = Class.forName("sun.misc.Signal");
+
+ InvocationHandler ihandler = new InvocationHandler() {
+ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
+ synchronized (signalCaught) {
+ signalCaught.set(true);
+ signalCaught.notifyAll();
+ }
+ return null;
+ }
+ };
+ Object shandler = Proxy.newProxyInstance(CatchSigTerm.class.getClassLoader(),
+ new Class[] { shc },
+ ihandler);
+ Constructor[] c = ssc.getDeclaredConstructors();
+ assert c.length == 1;
+ Object sigterm = c[0].newInstance("TERM");
+ Method m = findMethod(ssc, "handle");
+ assert m != null; // "NoSuchMethodException"
+ m.invoke(null, sigterm, shandler);
+ } catch (ClassNotFoundException | InvocationTargetException | InstantiationException | IllegalAccessException e) {
+ System.err.println("FAILED setting up signal catching: "+e);
+ }
+ }
+
+ private static Method findMethod(Class<?> c, String name) {
+ for (Method m : c.getDeclaredMethods()) {
+ if (m.getName().equals(name)) {
+ return m;
+ }
+ }
+ return null;
+ }
+}
diff --git a/yolean/src/test/java/com/yahoo/yolean/system/CatchSigTermTestCase.java b/yolean/src/test/java/com/yahoo/yolean/system/CatchSigTermTestCase.java
new file mode 100644
index 00000000000..d2ad84948e1
--- /dev/null
+++ b/yolean/src/test/java/com/yahoo/yolean/system/CatchSigTermTestCase.java
@@ -0,0 +1,19 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.yolean.system;
+
+import com.yahoo.yolean.system.CatchSigTerm;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+/**
+ * @author arnej27959
+ */
+public class CatchSigTermTestCase extends junit.framework.TestCase {
+
+ public CatchSigTermTestCase(String name) {
+ super(name);
+ }
+
+ public void testThatSetupCompiles() {
+ CatchSigTerm.setup(new AtomicBoolean(false));
+ }
+}