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.ext.util;
054:
055: import java.lang.ref.ReferenceQueue;
056: import java.lang.ref.SoftReference;
057: import java.util.Map;
058:
059: import freemarker.template.ObjectWrapper;
060: import freemarker.template.TemplateModel;
061:
062: /**
063: * Internally used by various wrapper implementations to implement model
064: * caching.
065: * @version $Id: ModelCache.java,v 1.9 2003/01/12 23:40:15 revusky Exp $
066: * @author Attila Szegedi
067: */
068: public class ModelCache {
069: private boolean useCache = false;
070: private Map modelCache = null;
071: private ReferenceQueue refQueue = null;
072: private final ObjectWrapper wrapper;
073:
074: public ModelCache(ObjectWrapper wrapper) {
075: this .wrapper = wrapper;
076: }
077:
078: /**
079: * Sets whether this wrapper caches model instances. Default is false.
080: * When set to true, calling {@link #getInstance(Object, ModelFactory)}
081: * multiple times for the same object will return the same model.
082: */
083: public synchronized void setUseCache(boolean useCache) {
084: this .useCache = useCache;
085: if (useCache) {
086: modelCache = new IdentityHashMap();
087: refQueue = new ReferenceQueue();
088: } else {
089: modelCache = null;
090: refQueue = null;
091: }
092: }
093:
094: public TemplateModel getInstance(Object object, ModelFactory factory) {
095: if (useCache) {
096: TemplateModel model = lookup(object);
097: if (model == null) {
098: model = factory.create(object, wrapper);
099: register(model, object);
100: }
101: return model;
102: }
103:
104: return factory.create(object, wrapper);
105: }
106:
107: public void clearCache() {
108: if (modelCache != null) {
109: synchronized (modelCache) {
110: modelCache.clear();
111: }
112: }
113: }
114:
115: private final TemplateModel lookup(Object object) {
116: ModelReference ref = null;
117: // NOTE: we're doing minimal synchronizations -- which can lead to
118: // duplicate wrapper creation. However, this has no harmful side-effects and
119: // is a lesser performance hit.
120: synchronized (modelCache) {
121: ref = (ModelReference) modelCache.get(object);
122: }
123:
124: if (ref != null)
125: return ref.getModel();
126:
127: return null;
128: }
129:
130: private final void register(TemplateModel model, Object object) {
131: synchronized (modelCache) {
132: // Remove cleared references
133: for (;;) {
134: ModelReference queuedRef = (ModelReference) refQueue
135: .poll();
136: if (queuedRef == null)
137: break;
138: modelCache.remove(queuedRef.object);
139: }
140: // Register new reference
141: modelCache.put(object, new ModelReference(model, object,
142: refQueue));
143: }
144: }
145:
146: /**
147: * A special soft reference that is registered in the modelCache.
148: * When it gets cleared (that is, the model became unreachable)
149: * it will remove itself from the model cache.
150: */
151: private static final class ModelReference extends SoftReference {
152: Object object;
153:
154: ModelReference(TemplateModel ref, Object object,
155: ReferenceQueue refQueue) {
156: super (ref);
157: this .object = object;
158: }
159:
160: TemplateModel getModel() {
161: return (TemplateModel) this.get();
162: }
163: }
164:
165: }
|