KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > ui > velocity > SpringResourceLoader


1 /*
2  * Copyright 2002-2006 the original author or authors.
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.springframework.ui.velocity;
18
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.util.Arrays JavaDoc;
22
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.velocity.exception.ResourceNotFoundException;
27 import org.apache.velocity.runtime.resource.Resource;
28 import org.apache.velocity.runtime.resource.loader.ResourceLoader;
29
30 import org.springframework.util.StringUtils;
31
32 /**
33  * Velocity ResourceLoader adapter that loads via a Spring ResourceLoader.
34  * Used by VelocityEngineFactory for any resource loader path that cannot
35  * be resolved to a <code>java.io.File</code>.
36  *
37  * <p>Note that this loader does not allow for modification detection:
38  * Use Velocity's default FileResourceLoader for <code>java.io.File</code>
39  * resources.
40  *
41  * <p>Expects "spring.resource.loader" and "spring.resource.loader.path"
42  * application attributes in the Velocity runtime: the former of type
43  * <code>org.springframework.core.io.ResourceLoader</code>, the latter a String.
44  *
45  * @author Juergen Hoeller
46  * @since 14.03.2004
47  * @see VelocityEngineFactory#setResourceLoaderPath
48  * @see org.springframework.core.io.ResourceLoader
49  * @see org.apache.velocity.runtime.resource.loader.FileResourceLoader
50  */

51 public class SpringResourceLoader extends ResourceLoader {
52
53     public static final String JavaDoc NAME = "spring";
54
55     public static final String JavaDoc SPRING_RESOURCE_LOADER_CLASS = "spring.resource.loader.class";
56
57     public static final String JavaDoc SPRING_RESOURCE_LOADER_CACHE = "spring.resource.loader.cache";
58
59     public static final String JavaDoc SPRING_RESOURCE_LOADER = "spring.resource.loader";
60
61     public static final String JavaDoc SPRING_RESOURCE_LOADER_PATH = "spring.resource.loader.path";
62
63
64     protected final Log logger = LogFactory.getLog(getClass());
65
66     private org.springframework.core.io.ResourceLoader resourceLoader;
67
68     private String JavaDoc[] resourceLoaderPaths;
69
70
71     public void init(ExtendedProperties configuration) {
72         this.resourceLoader = (org.springframework.core.io.ResourceLoader)
73                 this.rsvc.getApplicationAttribute(SPRING_RESOURCE_LOADER);
74         String JavaDoc resourceLoaderPath = (String JavaDoc) this.rsvc.getApplicationAttribute(SPRING_RESOURCE_LOADER_PATH);
75         if (this.resourceLoader == null) {
76             throw new IllegalArgumentException JavaDoc(
77                     "'resourceLoader' application attribute must be present for SpringResourceLoader");
78         }
79         if (resourceLoaderPath == null) {
80             throw new IllegalArgumentException JavaDoc(
81                     "'resourceLoaderPath' application attribute must be present for SpringResourceLoader");
82         }
83         this.resourceLoaderPaths = StringUtils.commaDelimitedListToStringArray(resourceLoaderPath);
84         for (int i = 0; i < this.resourceLoaderPaths.length; i++) {
85             String JavaDoc path = this.resourceLoaderPaths[i];
86             if (!path.endsWith("/")) {
87                 this.resourceLoaderPaths[i] = path + "/";
88             }
89         }
90         if (logger.isInfoEnabled()) {
91             logger.info("SpringResourceLoader for Velocity: using resource loader [" + this.resourceLoader +
92                     "] and resource loader paths " + Arrays.asList(this.resourceLoaderPaths));
93         }
94     }
95
96     public InputStream JavaDoc getResourceStream(String JavaDoc source) throws ResourceNotFoundException {
97         if (logger.isDebugEnabled()) {
98             logger.debug("Looking for Velocity resource with name [" + source + "]");
99         }
100         for (int i = 0; i < this.resourceLoaderPaths.length; i++) {
101             org.springframework.core.io.Resource resource =
102                     this.resourceLoader.getResource(this.resourceLoaderPaths[i] + source);
103             try {
104                 return resource.getInputStream();
105             }
106             catch (IOException JavaDoc ex) {
107                 if (logger.isDebugEnabled()) {
108                     logger.debug("Could not find Velocity resource: " + resource);
109                 }
110             }
111         }
112         throw new ResourceNotFoundException(
113                 "Could not find resource [" + source + "] in Spring resource loader path");
114     }
115
116     public boolean isSourceModified(Resource resource) {
117         return false;
118     }
119
120     public long getLastModified(Resource resource) {
121         return 0;
122     }
123
124 }
125
Popular Tags