| 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 sun.misc.Signal; |
| 17 |
import sun.misc.SignalHandler; |
| 18 |
|
| 19 |
import java.util.HashMap; |
| 20 |
import java.util.concurrent.Callable; |
| 21 |
|
| 22 |
class SignalInterceptorHelper { |
| 23 |
|
| 24 |
static void register(String signame, Callable fn) { |
| 25 |
Signal sig = new Signal(signame); |
| 26 |
WrappedCallable newHandler = new WrappedCallable(fn); |
| 27 |
SignalHandler oldHandler = Signal.handle(sig, newHandler); |
| 28 |
newHandler.attachOld(oldHandler); |
| 29 |
} |
| 30 |
|
| 31 |
private static class WrappedCallable implements SignalHandler { |
| 32 |
private Callable fn; |
| 33 |
private SignalHandler oldHandler; |
| 34 |
|
| 35 |
public WrappedCallable(Callable fn) { |
| 36 |
this.fn = fn; |
| 37 |
this.oldHandler = null; |
| 38 |
} |
| 39 |
|
| 40 |
public void attachOld(SignalHandler oldHandler) { |
| 41 |
this.oldHandler = oldHandler; |
| 42 |
} |
| 43 |
|
| 44 |
public void handle(Signal sig) { |
| 45 |
boolean cont = true; |
| 46 |
try { |
| 47 |
cont = (Boolean) fn.call(); |
| 48 |
} |
| 49 |
catch (Exception e) {} |
| 50 |
finally { |
| 51 |
if (cont && (oldHandler != null)) { |
| 52 |
oldHandler.handle(sig); |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
} |