KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > view > velocity > VelocityConfigurer


1 /*
2  * Copyright 2002-2005 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 JavaDoc;
20
21 import org.apache.velocity.app.VelocityEngine;
22 import org.apache.velocity.exception.VelocityException;
23 import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
24
25 import org.springframework.beans.factory.InitializingBean;
26 import org.springframework.context.ResourceLoaderAware;
27 import org.springframework.ui.velocity.VelocityEngineFactory;
28
29 /**
30  * JavaBean to configure Velocity for web usage, via the "configLocation"
31  * and/or "velocityProperties" and/or "resourceLoaderPath" bean properties.
32  * The simplest way to use this class is to specify just a "resourceLoaderPath";
33  * you do not need any further configuration then.
34  *
35  * <pre>
36  * &lt;bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer"&gt;
37  * &lt;property name="resourceLoaderPath">&lt;value&gt;/WEB-INF/velocity/&lt;/value>&lt;/property&gt;
38  * &lt;/bean&gt;</pre>
39  *
40  * This bean must be included in the application context of any application
41  * using Spring's VelocityView for web MVC. It exists purely to configure Velocity;
42  * it is not meant to be referenced by application components but just internally
43  * by VelocityView. Implements VelocityConfig to be found by VelocityView without
44  * depending on the bean name of the configurer. Each DispatcherServlet can define
45  * its own VelocityConfigurer if desired.
46  *
47  * <p>Note that you can also refer to a preconfigured VelocityEngine instance, for
48  * example one set up by VelocityEngineFactoryBean, via the "velocityEngine" property.
49  * This allows to share a VelocityEngine for web and email usage, for example.
50  *
51  * <p>This configurer registers the "spring.vm" Velocimacro library for web views
52  * (contained in this package and thus in spring.jar), which makes all macros
53  * defined in it implicitly available:
54  *
55  * <pre>
56  * #springBind("person.age")
57  * age is ${status.value}</pre>
58  *
59  * @author Rod Johnson
60  * @author Juergen Hoeller
61  * @author Darren Davison
62  * @see #setConfigLocation
63  * @see #setVelocityProperties
64  * @see #setResourceLoaderPath
65  * @see #setVelocityEngine
66  * @see org.springframework.ui.velocity.VelocityEngineFactoryBean
67  * @see VelocityView
68  */

69 public class VelocityConfigurer extends VelocityEngineFactory
70         implements VelocityConfig, InitializingBean, ResourceLoaderAware {
71
72     /** the name of the resource loader for Spring's bind macros */
73     private static final String JavaDoc SPRING_MACRO_RESOURCE_LOADER_NAME = "springMacro";
74
75     /** the key for the class of Spring's bind macro resource loader */
76     private static final String JavaDoc SPRING_MACRO_RESOURCE_LOADER_CLASS = "springMacro.resource.loader.class";
77
78     /** the name of Spring's default bind macro library */
79     private static final String JavaDoc SPRING_MACRO_LIBRARY = "org/springframework/web/servlet/view/velocity/spring.vm";
80
81
82     private VelocityEngine velocityEngine;
83
84     /**
85      * Set a preconfigured VelocityEngine to use for the Velocity web config, e.g.
86      * a shared one for web and email usage, set up via VelocityEngineFactoryBean.
87      * If this is not set, VelocityEngineFactory's properties (inherited by this
88      * class) have to be specified.
89      * @see org.springframework.ui.velocity.VelocityEngineFactoryBean
90      */

91     public void setVelocityEngine(VelocityEngine velocityEngine) {
92         this.velocityEngine = velocityEngine;
93     }
94
95     /**
96      * Initialize VelocityEngineFactory's VelocityEngine
97      * if not overridden by a preconfigured VelocityEngine.
98      * @see #createVelocityEngine
99      * @see #setVelocityEngine
100      */

101     public void afterPropertiesSet() throws IOException JavaDoc, VelocityException {
102         if (this.velocityEngine == null) {
103             this.velocityEngine = createVelocityEngine();
104         }
105     }
106
107     /**
108      * Provides a ClasspathResourceLoader in addition to any default or user-defined
109      * loader in order to load the spring Velocity macros from the class path.
110      * @see org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
111      */

112     protected void postProcessVelocityEngine(VelocityEngine velocityEngine) {
113         velocityEngine.setProperty(
114                 SPRING_MACRO_RESOURCE_LOADER_CLASS, ClasspathResourceLoader.class.getName());
115         velocityEngine.addProperty(
116                 VelocityEngine.RESOURCE_LOADER, SPRING_MACRO_RESOURCE_LOADER_NAME);
117         velocityEngine.addProperty(
118                 VelocityEngine.VM_LIBRARY, SPRING_MACRO_LIBRARY);
119         if (logger.isInfoEnabled()) {
120             logger.info("ClasspathResourceLoader with name '" + SPRING_MACRO_RESOURCE_LOADER_NAME +
121                     "' added to configured VelocityEngine");
122         }
123     }
124
125     public VelocityEngine getVelocityEngine() {
126         return this.velocityEngine;
127     }
128
129 }
130
Popular Tags