001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.activemq.store.kahadb;
019    
020    import org.apache.activemq.command.ConnectionId;
021    import org.apache.activemq.command.LocalTransactionId;
022    import org.apache.activemq.command.TransactionId;
023    import org.apache.activemq.command.XATransactionId;
024    import org.apache.activemq.protobuf.Buffer;
025    import org.apache.activemq.store.kahadb.data.KahaLocalTransactionId;
026    import org.apache.activemq.store.kahadb.data.KahaTransactionInfo;
027    import org.apache.activemq.store.kahadb.data.KahaXATransactionId;
028    
029    public class TransactionIdConversion {
030    
031        static KahaTransactionInfo convertToLocal(TransactionId tx) {
032            KahaTransactionInfo rc = new KahaTransactionInfo();
033            LocalTransactionId t = (LocalTransactionId) tx;
034            KahaLocalTransactionId kahaTxId = new KahaLocalTransactionId();
035            kahaTxId.setConnectionId(t.getConnectionId().getValue());
036            kahaTxId.setTransactionId(t.getValue());
037            rc.setLocalTransactionId(kahaTxId);
038            return rc;
039        }
040    
041        static KahaTransactionInfo convert(TransactionId txid) {
042            if (txid == null) {
043                return null;
044            }
045            KahaTransactionInfo rc;
046    
047            if (txid.isLocalTransaction()) {
048                rc = convertToLocal(txid);
049            } else {
050                rc = new KahaTransactionInfo();
051                XATransactionId t = (XATransactionId) txid;
052                KahaXATransactionId kahaTxId = new KahaXATransactionId();
053                kahaTxId.setBranchQualifier(new Buffer(t.getBranchQualifier()));
054                kahaTxId.setGlobalTransactionId(new Buffer(t.getGlobalTransactionId()));
055                kahaTxId.setFormatId(t.getFormatId());
056                rc.setXaTransactionId(kahaTxId);
057            }
058            return rc;
059        }
060    
061        static TransactionId convert(KahaTransactionInfo transactionInfo) {
062            if (transactionInfo.hasLocalTransactionId()) {
063                KahaLocalTransactionId tx = transactionInfo.getLocalTransactionId();
064                LocalTransactionId rc = new LocalTransactionId();
065                rc.setConnectionId(new ConnectionId(tx.getConnectionId()));
066                rc.setValue(tx.getTransactionId());
067                return rc;
068            } else {
069                KahaXATransactionId tx = transactionInfo.getXaTransactionId();
070                XATransactionId rc = new XATransactionId();
071                rc.setBranchQualifier(tx.getBranchQualifier().toByteArray());
072                rc.setGlobalTransactionId(tx.getGlobalTransactionId().toByteArray());
073                rc.setFormatId(tx.getFormatId());
074                return rc;
075            }
076        }
077    }