001: /*
002: * Copyright 2000-2001,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.wsrp4j.exception;
018:
019: /**
020: * Common Exception class within the WSRP environment
021: */
022: public class WSRPException extends Exception {
023:
024: /*
025: Holds the message identifier - enables a more comfortable error handling
026: */
027: private int errCode = 0;
028:
029: private Throwable nestedThrowable = null;
030:
031: // exception is in the common range
032: private static final int COMMON_EXCEPTION = 1;
033:
034: // exception is in the consumer range
035: private static final int CONSUMER_EXCEPTION = 2;
036:
037: // exception is in the producer range
038: private static final int PRODUCER_EXCEPTION = 4;
039:
040: // exception is in the provider range
041: private static final int PROVIDER_EXCEPTION = 8;
042:
043: // type (range) of the exception
044: private int exceptionRange = 0;
045:
046: /*
047: Creates a new common exception
048: @deprecated
049: */
050: public WSRPException() {
051: this (0, null);
052: }
053:
054: /**
055: Creates a new common excpetion. The message to be passed will be ignored
056: @param errorCode integer representing an error code
057: */
058: public WSRPException(int errorCode) {
059: this (errorCode, null);
060: }
061:
062: /**
063: Creates a new common excpetion. The message to be passed will be ignored
064: @param errorCode integer representing an error code
065: @param t Throwable to be wrapped
066: */
067: public WSRPException(int errorCode, Throwable t) {
068: //String message = Messages.get(errorCode);
069: super (Messages.get(errorCode));
070: errCode = errorCode;
071: nestedThrowable = t;
072: }
073:
074: /**
075: Returns an error code
076: @return integer representing an error code
077: */
078: public int getErrorCode() {
079: return errCode;
080: }
081:
082: /**
083: Returns a nested Throwable
084: @return Throwable if a nested Throwable exists or null
085: */
086: public Throwable getNestedThrowable() {
087: return nestedThrowable;
088: }
089:
090: /**
091: * Returns the exception as String
092: */
093: public String toString() {
094: StringBuffer s = new StringBuffer(this .getClass().getName());
095: s.append(": ");
096: s.append(getMessage());
097: if (nestedThrowable != null) {
098: s.append("\n\nNested Throwable is:\n");
099: s.append(nestedThrowable.toString());
100: }
101: return s.toString();
102: }
103:
104: /**
105: * Returns the Exception in the HTML string format. Nested
106: * exceptions are included in the report.
107: *
108: * @returns exception message formatted as HTML string
109: */
110: public String toHTMLString() {
111:
112: StringBuffer s = new StringBuffer();
113:
114: s.append("<H2>Exception occured!</H2><br>");
115: s.append("<b>" + this .getClass().getName() + "</><br>");
116: s.append(" Message = " + getMessage() + "<br>");
117: s.append(" Type = " + getExceptionRange() + "<br>");
118:
119: if (this .nestedThrowable != null) {
120:
121: Throwable t = nestedThrowable;
122: s.append("<H3>Exception stack:</H3>");
123:
124: while (t != null) {
125: s
126: .append("<br><b>" + t.getClass().getName()
127: + "</><br>");
128: if (t instanceof WSRPException) {
129: s
130: .append(" Message = "
131: + ((WSRPException) t).getMessage()
132: + "<br>");
133: s.append(" Type = "
134: + ((WSRPException) t).getExceptionRange()
135: + "<br>");
136: t = ((WSRPException) t).getNestedThrowable();
137: } else {
138: s.append(" Message = " + t.getMessage() + "<br>");
139: t = null;
140: }
141: }
142: }
143:
144: return s.toString();
145: }
146:
147: /**
148: * Returns the range identifier of this exception. E.g.
149: * Common, Consumer, Producer or Provider range.
150: *
151: * @return the exception range id
152: */
153: public int getExceptionRange() {
154:
155: return exceptionRange;
156: }
157:
158: /**
159: * Assign the exception to the common range
160: */
161: public void setCommonExceptionRange() {
162:
163: exceptionRange = COMMON_EXCEPTION;
164: }
165:
166: /**
167: * Assign the exception to the consumer range
168: */
169: public void setConsumerExceptionRange() {
170:
171: exceptionRange = CONSUMER_EXCEPTION;
172: }
173:
174: /**
175: * Assign the exception to the producer range
176: */
177: public void setProducerExceptionRange() {
178:
179: exceptionRange = PRODUCER_EXCEPTION;
180: }
181:
182: /**
183: * Assign the exception to the provider range
184: */
185: public void setProviderExceptionRange() {
186:
187: exceptionRange = PROVIDER_EXCEPTION;
188: }
189: }
|