1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 package com.mjh.switchrmi;
27
28 import java.io.ByteArrayOutputStream;
29 import java.io.PrintStream;
30 import java.io.PrintWriter;
31
32 import java.util.*;
33
34 public class RmiRemoteException
35 extends Exception
36 {
37 private final static String PREFIX = "(-)";
38 private final static String SEPARATOR = "|";
39 private String originalExceptionType;
40 private String originalMessage;
41 private String remoteStackTrace;
42 private boolean isSwitchRMIException;
43
44 public RmiRemoteException(String string)
45 {
46 if (string.startsWith(PREFIX))
47 {
48 StringTokenizer st = new StringTokenizer(string, SEPARATOR);
49
50 originalExceptionType = st.nextToken();
51 originalMessage = st.nextToken();
52
53 while (st.hasMoreTokens())
54 {
55 remoteStackTrace += st.nextToken();
56 }
57
58 isSwitchRMIException = true;
59 }
60 else
61 {
62 originalMessage = string;
63 }
64 }
65
66 public RmiRemoteException(Exception ex)
67 {
68 originalExceptionType = ex.getClass().getName();
69 originalMessage = ex.getMessage();
70
71 ByteArrayOutputStream out = new ByteArrayOutputStream();
72
73 ex.printStackTrace(new PrintStream(out));
74 remoteStackTrace = out.toString();
75
76 isSwitchRMIException = true;
77 }
78
79 public String getMessage()
80 {
81 return originalMessage;
82 }
83
84 public String toString()
85 {
86 return isSwitchRMIException
87 ? (PREFIX + originalExceptionType + SEPARATOR + originalMessage
88 + SEPARATOR + remoteStackTrace) : originalMessage;
89 }
90
91 public void printStackTrace(PrintStream stream)
92 {
93 if (isSwitchRMIException)
94 {
95 stream.println("Remote Stack Trace:");
96 stream.print(remoteStackTrace);
97 stream.println();
98 stream.println("Local Stack Trace:");
99 }
100
101 super.printStackTrace(stream);
102 }
103
104 public void printStackTrace(PrintWriter writer)
105 {
106 if (isSwitchRMIException)
107 {
108 writer.println("Remote Stack Trace:");
109 writer.print(remoteStackTrace);
110 writer.println();
111 writer.println("Local Stack Trace:");
112 }
113
114 super.printStackTrace(writer);
115 }
116 }