Debian Codegraph @

src/com/hypirion/io/SignalInterceptor.java

raw
1 /*
2 * Copyright (c) 2013 Jean Niklas L'orange. All rights reserved.
3 *
4 * The use and distribution terms for this software are covered by the
5 * Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
6 * which can be found in the file LICENSE at the root of this distribution.
7 *
8 * By using this software in any fashion, you are agreeing to be bound by
9 * the terms of this license.
10 *
11 * You must not remove this notice, or any other, from this software.
12 */
13
14 package com.hypirion.io;
15
16 import java.util.concurrent.Callable;
17
18 /**
19 * The SignalInterceptor class is a static collection of methods used to
20 * intercept POSIX signals before they are sent to their original signal
21 * handlers. Signal interceptors may be chained: If one attach the handler
22 * <code>A</code> to signal <code>X</code>, then attach the handler
23 * <code>B</code> to <code>X</code> as well, then all raised signals of type
24 * <code>X</code> will first call <code>B</code>, then <code>A</code>, then the
25 * original handler.
26 * <p>
27 * Signal handlers are {@link java.util.concurrent.Callable}s, which return
28 * booleans. If the signal handler returns <code>true</code>, the call chain
29 * will continue. If the signal handler returns <code>false</code> or throws an
30 * Exception, the call chain will stop (and no Exception will be thrown). If the
31 * signal handler throws an Error, the Error will not be caught.
32 *
33 * @author Jean Niklas L'orange
34 * @since <code>com.hypirion.io 0.3.0</code>
35 */
36
37 public class SignalInterceptor {
38
39 /**
40 * Registers the {@link java.util.concurrent.Callable} object to the POSIX
41 * signal <code>signame</code>. The POSIX signal must be in upper case,
42 * with the beginning <code>SIG</code> part omitted.
43 * <p>
44 * E.g. an interception to <code>SIGINT</code> be converted to the symbol
45 * <code>"INT"</code>.
46 *
47 * @param signame the POSIX signal name with <code>SIG</code> omitted.
48 * @param fn the <code>Callable</code> to call. The <code>Callable</code>
49 * should return either <code>true</code> or <code>false</code>.
50 * @exception SignalInterceptorException if there is no signal with the name
51 * <code>signame</code>, or if the JVM doesn't implement the most common JVM
52 * signal handling facilities.
53 */
54 public static void register(String signame, Callable fn)
55 throws SignalInterceptorException {
56 try {
57 SignalInterceptorHelper.register(signame, fn);
58 } catch (Throwable e) { // Catching Linkage errors etc. here
59 throw new SignalInterceptorException(signame, e);
60 }
61 }
62
63 /**
64 * Registers the {@link java.util.concurrent.Callable} object to the POSIX
65 * signal <code>signame</code>. If successful, will return true. If the
66 * registering fails, returns false. The POSIX signal must be in upper case,
67 * with the beginning <code>SIG</code> part omitted.
68 * <p>
69 * E.g. an interception to <code>SIGINT</code> be converted to the symbol
70 * <code>"INT"</code>.
71 *
72 * @param signame the POSIX signal name with <code>SIG</code> omitted.
73 * @param fn the <code>Callable</code> to call. The <code>Callable</code>
74 * should return either <code>true</code> or <code>false</code>.
75 */
76 public static boolean tryRegister(String signame, Callable fn) {
77 try {
78 register(signame, fn);
79 return true;
80 } catch (SignalInterceptorException sie) {
81 return false;
82 }
83 }
84 }