001    /**
002     * 
003     * Copyright 2004 Protique Ltd
004     * 
005     * Licensed under the Apache License, Version 2.0 (the "License"); 
006     * you may not use this file except in compliance with the License. 
007     * 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.activemq.service.impl;
019    
020    import java.util.Map;
021    
022    import javax.transaction.xa.XAException;
023    
024    import org.apache.commons.logging.Log;
025    import org.apache.commons.logging.LogFactory;
026    import org.activemq.store.TransactionStore;
027    
028    /**
029     * @version $Revision: 1.1.1.1 $
030     */
031    public class LocalTransactionCommand extends AbstractTransaction {
032        private static final Log log = LogFactory.getLog(LocalTransactionCommand.class);
033    
034        private Map localTxs;
035        private String txid;
036    
037        private final TransactionStore transactionStore;
038    
039    
040        public LocalTransactionCommand(Map localTxs, String txid, TransactionStore transactionStore) {
041            this.localTxs = localTxs;
042            this.txid = txid;
043            this.transactionStore = transactionStore;
044        }
045    
046        public void commit(boolean onePhase) throws XAException {
047            // Get ready for commit.
048            try {
049                prePrepare();
050            }
051            catch (XAException e) {
052                throw e;
053            }
054            catch (Throwable e) {
055                log.warn("COMMIT FAILED: ", e);
056                rollback();
057                // Let them know we rolled back.
058                XAException xae = new XAException("COMMIT FAILED: Transaction rolled back.");
059                xae.errorCode = XAException.XA_RBOTHER;
060                xae.initCause(e);
061                throw xae;
062            }
063    
064            setState(AbstractTransaction.FINISHED_STATE);
065            localTxs.remove(txid);
066            transactionStore.commit(getTransactionId(), false);
067            
068            try {
069                postCommit();
070            }
071            catch (Throwable e) {
072                // I guess this could happen.  Post commit task failed
073                // to execute properly.
074                log.warn("POST COMMIT FAILED: ", e);
075                XAException xae = new XAException("POST COMMIT FAILED");
076                xae.errorCode = XAException.XAER_RMERR;
077                xae.initCause(e);
078                throw xae;
079            }
080        }
081    
082        public void rollback() throws XAException {
083    
084            setState(AbstractTransaction.FINISHED_STATE);
085            localTxs.remove(txid);
086            transactionStore.rollback(getTransactionId());
087    
088            try {
089                postRollback();
090            }
091            catch (Throwable e) {
092                log.warn("POST ROLLBACK FAILED: ", e);
093                XAException xae = new XAException("POST ROLLBACK FAILED");
094                xae.errorCode = XAException.XAER_RMERR;
095                xae.initCause(e);
096                throw xae;
097            }
098        }
099    
100        public int prepare() throws XAException {
101            XAException xae = new XAException("Prepare not implemented on Local Transactions.");
102            xae.errorCode = XAException.XAER_RMERR;
103            throw xae;
104        }
105    
106        public boolean isXaTransacted() {
107            return false;
108        }
109    
110        public Object getTransactionId() {
111            return txid;
112        }
113    
114    }