001: /* Copyright 2001-2004 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005:
006: package org.jasig.portal;
007:
008: import java.util.Date;
009:
010: import org.apache.commons.logging.Log;
011: import org.apache.commons.logging.LogFactory;
012:
013: /**
014: * Base portal exception class.
015: * Information contained in this class allows ErrorChannel
016: * to handle errors gracefully.
017: * This class also reports itself to the ProblemsTable whenever it is instantiated.
018: * The Problems servlet displays recently reported PortalExceptions.
019: * @author Peter Kharchenko
020: * @version $Revision: 36782 $
021: */
022: public class PortalException extends RuntimeException {
023:
024: private final Log log = LogFactory.getLog(PortalException.class);
025:
026: /**
027: * should the user be given an option to reinstantiate
028: * the channel in a given session
029: */
030: boolean reinstantiable = true;
031:
032: /**
033: * should the user be given an option to retry rendering
034: * that same channel instance
035: */
036: boolean refreshable = true;
037:
038: /**
039: * True if logging is pending on this exception instance
040: * (has not yet been logged but potentially will be). True
041: * if all the logging that ought to happen has happened.
042: */
043: boolean logPending = true;
044:
045: /**
046: * ErrorID categorizing this PortalException instance.
047: */
048: ErrorID errorID = Errors.legacy;
049:
050: /**
051: * Parameter to the ErrorID's template message.
052: */
053: String parameter = null;
054:
055: /**
056: * The time at which this PortalException instance was instantiated.
057: */
058: Date timestamp = new Date();
059:
060: /**
061: * Instantiate a generic PortalException.
062: * Instantiating a bare, no-message, no ErrorID, no frills
063: * PortalException is pretty anti-social. Wouldn't you rather
064: * use a constructor that provides more information?
065: */
066: public PortalException() {
067: ProblemsTable.store(this );
068: }
069:
070: /**
071: * Construct a new portal exception, recording an
072: * underlying cause.
073: *
074: * @param cause a <code>Throwable</code> causing this exception
075: */
076: public PortalException(Throwable cause) {
077: super (cause);
078: ProblemsTable.store(this );
079: }
080:
081: /**
082: * Creates a new <code>PortalException</code> instance,
083: * with a contained text message.
084: *
085: * @param msg describes exceptional condition
086: */
087: public PortalException(String msg) {
088: super (msg);
089: ProblemsTable.store(this );
090: }
091:
092: /**
093: * Instantiate a PortalException representing an instance of the
094: * type of error represented by the given ErrorID.
095: * @param errorid - type of error
096: */
097: public PortalException(ErrorID errorid) {
098: super (errorid.getMessage());
099: this .errorID = errorid;
100: ProblemsTable.store(this );
101: }
102:
103: /**
104: * Instantiate a PortalException with the given message and underlying cause.
105: * @param msg - message describing the error
106: * @param cause - underlying cause of the error
107: */
108: public PortalException(String msg, Throwable cause) {
109: super (msg, cause);
110: ProblemsTable.store(this );
111: }
112:
113: /**
114: * Instantiate a PortalException representing an instance of the type of error
115: * represented by the given ErrorID, with the given underlying cause.
116: * @param errorid - type of error
117: * @param cause - underlying cause of error.
118: */
119: public PortalException(ErrorID errorid, Throwable cause) {
120: super (errorid.getMessage(), cause);
121: this .errorID = errorid;
122: ProblemsTable.store(this );
123: }
124:
125: /**
126: * Check if user-mediated referesh is allowed.
127: * @return true if refresh allowed, false otherwise.
128: */
129: public boolean isRefreshable() {
130: return this .refreshable;
131: }
132:
133: /**
134: * Legacy support for old name of property accessor.
135: * @return isRefreshable()
136: * @deprecated use isRefreshable().
137: */
138: public boolean allowRefresh() {
139: return isRefreshable();
140: }
141:
142: /**
143: * Check if user-mediated reinstantiation is allowed.
144: * @return true if reinstantiation allowed, false otherwise
145: */
146: public boolean isReinstantiable() {
147: return this .reinstantiable;
148: }
149:
150: /**
151: * Legacy support for old name of property accessor
152: * @return isRinstantiable();
153: * @deprecated use isReinstantiable()
154: */
155: public boolean allowReinstantiation() {
156: return isReinstantiable();
157: }
158:
159: /**
160: * Retrieve an optionally recorded exception that
161: * caused the error.
162: * @return the cause if it is an Exception
163: * @deprecated - use Throwable.getCause()
164: */
165: public Exception getRecordedException() {
166: Throwable cause = this .getCause();
167: if (cause != null && cause instanceof Exception)
168: return (Exception) cause;
169: return null;
170: }
171:
172: /**
173: * Set if the user should be presented with an option
174: * to retry the same operation on the component that
175: * has generated the error.
176: *
177: * @param refresh a <code>boolean</code> value
178: */
179: public void setRefreshable(boolean refresh) {
180: this .refreshable = refresh;
181: }
182:
183: /**
184: * Set if the user should be presented with an option
185: * to reinstantiate the component (channel) that generated
186: * the error.
187: *
188: * @param reinstantiate a <code>boolean</code> value
189: */
190: public void setReinstantiable(boolean reinstantiate) {
191: this .reinstantiable = reinstantiate;
192: }
193:
194: /**
195: * Allows to record the exception that caused the error.
196: * The exception information can later be used in error
197: * reporting and user interaction.
198: *
199: * @param exc an <code>Exception</code> value
200: * @deprecated use initCause() instead.
201: */
202: public void setRecordedException(Exception exc) {
203: try {
204: this .initCause(exc);
205: } catch (Throwable t) {
206: // legacy implementation was setting a simple JavaBean property
207: // which could never throw an exception.
208: // we emulate that exceptionless behavior here.
209: if (log.isWarnEnabled())
210: log.warn(
211: "Exception setting the recorded exception of ["
212: + this + "] " + "to [" + exc + "]", t);
213: }
214:
215: }
216:
217: /**
218: * Determine whether logging is pending on this PortalException.
219: * @return <code>true</code> if the log is pending, otherwise <code>false</code>
220: */
221: public boolean isLogPending() {
222: return this .logPending;
223: }
224:
225: /**
226: * Set whether logging is pending on this PortalException.
227: * @param b true if logging is pending
228: */
229: public void setLogPending(boolean b) {
230: this .logPending = b;
231: }
232:
233: /**
234: * Get the ErrorID representing the type of this error.
235: * @return the error ID
236: */
237: public ErrorID getErrorID() {
238: return this .errorID;
239: }
240:
241: /**
242: * Set the ErrorID categorizing this PortalException.
243: * @param errorID the ErrorID categorizing this PortalException.
244: */
245: public void setErrorID(ErrorID errorID) {
246: this .errorID = errorID;
247: }
248:
249: /**
250: * Get the parameter to the ErrorID template message.
251: * @return the parameter
252: */
253: public String getParameter() {
254: return this .parameter;
255: }
256:
257: /**
258: * Set the parameter to the ErrorID template message.
259: * @param string - parameter to ErrorID template message.
260: */
261: public void setParameter(String string) {
262: this .parameter = string;
263: }
264:
265: /**
266: * Instantiate a PortalException with the given message and refresh,
267: * reinstantiate state.
268: * @param msg - message describing the problem
269: * @param refresh - whether refresh is appropriate response
270: * @param reinstantiate - whether reinstantiate is appropriate response
271: */
272: public PortalException(String msg, boolean refresh,
273: boolean reinstantiate) {
274: super (msg);
275: this .setReinstantiable(reinstantiate);
276: this .setRefreshable(refresh);
277: ProblemsTable.store(this );
278: }
279:
280: /**
281: * Instantiate a PortalException with the given message, underlying cause,
282: * refresh, and reinstantiate state.
283: * @param msg - message describing the problem
284: * @param cause - underlying cause of problem
285: * @param refresh - true if refresh is an appropriate response
286: * @param reinstantiate - true if reinstantiate is an appropriate response
287: */
288: public PortalException(String msg, Throwable cause,
289: boolean refresh, boolean reinstantiate) {
290: super (msg, cause);
291: this .setReinstantiable(reinstantiate);
292: this .setRefreshable(refresh);
293: ProblemsTable.store(this );
294: }
295:
296: /**
297: * Get the Date at which this PortalException instance was instantiated.
298: * @return Returns the timestamp.
299: */
300: public Date getTimestamp() {
301: return this.timestamp;
302: }
303:
304: }
|