Source code: org/activemq/broker/impl/DefaultPersistenceAdapterFactory.java
1 /**
2 *
3 * Copyright 2005 Hiram Chirino
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.activemq.broker.impl;
19
20 import java.io.File;
21 import java.io.IOException;
22
23 import org.activemq.io.util.MemoryBoundedObjectManager;
24 import org.activemq.store.PersistenceAdapter;
25 import org.activemq.store.PersistenceAdapterFactory;
26 import org.activemq.store.cache.MemoryBoundedCachePersistenceAdapter;
27 import org.activemq.store.jdbc.JDBCPersistenceAdapter;
28 import org.activemq.store.journal.JournalPersistenceAdapter;
29 import org.apache.derby.jdbc.EmbeddedDataSource;
30
31 /**
32 * Factory class that can create PersistenceAdapter objects.
33 *
34 * @version $Revision: 1.1.1.1 $
35 */
36 public class DefaultPersistenceAdapterFactory implements PersistenceAdapterFactory {
37
38 /**
39 * Creates a persistence Adapter that can use a given directory to store it's data.
40 * @throws IOException
41 */
42 public PersistenceAdapter createPersistenceAdapter(File dataDirectory, MemoryBoundedObjectManager memManager) throws IOException {
43
44 // Setup the Derby datasource.
45 System.setProperty("derby.system.home", dataDirectory.getCanonicalPath());
46 System.setProperty("derby.storage.fileSyncTransactionLog", "true");
47
48 EmbeddedDataSource ds = new EmbeddedDataSource();
49 ds.setDatabaseName("derbydb");
50 ds.setCreateDatabase("create");
51 JDBCPersistenceAdapter jdbcAdapter = new JDBCPersistenceAdapter();
52 jdbcAdapter.setDataSource(ds);
53
54 // Setup the Journal
55 File journalDir = new File(dataDirectory, "journal");
56 JournalPersistenceAdapter journalAdapter = new JournalPersistenceAdapter(journalDir, jdbcAdapter);
57 journalAdapter.setJournalType(JournalPersistenceAdapter.DEFAULT_JOURNAL_TYPE);
58
59 // Add a cache layer if the memory manager is available.
60 if( memManager==null )
61 return journalAdapter;
62
63 MemoryBoundedCachePersistenceAdapter cacheAdapter = new MemoryBoundedCachePersistenceAdapter(journalAdapter);
64 cacheAdapter.setMemoryManager(memManager);
65 return cacheAdapter;
66 }
67 }