1 /*
2  * Copyright 2006-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      https://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.springframework.retry.policy;
17
18 import java.util.Collections;
19 import java.util.HashMap;
20 import java.util.Map;
21
22 import org.springframework.retry.RetryContext;
23
24 /**
25  * Map-based implementation of {@link RetryContextCache}. The map backing the
26  * cache of contexts is synchronized.
27  * 
28  * @author Dave Syer
29  */

30 public class MapRetryContextCache implements RetryContextCache {
31
32     /**
33      * Default value for maximum capacity of the cache. This is set to a
34      * reasonably low value (4096) to avoid users inadvertently filling the
35      * cache with item keys that are inconsistent.
36      */

37     public static final int DEFAULT_CAPACITY = 4096;
38
39     private Map<Object, RetryContext> map = Collections.synchronizedMap(new HashMap<Object, RetryContext>());
40
41     private int capacity;
42
43     /**
44      * Create a {@link MapRetryContextCache} with default capacity.
45      */

46     public MapRetryContextCache() {
47         this(DEFAULT_CAPACITY);
48     }
49
50     /**
51      * @param defaultCapacity the default capacity
52      */

53     public MapRetryContextCache(int defaultCapacity) {
54         super();
55         this.capacity = defaultCapacity;
56     }
57
58     /**
59      * Public setter for the capacity. Prevents the cache from growing
60      * unboundedly if items that fail are misidentified and two references to an
61      * identical item actually do not have the same key. This can happen when
62      * users implement equals and hashCode based on mutable fields, for
63      * instance.
64      * 
65      * @param capacity the capacity to set
66      */

67     public void setCapacity(int capacity) {
68         this.capacity = capacity;
69     }
70
71     public boolean containsKey(Object key) {
72         return map.containsKey(key);
73     }
74
75     public RetryContext get(Object key) {
76         return map.get(key);
77     }
78
79     public void put(Object key, RetryContext context) {
80         if (map.size() >= capacity) {
81             throw new RetryCacheCapacityExceededException("Retry cache capacity limit breached. "
82                     + "Do you need to re-consider the implementation of the key generator, "
83                     + "or the equals and hashCode of the items that failed?");
84         }
85         map.put(key, context);
86     }
87
88     public void remove(Object key) {
89         map.remove(key);
90     }
91 }
92