001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.transaction.file;
018:
019: import java.io.PrintWriter;
020: import java.io.StringWriter;
021:
022: /**
023: * Signals any kind of error or failure state in a {@link ResourceManager}.
024: *
025: * @version $Id: ResourceManagerException.java 493628 2007-01-07 01:42:48Z joerg $
026: */
027: public class ResourceManagerException extends Exception implements
028: ResourceManagerErrorCodes {
029:
030: private static final int[] ERROR_CODES = { ERR_SYSTEM,
031: ERR_SYSTEM_INCONSISTENT, ERR_NO_TX, ERR_TXID_INVALID,
032: ERR_TX_INACTIVE, ERR_TX_INCONSISTENT, ERR_DUP_TX,
033: ERR_THREAD_INVALID, ERR_ISOLATION_LEVEL_UNSUPPORTED,
034: ERR_RESOURCEID_INVALID, ERR_RESOURCE_EXISTS,
035: ERR_NO_SUCH_RESOURCE, ERR_LOCK, ERR_NO_LOCK,
036: ERR_MARKED_FOR_ROLLBACK, };
037:
038: private static final String[] ERROR_CODE_STRINGS = { "ERR_SYSTEM",
039: "ERR_SYSTEM_INCONSISTENT", "ERR_NO_TX", "ERR_TXID_INVALID",
040: "ERR_TX_INACTIVE", "ERR_TX_INCONSISTENT", "ERR_DUP_TX",
041: "ERR_THREAD_INVALID", "ERR_ISOLATION_LEVEL_UNSUPPORTED",
042: "ERR_RESOURCEID_INVALID", "ERR_RESOURCE_EXISTS",
043: "ERR_NO_SUCH_RESOURCE", "ERR_LOCK", "ERR_NO_LOCK",
044: "ERR_MARKED_FOR_ROLLBACK", };
045:
046: private static final String[] ERROR_CODE_TEXTS = { "System error",
047: "Inconsistent system data", "Unknown transaction",
048: "Invalid transaction id", "Transaction inactive",
049: "Inconsistent transaction data",
050: "Duplicate transaction id",
051: "Thread of control is the one that not start tx",
052: "Isolation level unsupported",
053: "Specified resource id is invalid",
054: "Resource already exists", "No such resource",
055: "Locking error", "Could not acquire lock",
056: "Transaction already marked for rollback" };
057:
058: public static final String ERR_UNKNOWN_TEXT = "Unknown error";
059: public static final String ERR_UNKNOWN_CODE = "ERR_UNKNOWN";
060:
061: protected final int status;
062: protected final Object txId;
063:
064: protected static final String composeMessage(String msg,
065: int status, Object txId, Throwable cause) {
066: String message = composeMessage(msg, status, txId);
067: StringBuffer messageBuffer = new StringBuffer(message);
068: messageBuffer.append("\nCaused by: ");
069: StringWriter sw = new StringWriter();
070: cause.printStackTrace(new PrintWriter(sw));
071: messageBuffer.append(sw.getBuffer());
072: return messageBuffer.toString();
073: }
074:
075: protected static final String composeMessage(String msg,
076: int status, Object txId) {
077: StringBuffer composed = new StringBuffer();
078: if (txId != null) {
079: composed.append(txId).append(": ");
080: }
081: if (msg != null) {
082: composed.append(msg);
083: if (status != -1) {
084: composed.append(" (").append(statusToCode(status))
085: .append(')');
086: }
087: } else if (status != -1) {
088: composed.append(statusToText(status));
089: }
090:
091: return composed.toString();
092: }
093:
094: public static final String statusToText(int status) {
095: if (status == ERR_UNKNOWN) {
096: return ERR_UNKNOWN_TEXT;
097: } else {
098: int pos = -1;
099: for (int i = 0; i < ERROR_CODES.length; i++) {
100: int code = ERROR_CODES[i];
101: if (status == code) {
102: pos = i;
103: break;
104: }
105: }
106: if (pos == -1) {
107: return ERR_UNKNOWN_TEXT + ", code: " + status;
108: } else {
109: return ERROR_CODE_TEXTS[pos];
110: }
111: }
112: }
113:
114: public static final String statusToCode(int status) {
115: if (status == ERR_UNKNOWN) {
116: return ERR_UNKNOWN_CODE;
117: } else {
118: int pos = -1;
119: for (int i = 0; i < ERROR_CODES.length; i++) {
120: int code = ERROR_CODES[i];
121: if (status == code) {
122: pos = i;
123: break;
124: }
125: }
126: if (pos == -1) {
127: return ERR_UNKNOWN_CODE + ": " + status;
128: } else {
129: return ERROR_CODE_STRINGS[pos];
130: }
131: }
132: }
133:
134: public ResourceManagerException(String message, int status,
135: Object txId) {
136: super (ResourceManagerException.composeMessage(message, status,
137: txId));
138: this .status = status;
139: this .txId = txId;
140: }
141:
142: public ResourceManagerException(int status, Object txId) {
143: this (null, status, txId);
144: }
145:
146: public ResourceManagerException(String message) {
147: super (message);
148: this .status = ERR_UNKNOWN;
149: this .txId = null;
150: }
151:
152: public ResourceManagerException(String message, int status,
153: Object txId, Throwable cause) {
154: // XXX can not do this, as 1.3 Throwable does not allow cause in ctor :(
155: // super(ResourceManagerException.composeMessage(message, status, txId), cause);
156: // for now format cause by ourselves
157: super (ResourceManagerException.composeMessage(message, status,
158: txId, cause));
159: this .status = status;
160: this .txId = txId;
161: }
162:
163: public ResourceManagerException(String message, int status,
164: Throwable cause) {
165: this (message, status, null, cause);
166: }
167:
168: public ResourceManagerException(String message, Throwable cause) {
169: this (message, ERR_UNKNOWN, cause);
170: }
171:
172: public ResourceManagerException(int status, Object txId,
173: Throwable cause) {
174: this (null, status, txId, cause);
175: }
176:
177: public String statusToString() {
178: return ResourceManagerException.statusToText(status);
179: }
180:
181: public int getStatus() {
182: return status;
183: }
184:
185: }
|