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 java.io.Serializable;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.TreeMap;
28
29 import org.apache.struts.Constants;
30 import org.apache.struts.config.ActionConfig;
31 import org.apache.struts.config.ActionConfigMatcher;
32 import org.apache.struts.config.BaseConfig;
33 import org.apache.struts.config.ControllerConfig;
34 import org.apache.struts.config.ExceptionConfig;
35 import org.apache.struts.config.FormBeanConfig;
36 import org.apache.struts.config.ForwardConfig;
37 import org.apache.struts.config.MessageResourcesConfig;
38 import org.apache.struts.config.ModuleConfig;
39 import org.apache.struts.config.PlugInConfig;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56 public class ModuleConfigImpl extends BaseConfig implements Serializable,
57 ModuleConfig {
58
59 private static final long serialVersionUID = -5742785805411686899L;
60
61
62
63
64 private transient final Logger log =
65 LoggerFactory.getLogger(ModuleConfigImpl.class);
66
67
68
69
70
71
72
73
74 protected Map<String, ActionConfig> actionConfigs = null;
75
76
77
78
79
80 protected HashMap<String, ActionConfig> actionConfigIds = null;
81
82
83
84
85
86 protected ArrayList<ActionConfig> actionConfigList = null;
87
88
89
90
91
92 protected HashMap<String, ExceptionConfig> exceptions = null;
93
94
95
96
97
98 protected HashMap<String, FormBeanConfig> formBeans = null;
99
100
101
102
103
104 protected HashMap<String, ForwardConfig> forwards = null;
105
106
107
108
109
110 protected HashMap<String, MessageResourcesConfig> messageResources = null;
111
112
113
114
115
116 protected ArrayList<PlugInConfig> plugIns = null;
117
118
119
120
121 protected ControllerConfig controllerConfig = null;
122
123
124
125
126
127
128
129 protected String prefix = null;
130
131
132
133
134
135 protected String actionFormBeanClass =
136 "org.apache.struts.action.ActionFormBean";
137
138
139
140
141
142 protected String actionMappingClass =
143 "org.apache.struts.action.ActionMapping";
144
145
146
147
148
149 protected String actionForwardClass =
150 "org.apache.struts.action.ActionForward";
151
152
153
154
155 protected ActionConfigMatcher matcher = null;
156
157
158
159
160
161
162
163 public ModuleConfigImpl() {
164 this("");
165 }
166
167
168
169
170
171
172
173 public ModuleConfigImpl(String prefix) {
174 super();
175 this.prefix = prefix;
176 this.actionConfigs = new HashMap<>();
177 this.actionConfigIds = new HashMap<>();
178 this.actionConfigList = new ArrayList<>();
179 this.actionFormBeanClass = "org.apache.struts.action.ActionFormBean";
180 this.actionMappingClass = "org.apache.struts.action.ActionMapping";
181 this.actionForwardClass = "org.apache.struts.action.ActionForward";
182 this.configured = false;
183 this.controllerConfig = null;
184 this.exceptions = new HashMap<>();
185 this.formBeans = new HashMap<>();
186 this.forwards = new HashMap<>();
187 this.messageResources = new HashMap<>();
188 this.plugIns = new ArrayList<>();
189 }
190
191
192
193
194
195
196
197
198 public boolean getConfigured() {
199 return (this.configured);
200 }
201
202
203
204
205 public ControllerConfig getControllerConfig() {
206 if (this.controllerConfig == null) {
207 this.controllerConfig = new ControllerConfig();
208 }
209
210 return (this.controllerConfig);
211 }
212
213
214
215
216
217
218 public void setControllerConfig(ControllerConfig cc) {
219 throwIfConfigured();
220 this.controllerConfig = cc;
221 }
222
223
224
225
226
227
228
229 public String getPrefix() {
230 return (this.prefix);
231 }
232
233
234
235
236
237
238
239 public void setPrefix(String prefix) {
240 throwIfConfigured();
241 this.prefix = prefix;
242 }
243
244
245
246
247
248 public String getActionFormBeanClass() {
249 return this.actionFormBeanClass;
250 }
251
252
253
254
255
256
257
258
259 public void setActionFormBeanClass(String actionFormBeanClass) {
260 this.actionFormBeanClass = actionFormBeanClass;
261 }
262
263
264
265
266
267 public String getActionMappingClass() {
268 return this.actionMappingClass;
269 }
270
271
272
273
274
275
276
277
278 public void setActionMappingClass(String actionMappingClass) {
279 this.actionMappingClass = actionMappingClass;
280 }
281
282
283
284
285
286
287
288
289
290 public void addActionConfig(ActionConfig config) {
291 throwIfConfigured();
292 config.setModuleConfig(this);
293
294 String path = config.getPath();
295 if (actionConfigs.containsKey(path)) {
296 log.warn("Overriding ActionConfig of path {}", path);
297 }
298
299 String actionId = config.getActionId();
300 if ((actionId != null) && !actionId.equals("")) {
301 if (actionConfigIds.containsKey(actionId)) {
302 log.atWarn()
303 .setMessage("Overriding actionId[{}] for path[{}] with path[{}]")
304 .addArgument(actionId)
305 .addArgument(() -> actionConfigIds.get(actionId).getPath())
306 .addArgument(path)
307 .log();
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 = 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 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 (actionConfigList.toArray(results));
469 }
470
471
472
473
474
475
476
477 public ExceptionConfig findExceptionConfig(String type) {
478 return (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 (exceptions.values().toArray(results));
531 }
532
533
534
535
536
537
538
539 public FormBeanConfig findFormBeanConfig(String name) {
540 return (formBeans.get(name));
541 }
542
543
544
545
546
547 public FormBeanConfig[] findFormBeanConfigs() {
548 FormBeanConfig[] results = new FormBeanConfig[formBeans.size()];
549
550 return (formBeans.values().toArray(results));
551 }
552
553
554
555
556
557
558
559 public ForwardConfig findForwardConfig(String name) {
560 return (forwards.get(name));
561 }
562
563
564
565
566
567 public ForwardConfig[] findForwardConfigs() {
568 ForwardConfig[] results = new ForwardConfig[forwards.size()];
569
570 return (forwards.values().toArray(results));
571 }
572
573
574
575
576
577
578
579 public MessageResourcesConfig findMessageResourcesConfig(String key) {
580 return (messageResources.get(key));
581 }
582
583
584
585
586
587 public MessageResourcesConfig[] findMessageResourcesConfigs() {
588 MessageResourcesConfig[] results =
589 new MessageResourcesConfig[messageResources.size()];
590
591 return (messageResources.values().toArray(results));
592 }
593
594
595
596
597
598 public PlugInConfig[] findPlugInConfigs() {
599 PlugInConfig[] results = new PlugInConfig[plugIns.size()];
600
601 return (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
716 public void setProperty(String key, String value) {
717 super.setProperty(key, value);
718
719 if (Constants.STRUTS_URL_CASESENSITIVE.equals(key)) {
720 Map<String, ActionConfig> actionConfigs2;
721 if (!Boolean.valueOf(value).booleanValue()) {
722 actionConfigs2 = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
723 } else {
724 actionConfigs2 = new HashMap<>();
725 }
726 actionConfigs2.putAll(actionConfigs);
727 actionConfigs = actionConfigs2;
728 }
729 }
730 }