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