1 /***
2 * Licensed under the Artistic License; you may not use this file
3 * except in compliance with the License.
4 * You may obtain a copy of the License at
5 *
6 * http://displaytag.sourceforge.net/license.html
7 *
8 * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
9 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
10 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11 */
12 package org.displaytag.exception;
13
14 import javax.servlet.jsp.JspTagException;
15
16 import org.apache.commons.logging.Log;
17 import org.apache.commons.logging.LogFactory;
18 import org.displaytag.Messages;
19
20
21 /***
22 * Base exception: extendes JspTagException providing logging and exception nesting functionalities.
23 * @author Fabrizio Giustina
24 * @version $Revision: 1.14 $ ($Author: fgiust $)
25 */
26 public abstract class BaseNestableJspTagException extends JspTagException
27 {
28
29 /***
30 * Class where the exception has been generated.
31 */
32 private final Class sourceClass;
33
34 /***
35 * previous exception.
36 */
37 private Throwable nestedException;
38
39 /***
40 * Instantiate a new BaseNestableJspTagException.
41 * @param source Class where the exception is generated
42 * @param message message
43 */
44 public BaseNestableJspTagException(Class source, String message)
45 {
46 super(message);
47 this.sourceClass = source;
48
49
50 Log log = LogFactory.getLog(source);
51
52
53 if (getSeverity() == SeverityEnum.DEBUG)
54 {
55 log.debug(toString());
56 }
57 else if (getSeverity() == SeverityEnum.INFO)
58 {
59 log.info(toString());
60 }
61 else if (getSeverity() == SeverityEnum.WARN)
62 {
63 log.warn(toString());
64 }
65 else
66 {
67
68 log.error(toString());
69 }
70
71 }
72
73 /***
74 * Instantiate a new BaseNestableJspTagException.
75 * @param source Class where the exception is generated
76 * @param message message
77 * @param cause previous Exception
78 */
79 public BaseNestableJspTagException(Class source, String message, Throwable cause)
80 {
81 super(message);
82 this.sourceClass = source;
83 this.nestedException = cause;
84
85
86 Log log = LogFactory.getLog(source);
87
88
89 if (getSeverity() == SeverityEnum.DEBUG)
90 {
91 log.debug(toString(), cause);
92 }
93 else if (getSeverity() == SeverityEnum.INFO)
94 {
95 log.info(toString(), cause);
96 }
97 else if (getSeverity() == SeverityEnum.WARN)
98 {
99 log.warn(toString(), cause);
100 }
101 else
102 {
103
104 log.error(toString(), cause);
105 }
106
107 }
108
109 /***
110 * returns the previous exception.
111 * @return Throwable previous exception
112 */
113 public Throwable getCause()
114 {
115 return this.nestedException;
116 }
117
118 /***
119 * basic toString. Returns the message plus the previous exception (if a previous exception exists).
120 * @return String
121 */
122 public String toString()
123 {
124 String className = this.sourceClass.getName();
125 className = className.substring(className.lastIndexOf("."));
126
127 if (this.nestedException == null)
128 {
129 return Messages.getString("NestableException.msg",
130 new Object[]{className, getMessage()});
131 }
132
133 return Messages.getString("NestableException.msgcause",
134 new Object[]{className, getMessage(), this.nestedException.getMessage()});
135 }
136
137 /***
138 * subclasses need to define the getSeverity method to provide correct severity for logging.
139 * @return SeverityEnum exception severity
140 * @see org.displaytag.exception.SeverityEnum
141 */
142 public abstract SeverityEnum getSeverity();
143
144 }