1 28 29 package org.apache.commons.transaction.file; 30 31 import java.io.PrintWriter ; 32 import java.io.StringWriter ; 33 34 40 public class ResourceManagerException extends Exception 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 [] 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 [] 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 ERR_UNKNOWN_TEXT = "Unknown error"; 99 public static final String ERR_UNKNOWN_CODE = "ERR_UNKNOWN"; 100 101 protected final int status; 102 protected final Object txId; 103 104 protected static final String composeMessage(String msg, int status, Object txId, Throwable cause) { 105 String message = composeMessage(msg, status, txId); 106 StringBuffer messageBuffer = new StringBuffer (message); 107 messageBuffer.append("\nCaused by: "); 108 StringWriter sw = new StringWriter (); 109 cause.printStackTrace(new PrintWriter (sw)); 110 messageBuffer.append(sw.getBuffer()); 111 return messageBuffer.toString(); 112 } 113 114 protected static final String composeMessage(String msg, int status, Object txId) { 115 StringBuffer composed = new StringBuffer (); 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 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 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 message, int status, Object txId) { 172 super(ResourceManagerException.composeMessage(message, status, txId)); 173 this.status = status; 174 this.txId = txId; 175 } 176 177 public ResourceManagerException(int status, Object txId) { 178 this(null, status, txId); 179 } 180 181 public ResourceManagerException(String message) { 182 super(message); 183 this.status = ERR_UNKNOWN; 184 this.txId = null; 185 } 186 187 public ResourceManagerException(String message, int status, Object txId, Throwable cause) { 188 super(ResourceManagerException.composeMessage(message, status, txId, cause)); 192 this.status = status; 193 this.txId = txId; 194 } 195 196 public ResourceManagerException(String message, int status, Throwable cause) { 197 this(message, status, null, cause); 198 } 199 200 public ResourceManagerException(String message, Throwable cause) { 201 this(message, ERR_UNKNOWN, cause); 202 } 203 204 public ResourceManagerException(int status, Object txId, Throwable cause) { 205 this(null, status, txId, cause); 206 } 207 208 public String statusToString() { 209 return ResourceManagerException.statusToText(status); 210 } 211 212 public int getStatus() { 213 return status; 214 } 215 216 } 217 | Popular Tags |