1 22 package org.jboss.tm; 23 24 import java.io.IOException ; 25 import javax.transaction.xa.Xid ; 26 27 37 public class GlobalId 38 implements java.io.Externalizable 39 { 40 static final long serialVersionUID = -230282197045463046L; 41 42 private static String thisClassName; 43 44 static { 45 thisClassName = GlobalId.class.getName(); 46 thisClassName = 47 thisClassName.substring(thisClassName.lastIndexOf('.') + 1); 48 } 49 50 53 private int formatId; 54 55 61 private byte[] globalId; 62 63 67 private int hash; 68 69 71 public GlobalId() 72 { 73 } 75 76 80 GlobalId(int formatId, byte[] globalId, int hash) 81 { 82 this.formatId = formatId; 83 this.globalId = globalId; 84 this.hash = hash; 85 } 86 87 91 public GlobalId(int formatId, byte[] globalId) 92 { 93 this.formatId = formatId; 94 this.globalId = globalId; 95 hash = computeHash(); 96 } 97 98 public GlobalId(Xid xid) 99 { 100 formatId = xid.getFormatId(); 101 globalId = xid.getGlobalTransactionId(); 102 if (xid instanceof XidImpl && formatId == XidImpl.JBOSS_FORMAT_ID) 103 { 104 hash = xid.hashCode(); 106 } 107 else 108 { 109 hash = computeHash(); 111 } 112 } 113 114 public GlobalId(int formatId, int bqual_length, byte[] tid) 115 { 116 this.formatId = formatId; 117 if (bqual_length == 0) 118 globalId = tid; 119 else 120 { 121 int len = tid.length - bqual_length; 122 globalId = new byte[len]; 123 System.arraycopy(tid, 0, globalId, 0, len); 124 } 125 hash = computeHash(); 126 } 127 128 130 133 public byte[] getGlobalTransactionId() 134 { 135 return (byte[])globalId.clone(); 136 } 137 138 141 public int getFormatId() 142 { 143 return formatId; 144 } 145 146 152 public boolean equals(Object obj) 153 { 154 if (obj instanceof GlobalId) { 155 GlobalId other = (GlobalId)obj; 156 157 if (formatId != other.formatId) 158 return false; 159 160 if (globalId == other.globalId) 161 return true; 162 163 if (globalId.length != other.globalId.length) 164 return false; 165 166 int len = globalId.length; 167 for (int i = 0; i < len; ++i) 168 if (globalId[i] != other.globalId[i]) 169 return false; 170 171 return true; 172 } 173 return false; 174 } 175 176 public int hashCode() 177 { 178 return hash; 179 } 180 181 public String toString() 182 { 183 return thisClassName + "[formatId=" + formatId 184 + ", globalId=" + new String (globalId).trim() 185 + ", hash=" + hash + "]"; 186 } 187 188 190 public void writeExternal(java.io.ObjectOutput out) 191 throws IOException 192 { 193 out.writeInt(formatId); 194 out.writeObject(globalId); 195 } 196 197 public void readExternal(java.io.ObjectInput in) 198 throws IOException , ClassNotFoundException 199 { 200 formatId = in.readInt(); 201 globalId = (byte[])in.readObject(); 202 hash = computeHash(); 203 } 204 205 207 private int computeHash() 208 { 209 if (formatId == XidImpl.JBOSS_FORMAT_ID) 210 { 211 return (int)TransactionImpl.xidFactory.extractLocalIdFrom(globalId); 212 } 213 else 214 { 215 int len = globalId.length; 216 int hashval = 0; 217 218 for (int i = 0; i < len; ++i) 220 hashval = 3 * globalId[i] + hashval; 221 hashval += formatId; 222 return hashval; 223 } 224 } 225 226 } 227 | Popular Tags |