001    /** 
002     * 
003     * Copyright 2005 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.broker.impl;
019    
020    import java.io.File;
021    import java.io.IOException;
022    
023    import org.activemq.io.util.MemoryBoundedObjectManager;
024    import org.activemq.store.PersistenceAdapter;
025    import org.activemq.store.PersistenceAdapterFactory;
026    import org.activemq.store.cache.MemoryBoundedCachePersistenceAdapter;
027    import org.activemq.store.jdbc.JDBCPersistenceAdapter;
028    import org.activemq.store.journal.JournalPersistenceAdapter;
029    import org.apache.derby.jdbc.EmbeddedDataSource;
030    
031    /**
032     * Factory class that can create PersistenceAdapter objects.
033     *
034     * @version $Revision: 1.1.1.1 $
035     */
036    public class DefaultPersistenceAdapterFactory implements PersistenceAdapterFactory {
037        
038        /**
039         * Creates a persistence Adapter that can use a given directory to store it's data.
040         * @throws IOException 
041         */
042        public PersistenceAdapter createPersistenceAdapter(File dataDirectory, MemoryBoundedObjectManager memManager) throws IOException {
043    
044            // Setup the Derby datasource.
045            System.setProperty("derby.system.home", dataDirectory.getCanonicalPath());
046            System.setProperty("derby.storage.fileSyncTransactionLog", "true");
047            
048            EmbeddedDataSource ds = new EmbeddedDataSource();
049            ds.setDatabaseName("derbydb");
050            ds.setCreateDatabase("create");        
051            JDBCPersistenceAdapter jdbcAdapter = new JDBCPersistenceAdapter();
052            jdbcAdapter.setDataSource(ds);
053            
054            // Setup the Journal
055            File journalDir = new File(dataDirectory, "journal");
056            JournalPersistenceAdapter journalAdapter = new JournalPersistenceAdapter(journalDir, jdbcAdapter);
057            journalAdapter.setJournalType(JournalPersistenceAdapter.DEFAULT_JOURNAL_TYPE);
058            
059            // Add a cache layer if the memory manager is available.
060            if( memManager==null )
061                return journalAdapter;
062            
063            MemoryBoundedCachePersistenceAdapter cacheAdapter = new MemoryBoundedCachePersistenceAdapter(journalAdapter);
064            cacheAdapter.setMemoryManager(memManager);        
065            return cacheAdapter;
066        }
067    }