KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > sqlmap > engine > cache > memory > MemoryCacheController


1 /*
2  * Copyright 2004 Clinton Begin
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  * http://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 com.ibatis.sqlmap.engine.cache.memory;
17
18 import com.ibatis.sqlmap.engine.cache.CacheController;
19 import com.ibatis.sqlmap.engine.cache.CacheModel;
20
21 import java.lang.ref.SoftReference JavaDoc;
22 import java.lang.ref.WeakReference JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Properties JavaDoc;
27
28 /**
29  * Memory-based implementation of CacheController
30  */

31 public class MemoryCacheController implements CacheController {
32
33   private MemoryCacheLevel cacheLevel = MemoryCacheLevel.WEAK;
34   private Map JavaDoc cache = Collections.synchronizedMap(new HashMap JavaDoc());
35
36   /**
37    * Configures the cache
38    *
39    * @param props Optionally can contain properties [reference-type=WEAK|SOFT|STRONG]
40    */

41   public void configure(Properties JavaDoc props) {
42     String JavaDoc refType = props.getProperty("reference-type");
43     if (refType == null) {
44       refType = props.getProperty("referenceType");
45     }
46     if (refType != null) {
47       cacheLevel = MemoryCacheLevel.getByReferenceType(refType);
48     }
49   }
50
51   /**
52    * Add an object to the cache
53    *
54    * @param cacheModel The cacheModel
55    * @param key The key of the object to be cached
56    * @param value The object to be cached
57    */

58   public void putObject(CacheModel cacheModel, Object JavaDoc key, Object JavaDoc value) {
59     Object JavaDoc reference = null;
60     if (cacheLevel.equals(MemoryCacheLevel.WEAK)) {
61       reference = new WeakReference JavaDoc(value);
62     } else if (cacheLevel.equals(MemoryCacheLevel.SOFT)) {
63       reference = new SoftReference JavaDoc(value);
64     } else if (cacheLevel.equals(MemoryCacheLevel.STRONG)) {
65       reference = new StrongReference(value);
66     }
67     cache.put(key, reference);
68   }
69
70   /**
71    * Get an object out of the cache.
72    *
73    * @param cacheModel The cache model
74    * @param key The key of the object to be returned
75    * @return The cached object (or null)
76    */

77   public Object JavaDoc getObject(CacheModel cacheModel, Object JavaDoc key) {
78     Object JavaDoc value = null;
79     Object JavaDoc ref = cache.get(key);
80     if (ref != null) {
81       if (ref instanceof StrongReference) {
82         value = ((StrongReference) ref).get();
83       } else if (ref instanceof SoftReference JavaDoc) {
84         value = ((SoftReference JavaDoc) ref).get();
85       } else if (ref instanceof WeakReference JavaDoc) {
86         value = ((WeakReference JavaDoc) ref).get();
87       }
88     }
89     return value;
90   }
91
92   public Object JavaDoc removeObject(CacheModel cacheModel, Object JavaDoc key) {
93     Object JavaDoc value = null;
94     Object JavaDoc ref = cache.remove(key);
95     if (ref != null) {
96       if (ref instanceof StrongReference) {
97         value = ((StrongReference) ref).get();
98       } else if (ref instanceof SoftReference JavaDoc) {
99         value = ((SoftReference JavaDoc) ref).get();
100       } else if (ref instanceof WeakReference JavaDoc) {
101         value = ((WeakReference JavaDoc) ref).get();
102       }
103     }
104     return value;
105   }
106
107   /**
108    * Flushes the cache.
109    *
110    * @param cacheModel The cache model
111    */

112   public void flush(CacheModel cacheModel) {
113     cache.clear();
114   }
115
116   /**
117    * Class to implement a strong (permanent) reference.
118    */

119   private static class StrongReference {
120     private Object JavaDoc object;
121
122     /**
123      * StrongReference constructor for an object
124      * @param object - the Object to store
125      */

126     public StrongReference(Object JavaDoc object) {
127       this.object = object;
128     }
129
130     /**
131      * Getter to get the object stored in the StrongReference
132      * @return - the stored Object
133      */

134     public Object JavaDoc get() {
135       return object;
136     }
137   }
138
139 }
140
Popular Tags