001: /**
002: *
003: * Licensed to the Apache Software Foundation (ASF) under one or more
004: * contributor license agreements. See the NOTICE file distributed with
005: * this work for additional information regarding copyright ownership.
006: * The ASF licenses this file to You under the Apache License, Version 2.0
007: * (the "License"); you may not use this file except in compliance with
008: * 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: */package org.apache.openejb.client;
018:
019: import java.io.Externalizable;
020: import java.io.ObjectOutput;
021: import java.io.IOException;
022: import java.io.ObjectInput;
023: import java.util.Stack;
024:
025: /**
026: * @version $Revision: 570629 $ $Date: 2007-08-28 21:09:36 -0700 $
027: */
028: public class ThrowableArtifact implements Externalizable {
029:
030: private Throwable throwable;
031:
032: public ThrowableArtifact(Throwable throwable) {
033: this .throwable = throwable;
034: }
035:
036: public ThrowableArtifact() {
037: }
038:
039: public void writeExternal(ObjectOutput out) throws IOException {
040: Stack<MockThrowable> stack = new Stack<MockThrowable>();
041:
042: for (Throwable cause = throwable; cause != null; cause = cause
043: .getCause()) {
044: stack.add(new MockThrowable(cause));
045: }
046:
047: out.writeObject(stack);
048: try {
049: out.writeObject(throwable);
050: } catch (IOException dontCare) {
051: }
052: }
053:
054: public void readExternal(ObjectInput in) throws IOException,
055: ClassNotFoundException {
056: Stack<MockThrowable> stack = (Stack<MockThrowable>) in
057: .readObject();
058: try {
059: throwable = (Throwable) in.readObject();
060: } catch (Exception e) {
061: throwable = createMockThrowable(stack); // recreate exception
062: }
063: }
064:
065: private Throwable createMockThrowable(Stack<MockThrowable> stack) {
066: Throwable throwable = stack.pop();
067:
068: while (!stack.isEmpty()) {
069: throwable = stack.pop().initCause(throwable);
070: }
071:
072: return new RuntimeException(
073: "The exception sent could not be serialized or deserialized. This is a mock recreation:\n"
074: + throwable, throwable);
075: }
076:
077: public Throwable getThrowable() {
078: return throwable;
079: }
080:
081: public String toString() {
082: return throwable.toString();
083: }
084:
085: private static class MockThrowable extends Throwable {
086: private final String classType;
087:
088: public MockThrowable(Throwable t) {
089: this (t.getMessage(), t.getClass().getName(), t
090: .getStackTrace());
091: }
092:
093: public MockThrowable(String message, String classType,
094: StackTraceElement[] stackTrace) {
095: super (message);
096: this .classType = classType;
097: this .setStackTrace(stackTrace);
098: }
099:
100: public String toString() {
101: String s = classType;
102: String message = getLocalizedMessage();
103: return (message != null) ? (s + ": " + message) : s;
104: }
105: }
106: }
|