/** 
 * 
 * Copyright 2004 Protique Ltd
 * 
 * Licensed 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.codehaus.activemq.store.jdbm;

import jdbm.btree.BTree;
import jdbm.helper.Tuple;
import jdbm.helper.TupleBrowser;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.activemq.message.ActiveMQXid;
import org.codehaus.activemq.service.Transaction;
import org.codehaus.activemq.service.TransactionManager;
import org.codehaus.activemq.store.PreparedTransactionStore;

import javax.jms.JMSException;
import javax.transaction.xa.XAException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @version $Revision: 1.1 $
 */
public class JdbmPreparedTransactionStore implements PreparedTransactionStore {
    private static final Log log = LogFactory.getLog(JdbmPreparedTransactionStore.class);

    private BTree database;

    public JdbmPreparedTransactionStore(BTree database) {
        this.database = database;
    }

    public ActiveMQXid[] getXids() throws XAException {
        try {
            List list = new ArrayList();
            Tuple tuple = new Tuple();
            TupleBrowser iter = database.browse();
            while (iter.getNext(tuple)) {
                list.add(tuple.getKey());
            }
            ActiveMQXid[] answer = new ActiveMQXid[list.size()];
            list.toArray(answer);
            return answer;
        }
        catch (IOException e) {
            throw new XAException("Failed to recover Xids. Reason: " + e);
        }
    }

    public void remove(ActiveMQXid xid) throws XAException {
        try {
            database.remove(xid);
        }
        catch (IOException e) {
            throw new XAException("Failed to remove: " + xid + ". Reason: " + e);
        }
    }

    public void put(ActiveMQXid xid, Transaction transaction) throws XAException {
        try {
            database.insert(xid, transaction, true);
        }
        catch (IOException e) {
            throw new XAException("Failed to add: " + xid + " for transaction: " + transaction + ". Reason: " + e);
        }
    }

    public void loadPreparedTransactions(TransactionManager transactionManager) throws XAException {
        log.info("Recovering prepared transactions");

        try {
            Tuple tuple = new Tuple();
            TupleBrowser iter = database.browse();
            while (iter.getNext(tuple)) {
                ActiveMQXid xid = (ActiveMQXid) tuple.getKey();
                Transaction transaction = (Transaction) tuple.getValue();
                transactionManager.loadTransaction(xid, transaction);
            }
        }
        catch (IOException e) {
            log.error("Failed to recover prepared transactions: " + e, e);
            throw new XAException("Failed to recover prepared transactions. Reason: " + e);
        }
    }

    public void start() throws JMSException {
    }

    public void stop() throws JMSException {
    }
}
