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 final class ExceptionUtil {
38    private ExceptionUtil() {
39    }
40  
41    /**
42     * @return true if the throwable comes an interruption, false otherwise.
43     */
44    public static boolean isInterrupt(Throwable t) {
45      if (t instanceof InterruptedException) {
46        return true;
47      }
48  
49      if (t instanceof SocketTimeoutException) {
50        return false;
51      }
52  
53      return (t instanceof InterruptedIOException || t instanceof ClosedByInterruptException);
54    }
55  
56    /**
57     * @throws InterruptedIOException if t was an interruption. Does nothing otherwise.
58     */
59    public static void rethrowIfInterrupt(Throwable t) throws InterruptedIOException {
60      InterruptedIOException iie = asInterrupt(t);
61  
62      if (iie != null) {
63        throw iie;
64      }
65    }
66  
67    /**
68     * @return an InterruptedIOException if t was an interruption, null otherwise
69     */
70    public static InterruptedIOException asInterrupt(Throwable t) {
71      if (t instanceof SocketTimeoutException) {
72        return null;
73      }
74  
75      if (t instanceof InterruptedIOException) {
76        return (InterruptedIOException) t;
77      }
78  
79      if (t instanceof InterruptedException || t instanceof ClosedByInterruptException) {
80        InterruptedIOException iie =
81            new InterruptedIOException("Origin: " + t.getClass().getSimpleName());
82        iie.initCause(t);
83        return iie;
84      }
85  
86      return null;
87    }
88  }