1 21 22 package org.apache.derby.iapi.store.access.xa; 23 24 import org.apache.derby.iapi.services.sanity.SanityManager; 25 26 import org.apache.derby.iapi.store.access.GlobalXact; 27 28 import javax.transaction.xa.Xid ; 29 import javax.transaction.xa.XAException ; 30 31 43 44 public class XAXactId extends GlobalXact implements Xid 45 { 46 50 private static final char COLON = ':'; 51 52 56 57 61 private void copy_init_xid( 62 int format_id, 63 byte[] global_id, 64 byte[] branch_id) 65 { 66 this.format_id = format_id; 67 this.global_id = new byte[global_id.length]; 68 System.arraycopy(global_id, 0, this.global_id, 0, global_id.length); 69 this.branch_id = new byte[branch_id.length]; 70 System.arraycopy(branch_id, 0, this.branch_id, 0, branch_id.length); 71 } 72 73 77 public XAXactId( 78 int format_id, 79 byte[] global_id, 80 byte[] branch_id) 81 { 82 copy_init_xid(format_id, global_id, branch_id); 83 } 84 85 90 public XAXactId(Xid xid) throws XAException 91 { 92 if (xid == null) 93 throw new XAException (XAException.XAER_NOTA); 94 95 copy_init_xid( 96 xid.getFormatId(), 97 xid.getGlobalTransactionId(), 98 xid.getBranchQualifier()); 99 } 100 101 102 103 104 105 public String toHexString() 106 { 107 118 int maxLength = 20+(global_id.length+branch_id.length)*2; 120 121 StringBuffer hexdump = new StringBuffer (maxLength); 122 hexdump.append(COLON). 123 append(Integer.toString(global_id.length)).append(COLON). 124 append(Integer.toString(branch_id.length)).append(COLON). 125 append(Integer.toString(format_id, 16)).append(COLON). 126 append(org.apache.derby.iapi.util.StringUtil.toHexString(global_id, 0, global_id.length)).append(COLON). 127 append(org.apache.derby.iapi.util.StringUtil.toHexString(branch_id, 0, branch_id.length)).append(COLON); 128 129 return hexdump.toString(); 130 131 } 132 133 public XAXactId(String xactIdString) 134 { 135 int start, end, length; 137 138 start = 1; 140 end = xactIdString.indexOf(COLON, start); 141 if (SanityManager.DEBUG) 142 SanityManager.ASSERT(end != -1, "illegal string format"); 143 144 String xx = xactIdString.substring(start, end); 145 int N = Integer.parseInt(xx); 146 147 if (SanityManager.DEBUG) 148 { 149 SanityManager.ASSERT(N > 0 && N <= Xid.MAXGTRIDSIZE, "illegal gtrid size"); 150 } 151 152 start = end+1; end = xactIdString.indexOf(COLON, start); 155 if (SanityManager.DEBUG) 156 SanityManager.ASSERT(end != -1, "illegal string format"); 157 158 String yy = xactIdString.substring(start,end); 159 int M = Integer.parseInt(yy); 160 161 if (SanityManager.DEBUG) 162 SanityManager.ASSERT(M > 0 && N <= Xid.MAXBQUALSIZE, "illegal bqual size"); 163 164 start = end+1; end = xactIdString.indexOf(COLON, start); 167 if (SanityManager.DEBUG) 168 SanityManager.ASSERT(end != -1, "illegal string format"); 169 170 String f = xactIdString.substring(start,end); 171 format_id = Integer.parseInt(f, 16); 172 173 start = end+1; end = xactIdString.indexOf(COLON, start); 176 if (SanityManager.DEBUG) 177 SanityManager.ASSERT(end != -1, "illegal string format"); 178 179 global_id = org.apache.derby.iapi.util.StringUtil.fromHexString(xactIdString, start, (end-start)); 180 181 if (SanityManager.DEBUG) 182 SanityManager.ASSERT(global_id.length == N, "inconsistent global_id length"); 183 184 185 start = end+1; end = xactIdString.indexOf(COLON, start); 188 if (SanityManager.DEBUG) 189 SanityManager.ASSERT(end != -1, "illegal string format"); 190 191 branch_id = org.apache.derby.iapi.util.StringUtil.fromHexString(xactIdString, start, (end-start)); 192 193 if (SanityManager.DEBUG) 194 SanityManager.ASSERT(branch_id.length == M, 195 "inconsistent branch_id length, expect " + M + " got " + 196 branch_id.length); 197 198 } 199 200 201 202 206 207 211 212 218 public int getFormatId() 219 { 220 return(format_id); 221 } 222 223 230 public byte[] getGlobalTransactionId() 231 { 232 return(global_id); 233 } 234 235 241 public byte[] getBranchQualifier() 242 { 243 return(branch_id); 244 } 245 246 247 248 public boolean equals(Object other) 249 { 250 if (other == this) 251 return true; 252 253 if (other == null) 254 return false; 255 256 try 257 { 258 if (other instanceof GlobalXact) 259 return super.equals(other); 260 Xid other_xid = (Xid ) other; 263 264 return( 265 java.util.Arrays.equals( 266 other_xid.getGlobalTransactionId(), 267 this.global_id) && 268 java.util.Arrays.equals( 269 other_xid.getBranchQualifier(), 270 this.branch_id) && 271 other_xid.getFormatId() == this.format_id); 272 273 } 274 catch(ClassCastException cce) 275 { 276 if (SanityManager.DEBUG) 278 SanityManager.THROWASSERT("comparing XAXactId with " + 279 other.getClass().getName()); 280 281 return false; 282 } 283 } 284 285 286 } 287 288 289 290 | Popular Tags |