1   /*
2    * Copyright 2000-2001,2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.wsrp4j.exception;
18  
19  /**
20   * Common Exception class within the WSRP environment 
21   */
22  public class WSRPException extends Exception {
23  
24      /*
25       Holds the message identifier - enables a  more comfortable error handling
26       */
27      private int errCode = 0;
28  
29      private Throwable nestedThrowable = null;
30  
31      // exception is in the common range
32      private static final int COMMON_EXCEPTION = 1;
33  
34      // exception is in the consumer range
35      private static final int CONSUMER_EXCEPTION = 2;
36  
37      // exception is in the producer range
38      private static final int PRODUCER_EXCEPTION = 4;
39  
40      // exception is in the provider range
41      private static final int PROVIDER_EXCEPTION = 8;
42  
43      // type (range) of the exception
44      private int exceptionRange = 0;
45  
46      /*
47       Creates a new common exception
48       @deprecated
49       */
50      public WSRPException() {
51          this(0, null);
52      }
53  
54      /**
55       Creates a new common excpetion. The message to be passed will be ignored
56       @param errorCode integer representing an error code
57       */
58      public WSRPException(int errorCode) {
59          this(errorCode, null);
60      }
61  
62      /**
63       Creates a new common excpetion. The message to be passed will be ignored
64       @param errorCode integer representing an error code
65       @param t Throwable to be wrapped
66       */
67      public WSRPException(int errorCode, Throwable t) {
68          //String message = Messages.get(errorCode);
69          super(Messages.get(errorCode));
70          errCode = errorCode;
71          nestedThrowable = t;
72      }
73  
74      /**
75       Returns an error code
76       @return integer representing an error code
77       */
78      public int getErrorCode() {
79          return errCode;
80      }
81  
82      /**
83       Returns a nested Throwable
84       @return Throwable if a nested Throwable exists or null
85       */
86      public Throwable getNestedThrowable() {
87          return nestedThrowable;
88      }
89  
90      /**
91       * Returns  the exception as String
92       */
93      public String toString() {
94          StringBuffer s = new StringBuffer(this.getClass().getName());
95          s.append(": ");
96          s.append(getMessage());
97          if (nestedThrowable != null) {
98              s.append("\n\nNested Throwable is:\n");
99              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.append("<br><b>" + t.getClass().getName() + "</><br>");
126                 if (t instanceof WSRPException) {
127                     s.append("   Message = " + ((WSRPException) t).getMessage()
128                             + "<br>");
129                     s.append("   Type    = "
130                             + ((WSRPException) t).getExceptionRange() + "<br>");
131                     t = ((WSRPException) t).getNestedThrowable();
132                 }
133                 else {
134                     s.append("   Message = " + t.getMessage() + "<br>");
135                     t = null;
136                 }
137             }
138         }
139 
140         return s.toString();
141     }
142 
143     /**
144      * Returns the range identifier of this exception. E.g.
145      * Common, Consumer, Producer or Provider range. 
146      * 
147      * @return the exception range id
148      */
149     public int getExceptionRange() {
150 
151         return exceptionRange;
152     }
153 
154     /**
155      * Assign the exception to the common range
156      */
157     public void setCommonExceptionRange() {
158 
159         exceptionRange = COMMON_EXCEPTION;
160     }
161 
162     /**
163      * Assign the exception to the consumer range
164      */
165     public void setConsumerExceptionRange() {
166 
167         exceptionRange = CONSUMER_EXCEPTION;
168     }
169 
170     /**
171      * Assign the exception to the producer range
172      */
173     public void setProducerExceptionRange() {
174 
175         exceptionRange = PRODUCER_EXCEPTION;
176     }
177 
178     /**
179      * Assign the exception to the provider range
180      */
181     public void setProviderExceptionRange() {
182 
183         exceptionRange = PROVIDER_EXCEPTION;
184     }
185 }