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