1 /*
2 * $Id: WebappClassTemplateLoader.java 1306435 2012-03-28 15:39:11Z nlebas $
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 package org.apache.tiles.request.freemarker.servlet;
23
24 import java.io.IOException;
25 import java.io.Reader;
26
27 import javax.servlet.ServletContext;
28
29 import freemarker.cache.ClassTemplateLoader;
30 import freemarker.cache.TemplateLoader;
31 import freemarker.cache.WebappTemplateLoader;
32
33 /**
34 * Delegates loading templates using a {@link WebappTemplateLoader} and, if not
35 * found, a {@link ClassTemplateLoader}. The resources are loaded from the
36 * webapp root and from the classpath root.
37 *
38 * @version $Rev: 1306435 $ $Date: 2012-03-29 02:39:11 +1100 (Thu, 29 Mar 2012) $
39 */
40 public class WebappClassTemplateLoader implements TemplateLoader {
41
42 /**
43 * The webapp template loader.
44 */
45 private WebappTemplateLoader webappTemplateLoader;
46
47 /**
48 * The webapp template loader.
49 */
50 private ClassTemplateLoader classTemplateLoader;
51
52 /**
53 * Constructor.
54 *
55 * @param servletContext The servlet context.
56 */
57 public WebappClassTemplateLoader(ServletContext servletContext) {
58 webappTemplateLoader = new WebappTemplateLoader(servletContext);
59 classTemplateLoader = new ClassTemplateLoader(getClass(), "/");
60 }
61
62 /** {@inheritDoc} */
63 public Object findTemplateSource(String name) throws IOException {
64 Object retValue = webappTemplateLoader.findTemplateSource(name);
65 if (retValue == null) {
66 retValue = classTemplateLoader.findTemplateSource(name);
67 }
68 return retValue;
69 }
70
71 /** {@inheritDoc} */
72 public void closeTemplateSource(Object templateSource) throws IOException {
73 webappTemplateLoader.closeTemplateSource(templateSource);
74 }
75
76 /** {@inheritDoc} */
77 public long getLastModified(Object templateSource) {
78 return webappTemplateLoader.getLastModified(templateSource);
79 }
80
81 /** {@inheritDoc} */
82 public Reader getReader(Object templateSource, String encoding)
83 throws IOException {
84 return webappTemplateLoader.getReader(templateSource, encoding);
85 }
86 }