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    package org.apache.activemq.broker.jmx;
018    
019    import org.apache.activemq.transaction.Synchronization;
020    import org.apache.activemq.transaction.XATransaction;
021    
022    public class RecoveredXATransactionView implements RecoveredXATransactionViewMBean {
023    
024        private final XATransaction transaction;
025    
026        public RecoveredXATransactionView(final ManagedRegionBroker managedRegionBroker, final XATransaction transaction) {
027            this.transaction = transaction;
028            transaction.addSynchronization(new Synchronization() {
029                @Override
030                public void afterCommit() throws Exception {
031                    managedRegionBroker.unregister(transaction);
032                }
033    
034                @Override
035                public void afterRollback() throws Exception {
036                    managedRegionBroker.unregister(transaction);
037                }
038            });
039        }
040    
041        @Override
042        public int getFormatId() {
043            return transaction.getXid().getFormatId();
044        }
045    
046        @Override
047        public byte[] getBranchQualifier() {
048            return transaction.getXid().getBranchQualifier();
049        }
050    
051        @Override
052        public byte[] getGlobalTransactionId() {
053            return transaction.getXid().getGlobalTransactionId();
054        }
055    
056        @Override
057        public void heuristicCommit() throws Exception {
058            transaction.commit(false);
059        }
060    
061        @Override
062        public void heuristicRollback() throws Exception {
063            transaction.rollback();
064        }
065    }