KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > transaction > file > ResourceManagerException


1 /*
2  * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//transaction/src/java/org/apache/commons/transaction/file/ResourceManagerException.java,v 1.1 2004/11/18 23:27:19 ozeigermann Exp $
3 <<<<<<< .mine
4  * $Revision: 1.1 $
5  * $Date: 2005-02-26 14:16:14 +0100 (Sa, 26 Feb 2005) $
6 =======
7  * $Revision$
8  * $Date: 2005-02-26 14:16:14 +0100 (Sa, 26 Feb 2005) $
9 >>>>>>> .r168169
10  *
11  * ====================================================================
12  *
13  * Copyright 1999-2002 The Apache Software Foundation
14  *
15  * Licensed under the Apache License, Version 2.0 (the "License");
16  * you may not use this file except in compliance with the License.
17  * You may obtain a copy of the License at
18  *
19  * http://www.apache.org/licenses/LICENSE-2.0
20  *
21  * Unless required by applicable law or agreed to in writing, software
22  * distributed under the License is distributed on an "AS IS" BASIS,
23  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24  * See the License for the specific language governing permissions and
25  * limitations under the License.
26  *
27  */

28
29 package org.apache.commons.transaction.file;
30
31 import java.io.PrintWriter JavaDoc;
32 import java.io.StringWriter JavaDoc;
33
34 /**
35  * Signals any kind of error or failure state in a {@link ResourceManager}.
36  *
37  * @version $Revision$
38  *
39  */

