KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > core > exception > DefaultExceptionDelegateImpl


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is The Carbon Component Framework.
12  *
13  * The Initial Developer of the Original Code is Sapient Corporation
14  *
15  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16  */

17
18 package org.sape.carbon.core.exception;
19
20
21 import org.sape.carbon.core.util.classify.SeverityEnum;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25
26
27
28 /**
29  * <P>This is the default implementation of the exception delegate interface
30  * to support basic exception handling as well as auto-logging of the
31  * exception.</P>
32  *
33  * Copyright 2002 Sapient
34  * @since carbon 1.0
35  * @author Greg Hinkle, April 2002
36  * @version $Revision: 1.13 $($Author: dvoet $ / $Date: 2003/05/05 21:21:21 $)
37  */

38 public class DefaultExceptionDelegateImpl implements ExceptionDelegate {
39
40     /**
41       * The exception object that this object is acting as the delegate for.
42       */

43     private Throwable JavaDoc delegatee;
44
45     /**
46      * The string message for this exception.
47      */

48     private String JavaDoc message;
49
50     /**
51      * The cause or previous exception of this exception.
52      */

53     private Throwable JavaDoc cause;
54
55     /**
56      * The source indicates the class and optionally the method from which
57      * this exception originated.
58      */

59     private Class JavaDoc exceptionSource;
60
61     /**
62      * The severity of this exception (how bad it was).
63      */

64     private SeverityEnum severity;
65
66
67
68     /**
69      * Default public constructor.
70      */

71     public DefaultExceptionDelegateImpl() {
72
73     }
74
75
76     /**
77      * This implementations handles the exception by logging its stack trace
78      * printout to the static logging service.
79      */

80     public void handleException() {
81
82         // The handle to Apache-commons logger
83
Log log = LogFactory.getLog(getExceptionSource());
84
85         if (this.severity == SeverityEnum.FATAL) {
86             if (log.isFatalEnabled()) {
87                 log.fatal(
88                     ExceptionUtility.printStackTracesToString(this.delegatee));
89             }
90         } else if (this.severity == SeverityEnum.ERROR) {
91             if (log.isErrorEnabled()) {
92                 log.error(
93                     ExceptionUtility.printStackTracesToString(this.delegatee));
94             }
95         } else if (this.severity == SeverityEnum.WARN) {
96             if (log.isWarnEnabled()) {
97                 log.warn(
98                     ExceptionUtility.printStackTracesToString(this.delegatee));
99             }
100         } else if (this.severity == SeverityEnum.INFO) {
101             if (log.isInfoEnabled()) {
102                 log.info(
103                 ExceptionUtility.printStackTracesToString(this.delegatee));
104             }
105         } else if (this.severity == SeverityEnum.DEBUG) {
106             if (log.isDebugEnabled()) {
107                 log.debug(
108                     ExceptionUtility.printStackTracesToString(this.delegatee));
109             }
110         } else {
111             // no severity provided will lead to a trace log
112
if (log.isTraceEnabled()) {
113                 log.trace(
114                     ExceptionUtility.printStackTracesToString(this.delegatee));
115             }
116         }
117     }
118
119     /**
120      * Gets the previousException
121      * @return String - previousException
122      */

123     public Throwable JavaDoc getCause() {
124         return this.cause;
125     }
126
127
128
129     /**
130      * Returns te severity of the underlying exception.
131      * @return int The severity of the underlying exception.
132      */

133     public SeverityEnum getSeverity() {
134         return this.severity;
135     }
136
137     /**
138      * Returns the producerId of the underlying exception.
139      * @return int The producerId of the underlying exception.
140      */

141     public Class JavaDoc getExceptionSource() {
142         return this.exceptionSource;
143     }
144
145     /**
146       * Returns the class name of the underlying exception.
147       * @return String The class name of the underlying exception.
148       */

149     public String JavaDoc getClassName() {
150         return this.delegatee.getClass().getName();
151     }
152
153
154
155     /**
156      * calls toString
157      * @return String - an error string based on exception's values
158      */

159     public String JavaDoc getMessage() {
160         return this.message;
161     }
162
163     /**
164      * Gets the deleagatee
165      * @return The exception that is using this delegate
166      */

167     public Throwable JavaDoc getDelegatee() {
168         return this.delegatee;
169     }
170
171     /**
172      * Sets the causal or previous exception of the current exception
173      * being handled by this delegate. This helps support the
174      * Exception Levelization pattern in which exceptions can be
175      * wrapped in other exceptions to create a common and consistent
176      * interface for a particular service as well as complete encapsulation
177      * of the implementation of that service.
178      *
179      * @param cause The causal or previous exception
180      */

181     public void setCause(Throwable JavaDoc cause) {
182         this.cause = cause;
183     }
184
185     /**
186      * Sets the throwable which is requesting the creation of this delegate.
187      * @param delegatee the delegatee this delegate is working for
188      */

189     public void setDelegatee(Throwable JavaDoc delegatee) {
190         this.delegatee = delegatee;
191     }
192
193     /**
194      * Sets the string explanation of this exception
195      * @param message The string explanation of this exception
196      */

197     public void setMessage(String JavaDoc message) {
198         this.message = message;
199     }
200
201     /**
202      * Sets the severity of the exception. This is used to determine
203      * how bad an exception is and is also used to integrate with the
204      * logging service during the default handling of an exception.
205      *
206      * @param severity the Severity of the exception
207      */

208     public void setSeverity(SeverityEnum severity) {
209         this.severity = severity;
210     }
211
212     /**
213      * This is the class and method from which this exception eminates.
214      * This is normally passed during exception creation in order to get
215      * the exact location of the failure. It is also used to integrate
216      * with logging service's filter functionality in order to
217      * pin-point problems in certain services by whatching
218      * the occurances in only one area of the system.
219      *
220      * @param exceptionSource the class and method of this exception
221      */

222     public void setExceptionSource(Class JavaDoc exceptionSource) {
223         this.exceptionSource = exceptionSource;
224     }
225
226
227     /**
228      * This method builds a recursive view of this exception and its
229      * message as well as its cause or previous exception and so on.
230      *
231      * @return the String view of the current exception
232      */

233     public String JavaDoc toString() {
234
235         StringBuffer JavaDoc buf =
236             new StringBuffer JavaDoc("EXCEPTION: ");
237
238         buf.append(this.delegatee.getClass().getName());
239         buf.append(",\n MESSAGE: " + this.message);
240
241         // if an embedded exception exists, add it to the message
242
if (this.cause != null) {
243             buf.append(";\n CAUSE: (");
244             buf.append(this.cause.toString() + ")");
245         }
246         //buf.append("\n");
247
//buf.append(ExceptionUtility.printStackTraceToString(this.delegatee));
248

249         return buf.toString();
250     }
251 }
Popular Tags