001: /*
002: * Copyright 2002-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.ui.velocity;
018:
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.util.Arrays;
022:
023: import org.apache.commons.collections.ExtendedProperties;
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026: import org.apache.velocity.exception.ResourceNotFoundException;
027: import org.apache.velocity.runtime.resource.Resource;
028: import org.apache.velocity.runtime.resource.loader.ResourceLoader;
029:
030: import org.springframework.util.StringUtils;
031:
032: /**
033: * Velocity ResourceLoader adapter that loads via a Spring ResourceLoader.
034: * Used by VelocityEngineFactory for any resource loader path that cannot
035: * be resolved to a <code>java.io.File</code>.
036: *
037: * <p>Note that this loader does not allow for modification detection:
038: * Use Velocity's default FileResourceLoader for <code>java.io.File</code>
039: * resources.
040: *
041: * <p>Expects "spring.resource.loader" and "spring.resource.loader.path"
042: * application attributes in the Velocity runtime: the former of type
043: * <code>org.springframework.core.io.ResourceLoader</code>, the latter a String.
044: *
045: * @author Juergen Hoeller
046: * @since 14.03.2004
047: * @see VelocityEngineFactory#setResourceLoaderPath
048: * @see org.springframework.core.io.ResourceLoader
049: * @see org.apache.velocity.runtime.resource.loader.FileResourceLoader
050: */
051: public class SpringResourceLoader extends ResourceLoader {
052:
053: public static final String NAME = "spring";
054:
055: public static final String SPRING_RESOURCE_LOADER_CLASS = "spring.resource.loader.class";
056:
057: public static final String SPRING_RESOURCE_LOADER_CACHE = "spring.resource.loader.cache";
058:
059: public static final String SPRING_RESOURCE_LOADER = "spring.resource.loader";
060:
061: public static final String SPRING_RESOURCE_LOADER_PATH = "spring.resource.loader.path";
062:
063: protected final Log logger = LogFactory.getLog(getClass());
064:
065: private org.springframework.core.io.ResourceLoader resourceLoader;
066:
067: private String[] resourceLoaderPaths;
068:
069: public void init(ExtendedProperties configuration) {
070: this .resourceLoader = (org.springframework.core.io.ResourceLoader) this .rsvc
071: .getApplicationAttribute(SPRING_RESOURCE_LOADER);
072: String resourceLoaderPath = (String) this .rsvc
073: .getApplicationAttribute(SPRING_RESOURCE_LOADER_PATH);
074: if (this .resourceLoader == null) {
075: throw new IllegalArgumentException(
076: "'resourceLoader' application attribute must be present for SpringResourceLoader");
077: }
078: if (resourceLoaderPath == null) {
079: throw new IllegalArgumentException(
080: "'resourceLoaderPath' application attribute must be present for SpringResourceLoader");
081: }
082: this .resourceLoaderPaths = StringUtils
083: .commaDelimitedListToStringArray(resourceLoaderPath);
084: for (int i = 0; i < this .resourceLoaderPaths.length; i++) {
085: String path = this .resourceLoaderPaths[i];
086: if (!path.endsWith("/")) {
087: this .resourceLoaderPaths[i] = path + "/";
088: }
089: }
090: if (logger.isInfoEnabled()) {
091: logger
092: .info("SpringResourceLoader for Velocity: using resource loader ["
093: + this .resourceLoader
094: + "] and resource loader paths "
095: + Arrays.asList(this .resourceLoaderPaths));
096: }
097: }
098:
099: public InputStream getResourceStream(String source)
100: throws ResourceNotFoundException {
101: if (logger.isDebugEnabled()) {
102: logger.debug("Looking for Velocity resource with name ["
103: + source + "]");
104: }
105: for (int i = 0; i < this .resourceLoaderPaths.length; i++) {
106: org.springframework.core.io.Resource resource = this .resourceLoader
107: .getResource(this .resourceLoaderPaths[i] + source);
108: try {
109: return resource.getInputStream();
110: } catch (IOException ex) {
111: if (logger.isDebugEnabled()) {
112: logger.debug("Could not find Velocity resource: "
113: + resource);
114: }
115: }
116: }
117: throw new ResourceNotFoundException("Could not find resource ["
118: + source + "] in Spring resource loader path");
119: }
120:
121: public boolean isSourceModified(Resource resource) {
122: return false;
123: }
124:
125: public long getLastModified(Resource resource) {
126: return 0;
127: }
128:
129: }
|