KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > velocity > runtime > resource > loader > ClasspathResourceLoader


1 package org.apache.velocity.runtime.resource.loader;
2
3 /*
4  * Copyright 2001,2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18  
19 import java.io.InputStream JavaDoc;
20
21 import org.apache.velocity.runtime.resource.Resource;
22 import org.apache.velocity.exception.ResourceNotFoundException;
23
24 import org.apache.commons.collections.ExtendedProperties;
25
26 /**
27  * ClasspathResourceLoader is a simple loader that will load
28  * templates from the classpath.
29  * <br>
30  * <br>
31  * Will load templates from from multiple instances of
32  * and arbitrary combinations of :
33  * <ul>
34  * <li> jar files
35  * <li> zip files
36  * <li> template directories (any directory containing templates)
37  * </ul>
38  * This is a configuration-free loader, in that there are no
39  * parameters to be specified in the configuration properties,
40  * other than specifying this as the loader to use. For example
41  * the following is all that the loader needs to be functional :
42  * <br>
43  * <br>
44  * resource.loader = class
45  * class.resource.loader.class =
46  * org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
47  * <br>
48  * <br>
49  * To use, put your template directories, jars
50  * and zip files into the classpath or other mechanisms that make
51  * resources accessable to the classloader.
52  * <br>
53  * <br>
54  * This makes deployment trivial for web applications running in
55  * any Servlet 2.2 compliant servlet runner, such as Tomcat 3.2
56  * and others.
57  * <br>
58  * <br>
59  * For a Servlet Spec v2.2 servlet runner,
60  * just drop the jars of template files into the WEB-INF/lib
61  * directory of your webapp, and you won't have to worry about setting
62  * template paths or altering them with the root of the webapp
63  * before initializing.
64  * <br>
65  * <br>
66  * I have also tried it with a WAR deployment, and that seemed to
67  * work just fine.
68  *
69  * @author <a HREF="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
70  * @version $Id: ClasspathResourceLoader.java,v 1.8.4.1 2004/03/03 23:23:02 geirm Exp $
71  */

72 public class ClasspathResourceLoader extends ResourceLoader
73 {
74
75     /**
76      * This is abstract in the base class, so we need it
77      */

78     public void init( ExtendedProperties configuration)
79     {
80         rsvc.info("ClasspathResourceLoader : initialization starting.");
81         rsvc.info("ClasspathResourceLoader : initialization complete.");
82     }
83
84     /**
85      * Get an InputStream so that the Runtime can build a
86      * template with it.
87      *
88      * @param name name of template to get
89      * @return InputStream containing the template
90      * @throws ResourceNotFoundException if template not found
91      * in classpath.
92      */

93     public synchronized InputStream JavaDoc getResourceStream( String JavaDoc name )
94         throws ResourceNotFoundException
95     {
96         InputStream JavaDoc result = null;
97         
98         if (name == null || name.length() == 0)
99         {
100             throw new ResourceNotFoundException ("No template name provided");
101         }
102         
103         try
104         {
105             ClassLoader JavaDoc classLoader = this.getClass().getClassLoader();
106             result= classLoader.getResourceAsStream( name );
107         }
108         catch( Exception JavaDoc fnfe )
109         {
110             /*
111              * log and convert to a general Velocity ResourceNotFoundException
112              */

113             
114             throw new ResourceNotFoundException( fnfe.getMessage() );
115         }
116         
117         return result;
118     }
119     
120     /**
121      * Defaults to return false.
122      */

123     public boolean isSourceModified(Resource resource)
124     {
125         return false;
126     }
127
128     /**
129      * Defaults to return 0
130      */

131     public long getLastModified(Resource resource)
132     {
133         return 0;
134     }
135 }
136
137
Popular Tags