KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > startup > WebRuleSet


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18
19 package org.apache.catalina.startup;
20
21
22 import java.lang.reflect.Method JavaDoc;
23 import java.util.ArrayList JavaDoc;
24
25 import org.apache.catalina.Context;
26 import org.apache.catalina.Wrapper;
27 import org.apache.catalina.deploy.SecurityConstraint;
28 import org.apache.tomcat.util.IntrospectionUtils;
29 import org.apache.tomcat.util.digester.CallMethodRule;
30 import org.apache.tomcat.util.digester.CallParamRule;
31 import org.apache.tomcat.util.digester.Digester;
32 import org.apache.tomcat.util.digester.Rule;
33 import org.apache.tomcat.util.digester.RuleSetBase;
34 import org.xml.sax.Attributes JavaDoc;
35
36
37 /**
38  * <p><strong>RuleSet</strong> for processing the contents of a web application
39  * deployment descriptor (<code>/WEB-INF/web.xml</code>) resource.</p>
40  *
41  * @author Craig R. McClanahan
42  * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
43  */

44
45 public class WebRuleSet extends RuleSetBase {
46
47
48     // ----------------------------------------------------- Instance Variables
49

50
51     /**
52      * The matching pattern prefix to use for recognizing our elements.
53      */

54     protected String JavaDoc prefix = null;
55     
56     
57     /**
58      * The <code>SetSessionConfig</code> rule used to parse the web.xml
59      */

60     protected SetSessionConfig sessionConfig;
61     
62     
63     /**
64      * The <code>SetLoginConfig</code> rule used to parse the web.xml
65      */

66     protected SetLoginConfig loginConfig;
67
68     
69     /**
70      * The <code>SetJspConfig</code> rule used to parse the web.xml
71      */

72     protected SetJspConfig jspConfig;
73
74
75     // ------------------------------------------------------------ Constructor
76

77
78     /**
79      * Construct an instance of this <code>RuleSet</code> with the default
80      * matching pattern prefix.
81      */

82     public WebRuleSet() {
83
84         this("");
85
86     }
87
88
89     /**
90      * Construct an instance of this <code>RuleSet</code> with the specified
91      * matching pattern prefix.
92      *
93      * @param prefix Prefix for matching pattern rules (including the
94      * trailing slash character)
95      */

96     public WebRuleSet(String JavaDoc prefix) {
97
98         super();
99         this.namespaceURI = null;
100         this.prefix = prefix;
101
102     }
103
104
105     // --------------------------------------------------------- Public Methods
106

107
108     /**
109      * <p>Add the set of Rule instances defined in this RuleSet to the
110      * specified <code>Digester</code> instance, associating them with
111      * our namespace URI (if any). This method should only be called
112      * by a Digester instance.</p>
113      *
114      * @param digester Digester instance to which the new Rule instances
115      * should be added.
116      */

117     public void addRuleInstances(Digester digester) {
118         sessionConfig = new SetSessionConfig();
119         jspConfig = new SetJspConfig();
120         loginConfig = new SetLoginConfig();
121         
122         digester.addRule(prefix + "web-app",
123                          new SetPublicIdRule("setPublicId"));
124         digester.addRule(prefix + "web-app",
125                          new IgnoreAnnotationsRule());
126
127         digester.addCallMethod(prefix + "web-app/context-param",
128                                "addParameter", 2);
129         digester.addCallParam(prefix + "web-app/context-param/param-name", 0);
130         digester.addCallParam(prefix + "web-app/context-param/param-value", 1);
131
132         digester.addCallMethod(prefix + "web-app/display-name",
133                                "setDisplayName", 0);
134
135         digester.addRule(prefix + "web-app/distributable",
136                          new SetDistributableRule());
137
138         digester.addObjectCreate(prefix + "web-app/ejb-local-ref",
139                                  "org.apache.catalina.deploy.ContextLocalEjb");
140         digester.addRule(prefix + "web-app/ejb-local-ref",
141                 new SetNextNamingRule("addLocalEjb",
142                             "org.apache.catalina.deploy.ContextLocalEjb"));
143
144         digester.addCallMethod(prefix + "web-app/ejb-local-ref/description",
145                                "setDescription", 0);
146         digester.addCallMethod(prefix + "web-app/ejb-local-ref/ejb-link",
147                                "setLink", 0);
148         digester.addCallMethod(prefix + "web-app/ejb-local-ref/ejb-ref-name",
149                                "setName", 0);
150         digester.addCallMethod(prefix + "web-app/ejb-local-ref/ejb-ref-type",
151                                "setType", 0);
152         digester.addCallMethod(prefix + "web-app/ejb-local-ref/local",
153                                "setLocal", 0);
154         digester.addCallMethod(prefix + "web-app/ejb-local-ref/local-home",
155                                "setHome", 0);
156
157         digester.addObjectCreate(prefix + "web-app/ejb-ref",
158                                  "org.apache.catalina.deploy.ContextEjb");
159         digester.addRule(prefix + "web-app/ejb-ref",
160                 new SetNextNamingRule("addEjb",
161                             "org.apache.catalina.deploy.ContextEjb"));
162
163         digester.addCallMethod(prefix + "web-app/ejb-ref/description",
164                                "setDescription", 0);
165         digester.addCallMethod(prefix + "web-app/ejb-ref/ejb-link",
166                                "setLink", 0);
167         digester.addCallMethod(prefix + "web-app/ejb-ref/ejb-ref-name",
168                                "setName", 0);
169         digester.addCallMethod(prefix + "web-app/ejb-ref/ejb-ref-type",
170                                "setType", 0);
171         digester.addCallMethod(prefix + "web-app/ejb-ref/home",
172                                "setHome", 0);
173         digester.addCallMethod(prefix + "web-app/ejb-ref/remote",
174                                "setRemote", 0);
175
176         digester.addObjectCreate(prefix + "web-app/env-entry",
177                                  "org.apache.catalina.deploy.ContextEnvironment");
178         digester.addRule(prefix + "web-app/env-entry",
179                 new SetNextNamingRule("addEnvironment",
180                             "org.apache.catalina.deploy.ContextEnvironment"));
181
182         digester.addCallMethod(prefix + "web-app/env-entry/description",
183                                "setDescription", 0);
184         digester.addCallMethod(prefix + "web-app/env-entry/env-entry-name",
185                                "setName", 0);
186         digester.addCallMethod(prefix + "web-app/env-entry/env-entry-type",
187                                "setType", 0);
188         digester.addCallMethod(prefix + "web-app/env-entry/env-entry-value",
189                                "setValue", 0);
190
191         digester.addObjectCreate(prefix + "web-app/error-page",
192                                  "org.apache.catalina.deploy.ErrorPage");
193         digester.addSetNext(prefix + "web-app/error-page",
194                             "addErrorPage",
195                             "org.apache.catalina.deploy.ErrorPage");
196
197         digester.addCallMethod(prefix + "web-app/error-page/error-code",
198                                "setErrorCode", 0);
199         digester.addCallMethod(prefix + "web-app/error-page/exception-type",
200                                "setExceptionType", 0);
201         digester.addCallMethod(prefix + "web-app/error-page/location",
202                                "setLocation", 0);
203
204         digester.addObjectCreate(prefix + "web-app/filter",
205                                  "org.apache.catalina.deploy.FilterDef");
206         digester.addSetNext(prefix + "web-app/filter",
207                             "addFilterDef",
208                             "org.apache.catalina.deploy.FilterDef");
209
210         digester.addCallMethod(prefix + "web-app/filter/description",
211                                "setDescription", 0);
212         digester.addCallMethod(prefix + "web-app/filter/display-name",
213                                "setDisplayName", 0);
214         digester.addCallMethod(prefix + "web-app/filter/filter-class",
215                                "setFilterClass", 0);
216         digester.addCallMethod(prefix + "web-app/filter/filter-name",
217                                "setFilterName", 0);
218         digester.addCallMethod(prefix + "web-app/filter/large-icon",
219                                "setLargeIcon", 0);
220         digester.addCallMethod(prefix + "web-app/filter/small-icon",
221                                "setSmallIcon", 0);
222
223         digester.addCallMethod(prefix + "web-app/filter/init-param",
224                                "addInitParameter", 2);
225         digester.addCallParam(prefix + "web-app/filter/init-param/param-name",
226                               0);
227         digester.addCallParam(prefix + "web-app/filter/init-param/param-value",
228                               1);
229
230         digester.addObjectCreate(prefix + "web-app/filter-mapping",
231                                  "org.apache.catalina.deploy.FilterMap");
232         digester.addSetNext(prefix + "web-app/filter-mapping",
233                                  "addFilterMap",
234                                  "org.apache.catalina.deploy.FilterMap");
235
236         digester.addCallMethod(prefix + "web-app/filter-mapping/filter-name",
237                                "setFilterName", 0);
238         digester.addCallMethod(prefix + "web-app/filter-mapping/servlet-name",
239                                "addServletName", 0);
240         digester.addCallMethod(prefix + "web-app/filter-mapping/url-pattern",
241                                "addURLPattern", 0);
242
243         digester.addCallMethod(prefix + "web-app/filter-mapping/dispatcher",
244                                "setDispatcher", 0);
245
246          digester.addCallMethod(prefix + "web-app/listener/listener-class",
247                                 "addApplicationListener", 0);
248          
249         digester.addRule(prefix + "web-app/jsp-config",
250                          jspConfig);
251         
252         digester.addCallMethod(prefix + "web-app/jsp-config/jsp-property-group/url-pattern",
253                                "addJspMapping", 0);
254
255         digester.addCallMethod(prefix + "web-app/listener/listener-class",
256                                "addApplicationListener", 0);
257         
258         digester.addRule(prefix + "web-app/login-config",
259                          loginConfig);
260
261         digester.addObjectCreate(prefix + "web-app/login-config",
262                                  "org.apache.catalina.deploy.LoginConfig");
263         digester.addSetNext(prefix + "web-app/login-config",
264                             "setLoginConfig",
265                             "org.apache.catalina.deploy.LoginConfig");
266
267         digester.addCallMethod(prefix + "web-app/login-config/auth-method",
268                                "setAuthMethod", 0);
269         digester.addCallMethod(prefix + "web-app/login-config/realm-name",
270                                "setRealmName", 0);
271         digester.addCallMethod(prefix + "web-app/login-config/form-login-config/form-error-page",
272                                "setErrorPage", 0);
273         digester.addCallMethod(prefix + "web-app/login-config/form-login-config/form-login-page",
274                                "setLoginPage", 0);
275
276         digester.addCallMethod(prefix + "web-app/mime-mapping",
277                                "addMimeMapping", 2);
278         digester.addCallParam(prefix + "web-app/mime-mapping/extension", 0);
279         digester.addCallParam(prefix + "web-app/mime-mapping/mime-type", 1);
280
281         digester.addObjectCreate(prefix + "web-app/resource-env-ref",
282             "org.apache.catalina.deploy.ContextResourceEnvRef");
283         digester.addRule(prefix + "web-app/resource-env-ref",
284                     new SetNextNamingRule("addResourceEnvRef",
285                         "org.apache.catalina.deploy.ContextResourceEnvRef"));
286
287         digester.addCallMethod(prefix + "web-app/resource-env-ref/resource-env-ref-name",
288                 "setName", 0);
289         digester.addCallMethod(prefix + "web-app/resource-env-ref/resource-env-ref-type",
290                 "setType", 0);
291
292         digester.addObjectCreate(prefix + "web-app/message-destination",
293                                  "org.apache.catalina.deploy.MessageDestination");
294         digester.addSetNext(prefix + "web-app/message-destination",
295                             "addMessageDestination",
296                             "org.apache.catalina.deploy.MessageDestination");
297
298         digester.addCallMethod(prefix + "web-app/message-destination/description",
299                                "setDescription", 0);
300         digester.addCallMethod(prefix + "web-app/message-destination/display-name",
301                                "setDisplayName", 0);
302         digester.addCallMethod(prefix + "web-app/message-destination/icon/large-icon",
303                                "setLargeIcon", 0);
304         digester.addCallMethod(prefix + "web-app/message-destination/icon/small-icon",
305                                "setSmallIcon", 0);
306         digester.addCallMethod(prefix + "web-app/message-destination/message-destination-name",
307                                "setName", 0);
308
309         digester.addObjectCreate(prefix + "web-app/message-destination-ref",
310                                  "org.apache.catalina.deploy.MessageDestinationRef");
311         digester.addSetNext(prefix + "web-app/message-destination-ref",
312                             "addMessageDestinationRef",
313                             "org.apache.catalina.deploy.MessageDestinationRef");
314
315         digester.addCallMethod(prefix + "web-app/message-destination-ref/description",
316                                "setDescription", 0);
317         digester.addCallMethod(prefix + "web-app/message-destination-ref/message-destination-link",
318                                "setLink", 0);
319         digester.addCallMethod(prefix + "web-app/message-destination-ref/message-destination-ref-name",
320                                "setName", 0);
321         digester.addCallMethod(prefix + "web-app/message-destination-ref/message-destination-type",
322                                "setType", 0);
323         digester.addCallMethod(prefix + "web-app/message-destination-ref/message-destination-usage",
324                                "setUsage", 0);
325
326         digester.addObjectCreate(prefix + "web-app/resource-ref",
327                                  "org.apache.catalina.deploy.ContextResource");
328         digester.addRule(prefix + "web-app/resource-ref",
329                 new SetNextNamingRule("addResource",
330                             "org.apache.catalina.deploy.ContextResource"));
331
332         digester.addCallMethod(prefix + "web-app/resource-ref/description",
333                                "setDescription", 0);
334         digester.addCallMethod(prefix + "web-app/resource-ref/res-auth",
335                                "setAuth", 0);
336         digester.addCallMethod(prefix + "web-app/resource-ref/res-ref-name",
337                                "setName", 0);
338         digester.addCallMethod(prefix + "web-app/resource-ref/res-sharing-scope",
339                                "setScope", 0);
340         digester.addCallMethod(prefix + "web-app/resource-ref/res-type",
341                                "setType", 0);
342
343         digester.addObjectCreate(prefix + "web-app/security-constraint",
344                                  "org.apache.catalina.deploy.SecurityConstraint");
345         digester.addSetNext(prefix + "web-app/security-constraint",
346                             "addConstraint",
347                             "org.apache.catalina.deploy.SecurityConstraint");
348
349         digester.addRule(prefix + "web-app/security-constraint/auth-constraint",
350                          new SetAuthConstraintRule());
351         digester.addCallMethod(prefix + "web-app/security-constraint/auth-constraint/role-name",
352                                "addAuthRole", 0);
353         digester.addCallMethod(prefix + "web-app/security-constraint/display-name",
354                                "setDisplayName", 0);
355         digester.addCallMethod(prefix + "web-app/security-constraint/user-data-constraint/transport-guarantee",
356                                "setUserConstraint", 0);
357
358         digester.addObjectCreate(prefix + "web-app/security-constraint/web-resource-collection",
359                                  "org.apache.catalina.deploy.SecurityCollection");
360         digester.addSetNext(prefix + "web-app/security-constraint/web-resource-collection",
361                             "addCollection",
362                             "org.apache.catalina.deploy.SecurityCollection");
363         digester.addCallMethod(prefix + "web-app/security-constraint/web-resource-collection/http-method",
364                                "addMethod", 0);
365         digester.addCallMethod(prefix + "web-app/security-constraint/web-resource-collection/url-pattern",
366                                "addPattern", 0);
367         digester.addCallMethod(prefix + "web-app/security-constraint/web-resource-collection/web-resource-name",
368                                "setName", 0);
369
370         digester.addCallMethod(prefix + "web-app/security-role/role-name",
371                                "addSecurityRole", 0);
372
373         digester.addObjectCreate(prefix + "web-app/service-ref",
374                                  "org.apache.catalina.deploy.ContextService");
375         digester.addRule(prefix + "web-app/service-ref",
376                          new SetNextNamingRule("addService",
377                          "org.apache.catalina.deploy.ContextService"));
378         
379         digester.addCallMethod(prefix + "web-app/service-ref/description",
380                                "setDescription", 0);
381         digester.addCallMethod(prefix + "web-app/service-ref/display-name",
382                                "setDisplayname", 0);
383         digester.addCallMethod(prefix + "web-app/service-ref/icon",
384                                "setIcon", 0);
385         digester.addCallMethod(prefix + "web-app/service-ref/service-ref-name",
386                                "setName", 0);
387         digester.addCallMethod(prefix + "web-app/service-ref/service-interface",
388                                "setServiceinterface", 0);
389         digester.addCallMethod(prefix + "web-app/service-ref/wsdl-file",
390                                "setWsdlfile", 0);
391         digester.addCallMethod(prefix + "web-app/service-ref/jaxrpc-mapping-file",
392                                "setJaxrpcmappingfile", 0);
393         digester.addCallMethod(prefix + "web-app/service-ref/service-qname/namespaceURI",
394                                "setNamespaceURI", 0);
395         digester.addCallMethod(prefix + "web-app/service-ref/service-qname/localpart",
396                                "setLocalpart", 0);
397         digester.addCallMethod(prefix +
398                                "web-app/service-ref/port-component/service-endpoint-interface",
399                                "setServiceendpoint", 0);
400         digester.addCallMethod(prefix + "web-app/service-ref/port-component/port-component-link",
401                                "setPortlink", 0);
402         digester.addCallMethod(prefix + "web-app/service-ref/handler",
403                                "setHandler", 0);
404         digester.addCallMethod(prefix + "web-app/service-ref/service-ref-type",
405                                "setType", 0);
406         
407         digester.addRule(prefix + "web-app/servlet",
408                          new WrapperCreateRule());
409         digester.addSetNext(prefix + "web-app/servlet",
410                             "addChild",
411                             "org.apache.catalina.Container");
412
413         digester.addCallMethod(prefix + "web-app/servlet/init-param",
414                                "addInitParameter", 2);
415         digester.addCallParam(prefix + "web-app/servlet/init-param/param-name",
416                               0);
417         digester.addCallParam(prefix + "web-app/servlet/init-param/param-value",
418                               1);
419
420         digester.addCallMethod(prefix + "web-app/servlet/jsp-file",
421                                "setJspFile", 0);
422         digester.addCallMethod(prefix + "web-app/servlet/load-on-startup",
423                                "setLoadOnStartupString", 0);
424         digester.addCallMethod(prefix + "web-app/servlet/run-as/role-name",
425                                "setRunAs", 0);
426
427         digester.addCallMethod(prefix + "web-app/servlet/security-role-ref",
428                                "addSecurityReference", 2);
429         digester.addCallParam(prefix + "web-app/servlet/security-role-ref/role-link", 1);
430         digester.addCallParam(prefix + "web-app/servlet/security-role-ref/role-name", 0);
431
432         digester.addCallMethod(prefix + "web-app/servlet/servlet-class",
433                               "setServletClass", 0);
434         digester.addCallMethod(prefix + "web-app/servlet/servlet-name",
435                               "setName", 0);
436
437         digester.addRule(prefix + "web-app/servlet-mapping",
438                                new CallMethodMultiRule("addServletMapping", 2, 0));
439         digester.addCallParam(prefix + "web-app/servlet-mapping/servlet-name", 1);
440         digester.addRule(prefix + "web-app/servlet-mapping/url-pattern", new CallParamMultiRule(0));
441
442         digester.addRule(prefix + "web-app/session-config",
443                          sessionConfig);
444         
445         digester.addCallMethod(prefix + "web-app/session-config/session-timeout",
446                                "setSessionTimeout", 1,
447                                new Class JavaDoc[] { Integer.TYPE });
448         digester.addCallParam(prefix + "web-app/session-config/session-timeout", 0);
449
450         digester.addCallMethod(prefix + "web-app/taglib",
451                                "addTaglib", 2);
452         digester.addCallParam(prefix + "web-app/taglib/taglib-location", 1);
453         digester.addCallParam(prefix + "web-app/taglib/taglib-uri", 0);
454
455         digester.addCallMethod(prefix + "web-app/welcome-file-list/welcome-file",
456                                "addWelcomeFile", 0);
457
458         digester.addCallMethod(prefix + "web-app/locale-encoding-mapping-list/locale-encoding-mapping",
459                               "addLocaleEncodingMappingParameter", 2);
460         digester.addCallParam(prefix + "web-app/locale-encoding-mapping-list/locale-encoding-mapping/locale", 0);
461         digester.addCallParam(prefix + "web-app/locale-encoding-mapping-list/locale-encoding-mapping/encoding", 1);
462
463     }
464
465     /**
466      * Reset counter used for validating the web.xml file.
467      */

468     public void recycle(){
469         jspConfig.isJspConfigSet = false;
470         sessionConfig.isSessionConfigSet = false;
471         loginConfig.isLoginConfigSet = false;
472     }
473 }
474
475
476 // ----------------------------------------------------------- Private Classes
477

