KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freemarker > cache > MultiTemplateLoader


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.cache;
54
55 import java.io.IOException JavaDoc;
56 import java.io.Reader JavaDoc;
57 import java.util.Collections JavaDoc;
58 import java.util.HashMap JavaDoc;
59 import java.util.Map JavaDoc;
60
61 /**
62  * A {@link TemplateLoader} that uses a set of other loaders to load the templates.
63  * On every request, loaders are queried in the order of their appearance in the
64  * array of loaders that this Loader owns. If a request for some template name
65  * was already satisfied in the past by one of the loaders, that Loader is queried
66  * first (a soft affinity).
67  * This class is <em>NOT</em> thread-safe. If it is accessed from multiple
68  * threads concurrently, proper synchronization must be provided by the callers.
69  * Note that {@link TemplateCache}, the natural user of this class provides the
70  * necessary synchronizations when it uses the class.
71  * @author Attila Szegedi, szegedia at freemail dot hu
72  * @version $Id: MultiTemplateLoader.java,v 1.12 2003/08/08 10:10:58 szegedia Exp $
73  */

74 public class MultiTemplateLoader implements TemplateLoader
75 {
76     private final TemplateLoader[] loaders;
77     private final Map JavaDoc lastLoaderForName = Collections.synchronizedMap(new HashMap JavaDoc());
78     
79     /**
80      * Creates a new multi template Loader that will use the specified loaders.
81      * @param loaders the loaders that are used to load templates.
82      */

83     public MultiTemplateLoader(TemplateLoader[] loaders)
84     {
85         this.loaders = (TemplateLoader[])loaders.clone();
86     }
87     
88     public Object JavaDoc findTemplateSource(String JavaDoc name)
89     throws
90         IOException JavaDoc
91     {
92         // Use soft affinity - give the loader that last found this
93
// resource a chance to find it again first.
94
TemplateLoader lastLoader = (TemplateLoader)lastLoaderForName.get(name);
95         if(lastLoader != null)
96         {
97             Object JavaDoc source = lastLoader.findTemplateSource(name);
98             if(source != null)
99             {
100                 return new MultiSource(source, lastLoader);
101             }
102         }
103         
104         // If there is no affine loader, or it could not find the resource
105
// again, try all loaders in order of appearance. If any manages
106
// to find the resource, then associate it as the new affine loader
107
// for this resource.
108
for(int i = 0; i < loaders.length; ++i)
109         {
110             TemplateLoader loader = loaders[i];
111             Object JavaDoc source = loader.findTemplateSource(name);
112             if(source != null)
113             {
114                 lastLoaderForName.put(name, loader);
115                 return new MultiSource(source, loader);
116             }
117         }
118         
119         lastLoaderForName.remove(name);
120         // Resource not found
121
return null;
122     }
123     
124     public long getLastModified(Object JavaDoc templateSource)
125     {
126         return ((MultiSource)templateSource).getLastModified();
127     }
128     
129     public Reader JavaDoc getReader(Object JavaDoc templateSource, String JavaDoc encoding)
130     throws
131         IOException JavaDoc
132     {
133         return ((MultiSource)templateSource).getReader(encoding);
134     }
135     
136     public void closeTemplateSource(Object JavaDoc templateSource)
137     throws
138         IOException JavaDoc
139     {
140         ((MultiSource)templateSource).close();
141     }
142
143     /**
144      * Represents a template source bound to a specific template loader. It
145      * serves as the complete template source descriptor used by the
146      * MultiTemplateLoader class.
147      */

148     private static final class MultiSource
149     {
150         private final Object JavaDoc source;
151         private final TemplateLoader loader;
152         
153         MultiSource(Object JavaDoc source, TemplateLoader loader)
154         {
155             this.source = source;
156             this.loader = loader;
157         }
158         
159         long getLastModified()
160         {
161             return loader.getLastModified(source);
162         }
163         
164         Reader JavaDoc getReader(String JavaDoc encoding)
165         throws
166             IOException JavaDoc
167         {
168             return loader.getReader(source, encoding);
169         }
170         
171         void close()
172         throws
173             IOException JavaDoc
174         {
175             loader.closeTemplateSource(source);
176         }
177         
178         public boolean equals(Object JavaDoc o) {
179             if(o instanceof MultiSource) {
180                 MultiSource m = (MultiSource)o;
181                 return m.loader.equals(loader) && m.source.equals(source);
182             }
183             return false;
184         }
185         
186         public int hashCode() {
187             return loader.hashCode() + 31 * source.hashCode();
188         }
189         
190         public String JavaDoc toString() {
191             return source.toString();
192         }
193     }
194 }
195
Popular Tags