1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.struts.tiles.definition;
20
21 import java.util.HashMap;
22 import java.util.Map;
23
24 import javax.servlet.ServletContext;
25 import javax.servlet.ServletRequest;
26
27 import org.apache.struts.tiles.ComponentDefinition;
28 import org.apache.struts.tiles.ComponentDefinitionsFactory;
29 import org.apache.struts.tiles.DefinitionsFactory;
30 import org.apache.struts.tiles.DefinitionsFactoryConfig;
31 import org.apache.struts.tiles.DefinitionsFactoryException;
32 import org.apache.struts.tiles.NoSuchDefinitionException;
33 import org.apache.struts.util.RequestUtils;
34
35 /***
36 * Wrapper from new definition factory interface to old interface.
37 * This class provides mapping from the old interface's life cycle to the new life cycle.
38 * @since 20020708
39 */
40 public class ComponentDefinitionsFactoryWrapper implements DefinitionsFactory {
41
42 /***
43 * The underlying factory.
44 */
45 private ComponentDefinitionsFactory factory = null;
46
47 /***
48 * Factory configuration,
49 */
50 private DefinitionsFactoryConfig config = null;
51
52 /***
53 * Constructor.
54 * Create new wrapper for specified factory.
55 * @param factory The factory to create a wrapper for.
56 */
57 public ComponentDefinitionsFactoryWrapper(ComponentDefinitionsFactory factory) {
58 this.factory = factory;
59 }
60
61 /***
62 * Constructor.
63 * Create new wrapper.
64 * The config object passed to init method should reference a factory implementing
65 * {@link ComponentDefinitionsFactory}.
66 */
67 public ComponentDefinitionsFactoryWrapper() {
68 super();
69 }
70
71 /***
72 * Get requested definition.
73 * @param name Name of the definition.
74 * @param request The request we are processing.
75 * @param servletContext Our servlet context.
76 * @return ComponentDefition
77 */
78 public ComponentDefinition getDefinition(
79 String name,
80 ServletRequest request,
81 ServletContext servletContext)
82 throws NoSuchDefinitionException, DefinitionsFactoryException {
83
84 return factory.getDefinition(name, request, servletContext);
85 }
86
87 /***
88 * Call underlying factory init method.
89 * @param config DefinitionsFactoryConfig.
90 * @param servletContext Our servlet context.
91 */
92 public void init(DefinitionsFactoryConfig config, ServletContext servletContext)
93 throws DefinitionsFactoryException {
94
95 this.config = config;
96
97
98 if (factory == null) {
99 factory = createFactoryInstance(config.getFactoryClassname());
100 }
101
102 factory.initFactory(servletContext, createConfigMap(config));
103 }
104
105 /***
106 * Do nothing because old life cycle has no equivalent.
107 */
108 public void destroy() {
109 factory = null;
110 }
111
112 /***
113 * Set underlying factory configuration.
114 * @param config DefinitionsFactoryConfig to use.
115 * @param servletContext Our servlet context.
116 *
117 */
118 public void setConfig(
119 DefinitionsFactoryConfig config,
120 ServletContext servletContext)
121 throws DefinitionsFactoryException {
122
123 ComponentDefinitionsFactory newFactory =
124 createFactoryInstance(config.getFactoryClassname());
125
126 newFactory.initFactory(servletContext, createConfigMap(config));
127 factory = newFactory;
128 }
129
130 /***
131 * Get underlying factory configuration.
132 * @return DefinitionsFactoryConfig.
133 */
134 public DefinitionsFactoryConfig getConfig() {
135 return config;
136 }
137
138 /***
139 * Get internal factory.
140 * @return The internal ComponentDefitionsFactory.
141 */
142 public ComponentDefinitionsFactory getInternalFactory() {
143 return factory;
144 }
145
146 /***
147 * Create Definition factory from provided classname which must implement {@link ComponentDefinitionsFactory}.
148 * Factory class must extend {@link DefinitionsFactory}.
149 * @param classname Class name of the factory to create.
150 * @return newly created factory.
151 * @throws DefinitionsFactoryException If an error occur while initializing factory
152 */
153 protected ComponentDefinitionsFactory createFactoryInstance(String classname)
154 throws DefinitionsFactoryException {
155
156 try {
157 Class factoryClass = RequestUtils.applicationClass(classname);
158 Object factory = factoryClass.newInstance();
159 return (ComponentDefinitionsFactory) factory;
160
161 } catch (ClassCastException ex) {
162 throw new DefinitionsFactoryException(
163 "Error - createDefinitionsFactory : Factory class '"
164 + classname
165 + " must implement 'DefinitionsFactory'.",
166 ex);
167
168 } catch (ClassNotFoundException ex) {
169 throw new DefinitionsFactoryException(
170 "Error - createDefinitionsFactory : Bad class name '"
171 + classname
172 + "'.",
173 ex);
174
175 } catch (InstantiationException ex) {
176 throw new DefinitionsFactoryException(ex);
177
178 } catch (IllegalAccessException ex) {
179 throw new DefinitionsFactoryException(ex);
180 }
181
182 }
183
184 /***
185 * Return String representation.
186 * Calls toString() on underlying factory.
187 * @return String representation.
188 */
189 public String toString() {
190 return getInternalFactory().toString();
191 }
192
193 /***
194 * Create map of configuration attributes from configuration object.
195 * Mapping is done between old names and new names.
196 * @param config The DefinitionsFactoryConfig to use.
197 * @return Map Map of name/value pairs.
198 */
199 public static Map createConfigMap(DefinitionsFactoryConfig config) {
200 Map map = new HashMap(config.getAttributes());
201
202 map.put(
203 DefinitionsFactoryConfig.DEFINITIONS_CONFIG_PARAMETER_NAME,
204 config.getDefinitionConfigFiles());
205
206 map.put(
207 DefinitionsFactoryConfig.PARSER_VALIDATE_PARAMETER_NAME,
208 (config.getParserValidate() ? Boolean.TRUE.toString() : Boolean.FALSE.toString()));
209
210 if (!"org.apache.struts.tiles.xmlDefinition.I18nFactorySet"
211 .equals(config.getFactoryClassname())) {
212
213 map.put(
214 DefinitionsFactoryConfig.FACTORY_CLASSNAME_PARAMETER_NAME,
215 config.getFactoryClassname());
216 }
217
218 return map;
219 }
220
221 }