001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/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 04:31:31 -0800 (Thu, 27 Dec 2007) $
063: */
064: public class ServiceException extends Exception implements
065: java.io.Serializable {
066:
067: /**
068: *
069: */
070: private static final long serialVersionUID = 1L;
071:
072: // the nested exception
073: private Throwable nestedException;
074:
075: // String representation of stack trace - not transient!
076: private String stackTraceString;
077:
078: /**
079: * Convert a stack trace to a String so it can be serialized
080: *
081: * @param t
082: *
083: * @return
084: */
085: public static String generateStackTraceString(Throwable t) {
086: StringWriter s = new StringWriter();
087:
088: t.printStackTrace(new PrintWriter(s));
089:
090: return s.toString();
091: }
092:
093: /**
094: * java.lang.Exception constructors
095: */
096: public ServiceException() {
097: }
098:
099: /**
100: * Constructor declaration
101: *
102: * @param msg
103: *
104: */
105: public ServiceException(String msg) {
106: super (msg);
107: }
108:
109: /**
110: * additional c'tors - nest the exceptions, storing the stack trace
111: *
112: * @param nestedException
113: */
114: public ServiceException(Throwable nestedException) {
115: this .nestedException = nestedException;
116: stackTraceString = generateStackTraceString(nestedException);
117: }
118:
119: /**
120: * Constructor declaration
121: *
122: *
123: * @param msg
124: * @param nestedException
125: *
126: *
127: */
128: public ServiceException(String msg, Throwable nestedException) {
129: this (msg);
130:
131: this .nestedException = nestedException;
132: stackTraceString = generateStackTraceString(nestedException);
133: }
134:
135: // methods
136:
137: /**
138: * Method declaration
139: *
140: *
141: * @return nestedException
142: *
143: */
144: public Throwable getNestedException() {
145: return nestedException;
146: }
147:
148: /**
149: * descend through linked-list of nesting exceptions, & output trace note that this displays the
150: * 'deepest' trace first
151: *
152: * @return
153: *
154: */
155: public String getStackTraceString() {
156:
157: // if there's no nested exception, there's no stackTrace
158: if (nestedException == null) {
159: return null;
160: }
161:
162: StringBuffer traceBuffer = new StringBuffer();
163:
164: if (nestedException instanceof ServiceException) {
165: traceBuffer.append(((ServiceException) nestedException)
166: .getStackTraceString());
167: traceBuffer.append(" nested by:\n");
168: }
169:
170: traceBuffer.append(stackTraceString);
171:
172: return traceBuffer.toString();
173: }
174:
175: // overrides Exception.getMessage()
176:
177: /**
178: * Method declaration
179: *
180: *
181: * @return
182: *
183: */
184: public String getMessage() {
185:
186: // superMsg will contain whatever String was passed into the
187: // constructor, and null otherwise.
188: String super Msg = super .getMessage();
189:
190: // if there's no nested exception, do like we would always do
191: if (getNestedException() == null) {
192: return super Msg;
193: }
194:
195: StringBuffer theMsg = new StringBuffer();
196:
197: // get the nested exception's message
198: String nestedMsg = getNestedException().getMessage();
199:
200: if (super Msg != null) {
201: theMsg.append(super Msg).append(": ").append(nestedMsg);
202: } else {
203: theMsg.append(nestedMsg);
204: }
205:
206: return theMsg.toString();
207: }
208:
209: // overrides Exception.toString()
210:
211: /**
212: * Method declaration
213: *
214: *
215: * @return
216: *
217: */
218: public String toString() {
219: StringBuffer theMsg = new StringBuffer(super .toString());
220:
221: if (getNestedException() != null) {
222: theMsg.append("; \n\t---> nested ").append(
223: getNestedException());
224: }
225:
226: return theMsg.toString();
227: }
228:
229: /**
230: * Method declaration
231: *
232: *
233: *
234: */
235: public void printStackTrace() {
236: if (this .getNestedException() != null) {
237: this .getNestedException().printStackTrace();
238: } else {
239: super .printStackTrace();
240: }
241: }
242:
243: /**
244: * Method declaration
245: *
246: *
247: * @param inPrintStream
248: *
249: *
250: */
251: public void printStackTrace(PrintStream inPrintStream) {
252: this .printStackTrace(new PrintWriter(inPrintStream));
253: }
254:
255: /**
256: * Method declaration
257: *
258: *
259: * @param inPrintWriter
260: *
261: *
262: */
263: public void printStackTrace(PrintWriter inPrintWriter) {
264: if (this.getNestedException() != null) {
265: this.getNestedException().printStackTrace(inPrintWriter);
266: } else {
267: super.printStackTrace(inPrintWriter);
268: }
269: }
270:
271: }
|