001: /*
002: * $Id: ComponentDefinitionsFactoryWrapper.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.definition;
023:
024: import java.util.HashMap;
025: import java.util.Map;
026:
027: import javax.servlet.ServletContext;
028: import javax.servlet.ServletRequest;
029:
030: import org.apache.struts.tiles.ComponentDefinition;
031: import org.apache.struts.tiles.ComponentDefinitionsFactory;
032: import org.apache.struts.tiles.DefinitionsFactory;
033: import org.apache.struts.tiles.DefinitionsFactoryConfig;
034: import org.apache.struts.tiles.DefinitionsFactoryException;
035: import org.apache.struts.tiles.NoSuchDefinitionException;
036: import org.apache.struts.util.RequestUtils;
037:
038: /**
039: * Wrapper from new definition factory interface to old interface.
040: * This class provides mapping from the old interface's life cycle to the new life cycle.
041: * @since 20020708
042: */
043: public class ComponentDefinitionsFactoryWrapper implements
044: DefinitionsFactory {
045:
046: /**
047: * The underlying factory.
048: */
049: private ComponentDefinitionsFactory factory = null;
050:
051: /**
052: * Factory configuration,
053: */
054: private DefinitionsFactoryConfig config = null;
055:
056: /**
057: * Constructor.
058: * Create new wrapper for specified factory.
059: * @param factory The factory to create a wrapper for.
060: */
061: public ComponentDefinitionsFactoryWrapper(
062: ComponentDefinitionsFactory factory) {
063: this .factory = factory;
064: }
065:
066: /**
067: * Constructor.
068: * Create new wrapper.
069: * The config object passed to init method should reference a factory implementing
070: * {@link ComponentDefinitionsFactory}.
071: */
072: public ComponentDefinitionsFactoryWrapper() {
073: super ();
074: }
075:
076: /**
077: * Get requested definition.
078: * @param name Name of the definition.
079: * @param request The request we are processing.
080: * @param servletContext Our servlet context.
081: * @return ComponentDefition
082: */
083: public ComponentDefinition getDefinition(String name,
084: ServletRequest request, ServletContext servletContext)
085: throws NoSuchDefinitionException,
086: DefinitionsFactoryException {
087:
088: return factory.getDefinition(name, request, servletContext);
089: }
090:
091: /**
092: * Call underlying factory init method.
093: * @param config DefinitionsFactoryConfig.
094: * @param servletContext Our servlet context.
095: */
096: public void init(DefinitionsFactoryConfig config,
097: ServletContext servletContext)
098: throws DefinitionsFactoryException {
099:
100: this .config = config;
101:
102: // create factory and initialize it
103: if (factory == null) {
104: factory = createFactoryInstance(config
105: .getFactoryClassname());
106: }
107:
108: factory.initFactory(servletContext, createConfigMap(config));
109: }
110:
111: /**
112: * Do nothing because old life cycle has no equivalent.
113: */
114: public void destroy() {
115: factory = null;
116: }
117:
118: /**
119: * Set underlying factory configuration.
120: * @param config DefinitionsFactoryConfig to use.
121: * @param servletContext Our servlet context.
122: *
123: */
124: public void setConfig(DefinitionsFactoryConfig config,
125: ServletContext servletContext)
126: throws DefinitionsFactoryException {
127:
128: ComponentDefinitionsFactory newFactory = createFactoryInstance(config
129: .getFactoryClassname());
130:
131: newFactory.initFactory(servletContext, createConfigMap(config));
132: factory = newFactory;
133: }
134:
135: /**
136: * Get underlying factory configuration.
137: * @return DefinitionsFactoryConfig.
138: */
139: public DefinitionsFactoryConfig getConfig() {
140: return config;
141: }
142:
143: /**
144: * Get internal factory.
145: * @return The internal ComponentDefitionsFactory.
146: */
147: public ComponentDefinitionsFactory getInternalFactory() {
148: return factory;
149: }
150:
151: /**
152: * Create Definition factory from provided classname which must implement {@link ComponentDefinitionsFactory}.
153: * Factory class must extend {@link DefinitionsFactory}.
154: * @param classname Class name of the factory to create.
155: * @return newly created factory.
156: * @throws DefinitionsFactoryException If an error occur while initializing factory
157: */
158: protected ComponentDefinitionsFactory createFactoryInstance(
159: String classname) throws DefinitionsFactoryException {
160:
161: try {
162: Class factoryClass = RequestUtils
163: .applicationClass(classname);
164: Object factory = factoryClass.newInstance();
165: return (ComponentDefinitionsFactory) factory;
166:
167: } catch (ClassCastException ex) { // Bad classname
168: throw new DefinitionsFactoryException(
169: "Error - createDefinitionsFactory : Factory class '"
170: + classname
171: + " must implement 'DefinitionsFactory'.",
172: ex);
173:
174: } catch (ClassNotFoundException ex) { // Bad classname
175: throw new DefinitionsFactoryException(
176: "Error - createDefinitionsFactory : Bad class name '"
177: + classname + "'.", ex);
178:
179: } catch (InstantiationException ex) { // Bad constructor or error
180: throw new DefinitionsFactoryException(ex);
181:
182: } catch (IllegalAccessException ex) {
183: throw new DefinitionsFactoryException(ex);
184: }
185:
186: }
187:
188: /**
189: * Return String representation.
190: * Calls toString() on underlying factory.
191: * @return String representation.
192: */
193: public String toString() {
194: return getInternalFactory().toString();
195: }
196:
197: /**
198: * Create map of configuration attributes from configuration object.
199: * Mapping is done between old names and new names.
200: * @param config The DefinitionsFactoryConfig to use.
201: * @return Map Map of name/value pairs.
202: */
203: public static Map createConfigMap(DefinitionsFactoryConfig config) {
204: Map map = new HashMap(config.getAttributes());
205: // Add property attributes using old names
206: map
207: .put(
208: DefinitionsFactoryConfig.DEFINITIONS_CONFIG_PARAMETER_NAME,
209: config.getDefinitionConfigFiles());
210:
211: map
212: .put(
213: DefinitionsFactoryConfig.PARSER_VALIDATE_PARAMETER_NAME,
214: (config.getParserValidate() ? Boolean.TRUE
215: .toString() : Boolean.FALSE.toString()));
216:
217: if (!"org.apache.struts.tiles.xmlDefinition.I18nFactorySet"
218: .equals(config.getFactoryClassname())) {
219:
220: map
221: .put(
222: DefinitionsFactoryConfig.FACTORY_CLASSNAME_PARAMETER_NAME,
223: config.getFactoryClassname());
224: }
225:
226: return map;
227: }
228:
229: }
|