View Javadoc

1   package com.explosion.utilities.exception;
2   
3   /*
4    * =============================================================================
5    * 
6    * Copyright 2004 Stephen Cowx
7    * 
8    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
9    * use this file except in compliance with the License. You may obtain a copy of
10   * 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, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   * 
20   * =============================================================================
21   */
22  
23  import java.io.CharArrayWriter;
24  import java.io.PrintStream;
25  import java.io.PrintWriter;
26  
27  import org.apache.log4j.LogManager;
28  import org.apache.log4j.Logger;
29  
30  import com.explosion.utilities.GeneralConstants;
31  
32  /***
33   * This is a generic wrapper exception which can be used for multiple purposes
34   * There is an ability to specify a code if need be
35   */
36  
37  public class EnhancedException extends Exception
38  {
39  
40      private static Logger log = LogManager.getLogger(EnhancedException.class);
41  
42      private Exception originalException;
43  
44      private String originalStackTrace = "";
45  
46      private int exceptionSeverity;
47  
48      private int exceptionCode;
49  
50      private String message;
51  
52      private Object params[];
53  
54      public EnhancedException()
55      {}
56  
57      public EnhancedException(String message, Exception originalException)
58      {
59          this.message = message;
60          setOriginalException(originalException);
61      }
62      
63      public EnhancedException(String message)
64      {
65          this.message = message;
66      }
67      
68      public String getMessage()
69      {
70          return message;
71      }
72  
73      public void setMessage(String message)
74      {
75          this.message = message;
76      }
77  
78      public int getExceptionSeverity()
79      {
80          return exceptionSeverity;
81      }
82  
83      public void setExceptionSeverity(int exceptionSeverity)
84      {
85          this.exceptionSeverity = exceptionSeverity;
86      }
87  
88      public int getExceptionCode()
89      {
90          return exceptionCode;
91      }
92  
93      public void setExceptionCode(int exceptionCode)
94      {
95          this.exceptionCode = exceptionCode;
96      }
97  
98      public void setOriginalException(Exception originalException)
99      {
100         this.originalStackTrace = getStackTrace(originalException);
101         this.originalException = originalException;
102 
103         if (message == null) message = "";
104 
105         setMessage(message + originalException.getMessage());
106     }
107 
108     public Exception getOriginalException()
109     {
110         return originalException;
111     }
112 
113     /***
114      * This method returns the message and the first line of the stack trace.
115      */
116     public String toString()
117     {
118         String message = "";
119         if (originalException == null)
120         {
121             message = getMessage();
122         } else
123         {
124             int i = originalStackTrace.indexOf(GeneralConstants.LS);
125 
126             if (i > 0)
127                 message = originalStackTrace.substring(0, i);
128             else
129                 message = originalStackTrace;
130 
131             if (getMessage() != null) message = getMessage() + GeneralConstants.LS + message;
132 
133         }
134 
135         return message;
136     }
137 
138     /***
139      * This method returns the stack trace of the original exception combined
140      * with the message used when the CodedException was created.
141      */
142     private String getCombinedStackTrace() throws Exception
143     {
144         if (originalException == null)
145         {
146             return getStackTrace(this);
147         } else
148         {
149             if (getMessage() != null)
150                 return getMessage() + GeneralConstants.LS + originalStackTrace;
151             else
152                 return originalStackTrace;
153         }
154     }
155 
156     /***
157      * Handy method which returns the Stack trace of an exception
158      */
159     public static String getStackTrace(Throwable ex)
160     {
161         try
162         {
163             CharArrayWriter charWriter = new CharArrayWriter();
164             PrintWriter writer = new PrintWriter(charWriter);
165             ex.printStackTrace(writer);
166             writer.flush();
167 
168             String value = new String(charWriter.toCharArray());
169             writer.close();
170 
171             return value;
172         } catch (Exception e)
173         {
174             return "Error occurred while parsing stack trace !";
175         }
176     }
177 
178     /***
179      * Override of superclass
180      */
181     public void printStackTrace(PrintStream s)
182     {
183         if (originalException == null)
184             super.printStackTrace(s);
185         else
186             try
187             {
188                 s.println(getCombinedStackTrace());
189             } catch (Exception e)
190             {}
191     }
192 
193     /***
194      * Override of superclass
195      */
196     public void printStackTrace(PrintWriter s)
197     {
198         if (originalException == null)
199             super.printStackTrace(s);
200         else
201             try
202             {
203                 s.println(getCombinedStackTrace());
204             } catch (Exception e)
205             {}
206     }
207 
208     /***
209      * Override of superclass
210      */
211     public void printStackTrace()
212     {
213         if (originalException == null)
214             super.printStackTrace();
215         else
216             try
217             {
218                 log.debug(getCombinedStackTrace());
219             } catch (Exception e)
220             {}
221     }
222 
223 }