478
479 /**
480  * Rule to check that the <code>login-config</code> is occuring
481  * only 1 time within the web.xml
482  */

483 final class SetLoginConfig extends Rule {
484     protected boolean isLoginConfigSet = false;
485     public SetLoginConfig() {
486     }
487
488     public void begin(String JavaDoc namespace, String JavaDoc name, Attributes JavaDoc attributes)
489         throws Exception JavaDoc {
490         if (isLoginConfigSet){
491             throw new IllegalArgumentException JavaDoc(
492             "<login-config> element is limited to 1 occurance");
493         }
494         isLoginConfigSet = true;
495     }
496
497 }
498
499
500 /**
501  * Rule to check that the <code>jsp-config</code> is occuring
502  * only 1 time within the web.xml
503  */

504 final class SetJspConfig extends Rule {
505     protected boolean isJspConfigSet = false;
506     public SetJspConfig() {
507     }
508
509     public void begin(String JavaDoc namespace, String JavaDoc name, Attributes JavaDoc attributes)
510         throws Exception JavaDoc {
511         if (isJspConfigSet){
512             throw new IllegalArgumentException JavaDoc(
513             "<jsp-config> element is limited to 1 occurance");
514         }
515         isJspConfigSet = true;
516     }
517
518 }
519
520
521 /**
522  * Rule to check that the <code>session-config</code> is occuring
523  * only 1 time within the web.xml
524  */

