1 28 29 package com.caucho.transaction; 30 31 import com.caucho.util.Alarm; 32 import com.caucho.util.CharBuffer; 33 import com.caucho.util.L10N; 34 35 import javax.transaction.xa.Xid ; 36 37 40 public class XidImpl implements Xid { 41 private static L10N L = new L10N(XidImpl.class); 42 43 public static final int GLOBAL_LENGTH = 28; 44 45 private byte []_global; 46 private byte []_local; 47 48 53 public XidImpl(long serverId, long randomId) 54 { 55 _global = new byte[GLOBAL_LENGTH]; 56 _local = new byte[4]; 57 _local[0] = 1; 58 59 61 _global[0] = 'R'; 63 _global[1] = 'e'; 64 _global[2] = 's'; 65 _global[3] = 'n'; 66 67 _global[4] = (byte) (serverId >> 56); 69 _global[5] = (byte) (serverId >> 48); 70 _global[6] = (byte) (serverId >> 40); 71 _global[7] = (byte) (serverId >> 32); 72 _global[8] = (byte) (serverId >> 24); 73 _global[9] = (byte) (serverId >> 16); 74 _global[10] = (byte) (serverId >> 8); 75 _global[11] = (byte) (serverId); 76 77 long timestamp = Alarm.getCurrentTime(); 79 80 _global[12] = (byte) (timestamp >> 56); 81 _global[13] = (byte) (timestamp >> 48); 82 _global[14] = (byte) (timestamp >> 40); 83 _global[15] = (byte) (timestamp >> 32); 84 _global[16] = (byte) (timestamp >> 24); 85 _global[17] = (byte) (timestamp >> 16); 86 _global[18] = (byte) (timestamp >> 8); 87 _global[19] = (byte) (timestamp); 88 89 _global[20] = (byte) (randomId >> 56); 91 _global[21] = (byte) (randomId >> 48); 92 _global[22] = (byte) (randomId >> 40); 93 _global[23] = (byte) (randomId >> 32); 94 _global[24] = (byte) (randomId >> 24); 95 _global[25] = (byte) (randomId >> 16); 96 _global[26] = (byte) (randomId >> 8); 97 _global[27] = (byte) (randomId); 98 } 99 100 XidImpl(XidImpl base, int branch) 101 { 102 _global = new byte[base._global.length]; 103 _local = new byte[4]; 104 _local[0] = (byte) (branch); 105 106 System.arraycopy(base._global, 0, _global, 0, _global.length); 107 } 108 109 XidImpl(byte []global, byte []local) 110 { 111 _global = new byte[global.length]; 112 _local = new byte[local.length]; 113 114 System.arraycopy(global, 0, _global, 0, global.length); 115 System.arraycopy(local, 0, _local, 0, local.length); 116 } 117 118 XidImpl(byte []global) 119 { 120 _global = new byte[global.length]; 121 _local = new byte[4]; 122 123 System.arraycopy(global, 0, _global, 0, global.length); 124 _local[0] = 1; 125 } 126 127 XidImpl(byte []global, int length) 128 { 129 _global = new byte[length]; 130 _local = new byte[4]; 131 132 System.arraycopy(global, 0, _global, 0, length); 133 _local[0] = 1; 134 } 135 136 public int getFormatId() 137 { 138 return 1234; 139 } 140 141 public byte []getBranchQualifier() 142 { 143 return _local; 144 } 145 146 public byte []getGlobalTransactionId() 147 { 148 return _global; 149 } 150 151 154 public Object clone() 155 { 156 return new XidImpl(_global, _local); 157 } 158 159 162 public int hashCode() 163 { 164 byte []global = _global; 165 166 int hash = 37; 167 168 for (int i = global.length - 1; i >= 0; i--) 169 hash = 65521 * hash + global[i]; 170 171 return hash; 172 } 173 174 177 public boolean equals(Object o) 178 { 179 if (! (o instanceof Xid )) 180 return false; 181 182 Xid xid = (Xid ) o; 183 184 byte []global = xid.getGlobalTransactionId(); 185 byte []local = xid.getBranchQualifier(); 186 187 if (global.length != _global.length) 188 return false; 189 190 byte []selfGlobal = _global; 191 byte []selfLocal = _local; 192 193 for (int i = global.length - 1; i >= 0; i--) { 194 if (global[i] != selfGlobal[i]) 195 return false; 196 } 197 198 for (int i = local.length - 1; i >= 0; i--) { 199 if (local[i] != selfLocal[i]) 200 return false; 201 } 202 203 return true; 204 } 205 206 209 public String toString() 210 { 211 CharBuffer cb = CharBuffer.allocate(); 212 213 cb.append("Xid["); 214 215 byte []branch = getBranchQualifier(); 216 217 addByte(cb, branch[0]); 218 219 cb.append(":"); 220 221 byte []global = getGlobalTransactionId(); 222 for (int i = 24; i < 28; i++) 223 addByte(cb, global[i]); 224 225 cb.append("]"); 226 227 return cb.close(); 228 } 229 230 236 static private void addByte(CharBuffer cb, int b) 237 { 238 int h = (b / 16) & 0xf; 239 int l = b & 0xf; 240 241 if (h >= 10) 242 cb.append((char) ('a' + h - 10)); 243 else 244 cb.append((char) ('0' + h)); 245 246 if (l >= 10) 247 cb.append((char) ('a' + l - 10)); 248 else 249 cb.append((char) ('0' + l)); 250 } 251 } 252 | Popular Tags |