All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.googlecode.jmxtrans.util.SignalInterceptor Maven / Gradle / Ivy

Go to download

This module contains utilities that do not have direct dependencies on JmxTrans.

There is a newer version: 272
Show newest version
/**
 * The MIT License
 * Copyright © 2010 JmxTrans team
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.googlecode.jmxtrans.util;

import org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement;
import sun.misc.Signal;
import sun.misc.SignalHandler;

public abstract class SignalInterceptor {

	protected SignalInterceptor() {
	}

	/**
	 * 

* Register for the given signal. Note that the signal name should * not begin with SIG. For example, if you are interested in * SIGTERM, you should call register("TERM"). *

*

* If the registration fails for any reason, a * SignalInterceptorException will be thrown. This is usually * caused by one of the following conditions: *

*
    *
  • The sun.misc.Signal* classes are not available (e.g. you * are not using Sun's JVM).
  • *
  • signame is not a valid trappable signal name on this OS * (e.g. KILL can't be trapped, HUP does not exist on Windows) *
  • *
  • The JVM refuses to let you trap signame because it is * already being used for some other important purpose (e.g. QUIT * and/or BREAK cause the JVM to print diagnostic output).
  • *
*/ protected void register(String signame) throws SignalInterceptorException { try { new SignalInterceptorHelper(signame, this); } catch (Throwable e) { throw new SignalInterceptorException(signame, e); } } /** * Handle the given signal (which you had previously registered for). If * this method return false, or throws an exception, subsequent handlers in * the chain will not be called. */ protected abstract boolean handle(String signame); /** *

* Private helper class for SignalInterceptor. *

*

* This class exists separately from SignalInterceptor to * permit graceful handling of LinkageErrors when the * sun.misc.Signal* classes don't exist. *

*/ @IgnoreJRERequirement private static class SignalInterceptorHelper implements SignalHandler { private final SignalHandler oldHandler; private final SignalInterceptor interceptor; @IgnoreJRERequirement SignalInterceptorHelper(String signame, SignalInterceptor interceptor) { this.interceptor = interceptor; Signal signal = new Signal(signame); oldHandler = Signal.handle(signal, this); } @Override @IgnoreJRERequirement public void handle(Signal sig) { if (interceptor.handle(sig.getName()) && (oldHandler != null)) { oldHandler.handle(sig); } } } @SuppressWarnings("serial") private static class SignalInterceptorException extends Exception { SignalInterceptorException(String signal, Throwable cause) { super("Unable to register for SIG" + signal, cause); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy