Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
BEGIN LICENSE BLOCK ***** Version: EPL 1.0/GPL 2.0/LGPL 2.1 The contents of this file are subject to the Eclipse Public License Version 1.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. Copyright (C) 2007 Ola Bini <> Alternatively, the contents of this file may be used under the terms of either of the GNU General Public License Version 2 or later (the "GPL"), or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which case the provisions of the GPL or the LGPL are applicable instead of those above. If you wish to allow use of your version of this file only under the terms of either the GPL or the LGPL, and not to allow others to use your version of this file under the terms of the EPL, indicate your decision by deleting the provisions above and replace them with the notice and other provisions required by the GPL or the LGPL. If you do not delete the provisions above, a recipient may use your version of this file under the terms of any one of the EPL, the GPL or the LGPL. END LICENSE BLOCK ***
 package org.jruby.util;
 import org.jruby.Ruby;
 import java.util.Map;

Ola Bini
 public class SunSignalFacade implements SignalFacade {
Remembers the original signal handlers before JRuby started messing around with them, to emulate Signal.trap(...,"DEFAULT") that's supposed to restore the platform default handler.
     private final Map<Signal,SignalHandleroriginal = new HashMap<SignalSignalHandler>();
     private final static class JRubySignalHandler implements SignalHandler {
         private final Ruby runtime;
         private final IRubyObject block;
         private final String signal;
         private final BlockCallback blockCallback;
         public JRubySignalHandler(Ruby runtimeIRubyObject blockString signal) {
         public JRubySignalHandler(Ruby runtimeBlockCallback callbackString signal) {
         private JRubySignalHandler(Ruby runtimeIRubyObject blockBlockCallback callbackString signal) {
             this. = runtime;
             this. = block;
             this. = callback;
             this. = signal;
         public void handle(Signal signal) {
             ThreadContext context = .getCurrentContext();
             try {
                 if ( != null) {
                 } else {
                     .call(contextnew IRubyObject[0], .);
             } catch(RaiseException e) {
                 try {
                 } catch(Exception ignored) {}
             } catch (MainExitException mee) {
             } finally {
                 Signal.handle(new Signal(this.), this);
    public IRubyObject trap(final IRubyObject recvIRubyObject blkIRubyObject sig) {
        return trap(recv.getRuntime(), new JRubySignalHandler(recv.getRuntime(), blksig.toString()));
    public IRubyObject trap(final Ruby runtimeBlockCallback blkString sig) {
        return trap(runtimenew JRubySignalHandler(runtimeblksig));
    private IRubyObject trap(final Ruby runtimefinal JRubySignalHandler handler) {
        return trap(runtime,handler.signal,handler);
        SignalHandler handler;
        synchronized () {
            handler = .get(new Signal(sig.toString()));
        if (handler!=null)
            return trap(recv.getRuntime(),sig.toString(),handler);
        else {
            // JRuby hasn't touched this signal handler, so it should be the platform default already
            return recv.getRuntime().getNil();
    public IRubyObject restoreOSDefault(IRubyObject recvIRubyObject sig) {
        return trap(recv.getRuntime(),sig.toString(),.);
    public IRubyObject ignore(IRubyObject recvIRubyObject sig) {
        return trap(recv.getRuntime(),sig.toString(),.);
    private IRubyObject trap(final Ruby runtimefinal String signalNamefinal SignalHandler handler) {
        final SignalHandler oldHandler;
        final Signal signal;
        try {
            signal = new Signal(signalName);
        } catch (Throwable e) {
            return runtime.getNil();
        try {
            oldHandler = Signal.handle(signalhandler);
        } catch (Exception e) {
            throw runtime.newArgumentError(e.getMessage());
        synchronized () {
            if (!.containsKey(signal))
        BlockCallback callback = null;
        if (oldHandler instanceof JRubySignalHandler) {
            JRubySignalHandler jsHandler = (JRubySignalHandleroldHandler;
            if (jsHandler.blockCallback != null) {
                callback = jsHandler.blockCallback;
            } else {
                return jsHandler.block;
        if (callback == null) {
            callback = new BlockCallback() {
                public IRubyObject call(ThreadContext contextIRubyObject[] argsBlock block) {
                    return runtime.getNil();
        final RubyModule signalModule = runtime.getModule("Signal");
        Block block = CallBlock.newCallClosure(signalModulesignalModule, Arity.noArguments(),
        return RubyProc.newProc(runtimeblockblock.type);
}// SunSignalFacade
New to GrepCode? Check out our FAQ X