KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > rendering > velocity > WebappResourceLoader


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. The ASF licenses this file to You
4  * under the Apache License, Version 2.0 (the "License"); you may not
5  * 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. For additional information regarding
15  * copyright in this work, please see the NOTICE file in the top level
16  * directory of this distribution.
17  */

18
19 package org.apache.roller.ui.rendering.velocity;
20
21 import java.io.InputStream JavaDoc;
22 import javax.servlet.ServletContext JavaDoc;
23 import org.apache.commons.collections.ExtendedProperties;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.roller.ui.core.RollerContext;
27 import org.apache.velocity.exception.ResourceNotFoundException;
28 import org.apache.velocity.runtime.resource.Resource;
29 import org.apache.velocity.runtime.resource.loader.ResourceLoader;
30
31
32 /**
33  * Loads Velocity resources from the webapp.
34  *
35  * All resource urls begin from the root of the webapp. If a resource path
36  * is relative (does not begin with a /) then it is prefixed with the path
37  * /WEB-INF/velocity/, which is where Roller keeps its velocity files.
38  */

39 public class WebappResourceLoader extends ResourceLoader {
40     
41     private static Log log = LogFactory.getLog(WebappResourceLoader.class);
42     
43     private ServletContext JavaDoc mContext = null;
44     
45     
46     /**
47      * @see org.apache.velocity.runtime.resource.loader.ResourceLoader#init(org.apache.commons.collections.ExtendedProperties)
48      */

49     public void init(ExtendedProperties config) {
50         
51         log.debug("WebappResourceLoader : initialization starting.");
52         
53         if (mContext == null) {
54             mContext = RollerContext.getServletContext();
55             log.debug("Servlet Context = "+mContext.getRealPath("/WEB-INF/velocity/"));
56         }
57         
58         log.debug(config);
59         
60         log.debug("WebappResourceLoader : initialization complete.");
61     }
62     
63     
64     /**
65      * @see org.apache.velocity.runtime.resource.loader.ResourceLoader#getResourceStream(java.lang.String)
66      */

67     public InputStream JavaDoc getResourceStream(String JavaDoc name)
68             throws ResourceNotFoundException {
69         
70         log.debug("Looking up resource named ... "+name);
71         
72         if (name == null || name.length() == 0) {
73             throw new ResourceNotFoundException("No template name provided");
74         }
75         
76         InputStream JavaDoc result = null;
77         
78         try {
79             if(!name.startsWith("/"))
80                 name = "/WEB-INF/velocity/" + name;
81             
82             result = this.mContext.getResourceAsStream(name);
83             
84         } catch(Exception JavaDoc e) {
85             throw new ResourceNotFoundException(e.getMessage());
86         }
87         
88         if(result == null) {
89             throw new ResourceNotFoundException("Couldn't find "+name);
90         }
91         
92         return result;
93     }
94     
95     
96     /**
97      * Files loaded by this resource loader are considered static, so they are
98      * never reloaded by velocity.
99      *
100      * @see org.apache.velocity.runtime.resource.loader.ResourceLoader#isSourceModified(org.apache.velocity.runtime.resource.Resource)
101      */

102     public boolean isSourceModified(Resource arg0) {
103         return false;
104     }
105     
106     
107     /**
108      * Defaults to return 0.
109      *
110      * @see org.apache.velocity.runtime.resource.loader.ResourceLoader#getLastModified(org.apache.velocity.runtime.resource.Resource)
111      */

112     public long getLastModified(Resource arg0) {
113         return 0;
114     }
115     
116 }
117
Popular Tags