525 final class SetSessionConfig extends Rule {
526     protected boolean isSessionConfigSet = false;
527     public SetSessionConfig() {
528     }
529
530     public void begin(String JavaDoc namespace, String JavaDoc name, Attributes JavaDoc attributes)
531         throws Exception JavaDoc {
532         if (isSessionConfigSet){
533             throw new IllegalArgumentException JavaDoc(
534             "<session-config> element is limited to 1 occurance");
535         }
536         isSessionConfigSet = true;
537     }
538
539 }
540
541 /**
542  * A Rule that calls the <code>setAuthConstraint(true)</code> method of
543  * the top item on the stack, which must be of type
544  * <code>org.apache.catalina.deploy.SecurityConstraint</code>.
545  */

546
547 final class SetAuthConstraintRule extends Rule {
548
549     public SetAuthConstraintRule() {
550     }
551
552     public void begin(String JavaDoc namespace, String JavaDoc name, Attributes JavaDoc attributes)
553         throws Exception JavaDoc {
554         SecurityConstraint securityConstraint =
555             (SecurityConstraint) digester.peek();
556         securityConstraint.setAuthConstraint(true);
557         if (digester.getLogger().isDebugEnabled()) {
558             digester.getLogger()
559                .debug("Calling SecurityConstraint.setAuthConstraint(true)");
560         }
561     }
562
563 }
564
565
566 /**
567  * Class that calls <code>setDistributable(true)</code> for the top object
568  * on the stack, which must be a <code>org.apache.catalina.Context</code>.
569  */

