View Javadoc

1   /**
2    * Copyright 2007 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase;
21  
22  import java.io.IOException;
23  import java.lang.reflect.Constructor;
24  import java.lang.reflect.InvocationTargetException;
25  
26  import org.apache.hadoop.ipc.RemoteException;
27  
28  /**
29   * An immutable class which contains a static method for handling
30   * org.apache.hadoop.ipc.RemoteException exceptions.
31   */
32  public class RemoteExceptionHandler {
33    /* Not instantiable */
34    private RemoteExceptionHandler() {super();}
35  
36    /**
37     * Examine passed Throwable.  See if its carrying a RemoteException. If so,
38     * run {@link #decodeRemoteException(RemoteException)} on it.  Otherwise,
39     * pass back <code>t</code> unaltered.
40     * @param t Throwable to examine.
41     * @return Decoded RemoteException carried by <code>t</code> or
42     * <code>t</code> unaltered.
43     */
44    public static Throwable checkThrowable(final Throwable t) {
45      Throwable result = t;
46      if (t instanceof RemoteException) {
47        try {
48          result =
49            RemoteExceptionHandler.decodeRemoteException((RemoteException)t);
50        } catch (Throwable tt) {
51          result = tt;
52        }
53      }
54      return result;
55    }
56  
57    /**
58     * Examine passed IOException.  See if its carrying a RemoteException. If so,
59     * run {@link #decodeRemoteException(RemoteException)} on it.  Otherwise,
60     * pass back <code>e</code> unaltered.
61     * @param e Exception to examine.
62     * @return Decoded RemoteException carried by <code>e</code> or
63     * <code>e</code> unaltered.
64     */
65    public static IOException checkIOException(final IOException e) {
66      Throwable t = checkThrowable(e);
67      return t instanceof IOException? (IOException)t: new IOException(t);
68    }
69  
70    /**
71     * Converts org.apache.hadoop.ipc.RemoteException into original exception,
72     * if possible. If the original exception is an Error or a RuntimeException,
73     * throws the original exception.
74     *
75     * @param re original exception
76     * @return decoded RemoteException if it is an instance of or a subclass of
77     *         IOException, or the original RemoteException if it cannot be decoded.
78     *
79     * @throws IOException indicating a server error ocurred if the decoded
80     *         exception is not an IOException. The decoded exception is set as
81     *         the cause.
82     * @deprecated Use {@link RemoteException#unwrapRemoteException()} instead.
83     * In fact we should look into deprecating this whole class - St.Ack 2010929
84     */
85    public static IOException decodeRemoteException(final RemoteException re)
86    throws IOException {
87      IOException i = re;
88  
89      try {
90        Class<?> c = Class.forName(re.getClassName());
91  
92        Class<?>[] parameterTypes = { String.class };
93        Constructor<?> ctor = c.getConstructor(parameterTypes);
94  
95        Object[] arguments = { re.getMessage() };
96        Throwable t = (Throwable) ctor.newInstance(arguments);
97  
98        if (t instanceof IOException) {
99          i = (IOException) t;
100 
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 }