001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  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    package org.apache.activemq.store.amq;
018    
019    import java.io.File;
020    
021    import org.apache.activemq.kaha.impl.async.AsyncDataManager;
022    import org.apache.activemq.kaha.impl.index.hash.HashIndex;
023    import org.apache.activemq.store.PersistenceAdapter;
024    import org.apache.activemq.store.PersistenceAdapterFactory;
025    import org.apache.activemq.store.ReferenceStoreAdapter;
026    import org.apache.activemq.thread.TaskRunnerFactory;
027    import org.apache.activemq.util.IOHelper;
028    
029    /**
030     * An implementation of {@link PersistenceAdapterFactory}
031     * 
032     * @org.apache.xbean.XBean element="amqPersistenceAdapterFactory"
033     * 
034     * @version $Revision: 1.17 $
035     */
036    public class AMQPersistenceAdapterFactory implements PersistenceAdapterFactory {
037        static final int DEFAULT_MAX_REFERNCE_FILE_LENGTH=2*1024*1024;
038        private TaskRunnerFactory taskRunnerFactory;
039        private File dataDirectory;
040        private int journalThreadPriority = Thread.MAX_PRIORITY;
041        private String brokerName = "localhost";
042        private ReferenceStoreAdapter referenceStoreAdapter;
043        private boolean syncOnWrite;
044        private boolean syncOnTransaction=true;
045        private boolean persistentIndex=true;
046        private boolean useNio = true;
047        private int maxFileLength = AsyncDataManager.DEFAULT_MAX_FILE_LENGTH;
048        private long cleanupInterval = AsyncDataManager.DEFAULT_CLEANUP_INTERVAL;
049        private int indexBinSize = HashIndex.DEFAULT_BIN_SIZE;
050        private int indexKeySize = HashIndex.DEFAULT_KEY_SIZE;
051        private int indexPageSize = HashIndex.DEFAULT_PAGE_SIZE;
052        private int indexMaxBinSize = HashIndex.MAXIMUM_CAPACITY;
053        private int indexLoadFactor = HashIndex.DEFAULT_LOAD_FACTOR;
054        private int maxReferenceFileLength=DEFAULT_MAX_REFERNCE_FILE_LENGTH;
055        private boolean recoverReferenceStore=true;
056        private boolean forceRecoverReferenceStore=false;
057        private long checkpointInterval = 1000 * 20;
058    
059    
060        /**
061         * @return a AMQPersistenceAdapter
062         * @see org.apache.activemq.store.PersistenceAdapterFactory#createPersistenceAdapter()
063         */
064        public PersistenceAdapter createPersistenceAdapter() {
065            AMQPersistenceAdapter result = new AMQPersistenceAdapter();
066            result.setDirectory(getDataDirectory());
067            result.setTaskRunnerFactory(getTaskRunnerFactory());
068            result.setBrokerName(getBrokerName());
069            result.setSyncOnWrite(isSyncOnWrite());
070            result.setPersistentIndex(isPersistentIndex());
071            result.setReferenceStoreAdapter(getReferenceStoreAdapter());
072            result.setUseNio(isUseNio());
073            result.setMaxFileLength(getMaxFileLength());
074            result.setCleanupInterval(getCleanupInterval());
075            result.setCheckpointInterval(getCheckpointInterval());
076            result.setIndexBinSize(getIndexBinSize());
077            result.setIndexKeySize(getIndexKeySize());
078            result.setIndexPageSize(getIndexPageSize());
079            result.setIndexMaxBinSize(getIndexMaxBinSize());
080            result.setIndexLoadFactor(getIndexLoadFactor());
081            result.setMaxReferenceFileLength(getMaxReferenceFileLength());
082            result.setForceRecoverReferenceStore(isForceRecoverReferenceStore());
083            result.setRecoverReferenceStore(isRecoverReferenceStore());
084            return result;
085        }
086    
087        public long getCleanupInterval() {
088            return cleanupInterval;
089        }
090        
091        public void setCleanupInterval(long val) {
092            cleanupInterval = val;
093        }
094    
095        /**
096         * @return the dataDirectory
097         */
098        public File getDataDirectory() {
099            if (this.dataDirectory == null) {
100                this.dataDirectory = new File(IOHelper.getDefaultDataDirectory(), IOHelper.toFileSystemSafeName(brokerName));
101            }
102            return this.dataDirectory;
103        }
104    
105        /**
106         * @param dataDirectory the dataDirectory to set
107         */
108        public void setDataDirectory(File dataDirectory) {
109            this.dataDirectory = dataDirectory;
110        }
111    
112        /**
113         * @return the taskRunnerFactory
114         */
115        public TaskRunnerFactory getTaskRunnerFactory() {
116            if (taskRunnerFactory == null) {
117                taskRunnerFactory = new TaskRunnerFactory("AMQPersistenceAdaptor Task", journalThreadPriority,
118                                                          true, 1000);
119            }
120            return taskRunnerFactory;
121        }
122    
123        /**
124         * @param taskRunnerFactory the taskRunnerFactory to set
125         */
126        public void setTaskRunnerFactory(TaskRunnerFactory taskRunnerFactory) {
127            this.taskRunnerFactory = taskRunnerFactory;
128        }
129    
130        /**
131         * @return the journalThreadPriority
132         */
133        public int getJournalThreadPriority() {
134            return this.journalThreadPriority;
135        }
136    
137        /**
138         * @param journalThreadPriority the journalThreadPriority to set
139         */
140        public void setJournalThreadPriority(int journalThreadPriority) {
141            this.journalThreadPriority = journalThreadPriority;
142        }
143    
144        /**
145         * @return the brokerName
146         */
147        public String getBrokerName() {
148            return this.brokerName;
149        }
150    
151        /**
152         * @param brokerName the brokerName to set
153         */
154        public void setBrokerName(String brokerName) {
155            this.brokerName = brokerName;
156        }
157    
158        /**
159         * @return the referenceStoreAdapter
160         */
161        public ReferenceStoreAdapter getReferenceStoreAdapter() {
162            return this.referenceStoreAdapter;
163        }
164    
165        /**
166         * @param referenceStoreAdapter the referenceStoreAdapter to set
167         */
168        public void setReferenceStoreAdapter(ReferenceStoreAdapter referenceStoreAdapter) {
169            this.referenceStoreAdapter = referenceStoreAdapter;
170        }
171        
172        public boolean isPersistentIndex() {
173                    return persistentIndex;
174            }
175    
176            public void setPersistentIndex(boolean persistentIndex) {
177                    this.persistentIndex = persistentIndex;
178            }
179    
180            public boolean isSyncOnWrite() {
181                    return syncOnWrite;
182            }
183    
184            public void setSyncOnWrite(boolean syncOnWrite) {
185                    this.syncOnWrite = syncOnWrite;
186            }
187            
188            public boolean isSyncOnTransaction() {
189            return syncOnTransaction;
190        }
191    
192        public void setSyncOnTransaction(boolean syncOnTransaction) {
193            this.syncOnTransaction = syncOnTransaction;
194        }
195    
196            public boolean isUseNio() {
197                    return useNio;
198            }
199    
200            public void setUseNio(boolean useNio) {
201                    this.useNio = useNio;
202            }
203    
204            public int getMaxFileLength() {
205                    return maxFileLength;
206            }
207    
208            public void setMaxFileLength(int maxFileLength) {
209                    this.maxFileLength = maxFileLength;
210            }
211    
212        /**
213         * @return the indexBinSize
214         */
215        public int getIndexBinSize() {
216            return indexBinSize;
217        }
218    
219        /**
220         * @param indexBinSize the indexBinSize to set
221         */
222        public void setIndexBinSize(int indexBinSize) {
223            this.indexBinSize = indexBinSize;
224        }
225    
226        /**
227         * @return the indexKeySize
228         */
229        public int getIndexKeySize() {
230            return indexKeySize;
231        }
232    
233        /**
234         * @param indexKeySize the indexKeySize to set
235         */
236        public void setIndexKeySize(int indexKeySize) {
237            this.indexKeySize = indexKeySize;
238        }
239    
240        /**
241         * @return the indexPageSize
242         */
243        public int getIndexPageSize() {
244            return indexPageSize;
245        }
246    
247        /**
248         * @param indexPageSize the indexPageSize to set
249         */
250        public void setIndexPageSize(int indexPageSize) {
251            this.indexPageSize = indexPageSize;
252        }
253    
254        /**
255         * @return the indexMaxBinSize
256         */
257        public int getIndexMaxBinSize() {
258            return indexMaxBinSize;
259        }
260    
261        /**
262         * @param indexMaxBinSize the indexMaxBinSize to set
263         */
264        public void setIndexMaxBinSize(int indexMaxBinSize) {
265            this.indexMaxBinSize = indexMaxBinSize;
266        }
267    
268        /**
269         * @return the indexLoadFactor
270         */
271        public int getIndexLoadFactor() {
272            return indexLoadFactor;
273        }
274    
275        /**
276         * @param indexLoadFactor the indexLoadFactor to set
277         */
278        public void setIndexLoadFactor(int indexLoadFactor) {
279            this.indexLoadFactor = indexLoadFactor;
280        }
281    
282        /**
283         * @return the maxReferenceFileLength
284         */
285        public int getMaxReferenceFileLength() {
286            return maxReferenceFileLength;
287        }
288    
289        /**
290         * @param maxReferenceFileLength the maxReferenceFileLength to set
291         */
292        public void setMaxReferenceFileLength(int maxReferenceFileLength) {
293            this.maxReferenceFileLength = maxReferenceFileLength;
294        }
295    
296        /**
297         * @return the recoverReferenceStore
298         */
299        public boolean isRecoverReferenceStore() {
300            return recoverReferenceStore;
301        }
302    
303        /**
304         * @param recoverReferenceStore the recoverReferenceStore to set
305         */
306        public void setRecoverReferenceStore(boolean recoverReferenceStore) {
307            this.recoverReferenceStore = recoverReferenceStore;
308        }
309    
310        /**
311         * @return the forceRecoverReferenceStore
312         */
313        public boolean isForceRecoverReferenceStore() {
314            return forceRecoverReferenceStore;
315        }
316    
317        /**
318         * @param forceRecoverReferenceStore the forceRecoverReferenceStore to set
319         */
320        public void setForceRecoverReferenceStore(boolean forceRecoverReferenceStore) {
321            this.forceRecoverReferenceStore = forceRecoverReferenceStore;
322        }
323    
324        /**
325         * @return the checkpointInterval
326         */
327        public long getCheckpointInterval() {
328            return checkpointInterval;
329        }
330    
331        /**
332         * @param checkpointInterval the checkpointInterval to set
333         */
334        public void setCheckpointInterval(long checkpointInterval) {
335            this.checkpointInterval = checkpointInterval;
336        }
337    }