001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/tags/2.1/src/org/deegree/enterprise/ServiceException.java $
002    /*----------------    FILE HEADER  ------------------------------------------
003    
004     This file is part of deegree.
005     Copyright (C) 2001-2006 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: 6696 $, $Date: 2007-04-25 21:57:05 +0200 (Mi, 25 Apr 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         * @param t 
080         * 
081         * @return
082         */
083        public static String generateStackTraceString( Throwable t ) {
084            StringWriter s = new StringWriter();
085    
086            t.printStackTrace( new PrintWriter( s ) );
087    
088            return s.toString();
089        }
090    
091        /**
092         * java.lang.Exception constructors
093         */
094        public ServiceException() {
095        }
096    
097        /**
098         * Constructor declaration
099         * 
100         * @param msg
101         * 
102         */
103        public ServiceException( String msg ) {
104            super( msg );
105        }
106    
107        /**
108         * additional c'tors - nest the exceptions, storing the stack trace
109         * 
110         * @param nestedException
111         */
112        public ServiceException( Throwable nestedException ) {
113            this.nestedException = nestedException;
114            stackTraceString = generateStackTraceString( nestedException );
115        }
116    
117        /**
118         * Constructor declaration
119         * 
120         * 
121         * @param msg
122         * @param nestedException
123         * 
124         * 
125         */
126        public ServiceException( String msg, Throwable nestedException ) {
127            this( msg );
128    
129            this.nestedException = nestedException;
130            stackTraceString = generateStackTraceString( nestedException );
131        }
132    
133        // methods
134    
135        /**
136         * Method declaration
137         * 
138         * 
139         * @return nestedException
140         * 
141         */
142        public Throwable getNestedException() {
143            return nestedException;
144        }
145    
146        /**
147         * descend through linked-list of nesting exceptions, & output trace note that this displays the
148         * 'deepest' trace first
149         * 
150         * @return
151         * 
152         */
153        public String getStackTraceString() {
154    
155            // if there's no nested exception, there's no stackTrace
156            if ( nestedException == null ) {
157                return null;
158            }
159    
160            StringBuffer traceBuffer = new StringBuffer();
161    
162            if ( nestedException instanceof ServiceException ) {
163                traceBuffer.append( ( (ServiceException) nestedException ).getStackTraceString() );
164                traceBuffer.append( " nested by:\n" );
165            }
166    
167            traceBuffer.append( stackTraceString );
168    
169            return traceBuffer.toString();
170        }
171    
172        // overrides Exception.getMessage()
173    
174        /**
175         * Method declaration
176         * 
177         * 
178         * @return
179         * 
180         */
181        public String getMessage() {
182    
183            // superMsg will contain whatever String was passed into the
184            // constructor, and null otherwise.
185            String superMsg = super.getMessage();
186    
187            // if there's no nested exception, do like we would always do
188            if ( getNestedException() == null ) {
189                return superMsg;
190            }
191    
192            StringBuffer theMsg = new StringBuffer();
193    
194            // get the nested exception's message
195            String nestedMsg = getNestedException().getMessage();
196    
197            if ( superMsg != null ) {
198                theMsg.append( superMsg ).append( ": " ).append( nestedMsg );
199            } else {
200                theMsg.append( nestedMsg );
201            }
202    
203            return theMsg.toString();
204        }
205    
206        // overrides Exception.toString()
207    
208        /**
209         * Method declaration
210         * 
211         * 
212         * @return
213         * 
214         */
215        public String toString() {
216            StringBuffer theMsg = new StringBuffer( super.toString() );
217    
218            if ( getNestedException() != null ) {
219                theMsg.append( "; \n\t---> nested " ).append( getNestedException() );
220            }
221    
222            return theMsg.toString();
223        }
224    
225        /**
226         * Method declaration
227         * 
228         * 
229         * 
230         */
231        public void printStackTrace() {
232            if ( this.getNestedException() != null ) {
233                this.getNestedException().printStackTrace();
234            } else {
235                super.printStackTrace();
236            }
237        }
238    
239        /**
240         * Method declaration
241         * 
242         * 
243         * @param inPrintStream
244         * 
245         * 
246         */
247        public void printStackTrace( PrintStream inPrintStream ) {
248            this.printStackTrace( new PrintWriter( inPrintStream ) );
249        }
250    
251        /**
252         * Method declaration
253         * 
254         * 
255         * @param inPrintWriter
256         * 
257         * 
258         */
259        public void printStackTrace( PrintWriter inPrintWriter ) {
260            if ( this.getNestedException() != null ) {
261                this.getNestedException().printStackTrace( inPrintWriter );
262            } else {
263                super.printStackTrace( inPrintWriter );
264            }
265        }
266    
267    }
268    
269    /***************************************************************************************************
270     * <code>
271     Changes to this class. What the people have been up to:
272     $Log$
273     Revision 1.10  2007/03/06 10:02:20  wanhoff
274     Fixed Javadoc (@url removed - link in @see tag)
275    
276     Revision 1.9  2007/01/26 13:21:18  wanhoff
277     fixed Javadoc @return tag and footer
278    
279     Revision 1.8  2006/07/12 14:46:18  poth
280     comment footer added
281    
282     </code>
283     **************************************************************************************************/