KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > config > ActionConfig


1 /*
2  * $Id: ActionConfig.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;
20
21 import java.io.Serializable JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.HashMap JavaDoc;
24
25
26 /**
27  * <p>A JavaBean representing the configuration information of an
28  * <code>&lt;action&gt;</code> element from a Struts
29  * module configuration file.</p>
30  *
31  * @version $Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $
32  * @since Struts 1.1
33  */

34 public class ActionConfig implements Serializable JavaDoc {
35
36
37     // ----------------------------------------------------- Instance Variables
38

39
40     /**
41      * Indicates if configuration of this component been completed.
42      */

43     protected boolean configured = false;
44
45
46     /**
47      * The set of exception handling configurations for this
48      * action, if any, keyed by the <code>type</code> property.
49      */

50     protected HashMap JavaDoc exceptions = new HashMap JavaDoc();
51
52
53     /**
54      * The set of local forward configurations for this action, if any,
55      * keyed by the <code>name</code> property.
56      */

57     protected HashMap JavaDoc forwards = new HashMap JavaDoc();
58
59
60     // ------------------------------------------------------------- Properties
61

62
63     /**
64      * The module configuration with which we are associated.
65      */

66     protected ModuleConfig moduleConfig = null;
67
68     /**
69      * The module configuration with which we are associated.
70      */

71     public ModuleConfig getModuleConfig() {
72         return (this.moduleConfig);
73     }
74
75     /**
76      * The module configuration with which we are associated.
77      */

78     public void setModuleConfig(ModuleConfig moduleConfig) {
79         if (configured) {
80             throw new IllegalStateException JavaDoc("Configuration is frozen");
81         }
82
83         this.moduleConfig = moduleConfig;
84     }
85
86
87     /**
88      * The request-scope or session-scope attribute name under which our
89      * form bean is accessed, if it is different from the form bean's
90      * specified <code>name</code>.
91      */

92     protected String JavaDoc attribute = null;
93
94     /**
95      * Returns the request-scope or session-scope attribute name under which our
96      * form bean is accessed, if it is different from the form bean's
97      * specified <code>name</code>.
98      * @return attribute name under which our form bean is accessed.
99      */

100     public String JavaDoc getAttribute() {
101         if (this.attribute == null) {
102             return (this.name);
103         } else {
104             return (this.attribute);
105         }
106     }
107
108     /**
109      * Set the request-scope or session-scope attribute name under which our
110      * form bean is accessed, if it is different from the form bean's
111      * specified <code>name</code>.
112      * @param attribute the request-scope or session-scope attribute name under which our
113      * form bean is access.
114      */

115     public void setAttribute(String JavaDoc attribute) {
116         if (configured) {
117             throw new IllegalStateException JavaDoc("Configuration is frozen");
118         }
119         this.attribute = attribute;
120     }
121
122
123     /**
124      * Context-relative path of the web application resource that will process
125      * this request via RequestDispatcher.forward(), instead of instantiating
126      * and calling the <code>Action</code> class specified by "type".
127      * Exactly one of <code>forward</code>, <code>include</code>, or
128      * <code>type</code> must be specified.
129      */

130     protected String JavaDoc forward = null;
131
132     /**
133      * Returns context-relative path of the web application resource that will process
134      * this request.
135      * @return context-relative path of the web application resource that will process
136      * this request.
137      */

138     public String JavaDoc getForward() {
139         return (this.forward);
140     }
141
142     /**
143      * Set the context-relative path of the web application resource that will process
144      * this request.
145      * Exactly one of <code>forward</code>, <code>include</code>, or
146      * <code>type</code> must be specified.
147      * @param forward context-relative path of the web application resource that will process
148      * this request.
149      */

150     public void setForward(String JavaDoc forward) {
151         if (configured) {
152             throw new IllegalStateException JavaDoc("Configuration is frozen");
153         }
154         this.forward = forward;
155     }
156
157
158     /**
159      * Context-relative path of the web application resource that will process
160      * this request via RequestDispatcher.include(), instead of instantiating
161      * and calling the <code>Action</code> class specified by "type".
162      * Exactly one of <code>forward</code>, <code>include</code>, or
163      * <code>type</code> must be specified.
164      */

165     protected String JavaDoc include = null;
166
167     /**
168      * Context-relative path of the web application resource that will process
169      * this request.
170      * @return Context-relative path of the web application resource that will process
171      * this request.
172      */

173     public String JavaDoc getInclude() {
174         return (this.include);
175     }
176
177     /**
178      * Set context-relative path of the web application resource that will process
179      * this request.
180      * Exactly one of <code>forward</code>, <code>include</code>, or
181      * <code>type</code> must be specified.
182      * @param include context-relative path of the web application resource that will process
183      * this request.
184      */

185     public void setInclude(String JavaDoc include) {
186         if (configured) {
187             throw new IllegalStateException JavaDoc("Configuration is frozen");
188         }
189         this.include = include;
190     }
191
192
193     /**
194      * Context-relative path of the input form to which control should be
195      * returned if a validation error is encountered. Required if "name"
196      * is specified and the input bean returns validation errors.
197      */

198     protected String JavaDoc input = null;
199
200     /**
201      * Get the context-relative path of the input form to which control should be
202      * returned if a validation error is encountered.
203      * @return context-relative path of the input form to which control should be
204      * returned if a validation error is encountered.
205      */

206     public String JavaDoc getInput() {
207         return (this.input);
208     }
209
210     /**
211      * Set the context-relative path of the input form to which control should be
212      * returned if a validation error is encountered. Required if "name"
213      * is specified and the input bean returns validation errors.
214      * @param input context-relative path of the input form to which control should be
215      * returned if a validation error is encountered.
216      */

217     public void setInput(String JavaDoc input) {
218         if (configured) {
219             throw new IllegalStateException JavaDoc("Configuration is frozen");
220         }
221         this.input = input;
222     }
223
224
225     /**
226      * Fully qualified Java class name of the
227      * <code>MultipartRequestHandler</code> implementation class used to
228      * process multi-part request data for this Action.
229      */

230     protected String JavaDoc multipartClass = null;
231
232     /**
233      * Return the fully qualified Java class name of the
234      * <code>MultipartRequestHandler</code> implementation class used to
235      * process multi-part request data for this Action.
236      */

237     public String JavaDoc getMultipartClass() {
238         return (this.multipartClass);
239     }
240
241     /**
242      * Set the fully qualified Java class name of the
243      * <code>MultipartRequestHandler</code> implementation class used to
244      * process multi-part request data for this Action.
245      * @param multipartClass fully qualified class name of the
246      * <code>MultipartRequestHandler</code> implementation class.
247      */

248     public void setMultipartClass(String JavaDoc multipartClass) {
249         if (configured) {
250             throw new IllegalStateException JavaDoc("Configuration is frozen");
251         }
252         this.multipartClass = multipartClass;
253     }
254
255
256     /**
257      * Name of the form bean, if any, associated with this Action.
258      */

259     protected String JavaDoc name = null;
260
261     /**
262      * Return name of the form bean, if any, associated with this Action.
263      */

264     public String JavaDoc getName() {
265         return (this.name);
266     }
267
268     /**
269      * @param name name of the form bean associated with this Action.
270      */

271     public void setName(String JavaDoc name) {
272         if (configured) {
273             throw new IllegalStateException JavaDoc("Configuration is frozen");
274         }
275         this.name = name;
276     }
277
278
279     /**
280      * General purpose configuration parameter that can be used to pass
281      * extra information to the Action instance selected by this Action.
282      * Struts does not itself use this value in any way.
283      */

284     protected String JavaDoc parameter = null;
285
286     /**
287       * Return general purpose configuration parameter that can be used to pass
288       * extra information to the Action instance selected by this Action.
289       * Struts does not itself use this value in any way.
290       */

291     public String JavaDoc getParameter() {
292         return (this.parameter);
293     }
294
295     /**
296       * General purpose configuration parameter that can be used to pass
297       * extra information to the Action instance selected by this Action.
298       * Struts does not itself use this value in any way.
299      * @param parameter General purpose configuration parameter.
300       */

301     public void setParameter(String JavaDoc parameter) {
302         if (configured) {
303             throw new IllegalStateException JavaDoc("Configuration is frozen");
304         }
305         this.parameter = parameter;
306     }
307
308
309     /**
310      * Context-relative path of the submitted request, starting with a
311      * slash ("/") character, and omitting any filename extension if
312      * extension mapping is being used.
313      */

314     protected String JavaDoc path = null;
315
316     /**
317      * Return context-relative path of the submitted request, starting with a
318      * slash ("/") character, and omitting any filename extension if
319      * extension mapping is being used.
320      */

321     public String JavaDoc getPath() {
322         return (this.path);
323     }
324
325     /**
326      * Set context-relative path of the submitted request, starting with a
327      * slash ("/") character, and omitting any filename extension if
328      * extension mapping is being used.
329      * @param path context-relative path of the submitted request.
330      */

331     public void setPath(String JavaDoc path) {
332         if (configured) {
333             throw new IllegalStateException JavaDoc("Configuration is frozen");
334         }
335         this.path = path;
336     }
337
338
339     /**
340      * Prefix used to match request parameter names to form bean property
341      * names, if any.
342      */

343     protected String JavaDoc prefix = null;
344
345     /**
346      * Retruns prefix used to match request parameter names to form bean property
347      * names, if any.
348      */

349     public String JavaDoc getPrefix() {
350         return (this.prefix);
351     }
352
353     /**
354      * @param prefix Prefix used to match request parameter names to
355      * form bean property names, if any.
356      */

357     public void setPrefix(String JavaDoc prefix) {
358         if (configured) {
359             throw new IllegalStateException JavaDoc("Configuration is frozen");
360         }
361         this.prefix = prefix;
362     }
363
364
365     /**
366      * Comma-delimited list of security role names allowed to request
367      * this Action.
368      */

369     protected String JavaDoc roles = null;
370
371     public String JavaDoc getRoles() {
372         return (this.roles);
373     }
374
375     public void setRoles(String JavaDoc roles) {
376         if (configured) {
377             throw new IllegalStateException JavaDoc("Configuration is frozen");
378         }
379         this.roles = roles;
380         if (roles == null) {
381             roleNames = new String JavaDoc[0];
382             return;
383         }
384         ArrayList JavaDoc list = new ArrayList JavaDoc();
385         while (true) {
386             int comma = roles.indexOf(',');
387             if (comma < 0)
388                 break;
389             list.add(roles.substring(0, comma).trim());
390             roles = roles.substring(comma + 1);
391         }
392         roles = roles.trim();
393         if (roles.length() > 0)
394             list.add(roles);
395         roleNames = (String JavaDoc[]) list.toArray(new String JavaDoc[list.size()]);
396     }
397
398
399     /**
400      * The set of security role names used to authorize access to this
401      * Action, as an array for faster access.
402      */

403     protected String JavaDoc[] roleNames = new String JavaDoc[0];
404
405     /**
406      * Get array of security role names used to authorize access to this
407      * Action.
408      */

409     public String JavaDoc[] getRoleNames() {
410         return (this.roleNames);
411     }
412
413
414     /**
415      * Identifier of the scope ("request" or "session") within which
416      * our form bean is accessed, if any.
417      */

418     protected String JavaDoc scope = "session";
419
420     /**
421      * Get the scope ("request" or "session") within which
422      * our form bean is accessed, if any.
423      */

424     public String JavaDoc getScope() {
425         return (this.scope);
426     }
427
428     /**
429      * @param scope scope ("request" or "session") within which
430      * our form bean is accessed, if any.
431      */

432     public void setScope(String JavaDoc scope) {
433         if (configured) {
434             throw new IllegalStateException JavaDoc("Configuration is frozen");
435         }
436         this.scope = scope;
437     }
438
439
440     /**
441      * Suffix used to match request parameter names to form bean property
442      * names, if any.
443      */

444     protected String JavaDoc suffix = null;
445
446     /**
447      * Return suffix used to match request parameter names to form bean property
448      * names, if any.
449      */

450     public String JavaDoc getSuffix() {
451         return (this.suffix);
452     }
453
454     /**
455      * @param suffix Suffix used to match request parameter names to form bean property
456      * names, if any.
457      */

458     public void setSuffix(String JavaDoc suffix) {
459         if (configured) {
460             throw new IllegalStateException JavaDoc("Configuration is frozen");
461         }
462         this.suffix = suffix;
463     }
464
465
466     /**
467      * Fully qualified Java class name of the <code>Action</code> class
468      * to be used to process requests for this mapping if the
469      * <code>forward</code> and <code>include</code> properties are not set.
470      * Exactly one of <code>forward</code>, <code>include</code>, or
471      * <code>type</code> must be specified.
472      */

473     protected String JavaDoc type = null;
474
475     public String JavaDoc getType() {
476         return (this.type);
477     }
478
479     public void setType(String JavaDoc type) {
480         if (configured) {
481             throw new IllegalStateException JavaDoc("Configuration is frozen");
482         }
483         this.type = type;
484     }
485
486
487     /**
488      * Indicates Action be configured as the default one for this
489      * module, when true.
490      */

491     protected boolean unknown = false;
492
493     /**
494       * Determine whether Action is configured as the default one for this
495       * module.
496       */

497     public boolean getUnknown() {
498         return (this.unknown);
499     }
500
501     /**
502       * @param unknown Indicates Action is configured as the default one for this
503       * module, when true.
504       */

505     public void setUnknown(boolean unknown) {
506         if (configured) {
507             throw new IllegalStateException JavaDoc("Configuration is frozen");
508         }
509         this.unknown = unknown;
510     }
511
512     /**
513      * Should the <code>validate()</code> method of the form bean associated
514      * with this action be called?
515      */

516     protected boolean validate = true;
517
518     public boolean getValidate() {
519         return (this.validate);
520     }
521
522     public void setValidate(boolean validate) {
523         if (configured) {
524             throw new IllegalStateException JavaDoc("Configuration is frozen");
525         }
526         this.validate = validate;
527     }
528
529
530     // --------------------------------------------------------- Public Methods
531

532
533     /**
534      * Add a new <code>ExceptionConfig</code> instance to the set associated
535      * with this action.
536      *
537      * @param config The new configuration instance to be added
538      *
539      * @exception IllegalStateException if this module configuration
540      * has been frozen
541      */

542     public void addExceptionConfig(ExceptionConfig config) {
543
544         if (configured) {
545             throw new IllegalStateException JavaDoc("Configuration is frozen");
546         }
547         exceptions.put(config.getType(), config);
548
549     }
550
551
552     /**
553      * Add a new <code>ForwardConfig</code> instance to the set of global
554      * forwards associated with this action.
555      *
556      * @param config The new configuration instance to be added
557      *
558      * @exception IllegalStateException if this module configuration
559      * has been frozen
560      */

561     public void addForwardConfig(ForwardConfig config) {
562
563         if (configured) {
564             throw new IllegalStateException JavaDoc("Configuration is frozen");
565         }
566         forwards.put(config.getName(), config);
567
568     }
569
570
571     /**
572      * Return the exception configuration for the specified type, if any;
573      * otherwise return <code>null</code>.
574      *
575      * @param type Exception class name to find a configuration for
576      */

577     public ExceptionConfig findExceptionConfig(String JavaDoc type) {
578
579         return ((ExceptionConfig) exceptions.get(type));
580
581     }
582
583
584     /**
585      * Return the exception configurations for this action. If there
586      * are none, a zero-length array is returned.
587      */

588     public ExceptionConfig[] findExceptionConfigs() {
589
590         ExceptionConfig results[] = new ExceptionConfig[exceptions.size()];
591         return ((ExceptionConfig[]) exceptions.values().toArray(results));
592
593     }
594
595     /**
596      * <p>Find and return the <code>ExceptionConfig</code> instance defining
597      * how <code>Exceptions</code> of the specified type should be handled.
598      * This is performed by checking local and then global configurations for
599      * the specified exception's class, and then looking up the superclass chain
600      * (again checking local and then global configurations). If no handler
601      * configuration can be found, return <code>null</code>.</p>
602      *
603      * <p>Introduced in <code>ActionMapping</code> in Struts 1.1, but pushed
604      * up to <code>ActionConfig</code> in Struts 1.2.0.</p>
605      *
606      * @param type Exception class for which to find a handler
607      * @since Struts 1.2.0
608      */

609     public ExceptionConfig findException(Class JavaDoc type) {
610
611         // Check through the entire superclass hierarchy as needed
612
ExceptionConfig config = null;
613         while (true) {
614
615             // Check for a locally defined handler
616
String JavaDoc name = type.getName();
617             config = findExceptionConfig(name);
618             if (config != null) {
619                 return (config);
620             }
621
622             // Check for a globally defined handler
623
config = getModuleConfig().findExceptionConfig(name);
624             if (config != null) {
625                 return (config);
626             }
627
628             // Loop again for our superclass (if any)
629
type = type.getSuperclass();
630             if (type == null) {
631                 break;
632             }
633
634         }
635         return (null); // No handler has been configured
636

637     }
638
639
640
641     /**
642      * Return the forward configuration for the specified key, if any;
643      * otherwise return <code>null</code>.
644      *
645      * @param name Name of the forward configuration to return
646      */

647     public ForwardConfig findForwardConfig(String JavaDoc name) {
648
649         return ((ForwardConfig) forwards.get(name));
650
651     }
652
653
654     /**
655      * Return all forward configurations for this module. If there
656      * are none, a zero-length array is returned.
657      */

658     public ForwardConfig[] findForwardConfigs() {
659
660         ForwardConfig results[] = new ForwardConfig[forwards.size()];
661         return ((ForwardConfig[]) forwards.values().toArray(results));
662
663     }
664
665
666     /**
667      * Freeze the configuration of this action.
668      */

669     public void freeze() {
670
671         configured = true;
672
673         ExceptionConfig[] econfigs = findExceptionConfigs();
674         for (int i = 0; i < econfigs.length; i++) {
675             econfigs[i].freeze();
676         }
677
678         ForwardConfig[] fconfigs = findForwardConfigs();
679         for (int i = 0; i < fconfigs.length; i++) {
680             fconfigs[i].freeze();
681         }
682
683     }
684
685
686     /**
687      * Remove the specified exception configuration instance.
688      *
689      * @param config ExceptionConfig instance to be removed
690      *
691      * @exception IllegalStateException if this module configuration
692      * has been frozen
693      */

694     public void removeExceptionConfig(ExceptionConfig config) {
695
696         if (configured) {
697             throw new IllegalStateException JavaDoc("Configuration is frozen");
698         }
699         exceptions.remove(config.getType());
700
701     }
702
703
704     /**
705      * Remove the specified forward configuration instance.
706      *
707      * @param config ForwardConfig instance to be removed
708      *
709      * @exception IllegalStateException if this module configuration
710      * has been frozen
711      */

712     public void removeForwardConfig(ForwardConfig config) {
713
714         if (configured) {
715             throw new IllegalStateException JavaDoc("Configuration is frozen");
716         }
717         forwards.remove(config.getName());
718
719     }
720
721
722     /**
723      * Return a String representation of this object.
724      */

725     public String JavaDoc toString() {
726
727         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("ActionConfig[");
728         sb.append("path=");
729         sb.append(path);
730         if (attribute != null) {
731             sb.append(",attribute=");
732             sb.append(attribute);
733         }
734         if (forward != null) {
735             sb.append(",forward=");
736             sb.append(forward);
737         }
738         if (include != null) {
739             sb.append(",include=");
740             sb.append(include);
741         }
742         if (input != null) {
743             sb.append(",input=");
744             sb.append(input);
745         }
746         if (multipartClass != null) {
747             sb.append(",multipartClass=");
748             sb.append(multipartClass);
749         }
750         if (name != null) {
751             sb.append(",name=");
752             sb.append(name);
753         }
754         if (parameter != null) {
755             sb.append(",parameter=");
756             sb.append(parameter);
757         }
758         if (prefix != null) {
759             sb.append(",prefix=");
760             sb.append(prefix);
761         }
762         if (roles != null) {
763             sb.append(",roles=");
764             sb.append(roles);
765         }
766         if (scope != null) {
767             sb.append(",scope=");
768             sb.append(scope);
769         }
770         if (suffix != null) {
771             sb.append(",suffix=");
772             sb.append(suffix);
773         }
774         if (type != null) {
775             sb.append(",type=");
776             sb.append(type);
777         }
778         return (sb.toString());
779
780     }
781
782
783 }
784
Popular Tags