public static Object registerDefault(String name) { try { Class<?> signalHandlerClass = Class.forName("sun.misc.SignalHandler"); return doRegister(name, signalHandlerClass.getField("SIG_DFL").get(null)); } catch (Exception e) { // Ignore this one too, if the above failed, the signal API is incompatible with what we're expecting Log.debug("Error registering default handler for signal ", name, e); return null; } }
public static void unregister(String name, Object previous) { try { // We should make sure the current signal is the one we registered if (previous != null) { doRegister(name, previous); } } catch (Exception e) { // Ignore Log.debug("Error unregistering handler for signal ", name, e); } }
public static Object register(String name, final Runnable handler, ClassLoader loader) { try { Class<?> signalHandlerClass = Class.forName("sun.misc.SignalHandler"); // Implement signal handler Object signalHandler = Proxy.newProxyInstance(loader, new Class<?>[]{signalHandlerClass}, (proxy, method, args) -> { // only method we are proxying is handle() if (method.getDeclaringClass() == Object.class) { if ("toString".equals(method.getName())) { return handler.toString(); } } else if (method.getDeclaringClass() == signalHandlerClass) { Log.trace(() -> "Calling handler " + toString(handler) + " for signal " + name); handler.run(); } return null; }); return doRegister(name, signalHandler); } catch (Exception e) { // Ignore this one too, if the above failed, the signal API is incompatible with what we're expecting Log.debug("Error registering handler for signal ", name, e); return null; } }