KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > spi > ProviderException


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
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 package scriptella.spi;
17
18 import scriptella.core.SystemException;
19
20 import java.util.LinkedHashSet JavaDoc;
21 import java.util.Set JavaDoc;
22
23
24 /**
25  * Thrown by connection provider to indicate any kind of failure.
26  * <p>Service Providers must provide subclasses of this exception.
27  *
28  * @author Fyodor Kupolov
29  * @version 1.0
30  */

31 public abstract class ProviderException extends SystemException {
32     private Set JavaDoc<String JavaDoc> errorCodes = new LinkedHashSet JavaDoc<String JavaDoc>();
33     private String JavaDoc errorStatement;
34
35     public ProviderException() {
36     }
37
38     public ProviderException(String JavaDoc message) {
39         super(message);
40     }
41
42     public ProviderException(String JavaDoc message, Throwable JavaDoc cause) {
43         super(message, cause);
44     }
45
46     public ProviderException(Throwable JavaDoc cause) {
47         super(cause);
48     }
49
50     /**
51      * Returns error codes attached to this exception. For example
52      * JDBC drivers reports 2 error codes: SQLSTATE and vendor code.
53      * <p>Do not use &quot;<b>, . ;</b>&quot; in error codes.
54      *
55      * @return set of error codes.
56      */

57     public Set JavaDoc<String JavaDoc> getErrorCodes() {
58         return errorCodes;
59     }
60
61     /**
62      * Adds error code to this exception.
63      *
64      * @param errorCode vendor specific error code.
65      * @return this exception for convenience.
66      */

67     public ProviderException addErrorCode(String JavaDoc errorCode) {
68         errorCodes.add(errorCode);
69         return this;
70     }
71
72     /**
73      * Sets problem statement which caused this exception/
74      *
75      * @param errStmt statement text.
76      * @return this exception for convenience.
77      */

78     protected ProviderException setErrorStatement(String JavaDoc errStmt) {
79         this.errorStatement = errStmt;
80         return this;
81     }
82
83     /**
84      * This method should be overriden by providers relying on external APIs to work with connections.
85      * Used only for informative error reporting.
86      * <p>Examples: SQL Exceptions, LDAP connection exceptions etc.
87      *
88      * @return external API throwable wich may be important for user to recognize the problem.
89      */

90     public Throwable JavaDoc getNativeException() {
91         return getCause();
92     }
93
94     /**
95      * Returns a statement for this error if any.
96      *
97      * @return statement text and additional data.
98      */

99     public String JavaDoc getErrorStatement() {
100         return errorStatement;
101     }
102
103     /**
104      * Overriden by subclasses to provide user friendly provider name.
105      *
106      * @return provider name.
107      */

108     public abstract String JavaDoc getProviderName();
109
110     public String JavaDoc toString() {
111         StringBuilder JavaDoc res = new StringBuilder JavaDoc(super.toString());
112         String JavaDoc es = getErrorStatement();
113         if (es != null) {
114             res.append(". Error statement: ").append(es);
115         }
116         Set JavaDoc<String JavaDoc> codes = getErrorCodes();
117         if (codes != null && !codes.isEmpty()) {
118             res.append(". Error codes: ").append(codes);
119         }
120         return res.toString();
121
122     }
123
124
125 }
126
Popular Tags