KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > config > impl > ModuleConfigImpl


1 /*
2  * $Id: ModuleConfigImpl.java 54929 2004-10-16 16:38:42Z germuska $
3  *
4  * Copyright 1999-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 package org.apache.struts.config.impl;
20
21 import java.io.Serializable JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.HashMap JavaDoc;
25
26 import org.apache.struts.config.ActionConfig;
27 import org.apache.struts.config.ActionConfigMatcher;
28 import org.apache.struts.config.ControllerConfig;
29 import org.apache.struts.config.DataSourceConfig;
30 import org.apache.struts.config.ExceptionConfig;
31 import org.apache.struts.config.FormBeanConfig;
32 import org.apache.struts.config.ForwardConfig;
33 import org.apache.struts.config.MessageResourcesConfig;
34 import org.apache.struts.config.ModuleConfig;
35 import org.apache.struts.config.PlugInConfig;
36
37 /**
38  * <p>The collection of static configuration information that describes a
39  * Struts-based module. Multiple modules are identified by
40  * a <em>prefix</em> at the beginning of the context
41  * relative portion of the request URI. If no module prefix can be
42  * matched, the default configuration (with a prefix equal to a zero-length
43  * string) is selected, which is elegantly backwards compatible with the
44  * previous Struts behavior that only supported one module.</p>
45  *
46  * @version $Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $
47  * @since Struts 1.1
48  */

