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;
20  
21  import org.apache.hadoop.hbase.classification.InterfaceAudience;
22  import org.apache.hadoop.ipc.RemoteException;
23  
24  import java.io.IOException;
25  import java.lang.reflect.Constructor;
26  import java.lang.reflect.InvocationTargetException;
27  
28  /**
29   * An immutable class which contains a static method for handling
30   * org.apache.hadoop.ipc.RemoteException exceptions.
31   */
32  @InterfaceAudience.Private
33  public class RemoteExceptionHandler {
34    /* Not instantiable */
35    private RemoteExceptionHandler() {super();}
36  
37    /**
38     * Examine passed Throwable.  See if its carrying a RemoteException. If so,
39     * run {@link #decodeRemoteException(RemoteException)} on it.  Otherwise,
40     * pass back <code>t</code> unaltered.
41     * @param t Throwable to examine.
42     * @return Decoded RemoteException carried by <code>t</code> or
43     * <code>t</code> unaltered.
44     */
45    public static Throwable checkThrowable(final Throwable t) {
46      Throwable result = t;
47      if (t instanceof RemoteException) {
48        try {
49          result =
50            RemoteExceptionHandler.decodeRemoteException((RemoteException)t);
51        } catch (Throwable tt) {
52          result = tt;
53        }
54      }
55      return result;
56    }
57  
58    /**
59     * Examine passed IOException.  See if its carrying a RemoteException. If so,
60     * run {@link #decodeRemoteException(RemoteException)} on it.  Otherwise,
61     * pass back <code>e</code> unaltered.
62     * @param e Exception to examine.
63     * @return Decoded RemoteException carried by <code>e</code> or
64     * <code>e</code> unaltered.
65     */
66    public static IOException checkIOException(final IOException e) {
67      Throwable t = checkThrowable(e);
68      return t instanceof IOException? (IOException)t: new IOException(t);
69    }
70  
71    /**
72     * Converts org.apache.hadoop.ipc.RemoteException into original exception,
73     * if possible. If the original exception is an Error or a RuntimeException,
74     * throws the original exception.
75     *
76     * @param re original exception
77     * @return decoded RemoteException if it is an instance of or a subclass of
78     *         IOException, or the original RemoteException if it cannot be decoded.
79     *
80     * @throws IOException indicating a server error ocurred if the decoded
81     *         exception is not an IOException. The decoded exception is set as
82     *         the cause.
83     * @deprecated Use {@link RemoteException#unwrapRemoteException()} instead.
84     * In fact we should look into deprecating this whole class - St.Ack 2010929
85     */
86    public static IOException decodeRemoteException(final RemoteException re)
87    throws IOException {
88      IOException i = re;
89  
90      try {
91        Class<?> c = Class.forName(re.getClassName());
92  
93        Class<?>[] parameterTypes = { String.class };
94        Constructor<?> ctor = c.getConstructor(parameterTypes);
95  
96        Object[] arguments = { re.getMessage() };
97        Throwable t = (Throwable) ctor.newInstance(arguments);
98  
99        if (t instanceof IOException) {
100         i = (IOException) t;
101       } else {
102         i = new IOException("server error");
103         i.initCause(t);
104         throw i;
105       }
106 
107     } catch (ClassNotFoundException x) {
108       // continue
109     } catch (NoSuchMethodException x) {
110       // continue
111     } catch (IllegalAccessException x) {
112       // continue
113     } catch (InvocationTargetException x) {
114       // continue
115     } catch (InstantiationException x) {
116       // continue
117     }
118     return i;
119   }
120 }