001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.util; 019 020import java.io.InterruptedIOException; 021import java.net.SocketTimeoutException; 022import java.nio.channels.ClosedByInterruptException; 023import org.apache.yetus.audience.InterfaceAudience; 024 025/** 026 * This class handles the different interruption classes. It can be: - InterruptedException - 027 * InterruptedIOException (inherits IOException); used in IO - ClosedByInterruptException (inherits 028 * IOException) - SocketTimeoutException inherits InterruptedIOException but is not a real 029 * interruption, so we have to distinguish the case. This pattern is unfortunately common. 030 */ 031@InterfaceAudience.Private 032public final class ExceptionUtil { 033 private ExceptionUtil() { 034 } 035 036 /** Returns true if the throwable comes an interruption, false otherwise. */ 037 public static boolean isInterrupt(Throwable t) { 038 if (t instanceof InterruptedException) { 039 return true; 040 } 041 042 if (t instanceof SocketTimeoutException) { 043 return false; 044 } 045 046 return (t instanceof InterruptedIOException || t instanceof ClosedByInterruptException); 047 } 048 049 /** Throw InterruptedIOException if t was an interruption, nothing otherwise. */ 050 public static void rethrowIfInterrupt(Throwable t) throws InterruptedIOException { 051 InterruptedIOException iie = asInterrupt(t); 052 053 if (iie != null) { 054 throw iie; 055 } 056 } 057 058 /** Returns an InterruptedIOException if t was an interruption, null otherwise */ 059 public static InterruptedIOException asInterrupt(Throwable t) { 060 if (t instanceof SocketTimeoutException) { 061 return null; 062 } 063 064 if (t instanceof InterruptedIOException) { 065 return (InterruptedIOException) t; 066 } 067 068 if (t instanceof InterruptedException || t instanceof ClosedByInterruptException) { 069 InterruptedIOException iie = 070 new InterruptedIOException("Origin: " + t.getClass().getSimpleName()); 071 iie.initCause(t); 072 return iie; 073 } 074 075 return null; 076 } 077}