001: /*
002: * $Id: DefinitionsUtil.java 471754 2006-11-06 14:55:09Z husted $
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: package org.apache.struts.tiles;
023:
024: import java.lang.reflect.InvocationTargetException;
025: import java.util.Enumeration;
026: import java.util.HashMap;
027: import java.util.Map;
028:
029: import javax.servlet.ServletConfig;
030: import javax.servlet.ServletContext;
031: import javax.servlet.ServletRequest;
032:
033: import org.apache.commons.logging.Log;
034: import org.apache.commons.logging.LogFactory;
035: import org.apache.struts.tiles.taglib.ComponentConstants;
036:
037: /**
038: * Utilities class for definitions factory.
039: * Also define userDebugLevel property (TODO to be moved from this class ?).
040: * @deprecated Use {@link TilesUtil#createDefinitionsFactory(ServletContext, DefinitionsFactoryConfig)}
041: */
042: public class DefinitionsUtil extends TilesUtil implements
043: ComponentConstants {
044:
045: /**
046: * Commons Logging instance.
047: */
048: protected static Log log = LogFactory.getLog(DefinitionsUtil.class);
049:
050: /**
051: * Global user defined debug level.
052: * @deprecated This will be removed in a release after Struts 1.2.
053: */
054: public static int userDebugLevel = 0;
055:
056: /**
057: * User Debug level.
058: * @deprecated This will be removed in a release after Struts 1.2.
059: */
060: public static final int NO_DEBUG = 0;
061:
062: /**
063: * Name of init property carrying debug level.
064: */
065: public static final String DEFINITIONS_CONFIG_USER_DEBUG_LEVEL = "definitions-debug";
066:
067: /**
068: * Name of init property carrying factory class name.
069: */
070: public static final String DEFINITIONS_FACTORY_CLASSNAME = "definitions-factory-class";
071:
072: /**
073: * Constant name used to store factory in context.
074: */
075: public static final String DEFINITIONS_FACTORY = "org.apache.struts.tiles.DEFINITIONS_FACTORY";
076:
077: /**
078: * Constant name used to store definition in jsp context.
079: * Used to pass definition from a Struts action to servlet forward.
080: */
081: public static final String ACTION_DEFINITION = "org.apache.struts.tiles.ACTION_DEFINITION";
082:
083: /**
084: * Create Definition factory.
085: * If a factory class name is provided, a factory of this class is created. Otherwise,
086: * default factory is created.
087: * @param classname Class name of the factory to create.
088: * @param servletContext Servlet Context passed to newly created factory.
089: * @param properties Map of name/property used to initialize factory configuration object.
090: * @return newly created factory.
091: * @throws DefinitionsFactoryException If an error occur while initializing factory
092: * @deprecated Use createDefinitionsFactory(ServletContext servletContext, ServletConfig servletConfig)
093: */
094: public static DefinitionsFactory createDefinitionsFactory(
095: ServletContext servletContext, Map properties,
096: String classname) throws DefinitionsFactoryException {
097:
098: // Create config object
099: DefinitionsFactoryConfig factoryConfig = new DefinitionsFactoryConfig();
100: // populate it from map.
101: try {
102: factoryConfig.populate(properties);
103:
104: } catch (Exception ex) {
105: throw new DefinitionsFactoryException(
106: "Error - createDefinitionsFactory : Can't populate config object from properties map",
107: ex);
108: }
109:
110: // Add classname
111: if (classname != null)
112: factoryConfig.setFactoryClassname(classname);
113:
114: // Create factory using config object
115: return createDefinitionsFactory(servletContext, factoryConfig);
116: }
117:
118: /**
119: * Create default Definition factory.
120: * @param servletContext Servlet Context passed to newly created factory.
121: * @param properties Map of name/property used to initialize factory configuration object.
122: * @return newly created factory of type ConfigurableDefinitionsFactory.
123: * @throws DefinitionsFactoryException If an error occur while initializing factory
124: */
125: public static DefinitionsFactory createDefinitionsFactory(
126: ServletContext servletContext, Map properties)
127: throws DefinitionsFactoryException {
128:
129: return createDefinitionsFactory(servletContext, properties,
130: null);
131: }
132:
133: /**
134: * Create Definition factory.
135: * Create configuration object from servlet web.xml file, then create
136: * ConfigurableDefinitionsFactory and initialized it with object.
137: * <p>
138: * Convenience method. Calls createDefinitionsFactory(ServletContext servletContext, DefinitionsFactoryConfig factoryConfig)
139: *
140: * @param servletContext Servlet Context passed to newly created factory.
141: * @param servletConfig Servlet config containing parameters to be passed to factory configuration object.
142: * @return newly created factory of type ConfigurableDefinitionsFactory.
143: * @throws DefinitionsFactoryException If an error occur while initializing factory
144: */
145: public static DefinitionsFactory createDefinitionsFactory(
146: ServletContext servletContext, ServletConfig servletConfig)
147: throws DefinitionsFactoryException {
148:
149: DefinitionsFactoryConfig factoryConfig = readFactoryConfig(servletConfig);
150:
151: return createDefinitionsFactory(servletContext, factoryConfig);
152: }
153:
154: /**
155: * Create Definition factory.
156: * Create configuration object from servlet web.xml file, then create
157: * ConfigurableDefinitionsFactory and initialized it with object.
158: * <p>
159: * If checkIfExist is true, start by checking if factory already exist. If yes,
160: * return it. If no, create a new one.
161: * <p>
162: * If checkIfExist is false, factory is always created.
163: * <p>
164: * Convenience method. Calls createDefinitionsFactory(ServletContext servletContext, DefinitionsFactoryConfig factoryConfig)
165: *
166: * @param servletContext Servlet Context passed to newly created factory.
167: * @param servletConfig Servlet config containing parameters to be passed to factory configuration object.
168: * @param checkIfExist Check if factory already exist. If true and factory exist, return it.
169: * If true and factory doesn't exist, create it. If false, create it in all cases.
170: * @return newly created factory of type ConfigurableDefinitionsFactory.
171: * @throws DefinitionsFactoryException If an error occur while initializing factory
172: */
173: public static DefinitionsFactory createDefinitionsFactory(
174: ServletContext servletContext, ServletConfig servletConfig,
175: boolean checkIfExist) throws DefinitionsFactoryException {
176:
177: if (checkIfExist) {
178: // Check if already exist in context
179: DefinitionsFactory factory = getDefinitionsFactory(servletContext);
180: if (factory != null)
181: return factory;
182: }
183:
184: return createDefinitionsFactory(servletContext, servletConfig);
185: }
186:
187: /**
188: * Get definition factory from appropriate servlet context.
189: * @return Definitions factory or null if not found.
190: * @deprecated Use {@link TilesUtil#getDefinitionsFactory(ServletRequest, ServletContext)}
191: * @since 20020708
192: */
193: public static DefinitionsFactory getDefinitionsFactory(
194: ServletContext servletContext) {
195: return (DefinitionsFactory) servletContext
196: .getAttribute(DEFINITIONS_FACTORY);
197: }
198:
199: /**
200: * Get Definition stored in jsp context by an action.
201: * @return ComponentDefinition or null if not found.
202: */
203: public static ComponentDefinition getActionDefinition(
204: ServletRequest request) {
205: return (ComponentDefinition) request
206: .getAttribute(ACTION_DEFINITION);
207: }
208:
209: /**
210: * Store definition in jsp context.
211: * Mainly used by Struts to pass a definition defined in an Action to the forward.
212: */
213: public static void setActionDefinition(ServletRequest request,
214: ComponentDefinition definition) {
215:
216: request.setAttribute(ACTION_DEFINITION, definition);
217: }
218:
219: /**
220: * Remove Definition stored in jsp context.
221: * Mainly used by Struts to pass a definition defined in an Action to the forward.
222: */
223: public static void removeActionDefinition(ServletRequest request,
224: ComponentDefinition definition) {
225:
226: request.removeAttribute(ACTION_DEFINITION);
227: }
228:
229: /**
230: * Populate Definition Factory Config from web.xml properties.
231: * @param factoryConfig Definition Factory Config to populate.
232: * @param servletConfig Current servlet config containing web.xml properties.
233: * @exception IllegalAccessException if the caller does not have
234: * access to the property accessor method
235: * @exception java.lang.reflect.InvocationTargetException if the property accessor method
236: * throws an exception
237: * @see org.apache.commons.beanutils.BeanUtils
238: * @since tiles 20020708
239: */
240: public static void populateDefinitionsFactoryConfig(
241: DefinitionsFactoryConfig factoryConfig,
242: ServletConfig servletConfig) throws IllegalAccessException,
243: InvocationTargetException {
244:
245: Map properties = new DefinitionsUtil.ServletPropertiesMap(
246: servletConfig);
247: factoryConfig.populate(properties);
248: }
249:
250: /**
251: * Create FactoryConfig and initialize it from web.xml.
252: *
253: * @param servletConfig ServletConfig for the module with which
254: * this plug in is associated
255: * @exception DefinitionsFactoryException if this <code>PlugIn</code> cannot
256: * be successfully initialized
257: */
258: protected static DefinitionsFactoryConfig readFactoryConfig(
259: ServletConfig servletConfig)
260: throws DefinitionsFactoryException {
261:
262: // Create tiles definitions config object
263: DefinitionsFactoryConfig factoryConfig = new DefinitionsFactoryConfig();
264:
265: // Get init parameters from web.xml files
266: try {
267: DefinitionsUtil.populateDefinitionsFactoryConfig(
268: factoryConfig, servletConfig);
269:
270: } catch (Exception ex) {
271: ex.printStackTrace();
272: throw new DefinitionsFactoryException(
273: "Can't populate DefinitionsFactoryConfig class from 'web.xml'.",
274: ex);
275: }
276:
277: return factoryConfig;
278: }
279:
280: /**
281: * Inner class.
282: * Wrapper for ServletContext init parameters.
283: * Object of this class is an hashmap containing parameters and values
284: * defined in the servlet config file (web.xml).
285: */
286: static class ServletPropertiesMap extends HashMap {
287: /**
288: * Constructor.
289: */
290: ServletPropertiesMap(ServletConfig config) {
291: // This implementation is very simple.
292: // It is possible to avoid creation of a new structure, but this need
293: // imply writing all Map interface.
294: Enumeration e = config.getInitParameterNames();
295: while (e.hasMoreElements()) {
296: String key = (String) e.nextElement();
297: put(key, config.getInitParameter(key));
298: }
299: }
300: } // end inner class
301:
302: }
|