1 /*
2  * Copyright 2002-2008 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.web.servlet.view.velocity;
18 
19 import java.io.IOException;
20 
21 import javax.servlet.ServletContext;
22 
23 import org.apache.velocity.app.VelocityEngine;
24 import org.apache.velocity.exception.VelocityException;
25 import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
26 
27 import org.springframework.beans.factory.InitializingBean;
28 import org.springframework.context.ResourceLoaderAware;
29 import org.springframework.ui.velocity.VelocityEngineFactory;
30 import org.springframework.web.context.ServletContextAware;
31 
32 /**
33  * JavaBean to configure Velocity for web usage, via the "configLocation"
34  * and/or "velocityProperties" and/or "resourceLoaderPath" bean properties.
35  * The simplest way to use this class is to specify just a "resourceLoaderPath";
36  * you do not need any further configuration then.
37  *
38  * <pre class="code">
39  * &lt;bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer"&gt;
40  *   &lt;property name="resourceLoaderPath">&lt;value&gt;/WEB-INF/velocity/&lt;/value>&lt;/property&gt;
41  * &lt;/bean&gt;</pre>
42  *
43  * This bean must be included in the application context of any application
44  * using Spring's {@link VelocityView} for web MVC. It exists purely to configure
45  * Velocity; it is not meant to be referenced by application components (just
46  * internally by VelocityView). This class implements {@link VelocityConfig}
47  * in order to be found by VelocityView without depending on the bean name of
48  * this configurer. Each DispatcherServlet may define its own VelocityConfigurer
49  * if desired, potentially with different template loader paths.
50  *
51  * <p>Note that you can also refer to a pre-configured VelocityEngine
52  * instance via the "velocityEngine" property, e.g. set up by
53  * {@link org.springframework.ui.velocity.VelocityEngineFactoryBean},
54  * This allows to share a VelocityEngine for web and email usage, for example.
55  *
56  * <p>This configurer registers the "spring.vm" Velocimacro library for web views
57  * (contained in this package and thus in <code>spring.jar</code>), which makes
58  * all of Spring's default Velocity macros available to the views.
59  * This allows for using the Spring-provided macros such as follows:
60  *
61  * <pre class="code">
62  * #springBind("person.age")
63  * age is ${status.value}</pre>
64  *
65  * @author Rod Johnson
66  * @author Juergen Hoeller
67  * @author Darren Davison
68  * @see #setConfigLocation
69  * @see #setVelocityProperties
70  * @see #setResourceLoaderPath
71  * @see #setVelocityEngine
72  * @see VelocityView
73  */
74 public class VelocityConfigurer extends VelocityEngineFactory
75 		implements VelocityConfig, InitializingBean, ResourceLoaderAware, ServletContextAware {
76 
77 	/** the name of the resource loader for Spring's bind macros */
78 	private static final String SPRING_MACRO_RESOURCE_LOADER_NAME = "springMacro";
79 
80 	/** the key for the class of Spring's bind macro resource loader */
81 	private static final String SPRING_MACRO_RESOURCE_LOADER_CLASS = "springMacro.resource.loader.class";
82 
83 	/** the name of Spring's default bind macro library */
84 	private static final String SPRING_MACRO_LIBRARY = "org/springframework/web/servlet/view/velocity/spring.vm";
85 
86 
87 	private VelocityEngine velocityEngine;
88 
89 	private ServletContext servletContext;
90 
91 
92 	/**
93 	 * Set a pre-configured VelocityEngine to use for the Velocity web
94 	 * configuration: e.g. a shared one for web and email usage, set up via
95 	 * {@link org.springframework.ui.velocity.VelocityEngineFactoryBean}.
96 	 * <p>Note that the Spring macros will <i>not</i> be enabled automatically in
97 	 * case of an external VelocityEngine passed in here. Make sure to include
98 	 * <code>spring.vm</code> in your template loader path in such a scenario
99 	 * (if there is an actual need to use those macros).
100 	 * <p>If this is not set, VelocityEngineFactory's properties
101 	 * (inherited by this class) have to be specified.
102 	 */
setVelocityEngine(VelocityEngine velocityEngine)103 	public void setVelocityEngine(VelocityEngine velocityEngine) {
104 		this.velocityEngine = velocityEngine;
105 	}
106 
setServletContext(ServletContext servletContext)107 	public void setServletContext(ServletContext servletContext) {
108 		this.servletContext = servletContext;
109 	}
110 
111 	/**
112 	 * Initialize VelocityEngineFactory's VelocityEngine
113 	 * if not overridden by a pre-configured VelocityEngine.
114 	 * @see #createVelocityEngine
115 	 * @see #setVelocityEngine
116 	 */
afterPropertiesSet()117 	public void afterPropertiesSet() throws IOException, VelocityException {
118 		if (this.velocityEngine == null) {
119 			this.velocityEngine = createVelocityEngine();
120 		}
121 	}
122 
123 	/**
124 	 * Provides a ClasspathResourceLoader in addition to any default or user-defined
125 	 * loader in order to load the spring Velocity macros from the class path.
126 	 * @see org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
127 	 */
128 	@Override
postProcessVelocityEngine(VelocityEngine velocityEngine)129 	protected void postProcessVelocityEngine(VelocityEngine velocityEngine) {
130 		velocityEngine.setApplicationAttribute(ServletContext.class.getName(), this.servletContext);
131 		velocityEngine.setProperty(
132 				SPRING_MACRO_RESOURCE_LOADER_CLASS, ClasspathResourceLoader.class.getName());
133 		velocityEngine.addProperty(
134 				VelocityEngine.RESOURCE_LOADER, SPRING_MACRO_RESOURCE_LOADER_NAME);
135 		velocityEngine.addProperty(
136 				VelocityEngine.VM_LIBRARY, SPRING_MACRO_LIBRARY);
137 
138 		if (logger.isInfoEnabled()) {
139 			logger.info("ClasspathResourceLoader with name '" + SPRING_MACRO_RESOURCE_LOADER_NAME +
140 					"' added to configured VelocityEngine");
141 		}
142 	}
143 
getVelocityEngine()144 	public VelocityEngine getVelocityEngine() {
145 		return this.velocityEngine;
146 	}
147 
148 }
149