View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.util;
20  
21  import java.io.InterruptedIOException;
22  import java.net.SocketTimeoutException;
23  import java.nio.channels.ClosedByInterruptException;
24  
25  import org.apache.hadoop.hbase.classification.InterfaceAudience;
26  
27  /**
28   * This class handles the different interruption classes.
29   * It can be:
30   * - InterruptedException
31   * - InterruptedIOException (inherits IOException); used in IO
32   * - ClosedByInterruptException (inherits IOException)
33   * - SocketTimeoutException inherits InterruptedIOException but is not a real
34   * interruption, so we have to distinguish the case. This pattern is unfortunately common.
35   */
36  @InterfaceAudience.Private
37  public class ExceptionUtil {
38  
39    /**
40     * @return true if the throwable comes an interruption, false otherwise.
41     */
42    public static boolean isInterrupt(Throwable t) {
43      if (t instanceof InterruptedException) return true;
44      if (t instanceof SocketTimeoutException) return false;
45      return (t instanceof InterruptedIOException || t instanceof ClosedByInterruptException);
46    }
47  
48    /**
49     * @throws InterruptedIOException if t was an interruption. Does nothing otherwise.
50     */
51    public static void rethrowIfInterrupt(Throwable t) throws InterruptedIOException {
52      InterruptedIOException iie = asInterrupt(t);
53      if (iie != null) throw iie;
54    }
55  
56    /**
57     * @return an InterruptedIOException if t was an interruption, null otherwise
58     */
59    public static InterruptedIOException asInterrupt(Throwable t) {
60      if (t instanceof SocketTimeoutException) return null;
61  
62      if (t instanceof InterruptedIOException) return (InterruptedIOException) t;
63  
64      if (t instanceof InterruptedException || t instanceof ClosedByInterruptException) {
65        InterruptedIOException iie =
66            new InterruptedIOException("Origin: " + t.getClass().getSimpleName());
67        iie.initCause(t);
68        return iie;
69      }
70  
71      return null;
72    }
73  
74    private ExceptionUtil() {
75    }
76  }