1 49 package org.exolab.jms.tranlog; 50 51 import java.io.Externalizable ; 52 import java.io.IOException ; 53 import java.io.ObjectInput ; 54 import java.io.ObjectOutput ; 55 56 import javax.transaction.xa.Xid ; 57 58 59 67 public final class ExternalXid 68 implements Xid , Externalizable { 69 70 74 static final long serialVersionUID = 1; 75 76 80 protected final static char[] HEX_DIGITS = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', 81 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; 82 83 86 private int _formatId; 87 88 92 private byte[] _global; 93 94 98 private byte[] _branch; 99 100 103 private transient String _string = null; 104 105 106 109 public ExternalXid() { 110 } 111 112 119 public ExternalXid(Xid xid) { 120 this(xid.getFormatId(), xid.getGlobalTransactionId(), 121 xid.getBranchQualifier()); 122 } 123 124 132 public ExternalXid(int formatId, byte[] global, byte[] branch) { 133 _formatId = formatId; 134 135 if ((global == null) || 137 (global.length == 0)) { 138 _global = new byte[0]; 139 } else { 140 _global = new byte[global.length]; 141 System.arraycopy(global, 0, _global, 0, global.length); 142 } 143 144 if ((branch == null) || 146 (branch.length == 0)) { 147 _branch = new byte[0]; 148 } else { 149 _branch = new byte[branch.length]; 150 System.arraycopy(branch, 0, _branch, 0, branch.length); 151 } 152 } 153 154 public int getFormatId() { 156 return _formatId; 157 } 158 159 public byte[] getGlobalTransactionId() { 161 return _global; 162 } 163 164 public byte[] getBranchQualifier() { 166 return _branch; 167 } 168 169 175 public String toString() { 176 177 if (_string == null) { 178 StringBuffer buffer = new StringBuffer ("xid://"); 179 buffer.append(HEX_DIGITS[(int) ((_formatId >> 28) & 0x0F)]); 180 buffer.append(HEX_DIGITS[(int) ((_formatId >> 24) & 0x0F)]); 181 buffer.append(HEX_DIGITS[(int) ((_formatId >> 20) & 0x0F)]); 182 buffer.append(HEX_DIGITS[(int) ((_formatId >> 16) & 0x0F)]); 183 buffer.append(HEX_DIGITS[(int) ((_formatId >> 12) & 0x0F)]); 184 buffer.append(HEX_DIGITS[(int) ((_formatId >> 8) & 0x0F)]); 185 buffer.append(HEX_DIGITS[(int) ((_formatId >> 4) & 0x0F)]); 186 buffer.append(HEX_DIGITS[(int) (_formatId & 0x0F)]); 187 buffer.append('-'); 188 if (_global != null && _global.length > 0) { 189 for (int i = _global.length; i-- > 0;) { 190 buffer.append(HEX_DIGITS[(_global[i] & 0xF0) >> 4]); 191 buffer.append(HEX_DIGITS[(_global[i] & 0x0F)]); 192 } 193 } 194 buffer.append('-'); 195 if (_branch != null && _branch.length > 0) { 196 for (int i = _branch.length; i-- > 0;) { 197 buffer.append(HEX_DIGITS[(_branch[i] & 0xF0) >> 4]); 198 buffer.append(HEX_DIGITS[(_branch[i] & 0x0F)]); 199 } 200 } 201 _string = buffer.toString(); 202 } 203 204 return _string; 205 } 206 207 public int hashCode() { 209 return toString().hashCode(); 210 } 211 212 public boolean equals(Object obj) { 214 215 if (obj == this) { 216 return true; 217 } 218 219 if (obj instanceof ExternalXid) { 220 221 Xid xid = (Xid ) obj; 222 if (xid.getFormatId() != _formatId) { 224 return false; 225 } 226 227 byte[] global = xid.getGlobalTransactionId(); 229 if ((global == null) || 230 (global.length == 0)) { 231 if ((_global != null) || 232 (_global.length != 0)) { 233 return false; 234 } 235 } else { 236 if (global.length != _global.length) { 237 return false; 238 } 239 240 for (int index = 0; index < global.length; index++) { 241 if (global[index] != _global[index]) { 242 return false; 243 } 244 } 245 } 246 247 byte[] branch = xid.getBranchQualifier(); 249 if ((branch == null) || 250 (branch.length == 0)) { 251 if ((_branch != null) || 252 (_branch.length != 0)) { 253 return false; 254 } 255 } else { 256 if (branch.length != _branch.length) { 257 return false; 258 } 259 260 for (int index = 0; index < branch.length; index++) { 261 if (branch[index] != _branch[index]) { 262 return false; 263 } 264 } 265 } 266 return true; 267 } 268 269 return false; 270 } 271 272 public void writeExternal(ObjectOutput stream) 274 throws IOException { 275 stream.writeLong(serialVersionUID); 276 stream.writeInt(_formatId); 277 278 stream.writeInt(_global.length); 279 stream.write(_global); 280 281 stream.writeInt(_branch.length); 282 stream.write(_branch); 283 284 } 285 286 public void readExternal(ObjectInput stream) 288 throws IOException , ClassNotFoundException { 289 long version = stream.readLong(); 290 291 if (version == serialVersionUID) { 292 _formatId = stream.readInt(); 293 294 _global = new byte[stream.readInt()]; 295 stream.read(_global); 296 297 _branch = new byte[stream.readInt()]; 298 stream.read(_branch); 299 300 } else { 301 throw new IOException ("No support for ExternalXid " + 302 "with version " + version); 303 } 304 } 305 } 306 | Popular Tags |