40 public class ResourceManagerException extends Exception JavaDoc implements ResourceManagerErrorCodes {
41
42     private static final int[] ERROR_CODES =
43         {
44             ERR_SYSTEM,
45             ERR_SYSTEM_INCONSISTENT,
46             ERR_NO_TX,
47             ERR_TXID_INVALID,
48             ERR_TX_INACTIVE,
49             ERR_TX_INCONSISTENT,
50             ERR_DUP_TX,
51             ERR_THREAD_INVALID,
52             ERR_ISOLATION_LEVEL_UNSUPPORTED,
53             ERR_RESOURCEID_INVALID,
54             ERR_RESOURCE_EXISTS,
55             ERR_NO_SUCH_RESOURCE,
56             ERR_LOCK,
57             ERR_NO_LOCK,
58             ERR_MARKED_FOR_ROLLBACK,
59             };
60
61     private static final String JavaDoc[] ERROR_CODE_STRINGS =
62         {
63             "ERR_SYSTEM",
64             "ERR_SYSTEM_INCONSISTENT",
65             "ERR_NO_TX",
66             "ERR_TXID_INVALID",
67             "ERR_TX_INACTIVE",
68             "ERR_TX_INCONSISTENT",
69             "ERR_DUP_TX",
70             "ERR_THREAD_INVALID",
71             "ERR_ISOLATION_LEVEL_UNSUPPORTED",
72             "ERR_RESOURCEID_INVALID",
73             "ERR_RESOURCE_EXISTS",
74             "ERR_NO_SUCH_RESOURCE",
75             "ERR_LOCK",
76             "ERR_NO_LOCK",
77             "ERR_MARKED_FOR_ROLLBACK",
78             };
79
80     private static final String JavaDoc[] ERROR_CODE_TEXTS =
81         {
82             "System error",
83             "Inconsistent system data",
84             "Unknown transaction",
85             "Invalid transaction id",
86             "Transaction inactive",
87             "Inconsistent transaction data",
88             "Duplicate transaction id",
89             "Thread of control is the one that not start tx",
90             "Isolation level unsupported",
91             "Specified resource id is invalid",
92             "Resource already exists",
93             "No such resource",
94             "Locking error",
95             "Could not acquire lock",
96             "Transaction already marked for rollback" };
97
98     public static final String JavaDoc ERR_UNKNOWN_TEXT = "Unknown error";
99     public static final String JavaDoc ERR_UNKNOWN_CODE = "ERR_UNKNOWN";
100
101     protected final int status;
102     protected final Object JavaDoc txId;
103
104     protected static final String JavaDoc composeMessage(String JavaDoc msg, int status, Object JavaDoc txId, Throwable JavaDoc cause) {
105         String JavaDoc message = composeMessage(msg, status, txId);
106         StringBuffer JavaDoc messageBuffer = new StringBuffer JavaDoc(message);
107         messageBuffer.append("\nCaused by: ");
108         StringWriter JavaDoc sw = new StringWriter JavaDoc();
109         cause.printStackTrace(new PrintWriter JavaDoc(sw));
110         messageBuffer.append(sw.getBuffer());
111         return messageBuffer.toString();
112     }
113     
114     protected static final String JavaDoc composeMessage(String JavaDoc msg, int status, Object JavaDoc txId) {
115         StringBuffer JavaDoc composed = new StringBuffer JavaDoc();
116         if (txId != null) {
117             composed.append(txId).append(": ");
118         }
119         if (msg != null) {
120             composed.append(msg);
121             if (status != -1) {
122                 composed.append(" (").append(statusToCode(status)).append(')');
123             }
124         } else if (status != -1) {
125             composed.append(statusToText(status));
126         }
127
128         return composed.toString();
129     }
130
131     public static final String JavaDoc statusToText(int status) {
132         if (status == ERR_UNKNOWN) {
133             return ERR_UNKNOWN_TEXT;
134         } else {
135             int pos = -1;
136             for (int i = 0; i < ERROR_CODES.length; i++) {
137                 int code = ERROR_CODES[i];
138                 if (status == code) {
139                     pos = i;
140                     break;
141                 }
142             }
143             if (pos == -1) {
144                 return ERR_UNKNOWN_TEXT + ", code: " + status;
145             } else {
146                 return ERROR_CODE_TEXTS[pos];
147             }
148         }
149     }
150
151     public static final String JavaDoc statusToCode(int status) {
152         if (status == ERR_UNKNOWN) {
153             return ERR_UNKNOWN_CODE;
154         } else {
155             int pos = -1;
156             for (int i = 0; i < ERROR_CODES.length; i++) {
157                 int code = ERROR_CODES[i];
158                 if (status == code) {
159                     pos = i;
160                     break;
161                 }
162             }
163             if (pos == -1) {
164                 return ERR_UNKNOWN_CODE + ": " + status;
165             } else {
166                 return ERROR_CODE_STRINGS[pos];
167             }
168         }
169     }
170
171     public ResourceManagerException(String JavaDoc message, int status, Object JavaDoc txId) {
172         super(ResourceManagerException.composeMessage(message, status, txId));
173         this.status = status;
174         this.txId = txId;
175     }
176
177     public ResourceManagerException(int status, Object JavaDoc txId) {
178         this(null, status, txId);
179     }
180
181     public ResourceManagerException(String JavaDoc message) {
182         super(message);
183         this.status = ERR_UNKNOWN;
184         this.txId = null;
185     }
186
187     public ResourceManagerException(String JavaDoc message, int status, Object JavaDoc txId, Throwable JavaDoc cause) {
188         // XXX can not do this, as 1.3 Throwable does not allow cause in ctor :(
189
// super(ResourceManagerException.composeMessage(message, status, txId), cause);
190
// for now format cause by ourselves
191
super(ResourceManagerException.composeMessage(message, status, txId, cause));
192         this.status = status;
193         this.txId = txId;
194     }
195
196     public ResourceManagerException(String JavaDoc message, int status, Throwable JavaDoc cause) {
197         this(message, status, null, cause);
198     }
199
200     public ResourceManagerException(String JavaDoc message, Throwable JavaDoc cause) {
201         this(message, ERR_UNKNOWN, cause);
202     }
203
204     public ResourceManagerException(int status, Object JavaDoc txId, Throwable JavaDoc cause) {
205         this(null, status, txId, cause);
206     }
207
208     public String JavaDoc statusToString() {
209         return ResourceManagerException.statusToText(status);
210     }
211
212     public int getStatus() {
213         return status;
214     }
215
216 }
217
Popular Tags