001: package org.apache.velocity.runtime.resource.loader;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.io.InputStream;
023:
024: import org.apache.commons.collections.ExtendedProperties;
025: import org.apache.commons.lang.StringUtils;
026: import org.apache.velocity.exception.ResourceNotFoundException;
027: import org.apache.velocity.runtime.resource.Resource;
028: import org.apache.velocity.util.ClassUtils;
029: import org.apache.velocity.util.ExceptionUtils;
030:
031: /**
032: * ClasspathResourceLoader is a simple loader that will load
033: * templates from the classpath.
034: * <br>
035: * <br>
036: * Will load templates from from multiple instances of
037: * and arbitrary combinations of :
038: * <ul>
039: * <li> jar files
040: * <li> zip files
041: * <li> template directories (any directory containing templates)
042: * </ul>
043: * This is a configuration-free loader, in that there are no
044: * parameters to be specified in the configuration properties,
045: * other than specifying this as the loader to use. For example
046: * the following is all that the loader needs to be functional :
047: * <br>
048: * <br>
049: * resource.loader = class
050: * class.resource.loader.class =
051: * org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
052: * <br>
053: * <br>
054: * To use, put your template directories, jars
055: * and zip files into the classpath or other mechanisms that make
056: * resources accessable to the classloader.
057: * <br>
058: * <br>
059: * This makes deployment trivial for web applications running in
060: * any Servlet 2.2 compliant servlet runner, such as Tomcat 3.2
061: * and others.
062: * <br>
063: * <br>
064: * For a Servlet Spec v2.2 servlet runner,
065: * just drop the jars of template files into the WEB-INF/lib
066: * directory of your webapp, and you won't have to worry about setting
067: * template paths or altering them with the root of the webapp
068: * before initializing.
069: * <br>
070: * <br>
071: * I have also tried it with a WAR deployment, and that seemed to
072: * work just fine.
073: *
074: * @author <a href="mailto:mailmur@yahoo.com">Aki Nieminen</a>
075: * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
076: * @version $Id: ClasspathResourceLoader.java 471259 2006-11-04 20:26:57Z henning $
077: */
078: public class ClasspathResourceLoader extends ResourceLoader {
079:
080: /**
081: * This is abstract in the base class, so we need it
082: * @param configuration
083: */
084: public void init(ExtendedProperties configuration) {
085: if (log.isTraceEnabled()) {
086: log
087: .trace("ClasspathResourceLoader : initialization complete.");
088: }
089: }
090:
091: /**
092: * Get an InputStream so that the Runtime can build a
093: * template with it.
094: *
095: * @param name name of template to get
096: * @return InputStream containing the template
097: * @throws ResourceNotFoundException if template not found
098: * in classpath.
099: */
100: public InputStream getResourceStream(String name)
101: throws ResourceNotFoundException {
102: InputStream result = null;
103:
104: if (StringUtils.isEmpty(name)) {
105: throw new ResourceNotFoundException(
106: "No template name provided");
107: }
108:
109: /**
110: * look for resource in thread classloader first (e.g. WEB-INF\lib in
111: * a servlet container) then fall back to the system classloader.
112: */
113:
114: try {
115: result = ClassUtils.getResourceAsStream(getClass(), name);
116: } catch (Exception fnfe) {
117: throw (ResourceNotFoundException) ExceptionUtils
118: .createWithCause(ResourceNotFoundException.class,
119: "problem with template: " + name, fnfe);
120: }
121:
122: if (result == null) {
123: String msg = "ClasspathResourceLoader Error: cannot find resource "
124: + name;
125:
126: throw new ResourceNotFoundException(msg);
127: }
128:
129: return result;
130: }
131:
132: /**
133: * @see org.apache.velocity.runtime.resource.loader.ResourceLoader#isSourceModified(org.apache.velocity.runtime.resource.Resource)
134: */
135: public boolean isSourceModified(Resource resource) {
136: return false;
137: }
138:
139: /**
140: * @see org.apache.velocity.runtime.resource.loader.ResourceLoader#getLastModified(org.apache.velocity.runtime.resource.Resource)
141: */
142: public long getLastModified(Resource resource) {
143: return 0;
144: }
145: }
|