View Javadoc

    1   /***
    2    *
    3    * Copyright 2004 Protique Ltd
    4    *
    5    * Licensed under the Apache License, Version 2.0 (the "License");
    6    * you may not use this file except in compliance with the License.
    7    * 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   **/
    18  package org.codehaus.activemq.store.jdbm;
    19
    20  import jdbm.btree.BTree;
    21  import jdbm.helper.Tuple;
    22  import jdbm.helper.TupleBrowser;
    23  import org.apache.commons.logging.Log;
    24  import org.apache.commons.logging.LogFactory;
    25  import org.codehaus.activemq.message.ActiveMQXid;
    26  import org.codehaus.activemq.service.Transaction;
    27  import org.codehaus.activemq.service.TransactionManager;
    28  import org.codehaus.activemq.store.PreparedTransactionStore;
    29
    30  import javax.jms.JMSException;
    31  import javax.transaction.xa.XAException;
    32  import java.io.IOException;
    33  import java.util.ArrayList;
    34  import java.util.List;
    35
    36  /***
    37   * @version $Revision: 1.3 $
    38   */
    39  public class JdbmPreparedTransactionStore implements PreparedTransactionStore {
    40      private static final Log log = LogFactory.getLog(JdbmPreparedTransactionStore.class);
    41
    42      private BTree database;
    43
    44      public JdbmPreparedTransactionStore(BTree database) {
    45          this.database = database;
    46      }
    47
    48      public ActiveMQXid[] getXids() throws XAException {
    49          try {
    50              List list = new ArrayList();
    51              Tuple tuple = new Tuple();
    52              TupleBrowser iter = database.browse();
    53              while (iter.getNext(tuple)) {
    54                  list.add(tuple.getKey());
    55              }
    56              ActiveMQXid[] answer = new ActiveMQXid[list.size()];
    57              list.toArray(answer);
    58              return answer;
    59          }
    60          catch (IOException e) {
    61              throw new XAException("Failed to recover Xids. Reason: " + e);
    62          }
    63      }
    64
    65      public void remove(ActiveMQXid xid) throws XAException {
    66          try {
    67              database.remove(xid);
    68          }
    69          catch (IOException e) {
    70              throw new XAException("Failed to remove: " + xid + ". Reason: " + e);
    71          }
    72      }
    73
    74      public void put(ActiveMQXid xid, Transaction transaction) throws XAException {
    75          try {
    76              database.insert(xid, transaction, true);
    77          }
    78          catch (IOException e) {
    79              throw new XAException("Failed to add: " + xid + " for transaction: " + transaction + ". Reason: " + e);
    80          }
    81      }
    82
    83      public void loadPreparedTransactions(TransactionManager transactionManager) throws XAException {
    84          log.info("Recovering prepared transactions");
    85
    86          try {
    87              Tuple tuple = new Tuple();
    88              TupleBrowser iter = database.browse();
    89              while (iter.getNext(tuple)) {
    90                  ActiveMQXid xid = (ActiveMQXid) tuple.getKey();
    91                  Transaction transaction = (Transaction) tuple.getValue();
    92                  transactionManager.loadTransaction(xid, transaction);
    93              }
    94          }
    95          catch (IOException e) {
    96              log.error("Failed to recover prepared transactions: " + e, e);
    97              throw new XAException("Failed to recover prepared transactions. Reason: " + e);
    98          }
    99      }
    100
    101     public void start() throws JMSException {
    102     }
    103
    104     public void stop() throws JMSException {
    105     }
    106 }