| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
| MemoryMessageIdRepository |
|
| 1.4;1.4 |
| 1 | /** |
|
| 2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
|
| 3 | * contributor license agreements. See the NOTICE file distributed with |
|
| 4 | * this work for additional information regarding copyright ownership. |
|
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
|
| 6 | * (the "License"); you may not use this file except in compliance with |
|
| 7 | * the License. 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 | package org.apache.camel.processor.idempotent; |
|
| 18 | ||
| 19 | import java.util.HashMap; |
|
| 20 | import java.util.Map; |
|
| 21 | ||
| 22 | import org.apache.camel.util.LRUCache; |
|
| 23 | ||
| 24 | /** |
|
| 25 | * A memory based implementation of {@link MessageIdRepository}. Care should be |
|
| 26 | * taken to use a suitable underlying {@link Map} to avoid this class being a |
|
| 27 | * memory leak |
|
| 28 | * |
|
| 29 | * @version $Revision: 1.1 $ |
|
| 30 | */ |
|
| 31 | public class MemoryMessageIdRepository implements MessageIdRepository { |
|
| 32 | private Map cache; |
|
| 33 | ||
| 34 | 6 | public MemoryMessageIdRepository(Map set) { |
| 35 | 6 | this.cache = set; |
| 36 | 6 | } |
| 37 | ||
| 38 | /** |
|
| 39 | * Creates a new MemoryMessageIdRepository with a memory based respository. |
|
| 40 | * <b>Warning</b> this method should only really be used for testing as it |
|
| 41 | * will involve keeping all message IDs in RAM. |
|
| 42 | */ |
|
| 43 | public static MessageIdRepository memoryMessageIdRepository() { |
|
| 44 | 0 | return memoryMessageIdRepository(new HashMap()); |
| 45 | } |
|
| 46 | ||
| 47 | /** |
|
| 48 | * Creates a new MemoryMessageIdRepository with a memory based respository. |
|
| 49 | * <b>Warning</b> this method should only really be used for testing as it |
|
| 50 | * will involve keeping all message IDs in RAM. |
|
| 51 | */ |
|
| 52 | public static MessageIdRepository memoryMessageIdRepository(int cacheSize) { |
|
| 53 | 6 | return memoryMessageIdRepository(new LRUCache(cacheSize)); |
| 54 | } |
|
| 55 | ||
| 56 | /** |
|
| 57 | * Creates a new MemoryMessageIdRepository using the given {@link Map} to |
|
| 58 | * use to store the processed Message ID objects. Warning be cafeful of the |
|
| 59 | * implementation of Map you use as if you are not careful it could be a |
|
| 60 | * memory leak. |
|
| 61 | */ |
|
| 62 | public static MessageIdRepository memoryMessageIdRepository(Map cache) { |
|
| 63 | 6 | return new MemoryMessageIdRepository(cache); |
| 64 | } |
|
| 65 | ||
| 66 | public boolean contains(String messageId) { |
|
| 67 | 18 | synchronized (cache) { |
| 68 | 18 | if (cache.containsKey(messageId)) { |
| 69 | 9 | return true; |
| 70 | } else { |
|
| 71 | 9 | cache.put(messageId, messageId); |
| 72 | 9 | return false; |
| 73 | } |
|
| 74 | 0 | } |
| 75 | } |
|
| 76 | } |