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.Enumeration;
25 import java.util.HashMap;
26 import java.util.Map;
27
28 import jakarta.servlet.ServletConfig;
29 import jakarta.servlet.ServletContext;
30 import jakarta.servlet.ServletRequest;
31 import jakarta.servlet.http.HttpServletRequest;
32
33 import org.apache.struts.tiles.ComponentDefinition;
34 import org.apache.struts.tiles.DefinitionsFactoryException;
35 import org.apache.struts.tiles.FactoryNotFoundException;
36 import org.apache.struts.tiles.xmlDefinition.I18nFactorySet;
37 import org.apache.struts.util.RequestUtils;
38
39
40
41
42
43
44
45
46
47
48 @SuppressWarnings("deprecation")
49 public class ReloadableDefinitionsFactory implements org.apache.struts.tiles.ComponentDefinitionsFactory {
50 private static final long serialVersionUID = 1432127365659932325L;
51
52
53
54
55 protected org.apache.struts.tiles.ComponentDefinitionsFactory factory = null;
56
57
58
59
60 protected Map<String, Object> properties = null;
61
62
63
64
65 public static final String DEFINITIONS_FACTORY_CLASSNAME =
66 "definitions-factory-class";
67
68
69
70
71
72
73
74
75 public ReloadableDefinitionsFactory(
76 ServletContext servletContext,
77 ServletConfig servletConfig)
78 throws DefinitionsFactoryException {
79
80 properties = new ServletPropertiesMap(servletConfig);
81 factory = createFactory(servletContext, properties);
82 }
83
84
85
86
87
88
89
90
91 public ReloadableDefinitionsFactory(
92 ServletContext servletContext,
93 Map<String, Object> properties)
94 throws DefinitionsFactoryException {
95
96 this.properties = properties;
97 factory = createFactory(servletContext, properties);
98 }
99
100
101
102
103
104
105
106
107
108
109
110
111 public org.apache.struts.tiles.ComponentDefinitionsFactory createFactoryFromClassname(
112 ServletContext servletContext,
113 Map<String, Object> properties,
114 String classname)
115 throws DefinitionsFactoryException {
116
117 if (classname == null) {
118 return createFactory(servletContext, properties);
119 }
120
121
122 try {
123 Class<?> factoryClass = RequestUtils.applicationClass(classname);
124 org.apache.struts.tiles.ComponentDefinitionsFactory factory =
125 (org.apache.struts.tiles.ComponentDefinitionsFactory) factoryClass.newInstance();
126 factory.initFactory(servletContext, properties);
127 return factory;
128
129 } catch (ClassCastException ex) {
130 throw new DefinitionsFactoryException(
131 "Error - createDefinitionsFactory : Factory class '"
132 + classname
133 + " must implements 'ComponentDefinitionsFactory'.",
134 ex);
135
136 } catch (ClassNotFoundException ex) {
137 throw new DefinitionsFactoryException(
138 "Error - createDefinitionsFactory : Bad class name '"
139 + classname
140 + "'.",
141 ex);
142
143 } catch (InstantiationException ex) {
144 throw new DefinitionsFactoryException(ex);
145
146 } catch (IllegalAccessException ex) {
147 throw new DefinitionsFactoryException(ex);
148 }
149
150 }
151
152
153
154
155
156
157
158
159
160
161 public org.apache.struts.tiles.ComponentDefinitionsFactory createDefaultFactory(
162 ServletContext servletContext,
163 Map<String, Object> properties)
164 throws DefinitionsFactoryException {
165
166 org.apache.struts.tiles.ComponentDefinitionsFactory factory =
167 new I18nFactorySet(servletContext, properties);
168
169 return factory;
170 }
171
172
173
174
175
176
177
178
179
180 public org.apache.struts.tiles.ComponentDefinitionsFactory createFactory(
181 ServletContext servletContext,
182 Map<String, Object> properties)
183 throws DefinitionsFactoryException {
184
185 String classname = (String) properties.get(DEFINITIONS_FACTORY_CLASSNAME);
186
187 if (classname != null) {
188 return createFactoryFromClassname(servletContext, properties, classname);
189 }
190
191 return new I18nFactorySet(servletContext, properties);
192 }
193
194
195
196
197
198
199
200
201
202
203
204 public ComponentDefinition getDefinition(
205 String definitionName,
206 ServletRequest request,
207 ServletContext servletContext)
208 throws FactoryNotFoundException, DefinitionsFactoryException {
209
210 return factory.getDefinition(
211 definitionName,
212 (HttpServletRequest) request,
213 servletContext);
214 }
215
216
217
218
219
220
221
222
223 public void reload(ServletContext servletContext)
224 throws DefinitionsFactoryException {
225
226 org.apache.struts.tiles.ComponentDefinitionsFactory newInstance =
227 createFactory(servletContext, properties);
228
229 factory = newInstance;
230 }
231
232
233
234
235
236 public org.apache.struts.tiles.ComponentDefinitionsFactory getFactory() {
237 return factory;
238 }
239
240
241
242
243
244
245
246
247
248
249
250 public void initFactory(ServletContext servletContext, Map<String, Object> properties)
251 throws DefinitionsFactoryException {
252
253 }
254
255
256
257
258
259 public String toString() {
260 return factory.toString();
261 }
262
263
264
265
266
267
268
269 class ServletPropertiesMap extends HashMap<String, Object> {
270 private static final long serialVersionUID = -1834721489197689992L;
271
272
273
274
275 ServletPropertiesMap(ServletConfig config) {
276
277
278
279 Enumeration<String> e = config.getInitParameterNames();
280 while (e.hasMoreElements()) {
281 String key = e.nextElement();
282 put(key, config.getInitParameter(key));
283 }
284 }
285 }
286 }