001: /*
002: * Copyright (c) 2003 The Visigoth Software Society. All rights
003: * reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * 1. Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * 2. Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in
014: * the documentation and/or other materials provided with the
015: * distribution.
016: *
017: * 3. The end-user documentation included with the redistribution, if
018: * any, must include the following acknowledgement:
019: * "This product includes software developed by the
020: * Visigoth Software Society (http://www.visigoths.org/)."
021: * Alternately, this acknowledgement may appear in the software itself,
022: * if and wherever such third-party acknowledgements normally appear.
023: *
024: * 4. Neither the name "FreeMarker", "Visigoth", nor any of the names of the
025: * project contributors may be used to endorse or promote products derived
026: * from this software without prior written permission. For written
027: * permission, please contact visigoths@visigoths.org.
028: *
029: * 5. Products derived from this software may not be called "FreeMarker" or "Visigoth"
030: * nor may "FreeMarker" or "Visigoth" appear in their names
031: * without prior written permission of the Visigoth Software Society.
032: *
033: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
034: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
035: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
036: * DISCLAIMED. IN NO EVENT SHALL THE VISIGOTH SOFTWARE SOCIETY OR
037: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
038: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
039: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
040: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
041: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
042: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
043: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
044: * SUCH DAMAGE.
045: * ====================================================================
046: *
047: * This software consists of voluntary contributions made by many
048: * individuals on behalf of the Visigoth Software Society. For more
049: * information on the Visigoth Software Society, please see
050: * http://www.visigoths.org/
051: */
052:
053: package freemarker.cache;
054:
055: import java.lang.ref.Reference;
056: import java.lang.ref.ReferenceQueue;
057: import java.lang.ref.SoftReference;
058: import java.util.HashMap;
059: import java.util.Map;
060:
061: /**
062: * Strong cache storage is a cache storage that uses {@link SoftReference}
063: * objects to hold the objects it was passed, therefore allows the garbage
064: * collector to purge the cache when it determines that it wants to free up
065: * memory.
066: * This class is <em>NOT</em> thread-safe. If it is accessed from multiple
067: * threads concurrently, proper synchronization must be provided by the callers.
068: * Note that {@link TemplateCache}, the natural user of this class provides the
069: * necessary synchronizations when it uses the class.
070: * @author Attila Szegedi
071: * @version $Id: SoftCacheStorage.java,v 1.4 2003/09/22 20:47:03 ddekany Exp $
072: *
073: * @deprecated use {@link MruCacheStorage} instead.
074: */
075: public class SoftCacheStorage implements CacheStorage {
076: private final ReferenceQueue queue = new ReferenceQueue();
077: private final Map map;
078:
079: public SoftCacheStorage() {
080: this (new HashMap());
081: }
082:
083: public SoftCacheStorage(Map backingMap) {
084: this .map = backingMap;
085: }
086:
087: public Object get(Object key) {
088: processQueue();
089: Reference ref = (Reference) map.get(key);
090: return ref == null ? null : ref.get();
091: }
092:
093: public void put(Object key, Object value) {
094: processQueue();
095: map.put(key, new SoftValueReference(key, value, queue));
096: }
097:
098: public void remove(Object key) {
099: processQueue();
100: map.remove(key);
101: }
102:
103: public void clear() {
104: map.clear();
105: processQueue();
106: }
107:
108: private void processQueue() {
109: for (;;) {
110: SoftValueReference ref = (SoftValueReference) queue.poll();
111: if (ref == null) {
112: return;
113: }
114: Object key = ref.getKey();
115: // First check for reference identity, so that we don't remove
116: // a reloaded template with the same key inadvertently.
117: if (map.get(key) == ref) {
118: map.remove(key);
119: }
120: }
121: }
122:
123: private static final class SoftValueReference extends SoftReference {
124: private final Object key;
125:
126: SoftValueReference(Object key, Object value,
127: ReferenceQueue queue) {
128: super (value, queue);
129: this .key = key;
130: }
131:
132: Object getKey() {
133: return key;
134: }
135: }
136: }
|