User: chirino 
  Date: 01/07/10 19:46:19

  Added:       src/main/org/jbossmq/pm PersistenceManager.java
                        TxManager.java
  Log:
  Making a more solid interface to the persistence manager.
  
  Revision  Changes    Path
  1.1                  jbossmq/src/main/org/jbossmq/pm/PersistenceManager.java
  
  Index: PersistenceManager.java
  ===================================================================
  /*
   * JBossMQ, the OpenSource JMS implementation
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  package org.jbossmq.pm;
  
  import javax.jms.JMSException;
  
  import java.util.HashMap;
  import java.util.Iterator;
  import java.util.LinkedList;
  
  import org.jbossmq.xml.XElement;
  import org.jbossmq.SpyMessage;
  import org.jbossmq.SpyDestination;
  
  
  import org.jbossmq.ConnectionToken;
  
  /**
   *    This class allows provides the base for user supplied persistence packages.
   *
   *    @author Hiram Chirino ([EMAIL PROTECTED])
   *  @author Paul Kendall ([EMAIL PROTECTED])
   *
   *    @version $Revision: 1.1 $
   */
  public interface PersistenceManager {
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
        /**
         * Create and return a unique transaction id.
         */
        public abstract Long createPersistentTx() throws javax.jms.JMSException;
  
    /**
     * Commit the transaction to the persistent store.
     */
        public abstract void commitPersistentTx(Long txId) throws 
javax.jms.JMSException;
  
    /**
     * Rollback the transaction.
     */
        public abstract void rollbackPersistentTx(Long txId) throws 
javax.jms.JMSException;
  
    /**
     * Initialize the queue.
     */
        public abstract void initQueue( SpyDestination dest, String queueId ) throws 
javax.jms.JMSException;
  
    /**
     * Remove the queue, and all messages in it, from the persistent store
     */
        public abstract void destroyQueue( SpyDestination dest, String queueId ) 
throws javax.jms.JMSException;
  
    /**
     * Remove message from the persistent store.
     * If the message is part of a transaction, txId is not null.
     */
        public abstract void add(String queueId, SpyMessage message, Long txId) throws 
javax.jms.JMSException;
  
    /**
     * Remove message from the persistent store.
     * If the message is part of a transaction, txId is not null.
     */
        public abstract void remove(String queueId, SpyMessage message, Long txId) 
throws javax.jms.JMSException;
  
        public TxManager getTxManager();
  
        /**
         * Restore messages from the persistent queues.
         */
        public abstract void restore(org.jbossmq.server.JMSServer server) throws 
javax.jms.JMSException;
  }
  
  
  1.1                  jbossmq/src/main/org/jbossmq/pm/TxManager.java
  
  Index: TxManager.java
  ===================================================================
  /*
   * JBossMQ, the OpenSource JMS implementation
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  package org.jbossmq.pm;
  
  import javax.jms.JMSException;
  
  import java.util.HashMap;
  import java.util.Iterator;
  import java.util.LinkedList;
  
  import org.jbossmq.xml.XElement;
  import org.jbossmq.SpyMessage;
  import org.jbossmq.SpyDestination;
  
  
  import org.jbossmq.ConnectionToken;
  
  /**
   *    This class allows provides the base for user supplied persistence packages.
   *
   *    @author Hiram Chirino ([EMAIL PROTECTED])
   *  @author Paul Kendall ([EMAIL PROTECTED])
   *
   *    @version $Revision: 1.1 $
   */
  public class TxManager {
  
        // Maps (Long)txIds to LinkedList of Runnable tasks
        HashMap postCommitTasks = new HashMap();
        // Maps (Long)txIds to LinkedList of Runnable tasks
        HashMap postRollbackTasks = new HashMap();
        // Maps Global transactions to local transactions
        HashMap globalToLocal = new HashMap();
  
        class GlobalXID implements Runnable {
                ConnectionToken dc;
                Object xid;
  
                GlobalXID(ConnectionToken dc,Object xid) {
                        this.dc = dc;
                        this.xid = xid;
                }
  
                public boolean equals(Object obj)
                {
                        if (obj==null) return false;
                        if (obj.getClass()!=GlobalXID.class) return false;
                        return ((GlobalXID)obj).xid.equals( xid ) &&
                                   ((GlobalXID)obj).dc.equals(dc);
                }
  
                public int hashCode() {
                        return xid.hashCode();
                }
  
                public void run() {
                        synchronized (globalToLocal) {
                                globalToLocal.remove(this);
                        }
                }
        }
  
        /**
         * Create and return a unique transaction id.
         */
        public final Long createTx() throws javax.jms.JMSException {
                Long txId = persistenceManager.createPersistentTx();
                synchronized (postCommitTasks) {
                        postCommitTasks.put(txId, new LinkedList());
                        postRollbackTasks.put(txId, new LinkedList());
                }
                return txId;
        }
  
  
  
    /**
     * Commit the transaction to the persistent store.
     */
        public final void commitTx(Long txId) throws javax.jms.JMSException {
  
                LinkedList tasks;
                synchronized( postCommitTasks ) {
                        tasks = (LinkedList)postCommitTasks.remove(txId);
                        postRollbackTasks.remove(txId);
                }
                if( tasks == null )
                        throw new javax.jms.JMSException("Transaction is not active 
for commit.");
  
        persistenceManager.commitPersistentTx(txId);
  
        synchronized(tasks){
          Iterator iter = tasks.iterator();
          while( iter.hasNext() ) {
                Runnable task = (Runnable)iter.next();
                task.run();
          }
        }
    }        
  
        public final void addPostCommitTask(Long txId, Runnable task) throws 
javax.jms.JMSException {
  
                if( txId == null ) {
                        task.run();
                        return;
                }
  
                LinkedList tasks;
                synchronized (postCommitTasks) {
                        tasks = (LinkedList) postCommitTasks.get(txId);
                }
                if (tasks == null)
                        throw new javax.jms.JMSException("Transaction is not active.");
                synchronized (tasks) {
                        tasks.addLast(task);
                }
  
        }
  
    /**
     * Rollback the transaction.
     */
        public final void rollbackTx(Long txId) throws javax.jms.JMSException {
  
                LinkedList tasks;
                synchronized( postCommitTasks ) {
                        tasks = (LinkedList)postRollbackTasks.remove(txId);
                        postCommitTasks.remove(txId);
        }
                if( tasks == null )
                        throw new javax.jms.JMSException("Transaction is not active 
3.");
  
        persistenceManager.rollbackPersistentTx(txId);
  
        synchronized(tasks){
          Iterator iter = tasks.iterator();
          while( iter.hasNext() ) {
                Runnable task = (Runnable)iter.next();
                task.run();
          }
        }
  
    }        
  
        public final void addPostRollbackTask(Long txId, Runnable task) throws 
javax.jms.JMSException {
  
                if( txId == null ) {
                        return;
                }
  
                LinkedList tasks;
                synchronized( postCommitTasks ) {
                        tasks = (LinkedList)postRollbackTasks.get(txId);
                }
                if( tasks == null )
                        throw new javax.jms.JMSException("Transaction is not active 
4.");
                synchronized (tasks) {
                        tasks.addLast(task);
                }
  
        }
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
        PersistenceManager persistenceManager;
  
        public TxManager( PersistenceManager pm )  {
                persistenceManager = pm;
        }
  
        /**
         * Create and return a unique transaction id.
     *
     * Given a distributed connection and a transaction id object,
     * allocate a unique local transaction id if the remote id is not already
     * known.
         */
        public final Long createTx(ConnectionToken dc, Object xid) throws 
javax.jms.JMSException {
  
                GlobalXID gxid = new GlobalXID(dc, xid);
        synchronized(globalToLocal){
          if( globalToLocal.containsKey(gxid) )
                throw new JMSException("Duplicate transaction from: 
"+dc.getClientID()+" xid="+xid);
        }
  
                Long txId = createTx();
        synchronized(globalToLocal){
                  globalToLocal.put(gxid, txId);
        }
  
                //Tasks to remove the global to local mappings on commit/rollback
                addPostCommitTask(txId, gxid);
                addPostRollbackTask(txId, gxid);
  
                return txId;
    }        
  
    /**
     * Return the local transaction id for a distributed transaction id.
     */
        public final Long getPrepared(ConnectionToken dc, Object xid) throws 
javax.jms.JMSException {
  
                GlobalXID gxid = new GlobalXID(dc, xid);
        Long txid;
        synchronized(globalToLocal){
                  txid = (Long)globalToLocal.get(gxid);
        }
                if( txid == null )
                        throw new JMSException("Transaction does not exist from: 
"+dc.getClientID()+" xid="+xid);
  
                return txid;
    }        
  }
  
  

_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/jboss-development

Reply via email to