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 org.apache.commons.logging.Log;
15 import org.apache.commons.logging.LogFactory;
16 import org.displaytag.Messages;
17
18
19 /**
20 * Base runtime exception: extendes RuntimeException providing logging and exception nesting functionalities.
21 * @author Fabrizio Giustina
22 * @version $Revision: 1081 $ ($Author: fgiust $)
23 */
24 public abstract class BaseNestableRuntimeException extends RuntimeException
25 {
26
27 /**
28 * Class where the exception has been generated.
29 */
30 private final Class sourceClass;
31
32 /**
33 * previous exception.
34 */
35 private Throwable nestedException;
36
37 /**
38 * Instantiate a new BaseNestableRuntimeException.
39 * @param source Class where the exception is generated
40 * @param message message
41 */
42 public BaseNestableRuntimeException(Class source, String message)
43 {
44 super(message);
45 this.sourceClass = source;
46
47 // log exception
48 Log log = LogFactory.getLog(source);
49
50 // choose appropriate logging method
51 if (getSeverity() == SeverityEnum.DEBUG)
52 {
53 log.debug(toString());
54 }
55 else if (getSeverity() == SeverityEnum.INFO)
56 {
57 log.info(toString());
58 }
59 else if (getSeverity() == SeverityEnum.WARN)
60 {
61 log.warn(toString());
62 }
63 else
64 {
65 // error - default
66 log.error(toString());
67 }
68
69 }
70
71 /**
72 * Instantiate a new BaseNestableRuntimeException.
73 * @param source Class where the exception is generated
74 * @param message message
75 * @param cause previous Exception
76 */
77 public BaseNestableRuntimeException(Class source, String message, Throwable cause)
78 {
79 super(message);
80 this.sourceClass = source;
81 this.nestedException = cause;
82
83 // log exception
84 Log log = LogFactory.getLog(source);
85
86 // choose appropriate logging method
87 if (getSeverity() == SeverityEnum.DEBUG)
88 {
89 log.debug(toString(), cause);
90 }
91 else if (getSeverity() == SeverityEnum.INFO)
92 {
93 log.info(toString(), cause);
94 }
95 else if (getSeverity() == SeverityEnum.WARN)
96 {
97 log.warn(toString(), cause);
98 }
99 else
100 {
101 // error - default
102 log.error(toString(), cause);
103 }
104
105 }
106
107 /**
108 * returns the previous exception.
109 * @return Throwable previous exception
110 */
111 public Throwable getCause()
112 {
113 return this.nestedException;
114 }
115
116 /**
117 * basic toString. Returns the message plus the previous exception (if a previous exception exists).
118 * @return String
119 */
120 public String toString()
121 {
122 String className = this.sourceClass.getName();
123 className = className.substring(className.lastIndexOf(".")); //$NON-NLS-1$
124
125 if (this.nestedException == null)
126 {
127 return Messages.getString("NestableException.msg", //$NON-NLS-1$
128 new Object[]{className, getMessage()});
129 }
130
131 return Messages.getString("NestableException.msgcause", //$NON-NLS-1$
132 new Object[]{className, getMessage(), this.nestedException.getMessage()});
133 }
134
135 /**
136 * subclasses need to define the getSeverity method to provide correct severity for logging.
137 * @return SeverityEnum exception severity
138 * @see org.displaytag.exception.SeverityEnum
139 */
140 public abstract SeverityEnum getSeverity();
141
142 }