Home » activemq-parent-5.3.1-source-release » org.apache » activemq » command » [javadoc | source]

    1   /**
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.activemq.command;
   18   
   19   import java.util.Arrays;
   20   import javax.transaction.xa.Xid;
   21   import org.apache.activemq.util.HexSupport;
   22   
   23   /**
   24    * @openwire:marshaller code="112"
   25    * @version $Revision: 1.6 $
   26    */
   27   public class XATransactionId extends TransactionId implements Xid, Comparable {
   28   
   29       public static final byte DATA_STRUCTURE_TYPE = CommandTypes.ACTIVEMQ_XA_TRANSACTION_ID;
   30   
   31       private int formatId;
   32       private byte[] branchQualifier;
   33       private byte[] globalTransactionId;
   34   
   35       private transient int hash;
   36       private transient String transactionKey;
   37   
   38       public XATransactionId() {
   39       }
   40   
   41       public XATransactionId(Xid xid) {
   42           this.formatId = xid.getFormatId();
   43           this.globalTransactionId = xid.getGlobalTransactionId();
   44           this.branchQualifier = xid.getBranchQualifier();
   45       }
   46   
   47       public byte getDataStructureType() {
   48           return DATA_STRUCTURE_TYPE;
   49       }
   50   
   51       public synchronized String getTransactionKey() {
   52           if (transactionKey == null) {
   53               transactionKey = "XID:" + formatId + ":" + HexSupport.toHexFromBytes(globalTransactionId) + ":"
   54                                + HexSupport.toHexFromBytes(branchQualifier);
   55           }
   56           return transactionKey;
   57       }
   58   
   59       public String toString() {
   60           return getTransactionKey();
   61       }
   62   
   63       public boolean isXATransaction() {
   64           return true;
   65       }
   66   
   67       public boolean isLocalTransaction() {
   68           return false;
   69       }
   70   
   71       /**
   72        * @openwire:property version=1
   73        */
   74       public int getFormatId() {
   75           return formatId;
   76       }
   77   
   78       /**
   79        * @openwire:property version=1
   80        */
   81       public byte[] getGlobalTransactionId() {
   82           return globalTransactionId;
   83       }
   84   
   85       /**
   86        * @openwire:property version=1
   87        */
   88       public byte[] getBranchQualifier() {
   89           return branchQualifier;
   90       }
   91   
   92       public void setBranchQualifier(byte[] branchQualifier) {
   93           this.branchQualifier = branchQualifier;
   94           this.hash = 0;
   95       }
   96   
   97       public void setFormatId(int formatId) {
   98           this.formatId = formatId;
   99           this.hash = 0;
  100       }
  101   
  102       public void setGlobalTransactionId(byte[] globalTransactionId) {
  103           this.globalTransactionId = globalTransactionId;
  104           this.hash = 0;
  105       }
  106   
  107       public int hashCode() {
  108           if (hash == 0) {
  109               hash = formatId;
  110               hash = hash(globalTransactionId, hash);
  111               hash = hash(branchQualifier, hash);
  112               if (hash == 0) {
  113                   hash = 0xaceace;
  114               }
  115           }
  116           return hash;
  117       }
  118   
  119       private static int hash(byte[] bytes, int hash) {
  120           int size = bytes.length;
  121           for (int i = 0; i < size; i++) {
  122               hash ^= bytes[i] << ((i % 4) * 8);
  123           }
  124           return hash;
  125       }
  126   
  127       public boolean equals(Object o) {
  128           if (o == null || o.getClass() != XATransactionId.class) {
  129               return false;
  130           }
  131           XATransactionId xid = (XATransactionId)o;
  132           return xid.formatId == formatId && Arrays.equals(xid.globalTransactionId, globalTransactionId)
  133                  && Arrays.equals(xid.branchQualifier, branchQualifier);
  134       }
  135   
  136       public int compareTo(Object o) {
  137           if (o == null || o.getClass() != XATransactionId.class) {
  138               return -1;
  139           }
  140           XATransactionId xid = (XATransactionId)o;
  141           return getTransactionKey().compareTo(xid.getTransactionKey());
  142       }
  143   
  144   }

Home » activemq-parent-5.3.1-source-release » org.apache » activemq » command » [javadoc | source]