|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.hadoop.util; |
| 20 | + |
| 21 | +import org.apache.hadoop.classification.InterfaceAudience; |
| 22 | + |
| 23 | +import org.apache.hadoop.util.dynamic.BindingUtils; |
| 24 | +import org.apache.hadoop.util.dynamic.DynConstructors; |
| 25 | +import org.apache.hadoop.util.dynamic.DynMethods; |
| 26 | + |
| 27 | +import java.lang.reflect.Method; |
| 28 | +import java.lang.reflect.Proxy; |
| 29 | + |
| 30 | +@InterfaceAudience.Private |
| 31 | +public class SignalUtil { |
| 32 | + |
| 33 | + static final Class<?> JDK_SIGNAL_CLAZZ = |
| 34 | + BindingUtils.loadClassSafely("sun.misc.Signal"); |
| 35 | + static final Class<?> JDK_SIGNAL_HANDLER_CLAZZ = |
| 36 | + BindingUtils.loadClassSafely("sun.misc.SignalHandler"); |
| 37 | + |
| 38 | + static final DynConstructors.Ctor<?> JDK_SIGNAL_CTOR = |
| 39 | + new DynConstructors.Builder() |
| 40 | + .impl(JDK_SIGNAL_CLAZZ, String.class) |
| 41 | + .build(); |
| 42 | + |
| 43 | + static final DynMethods.StaticMethod JDK_SIGNAL_HANDLE_STATIC_METHOD = |
| 44 | + new DynMethods.Builder("handle") |
| 45 | + .impl(JDK_SIGNAL_CLAZZ, JDK_SIGNAL_CLAZZ, JDK_SIGNAL_HANDLER_CLAZZ) |
| 46 | + .buildStatic(); |
| 47 | + |
| 48 | + static final DynMethods.StaticMethod JDK_SIGNAL_RAISE_STATIC_METHOD = |
| 49 | + new DynMethods.Builder("raise") |
| 50 | + .impl(JDK_SIGNAL_CLAZZ, JDK_SIGNAL_CLAZZ) |
| 51 | + .buildStatic(); |
| 52 | + |
| 53 | + static final DynMethods.UnboundMethod JDK_SIGNAL_HANDLER_HANDLE_UNBOUND_METHOD = |
| 54 | + new DynMethods.Builder("handle") |
| 55 | + .impl(JDK_SIGNAL_HANDLER_CLAZZ, JDK_SIGNAL_CLAZZ) |
| 56 | + .build(); |
| 57 | + |
| 58 | + @InterfaceAudience.Private |
| 59 | + public static class Signal { |
| 60 | + private static final DynMethods.UnboundMethod GET_NUMBER_UNBOUND_METHOD = |
| 61 | + new DynMethods.Builder("getNumber").impl(JDK_SIGNAL_CLAZZ).build(); |
| 62 | + |
| 63 | + private static final DynMethods.UnboundMethod GET_NAME_UNBOUND_METHOD = |
| 64 | + new DynMethods.Builder("getName").impl(JDK_SIGNAL_CLAZZ).build(); |
| 65 | + |
| 66 | + private final Object/* sun.misc.Signal */ delegate; |
| 67 | + private final DynMethods.BoundMethod getNumberMethod; |
| 68 | + private final DynMethods.BoundMethod getNameMethod; |
| 69 | + |
| 70 | + public Signal(String name) { |
| 71 | + Preconditions.checkNotNull(name); |
| 72 | + this.delegate = JDK_SIGNAL_CTOR.newInstance(name); |
| 73 | + this.getNumberMethod = GET_NUMBER_UNBOUND_METHOD.bind(delegate); |
| 74 | + this.getNameMethod = GET_NAME_UNBOUND_METHOD.bind(delegate); |
| 75 | + } |
| 76 | + |
| 77 | + public Signal(Object delegate) { |
| 78 | + Preconditions.checkArgument(JDK_SIGNAL_CLAZZ.isInstance(delegate), |
| 79 | + String.format("Expected class is '%s', but actual class is '%s'", |
| 80 | + JDK_SIGNAL_CLAZZ.getName(), delegate.getClass().getName())); |
| 81 | + this.delegate = delegate; |
| 82 | + this.getNumberMethod = GET_NUMBER_UNBOUND_METHOD.bind(delegate); |
| 83 | + this.getNameMethod = GET_NAME_UNBOUND_METHOD.bind(delegate); |
| 84 | + } |
| 85 | + |
| 86 | + public int getNumber() { |
| 87 | + return getNumberMethod.invoke(); |
| 88 | + } |
| 89 | + |
| 90 | + public String getName() { |
| 91 | + return getNameMethod.invoke(); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public boolean equals(Object obj) { |
| 96 | + if (this == obj) { |
| 97 | + return true; |
| 98 | + } |
| 99 | + if (obj instanceof Signal) { |
| 100 | + return delegate.equals(((Signal)obj).delegate); |
| 101 | + } |
| 102 | + return false; |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public int hashCode() { |
| 107 | + return delegate.hashCode(); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public String toString() { |
| 112 | + return delegate.toString(); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + @InterfaceAudience.Private |
| 117 | + public interface Handler { |
| 118 | + void handle(Signal sig); |
| 119 | + } |
| 120 | + |
| 121 | + static class JdkSignalHandlerImpl implements Handler { |
| 122 | + |
| 123 | + private final Object/* sun.misc.SignalHandler */ delegate; |
| 124 | + private final DynMethods.BoundMethod jdkSignalHandlerHandleMethod; |
| 125 | + |
| 126 | + JdkSignalHandlerImpl(Handler handler) { |
| 127 | + this.delegate = Proxy.newProxyInstance( |
| 128 | + getClass().getClassLoader(), |
| 129 | + new Class<?>[] {JDK_SIGNAL_HANDLER_CLAZZ}, |
| 130 | + (proxyObj, method, args) -> { |
| 131 | + if ("handle".equals(method.getName()) && args.length == 1 |
| 132 | + && JDK_SIGNAL_CLAZZ.isInstance(args[0])) { |
| 133 | + handler.handle(new Signal(args[0])); |
| 134 | + return null; |
| 135 | + } else { |
| 136 | + Method delegateMethod = handler.getClass().getMethod( |
| 137 | + method.getName(), method.getParameterTypes()); |
| 138 | + return delegateMethod.invoke(handler, args); |
| 139 | + } |
| 140 | + } |
| 141 | + ); |
| 142 | + this.jdkSignalHandlerHandleMethod = JDK_SIGNAL_HANDLER_HANDLE_UNBOUND_METHOD.bind(delegate); |
| 143 | + } |
| 144 | + |
| 145 | + JdkSignalHandlerImpl(Object delegate) { |
| 146 | + Preconditions.checkArgument(JDK_SIGNAL_HANDLER_CLAZZ.isInstance(delegate), |
| 147 | + String.format("Expected class is '%s', but actual class is '%s'", |
| 148 | + JDK_SIGNAL_HANDLER_CLAZZ.getName(), delegate.getClass().getName())); |
| 149 | + this.delegate = delegate; |
| 150 | + this.jdkSignalHandlerHandleMethod = JDK_SIGNAL_HANDLER_HANDLE_UNBOUND_METHOD.bind(delegate); |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public void handle(Signal sig) { |
| 155 | + jdkSignalHandlerHandleMethod.invoke(sig.delegate); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + public static Handler handle(Signal sig, Handler handler) { |
| 160 | + Object preHandle = JDK_SIGNAL_HANDLE_STATIC_METHOD.invoke( |
| 161 | + sig.delegate, new JdkSignalHandlerImpl(handler).delegate); |
| 162 | + return new JdkSignalHandlerImpl(preHandle); |
| 163 | + } |
| 164 | + |
| 165 | + public static void raise(Signal sig) throws IllegalArgumentException { |
| 166 | + JDK_SIGNAL_RAISE_STATIC_METHOD.invoke(sig.delegate); |
| 167 | + } |
| 168 | +} |
0 commit comments