001    /*
002     * Copyright (C) 2012 eXo Platform SAS.
003     *
004     * This is free software; you can redistribute it and/or modify it
005     * under the terms of the GNU Lesser General Public License as
006     * published by the Free Software Foundation; either version 2.1 of
007     * the License, or (at your option) any later version.
008     *
009     * This software is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012     * Lesser General Public License for more details.
013     *
014     * You should have received a copy of the GNU Lesser General Public
015     * License along with this software; if not, write to the Free
016     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018     */
019    
020    package org.crsh.util;
021    
022    import org.slf4j.Logger;
023    import org.slf4j.LoggerFactory;
024    
025    import java.lang.reflect.Constructor;
026    import java.lang.reflect.InvocationHandler;
027    import java.lang.reflect.Method;
028    import java.lang.reflect.Proxy;
029    
030    public class InterruptHandler {
031    
032      /** . */
033      private final Runnable runnable;
034    
035      /** . */
036      private final Logger log = LoggerFactory.getLogger(InterruptHandler.class);
037    
038      private final InvocationHandler handler = new InvocationHandler() {
039        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
040          Class<?>[] parameterTypes = method.getParameterTypes();
041          if (method.getName().equals("hashCode") && parameterTypes.length == 0) {
042            return System.identityHashCode(runnable);
043          } else if (method.getName().equals("equals") && parameterTypes.length == 1 && parameterTypes[0] == Object.class) {
044            return runnable.equals(args[0]);
045          } else if (method.getName().equals("toString") && parameterTypes.length == 0) {
046            return runnable.toString();
047          } else if (method.getName().equals("handle")) {
048            runnable.run();
049            return null;
050          } else {
051            throw new UnsupportedOperationException("Method " + method + " not implemented");
052          }
053        }
054      };
055    
056      public InterruptHandler(Runnable runnable) {
057        this.runnable = runnable;
058      }
059    
060      public void install() {
061        ClassLoader cl = Thread.currentThread().getContextClassLoader();
062        Class<?> signalHandlerClass;
063        Class<?> signalClass;
064        Method handle;
065        Object INT;
066        try {
067          signalHandlerClass = cl.loadClass("sun.misc.SignalHandler");
068          signalClass = cl.loadClass("sun.misc.Signal");
069          handle = signalClass.getDeclaredMethod("handle", signalClass, signalHandlerClass);
070          Constructor ctor = signalClass.getConstructor(String.class);
071          INT = ctor.newInstance("INT");
072        }
073        catch (Exception e) {
074          return;
075        }
076    
077        //
078        Object proxy = Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class<?>[]{signalHandlerClass}, handler);
079    
080        //
081        try {
082          handle.invoke(null, INT, proxy);
083        }
084        catch (Exception e) {
085          log.error("Could not install signal handler", e);
086        }
087      }
088    }