570
571 final class SetDistributableRule extends Rule {
572
573     public SetDistributableRule() {
574     }
575
576     public void begin(String JavaDoc namespace, String JavaDoc name, Attributes JavaDoc attributes)
577         throws Exception JavaDoc {
578         Context context = (Context) digester.peek();
579         context.setDistributable(true);
580         if (digester.getLogger().isDebugEnabled()) {
581             digester.getLogger().debug
582                (context.getClass().getName() + ".setDistributable( true)");
583         }
584     }
585
586 }
587
588
589 /**
590  * Class that calls a property setter for the top object on the stack,
591  * passing the public ID of the entity we are currently processing.
592  */

593
594 final class SetPublicIdRule extends Rule {
595
596     public SetPublicIdRule(String JavaDoc method) {
597         this.method = method;
598     }
599
600     private String JavaDoc method = null;
601
602     public void begin(String JavaDoc namespace, String JavaDoc name, Attributes JavaDoc attributes)
603         throws Exception JavaDoc {
604
605         Context context = (Context) digester.peek(digester.getCount() - 1);
606         Object JavaDoc top = digester.peek();
607         Class JavaDoc paramClasses[] = new Class JavaDoc[1];
608         paramClasses[0] = "String".getClass();
609         String JavaDoc paramValues[] = new String JavaDoc[1];
610         paramValues[0] = digester.getPublicId();
611
612         Method JavaDoc m = null;
613         try {
614             m = top.getClass().getMethod(method, paramClasses);
615         } catch (NoSuchMethodException JavaDoc e) {
616             digester.getLogger().error("Can't find method " + method + " in "
617                                        + top + " CLASS " + top.getClass());
618             return;
619         }
620
621         m.invoke(top, (Object JavaDoc [])paramValues);
622         if (digester.getLogger().isDebugEnabled())
623             digester.getLogger().debug("" + top.getClass().getName() + "."
624                                        + method + "(" + paramValues[0] + ")");
625
626     }
627
628 }
629
630
631 /**
632  * A Rule that calls the factory method on the specified Context to
633  * create the object that is to be added to the stack.
634  */

