001 //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/enterprise/ServiceException.java $
002 /*---------------- FILE HEADER ------------------------------------------
003
004 This file is part of deegree.
005 Copyright (C) 2001-2008 by:
006 EXSE, Department of Geography, University of Bonn
007 http://www.giub.uni-bonn.de/deegree/
008 lat/lon GmbH
009 http://www.lat-lon.de
010
011 This library is free software; you can redistribute it and/or
012 modify it under the terms of the GNU Lesser General Public
013 License as published by the Free Software Foundation; either
014 version 2.1 of the License, or (at your option) any later version.
015
016 This library is distributed in the hope that it will be useful,
017 but WITHOUT ANY WARRANTY; without even the implied warranty of
018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019 Lesser General Public License for more details.
020
021 You should have received a copy of the GNU Lesser General Public
022 License along with this library; if not, write to the Free Software
023 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024
025 Contact:
026
027 Andreas Poth
028 lat/lon GmbH
029 Aennchenstr. 19
030 53115 Bonn
031 Germany
032 E-Mail: poth@lat-lon.de
033
034 Prof. Dr. Klaus Greve
035 Department of Geography
036 University of Bonn
037 Meckenheimer Allee 166
038 53115 Bonn
039 Germany
040 E-Mail: greve@giub.uni-bonn.de
041
042
043 ---------------------------------------------------------------------------*/
044 package org.deegree.enterprise;
045
046 // JDK 1.3
047 import java.io.PrintStream;
048 import java.io.PrintWriter;
049 import java.io.StringWriter;
050
051 /**
052 * The <code>ServiceException</code> class is used across all core framework services and is also
053 * suitable for use by developers extending the framework using the framework SPI.
054 *
055 * Based on code published by Terren Suydam in JavaWorld
056 *
057 * @see <a href="http://www.javaworld.com/javaworld/javatips/jw-javatip91.html">JavaWorld tip 91</a>
058 *
059 * @author <a href="mailto:tfr@users.sourceforge.net">Torsten Friebe </A>
060 * @author last edited by: $Author: apoth $
061 *
062 * @version $Revision: 9338 $, $Date: 2007-12-27 13:31:31 +0100 (Do, 27 Dez 2007) $
063 */
064 public class ServiceException extends Exception implements java.io.Serializable {
065
066 /**
067 *
068 */
069 private static final long serialVersionUID = 1L;
070
071 // the nested exception
072 private Throwable nestedException;
073
074 // String representation of stack trace - not transient!
075 private String stackTraceString;
076
077 /**
078 * Convert a stack trace to a String so it can be serialized
079 *
080 * @param t
081 *
082 * @return
083 */
084 public static String generateStackTraceString( Throwable t ) {
085 StringWriter s = new StringWriter();
086
087 t.printStackTrace( new PrintWriter( s ) );
088
089 return s.toString();
090 }
091
092 /**
093 * java.lang.Exception constructors
094 */
095 public ServiceException() {
096 }
097
098 /**
099 * Constructor declaration
100 *
101 * @param msg
102 *
103 */
104 public ServiceException( String msg ) {
105 super( msg );
106 }
107
108 /**
109 * additional c'tors - nest the exceptions, storing the stack trace
110 *
111 * @param nestedException
112 */
113 public ServiceException( Throwable nestedException ) {
114 this.nestedException = nestedException;
115 stackTraceString = generateStackTraceString( nestedException );
116 }
117
118 /**
119 * Constructor declaration
120 *
121 *
122 * @param msg
123 * @param nestedException
124 *
125 *
126 */
127 public ServiceException( String msg, Throwable nestedException ) {
128 this( msg );
129
130 this.nestedException = nestedException;
131 stackTraceString = generateStackTraceString( nestedException );
132 }
133
134 // methods
135
136 /**
137 * Method declaration
138 *
139 *
140 * @return nestedException
141 *
142 */
143 public Throwable getNestedException() {
144 return nestedException;
145 }
146
147 /**
148 * descend through linked-list of nesting exceptions, & output trace note that this displays the
149 * 'deepest' trace first
150 *
151 * @return
152 *
153 */
154 public String getStackTraceString() {
155
156 // if there's no nested exception, there's no stackTrace
157 if ( nestedException == null ) {
158 return null;
159 }
160
161 StringBuffer traceBuffer = new StringBuffer();
162
163 if ( nestedException instanceof ServiceException ) {
164 traceBuffer.append( ( (ServiceException) nestedException ).getStackTraceString() );
165 traceBuffer.append( " nested by:\n" );
166 }
167
168 traceBuffer.append( stackTraceString );
169
170 return traceBuffer.toString();
171 }
172
173 // overrides Exception.getMessage()
174
175 /**
176 * Method declaration
177 *
178 *
179 * @return
180 *
181 */
182 public String getMessage() {
183
184 // superMsg will contain whatever String was passed into the
185 // constructor, and null otherwise.
186 String superMsg = super.getMessage();
187
188 // if there's no nested exception, do like we would always do
189 if ( getNestedException() == null ) {
190 return superMsg;
191 }
192
193 StringBuffer theMsg = new StringBuffer();
194
195 // get the nested exception's message
196 String nestedMsg = getNestedException().getMessage();
197
198 if ( superMsg != null ) {
199 theMsg.append( superMsg ).append( ": " ).append( nestedMsg );
200 } else {
201 theMsg.append( nestedMsg );
202 }
203
204 return theMsg.toString();
205 }
206
207 // overrides Exception.toString()
208
209 /**
210 * Method declaration
211 *
212 *
213 * @return
214 *
215 */
216 public String toString() {
217 StringBuffer theMsg = new StringBuffer( super.toString() );
218
219 if ( getNestedException() != null ) {
220 theMsg.append( "; \n\t---> nested " ).append( getNestedException() );
221 }
222
223 return theMsg.toString();
224 }
225
226 /**
227 * Method declaration
228 *
229 *
230 *
231 */
232 public void printStackTrace() {
233 if ( this.getNestedException() != null ) {
234 this.getNestedException().printStackTrace();
235 } else {
236 super.printStackTrace();
237 }
238 }
239
240 /**
241 * Method declaration
242 *
243 *
244 * @param inPrintStream
245 *
246 *
247 */
248 public void printStackTrace( PrintStream inPrintStream ) {
249 this.printStackTrace( new PrintWriter( inPrintStream ) );
250 }
251
252 /**
253 * Method declaration
254 *
255 *
256 * @param inPrintWriter
257 *
258 *
259 */
260 public void printStackTrace( PrintWriter inPrintWriter ) {
261 if ( this.getNestedException() != null ) {
262 this.getNestedException().printStackTrace( inPrintWriter );
263 } else {
264 super.printStackTrace( inPrintWriter );
265 }
266 }
267
268 }