source: proiecte/HadoopJUnit/hadoop-0.20.1/src/core/org/apache/hadoop/ipc/RemoteException.java @ 120

Last change on this file since 120 was 120, checked in by (none), 14 years ago

Added the mail files for the Hadoop JUNit Project

  • Property svn:executable set to *
File size: 3.8 KB
Line 
1/**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements.  See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership.  The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License.  You may obtain a copy of the License at
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19package org.apache.hadoop.ipc;
20
21import java.io.IOException;
22import java.lang.reflect.Constructor;
23
24import org.xml.sax.Attributes;
25import org.znerd.xmlenc.XMLOutputter;
26
27public class RemoteException extends IOException {
28  /** For java.io.Serializable */
29  private static final long serialVersionUID = 1L;
30
31  private String className;
32 
33  public RemoteException(String className, String msg) {
34    super(msg);
35    this.className = className;
36  }
37 
38  public String getClassName() {
39    return className;
40  }
41
42  /**
43   * If this remote exception wraps up one of the lookupTypes
44   * then return this exception.
45   * <p>
46   * Unwraps any IOException.
47   *
48   * @param lookupTypes the desired exception class.
49   * @return IOException, which is either the lookupClass exception or this.
50   */
51  public IOException unwrapRemoteException(Class<?>... lookupTypes) {
52    if(lookupTypes == null)
53      return this;
54    for(Class<?> lookupClass : lookupTypes) {
55      if(!lookupClass.getName().equals(getClassName()))
56        continue;
57      try {
58        return instantiateException(lookupClass.asSubclass(IOException.class));
59      } catch(Exception e) {
60        // cannot instantiate lookupClass, just return this
61        return this;
62      }
63    }
64    // wrapped up exception is not in lookupTypes, just return this
65    return this;
66  }
67
68  /**
69   * Instantiate and return the exception wrapped up by this remote exception.
70   *
71   * <p> This unwraps any <code>Throwable</code> that has a constructor taking
72   * a <code>String</code> as a parameter.
73   * Otherwise it returns this.
74   *
75   * @return <code>Throwable
76   */
77  public IOException unwrapRemoteException() {
78    try {
79      Class<?> realClass = Class.forName(getClassName());
80      return instantiateException(realClass.asSubclass(IOException.class));
81    } catch(Exception e) {
82      // cannot instantiate the original exception, just return this
83    }
84    return this;
85  }
86
87  private IOException instantiateException(Class<? extends IOException> cls)
88      throws Exception {
89    Constructor<? extends IOException> cn = cls.getConstructor(String.class);
90    cn.setAccessible(true);
91    String firstLine = this.getMessage();
92    int eol = firstLine.indexOf('\n');
93    if (eol>=0) {
94      firstLine = firstLine.substring(0, eol);
95    }
96    IOException ex = cn.newInstance(firstLine);
97    ex.initCause(this);
98    return ex;
99  }
100
101  /** Write the object to XML format */
102  public void writeXml(String path, XMLOutputter doc) throws IOException {
103    doc.startTag(RemoteException.class.getSimpleName());
104    doc.attribute("path", path);
105    doc.attribute("class", getClassName());
106    String msg = getLocalizedMessage();
107    int i = msg.indexOf("\n");
108    if (i >= 0) {
109      msg = msg.substring(0, i);
110    }
111    doc.attribute("message", msg.substring(msg.indexOf(":") + 1).trim());
112    doc.endTag();
113  }
114
115  /** Create RemoteException from attributes */
116  public static RemoteException valueOf(Attributes attrs) {
117    return new RemoteException(attrs.getValue("class"),
118        attrs.getValue("message")); 
119  }
120}
Note: See TracBrowser for help on using the repository browser.