001: /* ServletException.java
002:
003: {{IS_NOTE
004: Purpose:
005: Description:
006: History:
007: 2001/11/13 20:17:38, Create, Tom M. Yeh.
008: }}IS_NOTE
009:
010: Copyright (C) 2001 Potix Corporation. All Rights Reserved.
011:
012: {{IS_RIGHT
013: This program is distributed under GPL Version 2.0 in the hope that
014: it will be useful, but WITHOUT ANY WARRANTY.
015: }}IS_RIGHT
016: */
017: package org.zkoss.web.servlet;
018:
019: import org.zkoss.lang.Expectable;
020: import org.zkoss.lang.Exceptions;
021: import org.zkoss.mesg.Messageable;
022:
023: /**
024: * Potix version of ServletException. It extends
025: * javax.servlet.ServletException to provide error codes and other features.
026: *
027: * @author tomyeh
028: */
029: public class ServletException extends javax.servlet.ServletException
030: implements Messageable {
031: /** Utilities.
032: *
033: * <p>The reason to use a class to hold static utilities is we can
034: * override the method's return type later.
035: */
036: public static class Aide {
037: /** Converts an exception to ServletException or OperationException
038: * depending on whether t implements Expetable.
039: * @see Exceptions#wrap
040: */
041: public static javax.servlet.ServletException wrap(Throwable t) {
042: t = Exceptions.unwrap(t);
043: if (t instanceof javax.servlet.ServletException)
044: return (javax.servlet.ServletException) t;
045: if (t instanceof Expectable)
046: return (OperationException) Exceptions.wrap(t,
047: OperationException.class);
048: return (ServletException) Exceptions.wrap(t,
049: ServletException.class);
050: }
051:
052: /** Converts an exception to ServletException or OperationException
053: * depending on whether t implements Expetable.
054: * @see Exceptions#wrap
055: */
056: public static ServletException wrap(Throwable t, String msg) {
057: t = Exceptions.unwrap(t);
058: if (t instanceof Expectable)
059: return (OperationException) Exceptions.wrap(t,
060: OperationException.class, msg);
061: return (ServletException) Exceptions.wrap(t,
062: ServletException.class, msg);
063: }
064:
065: /** Converts an exception to ServletException or OperationException
066: * depending on whether t implements Expetable.
067: * @see Exceptions#wrap
068: */
069: public static ServletException wrap(Throwable t, int code,
070: Object[] fmtArgs) {
071: t = Exceptions.unwrap(t);
072: if (t instanceof Expectable)
073: return (OperationException) Exceptions.wrap(t,
074: OperationException.class, code, fmtArgs);
075: return (ServletException) Exceptions.wrap(t,
076: ServletException.class, code, fmtArgs);
077: }
078:
079: /** Converts an exception to ServletException or OperationException
080: * depending on whether t implements Expetable.
081: * @see Exceptions#wrap
082: */
083: public static ServletException wrap(Throwable t, int code,
084: Object fmtArg) {
085: t = Exceptions.unwrap(t);
086: if (t instanceof Expectable)
087: return (OperationException) Exceptions.wrap(t,
088: OperationException.class, code, fmtArg);
089: return (ServletException) Exceptions.wrap(t,
090: ServletException.class, code, fmtArg);
091: }
092:
093: /** Converts an exception to ServletException or OperationException
094: * depending on whether t implements Expetable.
095: * @see Exceptions#wrap
096: */
097: public static ServletException wrap(Throwable t, int code) {
098: t = Exceptions.unwrap(t);
099: if (t instanceof Expectable)
100: return (OperationException) Exceptions.wrap(t,
101: OperationException.class, code);
102: return (ServletException) Exceptions.wrap(t,
103: ServletException.class, code);
104: }
105: }
106:
107: protected int _code = NULL_CODE;
108:
109: public ServletException(String msg, Throwable cause) {
110: super (msg, cause);
111: }
112:
113: public ServletException(String msg) {
114: super (msg);
115: }
116:
117: public ServletException(int code, Object[] fmtArgs, Throwable cause) {
118: super (Exceptions.getMessage(code, fmtArgs), cause);
119: _code = code;
120: }
121:
122: public ServletException(int code, Object fmtArg, Throwable cause) {
123: super (Exceptions.getMessage(code, fmtArg), cause);
124: _code = code;
125: }
126:
127: public ServletException(int code, Object[] fmtArgs) {
128: super (Exceptions.getMessage(code, fmtArgs));
129: _code = code;
130: }
131:
132: public ServletException(int code, Object fmtArg) {
133: super (Exceptions.getMessage(code, fmtArg));
134: _code = code;
135: }
136:
137: public ServletException(int code, Throwable cause) {
138: super (Exceptions.getMessage(code, null), cause);
139: _code = code;
140: }
141:
142: public ServletException(int code) {
143: super (Exceptions.getMessage(code, null));
144: _code = code;
145: }
146:
147: public ServletException(Throwable cause) {
148: super (cause);
149: }
150:
151: public ServletException() {
152: }
153:
154: //-- Messageable --//
155: public final int getCode() {
156: return _code;
157: }
158:
159: //-- override --//
160: private transient boolean _getCause = false;
161:
162: public Throwable getCause() {
163: //The case: ServletException implements getRootCause
164: //by returning getCause -> it causes a dead loop
165: if (_getCause) //recursive back?
166: return null;
167:
168: //The case: ServletException has implemented getCause (1.4-ware)
169: Throwable t = super .getCause();
170: if (t != null)
171: return t;
172:
173: try {
174: _getCause = true;
175: return getRootCause();
176: } finally {
177: _getCause = false;
178: }
179: }
180: }
|