635
636 final class WrapperCreateRule extends Rule {
637
638     public WrapperCreateRule() {
639     }
640
641     public void begin(String JavaDoc namespace, String JavaDoc name, Attributes JavaDoc attributes)
642         throws Exception JavaDoc {
643         Context context =
644             (Context) digester.peek(digester.getCount() - 1);
645         Wrapper wrapper = context.createWrapper();
646         digester.push(wrapper);
647         if (digester.getLogger().isDebugEnabled())
648             digester.getLogger().debug("new " + wrapper.getClass().getName());
649     }
650
651     public void end(String JavaDoc namespace, String JavaDoc name)
652         throws Exception JavaDoc {
653         Wrapper wrapper = (Wrapper) digester.pop();
654         if (digester.getLogger().isDebugEnabled())
655             digester.getLogger().debug("pop " + wrapper.getClass().getName());
656     }
657
658 }
659
660
661 /**
662  * A Rule that can be used to call multiple times a method as many times as needed
663  * (used for addServletMapping).
664  */

665 final class CallParamMultiRule extends CallParamRule {
666
667     public CallParamMultiRule(int paramIndex) {
668         super(paramIndex);
669     }
670
671     public void end(String JavaDoc namespace, String JavaDoc name) {
672         if (bodyTextStack != null && !bodyTextStack.empty()) {
673             // what we do now is push one parameter onto the top set of parameters
674
Object JavaDoc parameters[] = (Object JavaDoc[]) digester.peekParams();
675             ArrayList JavaDoc params = (ArrayList JavaDoc) parameters[paramIndex];
676             if (params == null) {
677                 params = new ArrayList JavaDoc();
678                 parameters[paramIndex] = params;
679             }
680             params.add(bodyTextStack.pop());
681         }
682     }
683
684 }
685
686
687 /**
688  * A Rule that can be used to call multiple times a method as many times as needed
689  * (used for addServletMapping).
690  */

