KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freemarker > ext > util > ModelCache


1 /*
2  * Copyright (c) 2003 The Visigoth Software Society. All rights
3  * reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  *
17  * 3. The end-user documentation included with the redistribution, if
18  * any, must include the following acknowledgement:
19  * "This product includes software developed by the
20  * Visigoth Software Society (http://www.visigoths.org/)."
21  * Alternately, this acknowledgement may appear in the software itself,
22  * if and wherever such third-party acknowledgements normally appear.
23  *
24  * 4. Neither the name "FreeMarker", "Visigoth", nor any of the names of the
25  * project contributors may be used to endorse or promote products derived
26  * from this software without prior written permission. For written
27  * permission, please contact visigoths@visigoths.org.
28  *
29  * 5. Products derived from this software may not be called "FreeMarker" or "Visigoth"
30  * nor may "FreeMarker" or "Visigoth" appear in their names
31  * without prior written permission of the Visigoth Software Society.
32  *
33  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
34  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36  * DISCLAIMED. IN NO EVENT SHALL THE VISIGOTH SOFTWARE SOCIETY OR
37  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
40  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
41  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
42  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
43  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44  * SUCH DAMAGE.
45  * ====================================================================
46  *
47  * This software consists of voluntary contributions made by many
48  * individuals on behalf of the Visigoth Software Society. For more
49  * information on the Visigoth Software Society, please see
50  * http://www.visigoths.org/
51  */

52
53 package freemarker.ext.util;
54
55 import java.lang.ref.ReferenceQueue JavaDoc;
56 import java.lang.ref.SoftReference JavaDoc;
57 import java.util.Map JavaDoc;
58
59 import freemarker.template.ObjectWrapper;
60 import freemarker.template.TemplateModel;
61
62 /**
63  * Internally used by various wrapper implementations to implement model
64  * caching.
65  * @version $Id: ModelCache.java,v 1.9 2003/01/12 23:40:15 revusky Exp $
66  * @author Attila Szegedi
67  */

68 public class ModelCache
69 {
70     private boolean useCache = false;
71     private Map JavaDoc modelCache = null;
72     private ReferenceQueue JavaDoc refQueue = null;
73     private final ObjectWrapper wrapper;
74     
75     public ModelCache(ObjectWrapper wrapper)
76     {
77         this.wrapper = wrapper;
78     }
79     
80     /**
81      * Sets whether this wrapper caches model instances. Default is false.
82      * When set to true, calling {@link #getInstance(Object, ModelFactory)}
83      * multiple times for the same object will return the same model.
84      */

85     public synchronized void setUseCache(boolean useCache)
86     {
87         this.useCache = useCache;
88         if(useCache)
89         {
90             modelCache = new IdentityHashMap();
91             refQueue = new ReferenceQueue JavaDoc();
92         }
93         else
94         {
95             modelCache = null;
96             refQueue = null;
97         }
98     }
99     
100     public TemplateModel getInstance(Object JavaDoc object, ModelFactory factory)
101     {
102         if(useCache)
103         {
104             TemplateModel model = lookup(object);
105             if(model == null)
106             {
107                 model = factory.create(object, wrapper);
108                 register(model, object);
109             }
110             return model;
111         }
112         
113         return factory.create(object, wrapper);
114     }
115     
116     public void clearCache()
117     {
118         if(modelCache != null)
119         {
120             synchronized(modelCache)
121             {
122                 modelCache.clear();
123             }
124         }
125     }
126
127     private final TemplateModel lookup(Object JavaDoc object)
128     {
129         ModelReference ref = null;
130         // NOTE: we're doing minimal synchronizations -- which can lead to
131
// duplicate wrapper creation. However, this has no harmful side-effects and
132
// is a lesser performance hit.
133
synchronized (modelCache)
134         {
135             ref = (ModelReference) modelCache.get(object);
136         }
137
138         if (ref != null)
139             return ref.getModel();
140
141         return null;
142     }
143
144     private final void register(TemplateModel model, Object JavaDoc object)
145     {
146         synchronized (modelCache)
147         {
148             // Remove cleared references
149
for (;;)
150             {
151                 ModelReference queuedRef = (ModelReference) refQueue.poll();
152                 if (queuedRef == null)
153                     break;
154                 modelCache.remove(queuedRef.object);
155             }
156             // Register new reference
157
modelCache.put(object, new ModelReference(model, object, refQueue));
158         }
159     }
160
161     /**
162      * A special soft reference that is registered in the modelCache.
163      * When it gets cleared (that is, the model became unreachable)
164      * it will remove itself from the model cache.
165      */

166     private static final class ModelReference extends SoftReference JavaDoc
167     {
168         Object JavaDoc object;
169
170         ModelReference(TemplateModel ref, Object JavaDoc object, ReferenceQueue JavaDoc refQueue)
171         {
172             super(ref);
173             this.object = object;
174         }
175
176         TemplateModel getModel()
177         {
178             return (TemplateModel) this.get();
179         }
180     }
181
182 }
183
Popular Tags