001    /** 
002     * 
003     * Copyright 2004 Hiram Chirino
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.store.cache;
019    
020    import java.io.IOException;
021    import java.util.Map;
022    
023    import javax.jms.JMSException;
024    
025    import org.activemq.store.MessageStore;
026    import org.activemq.store.PersistenceAdapter;
027    import org.activemq.store.TopicMessageStore;
028    import org.activemq.store.TransactionStore;
029    
030    /**
031     * Implements a {@link PersistenceAdapter} designed to
032     * to speed up access to recently added messages by using
033     * a MessageCache .
034     *
035     * @version $Revision: 1.1.1.1 $
036     */
037    public abstract class CachePersistenceAdapter implements PersistenceAdapter {
038    
039        private PersistenceAdapter longTermPersistence;    
040    
041        public CachePersistenceAdapter() {
042        }
043        
044        public CachePersistenceAdapter(PersistenceAdapter longTermPersistence) throws IOException {
045                    this.longTermPersistence = longTermPersistence;
046        }
047    
048        public Map getInitialDestinations() {
049            return longTermPersistence.getInitialDestinations();
050        }
051    
052        public MessageStore createQueueMessageStore(String destinationName) throws JMSException {
053            MessageStore longtermStore = longTermPersistence.createQueueMessageStore(destinationName);
054            CacheMessageStore store = new CacheMessageStore(this, longtermStore, createMessageCache(destinationName));
055            return store;
056        }
057    
058            public TopicMessageStore createTopicMessageStore(String destinationName) throws JMSException {
059                TopicMessageStore longtermStore = longTermPersistence.createTopicMessageStore(destinationName);
060            CacheTopicMessageStore store = new CacheTopicMessageStore(this, longtermStore, new SimpleMessageCache());
061            return store;
062        }
063    
064        public TransactionStore createTransactionStore() throws JMSException {
065            return longTermPersistence.createTransactionStore();
066        }
067    
068        public void beginTransaction() throws JMSException {
069            longTermPersistence.beginTransaction();
070        }
071    
072        public void commitTransaction() throws JMSException {
073            longTermPersistence.commitTransaction();
074        }
075    
076        public void rollbackTransaction() {
077            longTermPersistence.rollbackTransaction();
078        }
079    
080        public void start() throws JMSException {
081            longTermPersistence.start();
082        }
083    
084            public void stop() throws JMSException {
085            longTermPersistence.stop();
086        }
087            
088        /**
089         * Verifies if a dead letter has already been sent for a message  
090         * @param seq
091         * @param useLocking to prevent concurrency/dups
092         * @return
093         */
094        public boolean deadLetterAlreadySent(long seq, boolean useLocking) {
095            return longTermPersistence.deadLetterAlreadySent(seq, useLocking);
096        }
097    
098        // Properties
099        //-------------------------------------------------------------------------
100        public PersistenceAdapter getLongTermPersistence() {
101            return longTermPersistence;
102        }
103        
104        public void setLongTermPersistence(PersistenceAdapter longTermPersistence) {
105            this.longTermPersistence = longTermPersistence;
106        }
107    
108        /**
109         * Subclasses should override this method to change the type 
110         * of MessageCache that is used to cache messages.
111         *  
112         * @param destinationName
113         * @return
114         */
115            abstract protected MessageCache createMessageCache(String destinationName);
116    
117    }