KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2002-2007 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.lang.reflect.Method JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23 import javax.servlet.http.HttpServletResponse JavaDoc;
24
25 import org.apache.velocity.VelocityContext;
26 import org.apache.velocity.context.Context;
27 import org.apache.velocity.tools.view.ToolboxManager;
28 import org.apache.velocity.tools.view.context.ChainedContext;
29 import org.apache.velocity.tools.view.servlet.ServletToolboxManager;
30 import org.apache.velocity.tools.view.tools.ViewTool;
31
32 import org.springframework.util.ClassUtils;
33 import org.springframework.util.ReflectionUtils;
34
35 /**
36  * {@link VelocityView} subclass which adds support for Velocity Tools toolboxes
37  * and Velocity Tools ViewTool callbacks / Velocity Tools 1.3 init methods.
38  *
39  * <p>Specify a "toolboxConfigLocation", for example "/WEB-INF/toolbox.xml",
40  * to automatically load a Velocity Tools toolbox definition file and expose
41  * all defined tools in the specified scopes. If no config location is
42  * specified, no toolbox will be loaded and exposed.
43  *
44  * <p>This view will always create a special Velocity context, namely an
45  * instance of the ChainedContext class which is part of the view package
46  * of Velocity tools. This allows to use tools from the view package of
47  * Velocity Tools, like LinkTool, which need to be initialized with a special
48  * context that implements the ViewContext interface (i.e. a ChainedContext).
49  *
50  * <p>This view also checks tools that are specified as "toolAttributes":
51  * If they implement the ViewTool interface, they will get initialized with
52  * the Velocity context. This allows tools from the view package of Velocity
53  * Tools, such as LinkTool, to be defined as
54  * {@link #setToolAttributes "toolAttributes"} on a VelocityToolboxView,
55  * instead of in a separate toolbox XML file.
56  *
57  * <p>This is a separate class mainly to avoid a required dependency on
58  * the view package of Velocity Tools in {@link VelocityView} itself.
59  * As of Spring 2.0, this class requires Velocity Tools 1.2 or higher.
60  *
61  * @author Juergen Hoeller
62  * @since 1.1.3
63  * @see #setToolboxConfigLocation
64  * @see #initTool
65  * @see org.apache.velocity.tools.view.context.ViewContext
66  * @see org.apache.velocity.tools.view.context.ChainedContext
67  * @see org.apache.velocity.tools.view.tools.ViewTool
68  * @see org.apache.velocity.tools.view.tools.LinkTool
69  */

70 public class VelocityToolboxView extends VelocityView {
71
72     private String JavaDoc toolboxConfigLocation;
73
74
75     /**
76      * Set a Velocity Toolbox config location, for example "/WEB-INF/toolbox.xml",
77      * to automatically load a Velocity Tools toolbox definition file and expose
78      * all defined tools in the specified scopes. If no config location is
79      * specified, no toolbox will be loaded and exposed.
80      * <p>The specfied location string needs to refer to a ServletContext
81      * resource, as expected by ServletToolboxManager which is part of
82      * the view package of Velocity Tools.
83      * @see org.apache.velocity.tools.view.servlet.ServletToolboxManager#getInstance
84      */

85     public void setToolboxConfigLocation(String JavaDoc toolboxConfigLocation) {
86         this.toolboxConfigLocation = toolboxConfigLocation;
87     }
88
89     /**
90      * Return the Velocity Toolbox config location, if any.
91      */

92     protected String JavaDoc getToolboxConfigLocation() {
93         return this.toolboxConfigLocation;
94     }
95
96
97     /**
98      * Overridden to create a ChainedContext, which is part of the view package
99      * of Velocity Tools, as special context. ChainedContext is needed for
100      * initialization of ViewTool instances.
101      * @see #initTool
102      */

103     protected Context createVelocityContext(
104             Map JavaDoc model, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
105
106         // Create a ChainedContext instance.
107
ChainedContext velocityContext = new ChainedContext(
108                 new VelocityContext(model), getVelocityEngine(), request, response, getServletContext());
109
110         // Load a Velocity Tools toolbox, if necessary.
111
if (getToolboxConfigLocation() != null) {
112             ToolboxManager toolboxManager = ServletToolboxManager.getInstance(
113                     getServletContext(), getToolboxConfigLocation());
114             Map JavaDoc toolboxContext = toolboxManager.getToolbox(velocityContext);
115             velocityContext.setToolbox(toolboxContext);
116         }
117
118         return velocityContext;
119     }
120
121     /**
122      * Overridden to check for the ViewContext interface which is part of the
123      * view package of Velocity Tools. This requires a special Velocity context,
124      * like ChainedContext as set up by {@link #createVelocityContext} in this class.
125      * @see org.apache.velocity.tools.view.tools.ViewTool#init(Object)
126      * @see org.apache.velocity.tools.view.tools.LinkTool#init(Object)
127      */

128     protected void initTool(Object JavaDoc tool, Context velocityContext) throws Exception JavaDoc {
129         // Initialize ViewTool instances with the Velocity context.
130
// Despite having an "init(Object)" method, all known ViewTool
131
// implementations expect a ViewContext implementation as argument.
132
// ChainedContext implements the ViewContext interface.
133
if (tool instanceof ViewTool) {
134             // Velocity Tools 1.2: an actual ViewTool implementation.
135
((ViewTool) tool).init(velocityContext);
136         }
137         else {
138             // Velocity Tools 1.3: a class-level "init(Object)" method.
139
Method JavaDoc initMethod =
140                     ClassUtils.getMethodIfAvailable(tool.getClass(), "init", new Class JavaDoc[] {Object JavaDoc.class});
141             if (initMethod != null) {
142                 ReflectionUtils.invokeMethod(initMethod, tool, new Object JavaDoc[] {velocityContext});
143             }
144         }
145     }
146
147 }
148
Popular Tags