49 public class ModuleConfigImpl implements Serializable JavaDoc, ModuleConfig {
50     
51     /**
52      * Construct an ModuleConfigImpl object according to the specified
53      * parameter values.
54      *
55      * @param prefix Context-relative URI prefix for this module
56      */

57     public ModuleConfigImpl(String JavaDoc prefix) {
58         super();
59         this.prefix = prefix;
60         this.actionConfigs = new HashMap JavaDoc();
61         this.actionConfigList = new ArrayList JavaDoc();
62         this.actionFormBeanClass = "org.apache.struts.action.ActionFormBean";
63         this.actionMappingClass = "org.apache.struts.action.ActionMapping";
64         this.actionForwardClass = "org.apache.struts.action.ActionForward";
65         this.configured = false;
66         this.controllerConfig = null;
67         this.dataSources = new HashMap JavaDoc();
68         this.exceptions = new HashMap JavaDoc();
69         this.formBeans = new HashMap JavaDoc();
70         this.forwards = new HashMap JavaDoc();
71         this.messageResources = new HashMap JavaDoc();
72         this.plugIns = new ArrayList JavaDoc();
73     }
74
75
76     // --------------------------------------------------------- Public Methods
77

78     /**
79      * Has this module been completely configured yet. Once this flag
80      * has been set, any attempt to modify the configuration will return an
81      * IllegalStateException.
82      */

83     public boolean getConfigured() {
84         return (this.configured);
85     }
86
87     /**
88      * The controller configuration object for this module.
89      */

90     public ControllerConfig getControllerConfig() {
91         if (this.controllerConfig == null) {
92             this.controllerConfig = new ControllerConfig();
93         }
94         return (this.controllerConfig);
95     }
96
97     /**
98      * The controller configuration object for this module.
99      * @param cc The controller configuration object for this module.
100      */

101     public void setControllerConfig(ControllerConfig cc) {
102         if (configured) {
103             throw new IllegalStateException JavaDoc("Configuration is frozen");
104         }
105         this.controllerConfig = cc;
106     }
107
108     /**
109      * The prefix of the context-relative portion of the request URI, used to
110      * select this configuration versus others supported by the controller
111      * servlet. A configuration with a prefix of a zero-length String is the
112      * default configuration for this web module.
113      */

114     public String JavaDoc getPrefix() {
115         return (this.prefix);
116     }
117
118     /**
119      * The prefix of the context-relative portion of the request URI, used to
120      * select this configuration versus others supported by the controller
121      * servlet. A configuration with a prefix of a zero-length String is the
122      * default configuration for this web module.
123      */

124     public void setPrefix(String JavaDoc prefix) {
125         if (configured) {
126             throw new IllegalStateException JavaDoc("Configuration is frozen");
127         }
128         this.prefix = prefix;
129     }
130
131     /**
132      * The default class name to be used when creating action form bean
133      * instances.
134      */

135     public String JavaDoc getActionFormBeanClass() {
136         return this.actionFormBeanClass;
137     }
138
139     /**
140      * The default class name to be used when creating action form bean
141      * instances.
142      *
143      * @param actionFormBeanClass default class name to be used when creating
144      * action form bean instances.
145      */

146     public void setActionFormBeanClass(String JavaDoc actionFormBeanClass) {
147         this.actionFormBeanClass = actionFormBeanClass;
148     }
149
150     /**
151      * The default class name to be used when creating action mapping instances.
152      */

153     public String JavaDoc getActionMappingClass() {
154         return this.actionMappingClass;
155     }
156
157     /**
158      * The default class name to be used when creating action mapping instances.
159      *
160      * @param actionMappingClass default class name to be used when creating
161      * action mapping instances.
162      */

163     public void setActionMappingClass(String JavaDoc actionMappingClass) {
164         this.actionMappingClass = actionMappingClass;
165     }
166
167     /**
168      * Add a new <code>ActionConfig</code> instance to the set associated
169      * with this module.
170      *
171      * @param config The new configuration instance to be added
172      *
173      * @exception java.lang.IllegalStateException if this module configuration
174      * has been frozen
175      */

176     public void addActionConfig(ActionConfig config) {
177
178         if (configured) {
179             throw new IllegalStateException JavaDoc("Configuration is frozen");
180         }
181         config.setModuleConfig(this);
182         actionConfigs.put(config.getPath(), config);
183         actionConfigList.add(config);
184
185     }
186
187     /**
188      * Add a new <code>DataSourceConfig</code> instance to the set associated
189      * with this module.
190      *
191      * @param config The new configuration instance to be added
192      *
193      * @exception java.lang.IllegalStateException if this module configuration
194      * has been frozen
195      */

196     public void addDataSourceConfig(DataSourceConfig config) {
197
198         if (configured) {
199             throw new IllegalStateException JavaDoc("Configuration is frozen");
200         }
201         dataSources.put(config.getKey(), config);
202
203     }
204
205     /**
206      * Add a new <code>ExceptionConfig</code> instance to the set associated
207      * with this module.
208      *
209      * @param config The new configuration instance to be added
210      *
211      * @exception java.lang.IllegalStateException if this module configuration
212      * has been frozen
213      */

214     public void addExceptionConfig(ExceptionConfig config) {
215
216         if (configured) {
217             throw new IllegalStateException JavaDoc("Configuration is frozen");
218         }
219         exceptions.put(config.getType(), config);
220
221     }
222
223     /**
224      * Add a new <code>FormBeanConfig</code> instance to the set associated
225      * with this module.
226      *
227      * @param config The new configuration instance to be added
228      *
229      * @exception java.lang.IllegalStateException if this module configuration
230      * has been frozen
231      */

232     public void addFormBeanConfig(FormBeanConfig config) {
233
234         if (configured) {
235             throw new IllegalStateException JavaDoc("Configuration is frozen");
236         }
237         formBeans.put(config.getName(), config);
238
239     }
240
241     /**
242      * The default class name to be used when creating action forward instances.
243      */

244     public String JavaDoc getActionForwardClass() {
245         return this.actionForwardClass;
246     }
247
248     /**
249      * The default class name to be used when creating action forward instances.
250      *
251      * @param actionForwardClass default class name to be used when creating
252      * action forward instances.
253      */

254     public void setActionForwardClass(String JavaDoc actionForwardClass) {
255         this.actionForwardClass= actionForwardClass;
256     }
257
258     /**
259      * Add a new <code>ForwardConfig</code> instance to the set of global
260      * forwards associated with this module.
261      *
262      * @param config The new configuration instance to be added
263      *
264      * @exception java.lang.IllegalStateException if this module configuration
265      * has been frozen
266      */

267     public void addForwardConfig(ForwardConfig config) {
268
269         if (configured) {
270             throw new IllegalStateException JavaDoc("Configuration is frozen");
271         }
272         forwards.put(config.getName(), config);
273
274     }
275
276     /**
277      * Add a new <code>MessageResourcesConfig</code> instance to the set
278      * associated with this module.
279      *
280      * @param config The new configuration instance to be added
281      *
282      * @exception java.lang.IllegalStateException if this module configuration
283      * has been frozen
284      */

285     public void addMessageResourcesConfig(MessageResourcesConfig config) {
286
287         if (configured) {
288             throw new IllegalStateException JavaDoc("Configuration is frozen");
289         }
290         messageResources.put(config.getKey(), config);
291
292     }
293
294     /**
295      * Add a newly configured {@link org.apache.struts.config.PlugInConfig} instance to the set of
296      * plug-in Actions for this module.
297      *
298      * @param plugInConfig The new configuration instance to be added
299      */

300     public void addPlugInConfig(PlugInConfig plugInConfig) {
301
302         if (configured) {
303             throw new IllegalStateException JavaDoc("Configuration is frozen");
304         }
305         plugIns.add(plugInConfig);
306
307     }
308
309     /**
310      * Return the action configuration for the specified path, first looking
311      * a direct match, then if none found, a wildcard pattern match;
312      * otherwise return <code>null</code>.
313      *
314      * @param path Path of the action configuration to return
315      */

316     public ActionConfig findActionConfig(String JavaDoc path) {
317
318         ActionConfig config = (ActionConfig) actionConfigs.get(path);
319         
320         // If a direct match cannot be found, try to match action configs
321
// containing wildcard patterns
322
if (config == null) {
323             config = matcher.match(path);
324         }
325         
326         return config;
327
328     }
329
330     /**
331      * Return the action configurations for this module. If there are
332      * none, a zero-length array is returned.
333      */

334     public ActionConfig[] findActionConfigs() {
335
336         ActionConfig results[] = new ActionConfig[actionConfigList.size()];
337         return ((ActionConfig[]) actionConfigList.toArray(results));
338
339     }
340
341     /**
342      * Return the data source configuration for the specified key, if any;
343      * otherwise return <code>null</code>.
344      *
345      * @param key Key of the data source configuration to return
346      */

347     public DataSourceConfig findDataSourceConfig(String JavaDoc key) {
348
349         return ((DataSourceConfig) dataSources.get(key));
350
351     }
352
353     /**
354      * Return the data source configurations for this module. If there
355      * are none, a zero-length array is returned.
356      */

357     public DataSourceConfig[] findDataSourceConfigs() {
358
359         DataSourceConfig results[] = new DataSourceConfig[dataSources.size()];
360         return ((DataSourceConfig[]) dataSources.values().toArray(results));
361
362     }
363
364     /**
365      * Return the exception configuration for the specified type, if any;
366      * otherwise return <code>null</code>.
367      *
368      * @param type Exception class name to find a configuration for
369      */

370     public ExceptionConfig findExceptionConfig(String JavaDoc type) {
371
372         return ((ExceptionConfig) exceptions.get(type));
373
374     }
375
376     /**
377      * Return the exception configurations for this module. If there
378      * are none, a zero-length array is returned.
379      */

380     public ExceptionConfig[] findExceptionConfigs() {
381
382         ExceptionConfig results[] = new ExceptionConfig[exceptions.size()];
383         return ((ExceptionConfig[]) exceptions.values().toArray(results));
384
385     }
386
387     /**
388      * Return the form bean configuration for the specified key, if any;
389      * otherwise return <code>null</code>.
390      *
391      * @param name Name of the form bean configuration to return
392      */

393     public FormBeanConfig findFormBeanConfig(String JavaDoc name) {
394
395         return ((FormBeanConfig) formBeans.get(name));
396
397     }
398
399     /**
400      * Return the form bean configurations for this module. If there
401      * are none, a zero-length array is returned.
402      */

403     public FormBeanConfig[] findFormBeanConfigs() {
404
405         FormBeanConfig results[] = new FormBeanConfig[formBeans.size()];
406         return ((FormBeanConfig[]) formBeans.values().toArray(results));
407
408     }
409
410     /**
411      * Return the forward configuration for the specified key, if any;
412      * otherwise return <code>null</code>.
413      *
414      * @param name Name of the forward configuration to return
415      */

416     public ForwardConfig findForwardConfig(String JavaDoc name) {
417
418         return ((ForwardConfig) forwards.get(name));
419
420     }
421
422     /**
423      * Return the form bean configurations for this module. If there
424      * are none, a zero-length array is returned.
425      */

426     public ForwardConfig[] findForwardConfigs() {
427
428         ForwardConfig results[] = new ForwardConfig[forwards.size()];
429         return ((ForwardConfig[]) forwards.values().toArray(results));
430
431     }
432
433     /**
434      * Return the message resources configuration for the specified key,
435      * if any; otherwise return <code>null</code>.
436      *
437      * @param key Key of the data source configuration to return
438      */

439     public MessageResourcesConfig findMessageResourcesConfig(String JavaDoc key) {
440
441         return ((MessageResourcesConfig) messageResources.get(key));
442
443     }
444
445     /**
446      * Return the message resources configurations for this module.
447      * If there are none, a zero-length array is returned.
448      */

449     public MessageResourcesConfig[] findMessageResourcesConfigs() {
450
451         MessageResourcesConfig results[] =
452                 new MessageResourcesConfig[messageResources.size()];
453         return ((MessageResourcesConfig[])
454                 messageResources.values().toArray(results));
455
456     }
457
458     /**
459      * Return the configured plug-in actions for this module. If there
460      * are none, a zero-length array is returned.
461      */

462     public PlugInConfig[] findPlugInConfigs() {
463
464         PlugInConfig results[] = new PlugInConfig[plugIns.size()];
465         return ((PlugInConfig[]) plugIns.toArray(results));
466
467     }
468
469     /**
470      * Freeze the configuration of this module. After this method
471      * returns, any attempt to modify the configuration will return
472      * an IllegalStateException.
473      */

474     public void freeze() {
475
476         this.configured = true;
477
478         ActionConfig[] aconfigs = findActionConfigs();
479         for (int i = 0; i < aconfigs.length; i++) {
480             aconfigs[i].freeze();
481         }
482         matcher = new ActionConfigMatcher(aconfigs);
483
484         getControllerConfig().freeze();
485
486         DataSourceConfig[] dsconfigs = findDataSourceConfigs();
487         for (int i = 0; i < dsconfigs.length; i++) {
488             dsconfigs[i].freeze();
489         }
490
491         ExceptionConfig[] econfigs = findExceptionConfigs();
492         for (int i = 0; i < econfigs.length; i++) {
493             econfigs[i].freeze();
494         }
495
496         FormBeanConfig[] fbconfigs = findFormBeanConfigs();
497         for (int i = 0; i < fbconfigs.length; i++) {
498             fbconfigs[i].freeze();
499         }
500
501         ForwardConfig[] fconfigs = findForwardConfigs();
502         for (int i = 0; i < fconfigs.length; i++) {
503             fconfigs[i].freeze();
504         }
505
506         MessageResourcesConfig[] mrconfigs = findMessageResourcesConfigs();
507         for (int i = 0; i < mrconfigs.length; i++) {
508             mrconfigs[i].freeze();
509         }
510
511         PlugInConfig[] piconfigs = findPlugInConfigs();
512         for (int i = 0; i < piconfigs.length; i++) {
513             piconfigs[i].freeze();
514         }
515     }
516
517     /**
518      * Remove the specified action configuration instance.
519      *
520      * @param config ActionConfig instance to be removed
521      *
522      * @exception IllegalStateException if this module configuration
523      * has been frozen
524      */

525     public void removeActionConfig(ActionConfig config) {
526
527         if (configured) {
528             throw new IllegalStateException JavaDoc("Configuration is frozen");
529         }
530         config.setModuleConfig(null);
531         actionConfigs.remove(config.getPath());
532         actionConfigList.remove(config);
533
534     }
535
536     /**
537      * Remove the specified exception configuration instance.
538      *
539      * @param config ActionConfig instance to be removed
540      *
541      * @exception IllegalStateException if this module configuration
542      * has been frozen
543      */

544     public void removeExceptionConfig(ExceptionConfig config) {
545
546         if (configured) {
547             throw new IllegalStateException JavaDoc("Configuration is frozen");
548         }
549         exceptions.remove(config.getType());
550
551     }
552
553     /**
554      * Remove the specified data source configuration instance.
555      *
556      * @param config DataSourceConfig instance to be removed
557      *
558      * @exception IllegalStateException if this module configuration
559      * has been frozen
560      */

561     public void removeDataSourceConfig(DataSourceConfig config) {
562
563         if (configured) {
564             throw new IllegalStateException JavaDoc("Configuration is frozen");
565         }
566         dataSources.remove(config.getKey());
567
568     }
569
570     /**
571      * Remove the specified form bean configuration instance.
572      *
573      * @param config FormBeanConfig instance to be removed
574      *
575      * @exception IllegalStateException if this module configuration
576      * has been frozen
577      */

578     public void removeFormBeanConfig(FormBeanConfig config) {
579
580         if (configured) {
581             throw new IllegalStateException JavaDoc("Configuration is frozen");
582         }
583         formBeans.remove(config.getName());
584
585     }
586
587     /**
588      * Remove the specified forward configuration instance.
589      *
590      * @param config ForwardConfig instance to be removed
591      *
592      * @exception IllegalStateException if this module configuration
593      * has been frozen
594      */

595     public void removeForwardConfig(ForwardConfig config) {
596
597         if (configured) {
598             throw new IllegalStateException JavaDoc("Configuration is frozen");
599         }
600         forwards.remove(config.getName());
601
602     }
603
604     /**
605      * Remove the specified message resources configuration instance.
606      *
607      * @param config MessageResourcesConfig instance to be removed
608      *
609      * @exception IllegalStateException if this module configuration
610      * has been frozen
611      */

612     public void removeMessageResourcesConfig(MessageResourcesConfig config) {
613
614         if (configured) {
615             throw new IllegalStateException JavaDoc("Configuration is frozen");
616         }
617         messageResources.remove(config.getKey());
618
619     }
620
621     // ----------------------------------------------------- Instance Variables
622
// Instance Variables at end to make comparing Interface and implementation easier.
623

624     
625     /**
626      * The set of action configurations for this module, if any,
627      * keyed by the <code>path</code> property.
628      */

629     protected HashMap JavaDoc actionConfigs = null;
630     
631     /**
632      * The set of action configurations for this module, if any,
633      * listed in the order in which they are added.
634      */

635     protected List JavaDoc actionConfigList = null;
636     
637     /**
638      * The set of JDBC data source configurations for this
639      * module, if any, keyed by the <code>key</code> property.
640      */

641     protected HashMap JavaDoc dataSources = null;
642     
643     /**
644      * The set of exception handling configurations for this
645      * module, if any, keyed by the <code>type</code> property.
646      */

647     protected HashMap JavaDoc exceptions = null;
648     
649     /**
650      * The set of form bean configurations for this module, if any,
651      * keyed by the <code>name</code> property.
652      */

653     protected HashMap JavaDoc formBeans = null;
654     
655     /**
656      * The set of global forward configurations for this module, if any,
657      * keyed by the <code>name</code> property.
658      */

659     protected HashMap JavaDoc forwards = null;
660     
661     /**
662      * The set of message resources configurations for this
663      * module, if any, keyed by the <code>key</code> property.
664      */

665     protected HashMap JavaDoc messageResources = null;
666     
667     /**
668      * The set of configured plug-in Actions for this module,
669      * if any, in the order they were declared and configured.
670      */

671     protected ArrayList JavaDoc plugIns = null;
672     
673     /**
674      * Has this module been completely configured yet. Once this flag
675      * has been set, any attempt to modify the configuration will return an
676      * IllegalStateException.
677      */

678     protected boolean configured = false;
679     
680     /**
681      * The controller configuration object for this module.
682      */

683     protected ControllerConfig controllerConfig = null;
684     
685     /**
686      * The prefix of the context-relative portion of the request URI, used to
687      * select this configuration versus others supported by the controller
688      * servlet. A configuration with a prefix of a zero-length String is the
689      * default configuration for this web module.
690      */

691     protected String JavaDoc prefix = null;
692     
693     /**
694      * The default class name to be used when creating action form bean
695      * instances.
696      */

697     protected String JavaDoc actionFormBeanClass = "org.apache.struts.action.ActionFormBean";
698     
699     /**
700      * The default class name to be used when creating action mapping instances.
701      */

702     protected String JavaDoc actionMappingClass = "org.apache.struts.action.ActionMapping";
703     
704     /**
705      * The default class name to be used when creating action forward instances.
706      */

707     protected String JavaDoc actionForwardClass = "org.apache.struts.action.ActionForward";
708     
709     /**
710      * Matches action config paths against compiled wildcard patterns
711      */

712     protected ActionConfigMatcher matcher = null;
713
714 }
715
Popular Tags