1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.struts.tiles.definition;
23
24 import java.util.HashMap;
25 import java.util.Map;
26
27 import javax.servlet.ServletContext;
28 import javax.servlet.ServletRequest;
29
30 import org.apache.struts.tiles.ComponentDefinition;
31 import org.apache.struts.tiles.ComponentDefinitionsFactory;
32 import org.apache.struts.tiles.DefinitionsFactory;
33 import org.apache.struts.tiles.DefinitionsFactoryConfig;
34 import org.apache.struts.tiles.DefinitionsFactoryException;
35 import org.apache.struts.tiles.NoSuchDefinitionException;
36 import org.apache.struts.util.RequestUtils;
37
38
39
40
41
42
43 public class ComponentDefinitionsFactoryWrapper implements DefinitionsFactory {
44
45
46
47
48 private ComponentDefinitionsFactory factory = null;
49
50
51
52
53 private DefinitionsFactoryConfig config = null;
54
55
56
57
58
59
60 public ComponentDefinitionsFactoryWrapper(ComponentDefinitionsFactory factory) {
61 this.factory = factory;
62 }
63
64
65
66
67
68
69
70 public ComponentDefinitionsFactoryWrapper() {
71 super();
72 }
73
74
75
76
77
78
79
80
81 public ComponentDefinition getDefinition(
82 String name,
83 ServletRequest request,
84 ServletContext servletContext)
85 throws NoSuchDefinitionException, DefinitionsFactoryException {
86
87 return factory.getDefinition(name, request, servletContext);
88 }
89
90
91
92
93
94
95 public void init(DefinitionsFactoryConfig config, ServletContext servletContext)
96 throws DefinitionsFactoryException {
97
98 this.config = config;
99
100
101 if (factory == null) {
102 factory = createFactoryInstance(config.getFactoryClassname());
103 }
104
105 factory.initFactory(servletContext, createConfigMap(config));
106 }
107
108
109
110
111 public void destroy() {
112 factory = null;
113 }
114
115
116
117
118
119
120
121 public void setConfig(
122 DefinitionsFactoryConfig config,
123 ServletContext servletContext)
124 throws DefinitionsFactoryException {
125
126 ComponentDefinitionsFactory newFactory =
127 createFactoryInstance(config.getFactoryClassname());
128
129 newFactory.initFactory(servletContext, createConfigMap(config));
130 factory = newFactory;
131 }
132
133
134
135
136
137 public DefinitionsFactoryConfig getConfig() {
138 return config;
139 }
140
141
142
143
144
145 public ComponentDefinitionsFactory getInternalFactory() {
146 return factory;
147 }
148
149
150
151
152
153
154
155
156 protected ComponentDefinitionsFactory createFactoryInstance(String classname)
157 throws DefinitionsFactoryException {
158
159 try {
160 Class factoryClass = RequestUtils.applicationClass(classname);
161 Object factory = factoryClass.newInstance();
162 return (ComponentDefinitionsFactory) factory;
163
164 } catch (ClassCastException ex) {
165 throw new DefinitionsFactoryException(
166 "Error - createDefinitionsFactory : Factory class '"
167 + classname
168 + " must implement 'DefinitionsFactory'.",
169 ex);
170
171 } catch (ClassNotFoundException ex) {
172 throw new DefinitionsFactoryException(
173 "Error - createDefinitionsFactory : Bad class name '"
174 + classname
175 + "'.",
176 ex);
177
178 } catch (InstantiationException ex) {
179 throw new DefinitionsFactoryException(ex);
180
181 } catch (IllegalAccessException ex) {
182 throw new DefinitionsFactoryException(ex);
183 }
184
185 }
186
187
188
189
190
191
192 public String toString() {
193 return getInternalFactory().toString();
194 }
195
196
197
198
199
200
201
202 public static Map createConfigMap(DefinitionsFactoryConfig config) {
203 Map map = new HashMap(config.getAttributes());
204
205 map.put(
206 DefinitionsFactoryConfig.DEFINITIONS_CONFIG_PARAMETER_NAME,
207 config.getDefinitionConfigFiles());
208
209 map.put(
210 DefinitionsFactoryConfig.PARSER_VALIDATE_PARAMETER_NAME,
211 (config.getParserValidate() ? Boolean.TRUE.toString() : Boolean.FALSE.toString()));
212
213 if (!"org.apache.struts.tiles.xmlDefinition.I18nFactorySet"
214 .equals(config.getFactoryClassname())) {
215
216 map.put(
217 DefinitionsFactoryConfig.FACTORY_CLASSNAME_PARAMETER_NAME,
218 config.getFactoryClassname());
219 }
220
221 return map;
222 }
223
224 }