Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 
 package org.apache.geronimo.transaction.manager;
 
Factory for transaction ids. The Xid is constructed of three parts:
  1. 8 byte count (LSB first)
  2. 4 byte system id
  3. 2 byte entropy
  4. 4 or 16 byte IP address of host
    1. Version:
      $Rev: 1365021 $ $Date: 2012-07-24 14:54:45 +0200 (Tue, 24 Jul 2012) $ todo Should have a way of setting baseId
 
 public class XidFactoryImpl implements XidFactory {
     private final byte[] baseId = new byte[.];
     private final long start = System.currentTimeMillis();
     private long count = ;
 
     public XidFactoryImpl(byte[] tmId) {
         System.arraycopy(tmId, 0, , 8, tmId.length);
     }
 
     public XidFactoryImpl() {
         byte[] hostid;
         try {
             hostid = InetAddress.getLocalHost().getAddress();
         } catch (UnknownHostException e) {
             hostid = new byte[]{127, 0, 0, 1};
         }
         int uid = System.identityHashCode(this);
         [8] = (byteuid;
         [9] = (byte) (uid >>> 8);
         [10] = (byte) (uid >>> 16);
         [11] = (byte) (uid >>> 24);
 
         byte[] entropy = new byte[2];
         new Random().nextBytes(entropy);
         [12] = entropy[0];
         [13] = entropy[1];
 
         System.arraycopy(hostid, 0, , 14, hostid.length);
     }
 
     public Xid createXid() {
         byte[] globalId = (byte[]) .clone();
         long id;
         synchronized (this) {
             id = ++;
         }
         insertLong(idglobalId, 0);
         return new XidImpl(globalId);
     }
 
     public Xid createBranch(Xid globalIdint branch) {
         byte[] branchId = (byte[]) .clone();
         branchId[0] = (bytebranch;
         branchId[1] = (byte) (branch >>> 8);
         branchId[2] = (byte) (branch >>> 16);
         branchId[3] = (byte) (branch >>> 24);
         insertLong(branchId, 4);
         return new XidImpl(globalIdbranchId);
     }
 
     public boolean matchesGlobalId(byte[] globalTransactionId) {
         if (globalTransactionId.length != .) {
             return false;
         }
         for (int i = 8; i < globalTransactionId.lengthi++) {
             if (globalTransactionId[i] != [i]) {
                 return false;
             }
         }
         // for recovery, only match old transactions
         long id = extractLong(globalTransactionId, 0);
         return (id < );
     }
 
    public boolean matchesBranchId(byte[] branchQualifier) {
        if (branchQualifier.length != .) {
            return false;
        }
        long id = extractLong(branchQualifier, 4);
        if (id >= ) {
            // newly created branch, not recoverable
            return false;
        }
        for (int i = 12; i < branchQualifier.lengthi++) {
            if (branchQualifier[i] != [i]) {
                return false;
            }
        }
        return true;
    }
    public Xid recover(int formatIdbyte[] globalTransactionidbyte[] branchQualifier) {
        return new XidImpl(formatIdglobalTransactionidbranchQualifier);
    }
    static void insertLong(long valuebyte[] bytesint offset) {
        bytes[offset + 0] = (bytevalue;
        bytes[offset + 1] = (byte) (value >>> 8);
        bytes[offset + 2] = (byte) (value >>> 16);
        bytes[offset + 3] = (byte) (value >>> 24);
        bytes[offset + 4] = (byte) (value >>> 32);
        bytes[offset + 5] = (byte) (value >>> 40);
        bytes[offset + 6] = (byte) (value >>> 48);
        bytes[offset + 7] = (byte) (value >>> 56);
    }
    static long extractLong(byte[] bytesint offset) {
        return (bytes[offset + 0] & 0xff)
                + (((bytes[offset + 1] & 0xff)) << 8)
                + (((bytes[offset + 2] & 0xff)) << 16)
                + (((bytes[offset + 3] & 0xffL)) << 24)
                + (((bytes[offset + 4] & 0xffL)) << 32)
                + (((bytes[offset + 5] & 0xffL)) << 40)
                + (((bytes[offset + 6] & 0xffL)) << 48)
                + (((longbytes[offset + 7]) << 56);
    }
New to GrepCode? Check out our FAQ X