691 final class CallMethodMultiRule extends CallMethodRule {
692
693     protected int multiParamIndex = 0;
694     
695     public CallMethodMultiRule(String JavaDoc methodName, int paramCount, int multiParamIndex) {
696         super(methodName, paramCount);
697         this.multiParamIndex = multiParamIndex;
698     }
699
700     public void end() throws Exception JavaDoc {
701
702         // Retrieve or construct the parameter values array
703
Object JavaDoc parameters[] = null;
704         if (paramCount > 0) {
705             parameters = (Object JavaDoc[]) digester.popParams();
706         } else {
707             super.end();
708         }
709         
710         ArrayList JavaDoc multiParams = (ArrayList JavaDoc) parameters[multiParamIndex];
711         
712         // Construct the parameter values array we will need
713
// We only do the conversion if the param value is a String and
714
// the specified paramType is not String.
715
Object JavaDoc paramValues[] = new Object JavaDoc[paramTypes.length];
716         for (int i = 0; i < paramTypes.length; i++) {
717             if (i != multiParamIndex) {
718                 // convert nulls and convert stringy parameters
719
// for non-stringy param types
720
if(parameters[i] == null || (parameters[i] instanceof String JavaDoc
721                         && !String JavaDoc.class.isAssignableFrom(paramTypes[i]))) {
722                     paramValues[i] =
723                         IntrospectionUtils.convert((String JavaDoc) parameters[i], paramTypes[i]);
724                 } else {
725                     paramValues[i] = parameters[i];
726                 }
727             }
728         }
729
730         // Determine the target object for the method call
731
Object JavaDoc target;
732         if (targetOffset >= 0) {
733             target = digester.peek(targetOffset);
734         } else {
735             target = digester.peek(digester.getCount() + targetOffset);
736         }
737
738         if (target == null) {
739             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
740             sb.append("[CallMethodRule]{");
741             sb.append("");
742             sb.append("} Call target is null (");
743             sb.append("targetOffset=");
744             sb.append(targetOffset);
745             sb.append(",stackdepth=");
746             sb.append(digester.getCount());
747             sb.append(")");
748             throw new org.xml.sax.SAXException JavaDoc(sb.toString());
749         }
750         
751         for (int j = 0; j < multiParams.size(); j++) {
752             Object JavaDoc param = multiParams.get(j);
753             if(param == null || (param instanceof String JavaDoc
754                     && !String JavaDoc.class.isAssignableFrom(paramTypes[multiParamIndex]))) {
755                 paramValues[multiParamIndex] =
756                     IntrospectionUtils.convert((String JavaDoc) param, paramTypes[multiParamIndex]);
757             } else {
758                 paramValues[multiParamIndex] = param;
759             }
760             Object JavaDoc result = IntrospectionUtils.callMethodN(target, methodName,
761                     paramValues, paramTypes);
762         }
763         
764     }
765
766 }
767
768
769
770 /**
771  * A Rule that check if the annotations have to be loaded.
772  *
773  */

774
775 final class IgnoreAnnotationsRule extends Rule {
776
777     public IgnoreAnnotationsRule() {
778     }
779
780     public void begin(String JavaDoc namespace, String JavaDoc name, Attributes JavaDoc attributes)
781         throws Exception JavaDoc {
782         Context context = (Context) digester.peek(digester.getCount() - 1);
783         String JavaDoc value = attributes.getValue("metadata-complete");
784         if ("true".equals(value)) {
785             context.setIgnoreAnnotations(true);
786         }
787         if (digester.getLogger().isDebugEnabled()) {
788             digester.getLogger().debug
789                 (context.getClass().getName() + ".setIgnoreAnnotations( " +
790                     context.getIgnoreAnnotations() + ")");
791         }
792     }
793
794 }
795
Popular Tags