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