How do I install signal handling logic iff sun.misc.Signal is available?
Background
First generation of my code, which assumed signal handling availability, looked something like this:
class MyApp {
public static void main(String[] args) {
...
Signal.handle(term_sig, new SignalHandler() {
public void handle(Signal sig) { ... }
});
...
}
}
I believe I understand how to reflectively test for and use signal handlers — Class.forName("sun.misc.Signal"), reflectively call Signal.handle, and so forth.
My impulse was simply to instantiate another anonymous inner class with the dynamically obtained SignalHandler class, but I think that’s just wishful syntax.
You need to use a Dynamic Proxy to implement the SignalHandler interface. The rest is just basic reflection.
Update
Here’s how you do it. Note, I’ve omitted the try-catch that needs to wrap all of this