1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts.config.impl;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.struts.config.ActionConfig;
26 import org.apache.struts.config.ActionConfigMatcher;
27 import org.apache.struts.config.BaseConfig;
28 import org.apache.struts.config.ControllerConfig;
29 import org.apache.struts.config.ExceptionConfig;
30 import org.apache.struts.config.FormBeanConfig;
31 import org.apache.struts.config.ForwardConfig;
32 import org.apache.struts.config.MessageResourcesConfig;
33 import org.apache.struts.config.ModuleConfig;
34 import org.apache.struts.config.PlugInConfig;
35
36 import java.io.Serializable;
37
38 import java.util.ArrayList;
39 import java.util.HashMap;
40 import java.util.List;
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 public class ModuleConfigImpl extends BaseConfig implements Serializable,
56 ModuleConfig {
57
58
59
60 protected static Log log = LogFactory.getLog(ModuleConfigImpl.class);
61
62
63
64
65
66
67
68
69 protected HashMap actionConfigs = null;
70
71
72
73
74
75 protected HashMap actionConfigIds = null;
76
77
78
79
80
81 protected List actionConfigList = null;
82
83
84
85
86
87 protected HashMap exceptions = null;
88
89
90
91
92
93 protected HashMap formBeans = null;
94
95
96
97
98
99 protected HashMap forwards = null;
100
101
102
103
104
105 protected HashMap messageResources = null;
106
107
108
109
110
111 protected ArrayList plugIns = null;
112
113
114
115
116 protected ControllerConfig controllerConfig = null;
117
118
119
120
121
122
123
124 protected String prefix = null;
125
126
127
128
129
130 protected String actionFormBeanClass =
131 "org.apache.struts.action.ActionFormBean";
132
133
134
135
136
137 protected String actionMappingClass =
138 "org.apache.struts.action.ActionMapping";
139
140
141
142
143
144 protected String actionForwardClass =
145 "org.apache.struts.action.ActionForward";
146
147
148
149
150 protected ActionConfigMatcher matcher = null;
151
152
153
154
155
156
157
158 public ModuleConfigImpl() {
159 this("");
160 }
161
162
163
164
165
166
167
168 public ModuleConfigImpl(String prefix) {
169 super();
170 this.prefix = prefix;
171 this.actionConfigs = new HashMap();
172 this.actionConfigIds = new HashMap();
173 this.actionConfigList = new ArrayList();
174 this.actionFormBeanClass = "org.apache.struts.action.ActionFormBean";
175 this.actionMappingClass = "org.apache.struts.action.ActionMapping";
176 this.actionForwardClass = "org.apache.struts.action.ActionForward";
177 this.configured = false;
178 this.controllerConfig = null;
179 this.exceptions = new HashMap();
180 this.formBeans = new HashMap();
181 this.forwards = new HashMap();
182 this.messageResources = new HashMap();
183 this.plugIns = new ArrayList();
184 }
185
186
187
188
189
190
191
192
193 public boolean getConfigured() {
194 return (this.configured);
195 }
196
197
198
199
200 public ControllerConfig getControllerConfig() {
201 if (this.controllerConfig == null) {
202 this.controllerConfig = new ControllerConfig();
203 }
204
205 return (this.controllerConfig);
206 }
207
208
209
210
211
212
213 public void setControllerConfig(ControllerConfig cc) {
214 throwIfConfigured();
215 this.controllerConfig = cc;
216 }
217
218
219
220
221
222
223
224 public String getPrefix() {
225 return (this.prefix);
226 }
227
228
229
230
231
232
233
234 public void setPrefix(String prefix) {
235 throwIfConfigured();
236 this.prefix = prefix;
237 }
238
239
240
241
242
243 public String getActionFormBeanClass() {
244 return this.actionFormBeanClass;
245 }
246
247
248
249
250
251
252
253
254 public void setActionFormBeanClass(String actionFormBeanClass) {
255 this.actionFormBeanClass = actionFormBeanClass;
256 }
257
258
259
260
261
262 public String getActionMappingClass() {
263 return this.actionMappingClass;
264 }
265
266
267
268
269
270
271
272
273 public void setActionMappingClass(String actionMappingClass) {
274 this.actionMappingClass = actionMappingClass;
275 }
276
277
278
279
280
281
282
283
284
285 public void addActionConfig(ActionConfig config) {
286 throwIfConfigured();
287 config.setModuleConfig(this);
288
289 String path = config.getPath();
290 if (actionConfigs.containsKey(path)) {
291 log.warn("Overriding ActionConfig of path " + path);
292 }
293
294 String actionId = config.getActionId();
295 if ((actionId != null) && !actionId.equals("")) {
296 if (actionConfigIds.containsKey(actionId)) {
297 if (log.isWarnEnabled()) {
298 ActionConfig otherConfig = (ActionConfig) actionConfigIds.get(actionId);
299 StringBuffer msg = new StringBuffer("Overriding actionId[");
300 msg.append(actionId);
301 msg.append("] for path[");
302 msg.append(otherConfig.getPath());
303 msg.append("] with path[");
304 msg.append(path);
305 msg.append("]");
306 log.warn(msg);
307 }
308 }
309 actionConfigIds.put(actionId, config);
310 }
311
312 actionConfigs.put(path, config);
313 actionConfigList.add(config);
314 }
315
316
317
318
319
320
321
322
323
324 public void addExceptionConfig(ExceptionConfig config) {
325 throwIfConfigured();
326
327 String key = config.getType();
328
329 if (exceptions.containsKey(key)) {
330 log.warn("Overriding ExceptionConfig of type " + key);
331 }
332
333 exceptions.put(key, config);
334 }
335
336
337
338
339
340
341
342
343
344 public void addFormBeanConfig(FormBeanConfig config) {
345 throwIfConfigured();
346
347 String key = config.getName();
348
349 if (formBeans.containsKey(key)) {
350 log.warn("Overriding ActionForm of name " + key);
351 }
352
353 formBeans.put(key, config);
354 }
355
356
357
358
359
360 public String getActionForwardClass() {
361 return this.actionForwardClass;
362 }
363
364
365
366
367
368
369
370
371 public void setActionForwardClass(String actionForwardClass) {
372 this.actionForwardClass = actionForwardClass;
373 }
374
375
376
377
378
379
380
381
382
383 public void addForwardConfig(ForwardConfig config) {
384 throwIfConfigured();
385
386 String key = config.getName();
387
388 if (forwards.containsKey(key)) {
389 log.warn("Overriding global ActionForward of name " + key);
390 }
391
392 forwards.put(key, config);
393 }
394
395
396
397
398
399
400
401
402
403 public void addMessageResourcesConfig(MessageResourcesConfig config) {
404 throwIfConfigured();
405
406 String key = config.getKey();
407
408 if (messageResources.containsKey(key)) {
409 log.warn("Overriding MessageResources bundle of key " + key);
410 }
411
412 messageResources.put(key, config);
413 }
414
415
416
417
418
419
420
421 public void addPlugInConfig(PlugInConfig plugInConfig) {
422 throwIfConfigured();
423 plugIns.add(plugInConfig);
424 }
425
426
427
428
429
430
431
432
433 public ActionConfig findActionConfig(String path) {
434 ActionConfig config = (ActionConfig) actionConfigs.get(path);
435
436
437
438 if ((config == null) && (matcher != null)) {
439 config = matcher.match(path);
440 }
441
442 return config;
443 }
444
445
446
447
448
449
450
451
452
453
454 public ActionConfig findActionConfigId(String actionId) {
455 if (actionId != null) {
456 return (ActionConfig) this.actionConfigIds.get(actionId);
457 }
458 return null;
459 }
460
461
462
463
464
465 public ActionConfig[] findActionConfigs() {
466 ActionConfig[] results = new ActionConfig[actionConfigList.size()];
467
468 return ((ActionConfig[]) actionConfigList.toArray(results));
469 }
470
471
472
473
474
475
476
477 public ExceptionConfig findExceptionConfig(String type) {
478 return ((ExceptionConfig) exceptions.get(type));
479 }
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497 public ExceptionConfig findException(Class type) {
498
499 ExceptionConfig config = null;
500
501 while (true) {
502
503 String name = type.getName();
504
505 log.debug("findException: look locally for " + name);
506 config = findExceptionConfig(name);
507
508 if (config != null) {
509 return (config);
510 }
511
512
513 type = type.getSuperclass();
514
515 if (type == null) {
516 break;
517 }
518 }
519
520 return (null);
521 }
522
523
524
525
526
527 public ExceptionConfig[] findExceptionConfigs() {
528 ExceptionConfig[] results = new ExceptionConfig[exceptions.size()];
529
530 return ((ExceptionConfig[]) exceptions.values().toArray(results));
531 }
532
533
534
535
536
537
538
539 public FormBeanConfig findFormBeanConfig(String name) {
540 return ((FormBeanConfig) formBeans.get(name));
541 }
542
543
544
545
546
547 public FormBeanConfig[] findFormBeanConfigs() {
548 FormBeanConfig[] results = new FormBeanConfig[formBeans.size()];
549
550 return ((FormBeanConfig[]) formBeans.values().toArray(results));
551 }
552
553
554
555
556
557
558
559 public ForwardConfig findForwardConfig(String name) {
560 return ((ForwardConfig) forwards.get(name));
561 }
562
563
564
565
566
567 public ForwardConfig[] findForwardConfigs() {
568 ForwardConfig[] results = new ForwardConfig[forwards.size()];
569
570 return ((ForwardConfig[]) forwards.values().toArray(results));
571 }
572
573
574
575
576
577
578
579 public MessageResourcesConfig findMessageResourcesConfig(String key) {
580 return ((MessageResourcesConfig) messageResources.get(key));
581 }
582
583
584
585
586
587 public MessageResourcesConfig[] findMessageResourcesConfigs() {
588 MessageResourcesConfig[] results =
589 new MessageResourcesConfig[messageResources.size()];
590
591 return ((MessageResourcesConfig[]) messageResources.values().toArray(results));
592 }
593
594
595
596
597
598 public PlugInConfig[] findPlugInConfigs() {
599 PlugInConfig[] results = new PlugInConfig[plugIns.size()];
600
601 return ((PlugInConfig[]) plugIns.toArray(results));
602 }
603
604
605
606
607
608
609 public void freeze() {
610 super.freeze();
611
612 ActionConfig[] aconfigs = findActionConfigs();
613
614 for (int i = 0; i < aconfigs.length; i++) {
615 aconfigs[i].freeze();
616 }
617
618 matcher = new ActionConfigMatcher(aconfigs);
619
620 getControllerConfig().freeze();
621
622 ExceptionConfig[] econfigs = findExceptionConfigs();
623
624 for (int i = 0; i < econfigs.length; i++) {
625 econfigs[i].freeze();
626 }
627
628 FormBeanConfig[] fbconfigs = findFormBeanConfigs();
629
630 for (int i = 0; i < fbconfigs.length; i++) {
631 fbconfigs[i].freeze();
632 }
633
634 ForwardConfig[] fconfigs = findForwardConfigs();
635
636 for (int i = 0; i < fconfigs.length; i++) {
637 fconfigs[i].freeze();
638 }
639
640 MessageResourcesConfig[] mrconfigs = findMessageResourcesConfigs();
641
642 for (int i = 0; i < mrconfigs.length; i++) {
643 mrconfigs[i].freeze();
644 }
645
646 PlugInConfig[] piconfigs = findPlugInConfigs();
647
648 for (int i = 0; i < piconfigs.length; i++) {
649 piconfigs[i].freeze();
650 }
651 }
652
653
654
655
656
657
658
659
660 public void removeActionConfig(ActionConfig config) {
661 throwIfConfigured();
662 config.setModuleConfig(null);
663 actionConfigs.remove(config.getPath());
664 actionConfigList.remove(config);
665 }
666
667
668
669
670
671
672
673
674 public void removeExceptionConfig(ExceptionConfig config) {
675 throwIfConfigured();
676 exceptions.remove(config.getType());
677 }
678
679
680
681
682
683
684
685
686 public void removeFormBeanConfig(FormBeanConfig config) {
687 throwIfConfigured();
688 formBeans.remove(config.getName());
689 }
690
691
692
693
694
695
696
697
698 public void removeForwardConfig(ForwardConfig config) {
699 throwIfConfigured();
700 forwards.remove(config.getName());
701 }
702
703
704
705
706
707
708
709
710
711 public void removeMessageResourcesConfig(MessageResourcesConfig config) {
712 throwIfConfigured();
713 messageResources.remove(config.getKey());
714 }
715 }