001: /*
002: * Copyright 2004 Clinton Begin
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package com.ibatis.sqlmap.engine.cache.memory;
017:
018: import com.ibatis.sqlmap.engine.cache.CacheController;
019: import com.ibatis.sqlmap.engine.cache.CacheModel;
020:
021: import java.lang.ref.SoftReference;
022: import java.lang.ref.WeakReference;
023: import java.util.Collections;
024: import java.util.HashMap;
025: import java.util.Map;
026: import java.util.Properties;
027:
028: /**
029: * Memory-based implementation of CacheController
030: */
031: public class MemoryCacheController implements CacheController {
032:
033: private MemoryCacheLevel cacheLevel = MemoryCacheLevel.WEAK;
034: private Map cache = Collections.synchronizedMap(new HashMap());
035:
036: /**
037: * Configures the cache
038: *
039: * @param props Optionally can contain properties [reference-type=WEAK|SOFT|STRONG]
040: */
041: public void configure(Properties props) {
042: String refType = props.getProperty("reference-type");
043: if (refType == null) {
044: refType = props.getProperty("referenceType");
045: }
046: if (refType != null) {
047: cacheLevel = MemoryCacheLevel.getByReferenceType(refType);
048: }
049: }
050:
051: /**
052: * Add an object to the cache
053: *
054: * @param cacheModel The cacheModel
055: * @param key The key of the object to be cached
056: * @param value The object to be cached
057: */
058: public void putObject(CacheModel cacheModel, Object key,
059: Object value) {
060: Object reference = null;
061: if (cacheLevel.equals(MemoryCacheLevel.WEAK)) {
062: reference = new WeakReference(value);
063: } else if (cacheLevel.equals(MemoryCacheLevel.SOFT)) {
064: reference = new SoftReference(value);
065: } else if (cacheLevel.equals(MemoryCacheLevel.STRONG)) {
066: reference = new StrongReference(value);
067: }
068: cache.put(key, reference);
069: }
070:
071: /**
072: * Get an object out of the cache.
073: *
074: * @param cacheModel The cache model
075: * @param key The key of the object to be returned
076: * @return The cached object (or null)
077: */
078: public Object getObject(CacheModel cacheModel, Object key) {
079: Object value = null;
080: Object ref = cache.get(key);
081: if (ref != null) {
082: if (ref instanceof StrongReference) {
083: value = ((StrongReference) ref).get();
084: } else if (ref instanceof SoftReference) {
085: value = ((SoftReference) ref).get();
086: } else if (ref instanceof WeakReference) {
087: value = ((WeakReference) ref).get();
088: }
089: }
090: return value;
091: }
092:
093: public Object removeObject(CacheModel cacheModel, Object key) {
094: Object value = null;
095: Object ref = cache.remove(key);
096: if (ref != null) {
097: if (ref instanceof StrongReference) {
098: value = ((StrongReference) ref).get();
099: } else if (ref instanceof SoftReference) {
100: value = ((SoftReference) ref).get();
101: } else if (ref instanceof WeakReference) {
102: value = ((WeakReference) ref).get();
103: }
104: }
105: return value;
106: }
107:
108: /**
109: * Flushes the cache.
110: *
111: * @param cacheModel The cache model
112: */
113: public void flush(CacheModel cacheModel) {
114: cache.clear();
115: }
116:
117: /**
118: * Class to implement a strong (permanent) reference.
119: */
120: private static class StrongReference {
121: private Object object;
122:
123: /**
124: * StrongReference constructor for an object
125: * @param object - the Object to store
126: */
127: public StrongReference(Object object) {
128: this .object = object;
129: }
130:
131: /**
132: * Getter to get the object stored in the StrongReference
133: * @return - the stored Object
134: */
135: public Object get() {
136: return object;
137: }
138: }
139:
140: }
|