1    //
2    // SwitchRMI  Framework
3    // Copyright (c) 2000-2002 by Michael J. Henderson & Associates.
4    //
5    // Michael Henderson
6    // http://switchrmi.sf.net
7    // mailto:mikehenderson@dunelm.org.uk
8    //
9    // This library is free software.
10   //
11   // You may redistribute it and/or modify it under the terms of the GNU
12   // Lesser General Public License as published by the Free Software Foundation.
13   //
14   // Version 2.1 of the license should be included with this distribution in
15   // the file LICENSE, as well as License.html. If the license is not
16   // included with this distribution, you may find a copy at the FSF web
17   // site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
18   // Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
19   //
20   // This library is distributed in the hope that it will be useful,
21   // but WITHOUT ANY WARRANTY; without even the implied waranty of
22   // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23   // Lesser General Public License for more details.
24   //
25   // $Id: RmiRemoteException.java,v 1.1 2002/11/11 22:19:48 mikehenderson Exp $
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  }