KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > velocity > tools > view > servlet > WebappLoader


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

16
17 package org.apache.velocity.tools.view.servlet;
18
19 import javax.servlet.ServletContext JavaDoc;
20
21 import java.io.InputStream JavaDoc;
22
23 import org.apache.commons.collections.ExtendedProperties;
24
25 import org.apache.velocity.exception.ResourceNotFoundException;
26 import org.apache.velocity.runtime.RuntimeServices;
27 import org.apache.velocity.runtime.resource.Resource;
28 import org.apache.velocity.runtime.resource.loader.ResourceLoader;
29
30 /**
31  * Resource loader that uses the ServletContext of a webapp to
32  * load Velocity templates. (it's much easier to use with servlets than
33  * the standard FileResourceLoader, in particular the use of war files
34  * is transparent).
35  *
36  * The default search path is '/' (relative to the webapp root), but
37  * you can change this behaviour by specifying one or more paths
38  * by mean of as many webapp.resource.loader.path properties as needed
39  * in the velocity.properties file.
40  *
41  * All paths must be relative to the root of the webapp.
42  *
43  * @author <a HREF="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
44  * @author <a HREF="mailto:nathan@esha.com">Nathan Bubna</a>
45  * @author <a HREF="mailto:claude@savoirweb.com">Claude Brisson</a>
46  * @version $Id: WebappLoader.java,v 1.7 2004/02/18 20:07:02 nbubna Exp $
47  */

48 public class WebappLoader extends ResourceLoader
49 {
50
51     /** The root paths for templates (relative to webapp's root). */
52     protected String JavaDoc[] paths = null;
53
54     protected ServletContext JavaDoc servletContext = null;
55
56     /**
57      * This is abstract in the base class, so we need it.
58      * <br>
59      * NOTE: this expects that the ServletContext has already
60      * been placed in the runtime's application attributes
61      * under its full class name (i.e. "javax.servlet.ServletContext").
62      *
63      * @param configuration the {@link ExtendedProperties} associated with
64      * this resource loader.
65      */

66     public void init(ExtendedProperties configuration)
67     {
68         rsvc.info("WebappLoader : initialization starting.");
69         
70         /* get configured paths */
71         paths = configuration.getStringArray("path");
72         if (paths == null || paths.length == 0)
73         {
74             paths = new String JavaDoc[1];
75             paths[0] = "/";
76         }
77         else
78         {
79             /* make sure the paths end with a '/' */
80             for (int i=0; i < paths.length; i++)
81             {
82                 if (!paths[i].endsWith("/"))
83                 {
84                     paths[i] += '/';
85                 }
86                 rsvc.info("WebappLoader : added template path - '" + paths[i] + "'");
87             }
88         }
89
90         /* get the ServletContext */
91         Object JavaDoc obj = rsvc.getApplicationAttribute(ServletContext JavaDoc.class.getName());
92         if (obj instanceof ServletContext JavaDoc)
93         {
94             servletContext = (ServletContext JavaDoc)obj;
95         }
96         else
97         {
98             rsvc.error("WebappLoader : unable to retrieve ServletContext");
99         }
100
101         rsvc.info("WebappLoader : initialization complete.");
102     }
103
104     /**
105      * Get an InputStream so that the Runtime can build a
106      * template with it.
107      *
108      * @param name name of template to get
109      * @return InputStream containing the template
110      * @throws ResourceNotFoundException if template not found
111      * in classpath.
112      */

113     public synchronized InputStream JavaDoc getResourceStream( String JavaDoc name )
114         throws ResourceNotFoundException
115     {
116         InputStream JavaDoc result = null;
117         
118         if (name == null || name.length() == 0)
119         {
120             throw new ResourceNotFoundException ("WebappLoader : No template name provided");
121         }
122         
123         /* since the paths always ends in '/',
124          * make sure the name never ends in one */

125         while (name.startsWith("/"))
126         {
127             name = name.substring(1);
128         }
129
130         Exception JavaDoc exception = null;
131         for (int i=0; i < paths.length; i++)
132         {
133             try
134             {
135                 result = servletContext.getResourceAsStream(paths[i] + name);
136
137                 /* exit the loop if we found the template */
138                 if (result != null)
139                 {
140                     break;
141                 }
142             }
143             catch (Exception JavaDoc e)
144             {
145                 /* only save the first one for later throwing */
146                 if (exception == null)
147                 {
148                     exception = e;
149                 }
150             }
151         }
152
153         /* if we never found the template */
154         if (result == null)
155         {
156             String JavaDoc msg;
157             if (exception == null)
158             {
159                 msg = "WebappLoader : Resource '" + name + "' not found.";
160             }
161             else
162             {
163                 msg = exception.getMessage();
164             }
165             /* convert to a general Velocity ResourceNotFoundException */
166             throw new ResourceNotFoundException(msg);
167         }
168
169         return result;
170     }
171     
172     /**
173      * Defaults to return false.
174      */

175     public boolean isSourceModified(Resource resource)
176     {
177         return false;
178     }
179
180     /**
181      * Defaults to return 0
182      */

183     public long getLastModified(Resource resource)
184     {
185         return 0;
186     }
187 }
188
Popular Tags