• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.apache.velocity.runtime.resource.loader;
2 
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 import org.apache.commons.lang3.StringUtils;
23 import org.apache.velocity.exception.ResourceNotFoundException;
24 import org.apache.velocity.runtime.resource.Resource;
25 import org.apache.velocity.util.ClassUtils;
26 import org.apache.velocity.util.ExtProperties;
27 
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.io.Reader;
31 
32 /**
33  *  ClasspathResourceLoader is a simple loader that will load
34  *  templates from the classpath.
35  *  <br>
36  *  <br>
37  *  Will load templates from  from multiple instances of
38  *  and arbitrary combinations of:
39  *  <ul>
40  *  <li> jar files
41  *  <li> zip files
42  *  <li> template directories (any directory containing templates)
43  *  </ul>
44  *  This is a configuration-free loader, in that there are no
45  *  parameters to be specified in the configuration properties,
46  *  other than specifying this as the loader to use.  For example
47  *  the following is all that the loader needs to be functional:
48  *  <br>
49  *  <pre><code>
50  *  resource.loaders = class
51  *  resource.loader.class.class =org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
52   * </code></pre>
53  *  <br>
54  *  <br>
55  *  To use, put your template directories, jars
56  *  and zip files into the classpath or other mechanisms that make
57  *  resources accessible to the classloader.
58  *  <br>
59  *  <br>
60  *  This makes deployment trivial for web applications running in
61  *  any Servlet 2.2 compliant servlet runner, such as Tomcat 3.2
62  *  and others.
63  *  <br>
64  *  <br>
65  *  For a Servlet Spec v2.2 servlet runner,
66  *  just drop the jars of template files into the WEB-INF/lib
67  *  directory of your webapp, and you won't have to worry about setting
68  *  template paths or altering them with the root of the webapp
69  *  before initializing.
70  *  <br>
71  *  <br>
72  *  I have also tried it with a WAR deployment, and that seemed to
73  *  work just fine.
74  *
75  * @author <a href="mailto:mailmur@yahoo.com">Aki Nieminen</a>
76  * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
77  * @version $Id$
78  */
79 public class ClasspathResourceLoader extends ResourceLoader
80 {
81 
82     /**
83      *  This is abstract in the base class, so we need it
84      * @param configuration
85      */
86     @Override
init(ExtProperties configuration)87     public void init(ExtProperties configuration)
88     {
89         log.trace("ClasspathResourceLoader: initialization complete.");
90     }
91 
92     /**
93      * Get a Reader so that the Runtime can build a
94      * template with it.
95      *
96      * @param name name of template to get
97      * @param encoding asked encoding
98      * @return InputStream containing the template
99      * @throws ResourceNotFoundException if template not found
100      *         in  classpath.
101      * @since 2.0
102      */
103     @Override
getResourceReader(String name, String encoding )104     public Reader getResourceReader(String name, String encoding )
105             throws ResourceNotFoundException
106     {
107         Reader result = null;
108 
109         if (StringUtils.isEmpty(name))
110         {
111             throw new ResourceNotFoundException ("No template name provided");
112         }
113 
114         /*
115          * look for resource in thread classloader first (e.g. WEB-INF\lib in
116          * a servlet container) then fall back to the system classloader.
117          */
118 
119         InputStream rawStream = null;
120         try
121         {
122             rawStream = ClassUtils.getResourceAsStream( getClass(), name );
123             if (rawStream != null)
124             {
125                 result = buildReader(rawStream, encoding);
126             }
127         }
128         catch( Exception fnfe )
129         {
130             if (rawStream != null)
131             {
132                 try
133                 {
134                     rawStream.close();
135                 }
136                 catch (IOException ioe) {}
137             }
138             throw new ResourceNotFoundException("ClasspathResourceLoader problem with template: " + name, fnfe, rsvc.getLogContext().getStackTrace() );
139         }
140 
141         if (result == null)
142         {
143             String msg = "ClasspathResourceLoader Error: cannot find resource " + name;
144 
145             throw new ResourceNotFoundException( msg, null, rsvc.getLogContext().getStackTrace() );
146         }
147 
148         return result;
149     }
150 
151     /**
152      * @see ResourceLoader#isSourceModified(org.apache.velocity.runtime.resource.Resource)
153      */
154     @Override
isSourceModified(Resource resource)155     public boolean isSourceModified(Resource resource)
156     {
157         return false;
158     }
159 
160     /**
161      * @see ResourceLoader#getLastModified(org.apache.velocity.runtime.resource.Resource)
162      */
163     @Override
getLastModified(Resource resource)164     public long getLastModified(Resource resource)
165     {
166         return 0;
167     }
168 }
169 
170