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.camel.processor.idempotent;
018    
019    import java.util.HashMap;
020    import java.util.Map;
021    
022    import org.apache.camel.util.LRUCache;
023    
024    /**
025     * A memory based implementation of {@link MessageIdRepository}. Care should be
026     * taken to use a suitable underlying {@link Map} to avoid this class being a
027     * memory leak
028     * 
029     * @version $Revision: 42473 $
030     */
031    public class MemoryMessageIdRepository implements MessageIdRepository {
032        private final Map cache;
033    
034        public MemoryMessageIdRepository(Map set) {
035            this.cache = set;
036        }
037    
038        /**
039         * Creates a new MemoryMessageIdRepository with a memory based repository.
040         * <b>Warning</b> this method should only really be used for testing as it
041         * will involve keeping all message IDs in RAM.
042         */
043        public static MessageIdRepository memoryMessageIdRepository() {
044            return memoryMessageIdRepository(new HashMap());
045        }
046    
047        /**
048         * Creates a new MemoryMessageIdRepository with a memory based repository.
049         * <b>Warning</b> this method should only really be used for testing as it
050         * will involve keeping all message IDs in RAM.
051         */
052        public static MessageIdRepository memoryMessageIdRepository(int cacheSize) {
053            return memoryMessageIdRepository(new LRUCache(cacheSize));
054        }
055    
056        /**
057         * Creates a new MemoryMessageIdRepository using the given {@link Map} to
058         * use to store the processed Message ID objects. Warning be careful of the
059         * implementation of Map you use as if you are not careful it could be a
060         * memory leak.
061         */
062        public static MessageIdRepository memoryMessageIdRepository(Map cache) {
063            return new MemoryMessageIdRepository(cache);
064        }
065    
066        public boolean contains(String messageId) {
067            synchronized (cache) {
068                if (cache.containsKey(messageId)) {
069                    return true;
070                } else {
071                    cache.put(messageId, messageId);
072                    return false;
073                }
074            }
075        }
076    }