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.io.Serializable;
25 import java.lang.reflect.InvocationTargetException;
26 import java.util.HashMap;
27 import java.util.Map;
28
29 import org.apache.struts.tiles.xmlDefinition.XmlDefinition;
30 import org.apache.struts.util.RequestUtils;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34
35
36
37
38
39 public class ComponentDefinition implements Serializable {
40 private static final long serialVersionUID = -62457661179275424L;
41
42
43
44
45 private final static Logger LOG =
46 LoggerFactory.getLogger(ComponentDefinition.class);
47
48
49
50
51 protected String name = null;
52
53
54
55
56 protected String path = null;
57
58
59
60
61 protected Map<String, Object> attributes = null;
62
63
64
65
66 protected String role = null;
67
68
69 protected String controller = null;
70
71
72
73
74
75 protected String controllerType = null;
76
77
78
79
80 public static final String URL = "url";
81
82
83
84
85 public static final String CONTROLLER = "controller";
86
87
88
89
90 public static final String ACTION = "action";
91
92
93
94
95
96 private Controller controllerInstance = null;
97
98
99
100
101 public ComponentDefinition() {
102 attributes = new HashMap<>();
103 }
104
105
106
107
108
109
110
111 public ComponentDefinition(ComponentDefinition definition) {
112 attributes = new HashMap<>(definition.getAttributes());
113 this.name = definition.getName();
114 this.path = definition.getPath();
115 this.role = definition.getRole();
116 this.controllerInstance = definition.getControllerInstance();
117 this.controller = definition.getController();
118 this.controllerType = definition.getControllerType();
119 }
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135 public ComponentDefinition(XmlDefinition definition) {
136
137 this((ComponentDefinition) definition);
138 }
139
140
141
142
143 public ComponentDefinition(String name, String path, Map<String, Object> attributes) {
144 this.name = name;
145 this.path = path;
146 this.attributes = attributes;
147 }
148
149
150
151
152
153
154 public String getName() {
155 return name;
156 }
157
158
159
160
161
162
163 public void setName(String aName) {
164 name = aName;
165 }
166
167
168
169
170
171
172 public String getPage() {
173 return path;
174 }
175
176
177
178
179
180
181 public void setPage(String page) {
182 path = page;
183 }
184
185
186
187
188
189
190 public String getPath() {
191 return path;
192 }
193
194
195
196
197
198
199 public void setPath(String aPath) {
200 path = aPath;
201 }
202
203
204
205
206
207
208 public String getTemplate() {
209 return path;
210 }
211
212
213
214
215
216
217
218 public void setTemplate(String template) {
219 path = template;
220 }
221
222
223
224
225
226 public String getRole() {
227 return role;
228 }
229
230
231
232
233
234
235 public void setRole(String role) {
236 this.role = role;
237 }
238
239
240
241
242
243
244 public Map<String, Object> getAttributes() {
245 return attributes;
246 }
247
248
249
250
251
252
253
254 public Object getAttribute(String key) {
255 return attributes.get(key);
256 }
257
258
259
260
261
262
263
264 public void putAttribute(String key, Object value) {
265 attributes.put(key, value);
266 }
267
268
269
270
271
272
273
274 public void put(String name, Object content) {
275 put(name, content, false, null);
276 }
277
278
279
280
281
282
283
284
285 public void put(String name, Object content, boolean direct) {
286 put(name, content, direct, null);
287 }
288
289
290
291
292
293
294
295
296
297 public void put(String name, Object content, boolean direct, String role) {
298 if (direct == true) {
299 put(name, content, "string", role);
300 } else {
301 put(name, content, "template", role);
302 }
303
304 }
305
306
307
308
309
310
311
312
313
314 public void put(String name, Object content, String type, String role) {
315
316
317
318 AttributeDefinition attribute = null;
319
320 if (content != null
321 && type != null
322 && !(content instanceof AttributeDefinition)) {
323
324 String strValue = content.toString();
325 if (type.equalsIgnoreCase("string")) {
326 attribute = new DirectStringAttribute(strValue);
327
328 } else if (type.equalsIgnoreCase("page")) {
329 attribute = new PathAttribute(strValue);
330
331 } else if (type.equalsIgnoreCase("template")) {
332 attribute = new PathAttribute(strValue);
333
334 } else if (type.equalsIgnoreCase("instance")) {
335 attribute = new DefinitionNameAttribute(strValue);
336
337 } else if (type.equalsIgnoreCase("definition")) {
338 attribute = new DefinitionNameAttribute(strValue);
339 }
340 }
341
342 putAttribute(name, attribute);
343 }
344
345
346
347
348 public String toString() {
349 return "{name="
350 + name
351 + ", path="
352 + path
353 + ", role="
354 + role
355 + ", controller="
356 + controller
357 + ", controllerType="
358 + controllerType
359 + ", controllerInstance="
360 + controllerInstance
361 + ", attributes="
362 + attributes
363 + "}\n";
364 }
365
366
367
368
369
370 public String getControllerType() {
371 return controllerType;
372 }
373
374
375
376
377
378
379 public void setControllerType(String controllerType) {
380 this.controllerType = controllerType;
381 }
382
383
384
385
386
387
388
389
390 public void setControllerUrl(String controller) {
391 setController(controller);
392 setControllerType("url");
393 }
394
395
396
397
398
399
400
401
402 public void setControllerClass(String controller) {
403 setController(controller);
404 setControllerType("classname");
405 }
406
407
408
409
410
411
412 public String getController() {
413 return controller;
414 }
415
416
417
418
419
420
421
422 public void setController(String url) {
423 this.controller = url;
424 }
425
426
427
428
429
430 public Controller getControllerInstance() {
431 return controllerInstance;
432 }
433
434
435
436
437
438
439
440
441
442 public Controller getOrCreateController() throws InstantiationException {
443
444 if (controllerInstance != null) {
445 return controllerInstance;
446 }
447
448
449 if (controller == null && controllerType == null) {
450 return null;
451 }
452
453
454 if (controllerType != null && controller == null) {
455 throw new InstantiationException("Controller name should be defined if controllerType is set");
456 }
457
458 controllerInstance = createController(controller, controllerType);
459
460 return controllerInstance;
461 }
462
463
464
465
466 public void setControllerInstance(Controller controller) {
467 this.controllerInstance = controller;
468 }
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484 public static Controller createController(String name, String controllerType)
485 throws InstantiationException {
486
487 LOG.debug("Create controller name={}, type={}", name, controllerType);
488
489 Controller controller = null;
490
491 if (controllerType == null) {
492 try {
493 return createControllerFromClassname(name);
494
495 } catch (InstantiationException ex) {
496 controller = new UrlController(name);
497 }
498
499 } else if ("url".equalsIgnoreCase(controllerType)) {
500 controller = new UrlController(name);
501
502 } else if ("classname".equalsIgnoreCase(controllerType)) {
503 controller = createControllerFromClassname(name);
504 }
505
506 return controller;
507 }
508
509
510
511
512
513
514
515
516
517
518
519
520 public static Controller createControllerFromClassname(String classname)
521 throws InstantiationException {
522
523 try {
524 Class<?> requestedClass = RequestUtils.applicationClass(classname);
525 Object instance = requestedClass.getDeclaredConstructor().newInstance();
526
527 LOG.debug("Controller created : {}", instance);
528 return (Controller) instance;
529
530 } catch (ClassNotFoundException ex) {
531 InstantiationException e2 = new InstantiationException(
532 "Error - Class not found :" + classname);
533 e2.initCause(ex);
534 throw e2;
535
536 } catch (IllegalAccessException ex) {
537 InstantiationException e2 = new InstantiationException(
538 "Error - Illegal class access :" + classname);
539 e2.initCause(ex);
540 throw e2;
541
542 } catch (IllegalArgumentException ex) {
543 InstantiationException e2 = new InstantiationException(
544 "Error - Illegal class argument :" + classname);
545 e2.initCause(ex);
546 throw e2;
547
548 } catch (InvocationTargetException ex) {
549 InstantiationException e2 = new InstantiationException(
550 "Error - Invocation class target :" + classname);
551 e2.initCause(ex);
552 throw e2;
553
554 } catch (NoSuchMethodException ex) {
555 InstantiationException e2 = new InstantiationException(
556 "Error - No such method in class:" + classname);
557 e2.initCause(ex);
558 throw e2;
559
560 } catch (SecurityException ex) {
561 InstantiationException e2 = new InstantiationException(
562 "Error - Security exception in class :" + classname);
563 e2.initCause(ex);
564 throw e2;
565
566 } catch (InstantiationException ex) {
567 throw ex;
568
569 } catch (ClassCastException ex) {
570 InstantiationException e2 = new InstantiationException(
571 "Controller of class '"
572 + classname
573 + "' should implements 'Controller' or extends 'Action'");
574 e2.initCause(ex);
575 throw e2;
576 }
577 }
578 }