1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.struts.tiles;
20
21 import java.lang.reflect.InvocationTargetException;
22 import java.util.Enumeration;
23 import java.util.HashMap;
24 import java.util.Map;
25
26 import javax.servlet.ServletConfig;
27 import javax.servlet.ServletContext;
28 import javax.servlet.ServletRequest;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.apache.struts.tiles.taglib.ComponentConstants;
33
34 /***
35 * Utilities class for definitions factory.
36 * Also define userDebugLevel property (TODO to be moved from this class ?).
37 * @deprecated Use {@link TilesUtil#createDefinitionsFactory(ServletContext, DefinitionsFactoryConfig)}
38 */
39 public class DefinitionsUtil extends TilesUtil implements ComponentConstants {
40
41 /***
42 * Commons Logging instance.
43 */
44 protected static Log log = LogFactory.getLog(DefinitionsUtil.class);
45
46 /***
47 * Global user defined debug level.
48 * @deprecated This will be removed in a release after Struts 1.2.
49 */
50 public static int userDebugLevel = 0;
51
52 /***
53 * User Debug level.
54 * @deprecated This will be removed in a release after Struts 1.2.
55 */
56 public static final int NO_DEBUG = 0;
57
58 /***
59 * Name of init property carrying debug level.
60 */
61 public static final String DEFINITIONS_CONFIG_USER_DEBUG_LEVEL =
62 "definitions-debug";
63
64 /***
65 * Name of init property carrying factory class name.
66 */
67 public static final String DEFINITIONS_FACTORY_CLASSNAME =
68 "definitions-factory-class";
69
70 /***
71 * Constant name used to store factory in context.
72 */
73 public static final String DEFINITIONS_FACTORY =
74 "org.apache.struts.tiles.DEFINITIONS_FACTORY";
75
76 /***
77 * Constant name used to store definition in jsp context.
78 * Used to pass definition from a Struts action to servlet forward.
79 */
80 public static final String ACTION_DEFINITION =
81 "org.apache.struts.tiles.ACTION_DEFINITION";
82
83 /***
84 * Create Definition factory.
85 * If a factory class name is provided, a factory of this class is created. Otherwise,
86 * default factory is created.
87 * @param classname Class name of the factory to create.
88 * @param servletContext Servlet Context passed to newly created factory.
89 * @param properties Map of name/property used to initialize factory configuration object.
90 * @return newly created factory.
91 * @throws DefinitionsFactoryException If an error occur while initializing factory
92 * @deprecated Use createDefinitionsFactory(ServletContext servletContext, ServletConfig servletConfig)
93 */
94 public static DefinitionsFactory createDefinitionsFactory(
95 ServletContext servletContext,
96 Map properties,
97 String classname)
98 throws DefinitionsFactoryException {
99
100
101 DefinitionsFactoryConfig factoryConfig = new DefinitionsFactoryConfig();
102
103 try {
104 factoryConfig.populate(properties);
105
106 } catch (Exception ex) {
107 throw new DefinitionsFactoryException(
108 "Error - createDefinitionsFactory : Can't populate config object from properties map",
109 ex);
110 }
111
112
113 if (classname != null)
114 factoryConfig.setFactoryClassname(classname);
115
116
117 return createDefinitionsFactory(servletContext, factoryConfig);
118 }
119
120 /***
121 * Create default Definition factory.
122 * @param servletContext Servlet Context passed to newly created factory.
123 * @param properties Map of name/property used to initialize factory configuration object.
124 * @return newly created factory of type ConfigurableDefinitionsFactory.
125 * @throws DefinitionsFactoryException If an error occur while initializing factory
126 */
127 public static DefinitionsFactory createDefinitionsFactory(
128 ServletContext servletContext,
129 Map properties)
130 throws DefinitionsFactoryException {
131
132 return createDefinitionsFactory(servletContext, properties, null);
133 }
134
135 /***
136 * Create Definition factory.
137 * Create configuration object from servlet web.xml file, then create
138 * ConfigurableDefinitionsFactory and initialized it with object.
139 * <p>
140 * Convenience method. Calls createDefinitionsFactory(ServletContext servletContext, DefinitionsFactoryConfig factoryConfig)
141 *
142 * @param servletContext Servlet Context passed to newly created factory.
143 * @param servletConfig Servlet config containing parameters to be passed to factory configuration object.
144 * @return newly created factory of type ConfigurableDefinitionsFactory.
145 * @throws DefinitionsFactoryException If an error occur while initializing factory
146 */
147 public static DefinitionsFactory createDefinitionsFactory(
148 ServletContext servletContext,
149 ServletConfig servletConfig)
150 throws DefinitionsFactoryException {
151
152 DefinitionsFactoryConfig factoryConfig = readFactoryConfig(servletConfig);
153
154 return createDefinitionsFactory(servletContext, factoryConfig);
155 }
156
157 /***
158 * Create Definition factory.
159 * Create configuration object from servlet web.xml file, then create
160 * ConfigurableDefinitionsFactory and initialized it with object.
161 * <p>
162 * If checkIfExist is true, start by checking if factory already exist. If yes,
163 * return it. If no, create a new one.
164 * <p>
165 * If checkIfExist is false, factory is always created.
166 * <p>
167 * Convenience method. Calls createDefinitionsFactory(ServletContext servletContext, DefinitionsFactoryConfig factoryConfig)
168 *
169 * @param servletContext Servlet Context passed to newly created factory.
170 * @param servletConfig Servlet config containing parameters to be passed to factory configuration object.
171 * @param checkIfExist Check if factory already exist. If true and factory exist, return it.
172 * If true and factory doesn't exist, create it. If false, create it in all cases.
173 * @return newly created factory of type ConfigurableDefinitionsFactory.
174 * @throws DefinitionsFactoryException If an error occur while initializing factory
175 */
176 public static DefinitionsFactory createDefinitionsFactory(
177 ServletContext servletContext,
178 ServletConfig servletConfig,
179 boolean checkIfExist)
180 throws DefinitionsFactoryException {
181
182 if (checkIfExist) {
183
184 DefinitionsFactory factory = getDefinitionsFactory(servletContext);
185 if (factory != null)
186 return factory;
187 }
188
189 return createDefinitionsFactory(servletContext, servletConfig);
190 }
191
192 /***
193 * Get definition factory from appropriate servlet context.
194 * @return Definitions factory or null if not found.
195 * @deprecated Use {@link TilesUtil#getDefinitionsFactory(ServletRequest, ServletContext)}
196 * @since 20020708
197 */
198 public static DefinitionsFactory getDefinitionsFactory(ServletContext servletContext) {
199 return (DefinitionsFactory) servletContext.getAttribute(DEFINITIONS_FACTORY);
200 }
201
202 /***
203 * Get Definition stored in jsp context by an action.
204 * @return ComponentDefinition or null if not found.
205 */
206 public static ComponentDefinition getActionDefinition(ServletRequest request) {
207 return (ComponentDefinition) request.getAttribute(ACTION_DEFINITION);
208 }
209
210 /***
211 * Store definition in jsp context.
212 * Mainly used by Struts to pass a definition defined in an Action to the forward.
213 */
214 public static void setActionDefinition(
215 ServletRequest request,
216 ComponentDefinition definition) {
217
218 request.setAttribute(ACTION_DEFINITION, definition);
219 }
220
221 /***
222 * Remove Definition stored in jsp context.
223 * Mainly used by Struts to pass a definition defined in an Action to the forward.
224 */
225 public static void removeActionDefinition(
226 ServletRequest request,
227 ComponentDefinition definition) {
228
229 request.removeAttribute(ACTION_DEFINITION);
230 }
231
232 /***
233 * Populate Definition Factory Config from web.xml properties.
234 * @param factoryConfig Definition Factory Config to populate.
235 * @param servletConfig Current servlet config containing web.xml properties.
236 * @exception IllegalAccessException if the caller does not have
237 * access to the property accessor method
238 * @exception java.lang.reflect.InvocationTargetException if the property accessor method
239 * throws an exception
240 * @see org.apache.commons.beanutils.BeanUtils
241 * @since tiles 20020708
242 */
243 public static void populateDefinitionsFactoryConfig(
244 DefinitionsFactoryConfig factoryConfig,
245 ServletConfig servletConfig)
246 throws IllegalAccessException, InvocationTargetException {
247
248 Map properties = new DefinitionsUtil.ServletPropertiesMap(servletConfig);
249 factoryConfig.populate(properties);
250 }
251
252 /***
253 * Create FactoryConfig and initialize it from web.xml.
254 *
255 * @param servletConfig ServletConfig for the module with which
256 * this plug in is associated
257 * @exception DefinitionsFactoryException if this <code>PlugIn</code> cannot
258 * be successfully initialized
259 */
260 protected static DefinitionsFactoryConfig readFactoryConfig(ServletConfig servletConfig)
261 throws DefinitionsFactoryException {
262
263
264 DefinitionsFactoryConfig factoryConfig = new DefinitionsFactoryConfig();
265
266
267 try {
268 DefinitionsUtil.populateDefinitionsFactoryConfig(
269 factoryConfig,
270 servletConfig);
271
272 } catch (Exception ex) {
273 ex.printStackTrace();
274 throw new DefinitionsFactoryException(
275 "Can't populate DefinitionsFactoryConfig class from 'web.xml'.",
276 ex);
277 }
278
279 return factoryConfig;
280 }
281
282 /***
283 * Inner class.
284 * Wrapper for ServletContext init parameters.
285 * Object of this class is an hashmap containing parameters and values
286 * defined in the servlet config file (web.xml).
287 */
288 static class ServletPropertiesMap extends HashMap {
289 /***
290 * Constructor.
291 */
292 ServletPropertiesMap(ServletConfig config) {
293
294
295
296 Enumeration e = config.getInitParameterNames();
297 while (e.hasMoreElements()) {
298 String key = (String) e.nextElement();
299 put(key, config.getInitParameter(key));
300 }
301 }
302 }
303
304 }