KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > repository > exceptions > DjenericException


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, is permitted
5  * provided that the following conditions are met:
6  * - Redistributions of source code must retain the above copyright notice, this list of conditions
7  * and the following disclaimer.
8  * - Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * - All advertising materials mentioning features or use of this software must display the
12  * following acknowledgment: "This product includes Djeneric."
13  * - Products derived from this software may not be called "Djeneric" nor may
14  * "Djeneric" appear in their names without prior written permission of Genimen BV.
15  * - Redistributions of any form whatsoever must retain the following acknowledgment: "This
16  * product includes Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.repository.exceptions;
31
32 import java.io.PrintStream JavaDoc;
33 import java.io.PrintWriter JavaDoc;
34 import java.io.StringWriter JavaDoc;
35
36 public class DjenericException extends Exception JavaDoc
37 {
38   private static final long serialVersionUID = 4123101775394911796L;
39
40   private final static String JavaDoc CHAINED_MSG = "Chained to: ";
41
42   private String JavaDoc _stackTrace = null;
43   private transient boolean _ignoreMyOwnStack = false;
44   private Throwable JavaDoc _chained = null;
45
46   public DjenericException()
47   {
48   }
49
50   public DjenericException(Throwable JavaDoc chainThis)
51   {
52     super(chainThis.getMessage());
53     storeStack(chainThis);
54   }
55
56   public DjenericException(String JavaDoc message)
57   {
58     super(message);
59   }
60
61   public Throwable JavaDoc getRootException()
62   {
63     if (_chained instanceof DjenericException && _chained != this)
64     {
65       DjenericException dje = (DjenericException) _chained;
66       return dje.getRootException();
67     }
68     if (_chained == null) return this;
69     return _chained;
70   }
71
72   private void storeStack(Throwable JavaDoc theEx)
73   {
74     _chained = theEx;
75     _stackTrace = stack2String(theEx);
76     _ignoreMyOwnStack = true;
77   }
78
79   public void printStackTrace()
80   {
81     if (_stackTrace != null)
82     {
83       System.err.println(_stackTrace);
84       if (!_ignoreMyOwnStack) System.err.print(CHAINED_MSG);
85     }
86     if (!_ignoreMyOwnStack) super.printStackTrace();
87   }
88
89   public void printStackTrace(PrintStream JavaDoc printStream)
90   {
91     if (_stackTrace != null)
92     {
93       printStream.println(_stackTrace);
94       if (!_ignoreMyOwnStack) printStream.print(CHAINED_MSG);
95     }
96     if (!_ignoreMyOwnStack) super.printStackTrace(printStream);
97   }
98
99   public void printStackTrace(PrintWriter JavaDoc writer)
100   {
101     if (_stackTrace != null)
102     {
103       writer.println(_stackTrace);
104       if (!_ignoreMyOwnStack) writer.print(CHAINED_MSG);
105     }
106     if (!_ignoreMyOwnStack) super.printStackTrace(writer);
107   }
108
109   protected String JavaDoc getChainedClassName()
110   {
111     if (_chained != null)
112     {
113       if (_chained instanceof DjenericException) return getClass().getName() + "\n"
114                                                         + ((DjenericException) _chained).getChainedClassName();
115       else return getClass().getName() + "\n" + _chained.getClass().getName();
116     }
117     return getClass().getName();
118   }
119
120   public String JavaDoc toString()
121   {
122     String JavaDoc exceptionMessage = getChainedClassName() + ": ";
123
124     if (getMessage() != null) exceptionMessage += getMessage() + "\n";
125     return exceptionMessage.trim();
126   }
127
128   private String JavaDoc stack2String(Throwable JavaDoc x)
129   {
130     if (x == null) return "";
131
132     StringWriter JavaDoc stringWriter = new StringWriter JavaDoc();
133     java.io.PrintWriter JavaDoc stackTrace = new java.io.PrintWriter JavaDoc(stringWriter);
134     x.printStackTrace(stackTrace);
135     String JavaDoc result = stringWriter.toString().trim();
136     if (result.length() == 0) result = null;
137
138     return result;
139   }
140
141   public static String JavaDoc getNiceMessage(Exception JavaDoc x)
142   {
143     String JavaDoc msg = x.getMessage();
144     if (x instanceof NumberFormatException JavaDoc)
145     {
146       msg = "Invalid number entered";
147     }
148     return msg;
149   }
150
151 }
Popular Tags