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;
23
24 import java.util.Map;
25
26 import javax.servlet.ServletContext;
27 import javax.servlet.ServletException;
28 import javax.servlet.UnavailableException;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.apache.struts.action.ActionServlet;
33 import org.apache.struts.action.PlugIn;
34 import org.apache.struts.action.RequestProcessor;
35 import org.apache.struts.chain.ComposableRequestProcessor;
36 import org.apache.struts.config.ControllerConfig;
37 import org.apache.struts.config.ModuleConfig;
38 import org.apache.struts.config.PlugInConfig;
39 import org.apache.struts.util.RequestUtils;
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64 public class TilesPlugin implements PlugIn {
65
66
67
68
69 protected static Log log = LogFactory.getLog(TilesPlugin.class);
70
71
72
73
74 protected boolean moduleAware = false;
75
76
77
78
79
80 protected String tilesUtilImplClassname = null;
81
82
83
84
85 protected DefinitionsFactory definitionFactory = null;
86
87
88
89
90
91 protected PlugInConfig currentPlugInConfigObject=null;
92
93
94
95
96
97
98 public boolean isModuleAware() {
99 return moduleAware;
100 }
101
102
103
104
105
106
107
108
109 public void setModuleAware(boolean moduleAware) {
110 this.moduleAware = moduleAware;
111 }
112
113
114
115
116
117
118
119
120
121
122
123
124
125 public void init(ActionServlet servlet, ModuleConfig moduleConfig)
126 throws ServletException {
127
128
129 DefinitionsFactoryConfig factoryConfig =
130 readFactoryConfig(servlet, moduleConfig);
131
132
133
134 factoryConfig.setFactoryName(moduleConfig.getPrefix());
135
136
137 this.initRequestProcessorClass(moduleConfig);
138
139 this.initTilesUtil();
140
141 this.initDefinitionsFactory(servlet.getServletContext(), moduleConfig, factoryConfig);
142 }
143
144
145
146
147
148
149
150
151 private void initTilesUtil() throws ServletException {
152
153 if (TilesUtil.isTilesUtilImplSet()) {
154 return;
155 }
156
157
158
159
160
161 if (this.getTilesUtilImplClassname() == null) {
162
163 if (isModuleAware()) {
164 TilesUtil.setTilesUtil(new TilesUtilStrutsModulesImpl());
165 } else {
166 TilesUtil.setTilesUtil(new TilesUtilStrutsImpl());
167 }
168
169 } else {
170 try {
171 TilesUtilStrutsImpl impl =
172 (TilesUtilStrutsImpl) RequestUtils
173 .applicationClass(getTilesUtilImplClassname())
174 .newInstance();
175 TilesUtil.setTilesUtil(impl);
176
177 } catch (ClassCastException ex) {
178 throw new ServletException(
179 "Can't set TilesUtil implementation to '"
180 + getTilesUtilImplClassname()
181 + "'. TilesUtil implementation should be a subclass of '"
182 + TilesUtilStrutsImpl.class.getName()
183 + "'");
184
185 } catch (Exception ex) {
186 throw new ServletException(
187 "Can't set TilesUtil implementation.",
188 ex);
189 }
190 }
191
192 }
193
194
195
196
197
198
199
200
201 private void initDefinitionsFactory(
202 ServletContext servletContext,
203 ModuleConfig moduleConfig,
204 DefinitionsFactoryConfig factoryConfig)
205 throws ServletException {
206
207
208 definitionFactory =
209 ((TilesUtilStrutsImpl) TilesUtil.getTilesUtil()).getDefinitionsFactory(
210 servletContext,
211 moduleConfig);
212
213 if (definitionFactory != null) {
214 log.info(
215 "Factory already exists for module '"
216 + moduleConfig.getPrefix()
217 + "'. The factory found is from module '"
218 + definitionFactory.getConfig().getFactoryName()
219 + "'. No new creation.");
220
221 return;
222 }
223
224
225 try {
226 definitionFactory =
227 TilesUtil.createDefinitionsFactory(
228 servletContext,
229 factoryConfig);
230
231 } catch (DefinitionsFactoryException ex) {
232 log.error(
233 "Can't create Tiles definition factory for module '"
234 + moduleConfig.getPrefix()
235 + "'.");
236
237 throw new ServletException(ex);
238 }
239
240 log.info(
241 "Tiles definition factory loaded for module '"
242 + moduleConfig.getPrefix()
243 + "'.");
244 }
245
246
247
248
249 public void destroy() {
250 definitionFactory.destroy();
251 definitionFactory = null;
252 }
253
254
255
256
257
258
259
260
261
262
263
264 protected DefinitionsFactoryConfig readFactoryConfig(
265 ActionServlet servlet,
266 ModuleConfig config)
267 throws ServletException {
268
269
270 DefinitionsFactoryConfig factoryConfig = new DefinitionsFactoryConfig();
271
272 try {
273 DefinitionsUtil.populateDefinitionsFactoryConfig(
274 factoryConfig,
275 servlet.getServletConfig());
276
277 } catch (Exception ex) {
278 if (log.isDebugEnabled()){
279 log.debug("", ex);
280 }
281 ex.printStackTrace();
282 throw new UnavailableException(
283 "Can't populate DefinitionsFactoryConfig class from 'web.xml': "
284 + ex.getMessage());
285 }
286
287
288 try {
289 Map strutsProperties = findStrutsPlugInConfigProperties(servlet, config);
290 factoryConfig.populate(strutsProperties);
291
292 } catch (Exception ex) {
293 if (log.isDebugEnabled()) {
294 log.debug("", ex);
295 }
296
297 throw new UnavailableException(
298 "Can't populate DefinitionsFactoryConfig class from '"
299 + config.getPrefix()
300 + "/struts-config.xml':"
301 + ex.getMessage());
302 }
303
304 return factoryConfig;
305 }
306
307
308
309
310
311
312
313
314
315
316
317
318
319 protected Map findStrutsPlugInConfigProperties(
320 ActionServlet servlet,
321 ModuleConfig config)
322 throws ServletException {
323
324 return currentPlugInConfigObject.getProperties();
325 }
326
327
328
329
330
331
332
333
334
335
336
337 protected void initRequestProcessorClass(ModuleConfig config)
338 throws ServletException {
339
340 String tilesProcessorClassname = TilesRequestProcessor.class.getName();
341 ControllerConfig ctrlConfig = config.getControllerConfig();
342 String configProcessorClassname = ctrlConfig.getProcessorClass();
343
344
345 Class configProcessorClass;
346 try {
347 configProcessorClass =
348 RequestUtils.applicationClass(configProcessorClassname);
349
350 } catch (ClassNotFoundException ex) {
351 log.fatal(
352 "Can't set TilesRequestProcessor: bad class name '"
353 + configProcessorClassname
354 + "'.");
355 throw new ServletException(ex);
356 }
357
358
359
360 if (ComposableRequestProcessor.class.isAssignableFrom(configProcessorClass)) {
361 return;
362 }
363
364
365
366 if (configProcessorClassname.equals(RequestProcessor.class.getName())
367 || configProcessorClassname.endsWith(tilesProcessorClassname)) {
368
369 ctrlConfig.setProcessorClass(tilesProcessorClassname);
370 return;
371 }
372
373
374 Class tilesProcessorClass = TilesRequestProcessor.class;
375 if (!tilesProcessorClass.isAssignableFrom(configProcessorClass)) {
376
377 String msg =
378 "TilesPlugin : Specified RequestProcessor not compatible with TilesRequestProcessor";
379 if (log.isFatalEnabled()) {
380 log.fatal(msg);
381 }
382 throw new ServletException(msg);
383 }
384 }
385
386
387
388
389
390
391 public void setTilesUtilImplClassname(String tilesUtilImplClassname) {
392 this.tilesUtilImplClassname = tilesUtilImplClassname;
393 }
394
395
396
397
398
399 public String getTilesUtilImplClassname() {
400 return tilesUtilImplClassname;
401 }
402
403
404
405
406
407
408 public void setCurrentPlugInConfigObject(PlugInConfig plugInConfigObject) {
409 this.currentPlugInConfigObject = plugInConfigObject;
410 }
411
412 }