KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > integration > ant > deployment > webapp > WebXml


1 /*
2  * ========================================================================
3  *
4  * Copyright 2003-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  */

20 package org.apache.cactus.integration.ant.deployment.webapp;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25
26 import org.w3c.dom.Document JavaDoc;
27 import org.w3c.dom.DocumentType JavaDoc;
28 import org.w3c.dom.Element JavaDoc;
29 import org.w3c.dom.Node JavaDoc;
30 import org.w3c.dom.NodeList JavaDoc;
31
32 /**
33  * Encapsulates the DOM representation of a web deployment descriptor
34  * <code>web.xml</code> to provide convenience methods for easy access and
35  * manipulation.
36  *
37  * @since Cactus 1.5
38  * @version $Id: WebXml.java,v 1.1 2004/05/31 20:05:23 vmassol Exp $
39  */

40 public class WebXml
41 {
42     
43     // Private Constants -------------------------------------------------------
44

45     /**
46      * Specifies the order in which the top-level elements must appear in the
47      * descriptor, according to the DTD.
48      */

49     private static final WebXmlTag[] ELEMENT_ORDER = {
50         WebXmlTag.ICON,
51         WebXmlTag.DISPLAY_NAME,
52         WebXmlTag.DESCRIPTION,
53         WebXmlTag.DISTRIBUTABLE,
54         WebXmlTag.FILTER,
55         WebXmlTag.FILTER_MAPPING,
56         WebXmlTag.LISTENER,
57         WebXmlTag.SERVLET,
58         WebXmlTag.SERVLET_MAPPING,
59         WebXmlTag.SESSION_CONFIG,
60         WebXmlTag.MIME_MAPPING,
61         WebXmlTag.WELCOME_FILE_LIST,
62         WebXmlTag.ERROR_PAGE,
63         WebXmlTag.TAGLIB,
64         WebXmlTag.RESOURCE_ENV_REF,
65         WebXmlTag.RESOURCE_REF,
66         WebXmlTag.SECURITY_CONSTRAINT,
67         WebXmlTag.LOGIN_CONFIG,
68         WebXmlTag.SECURITY_ROLE,
69         WebXmlTag.ENV_ENTRY,
70         WebXmlTag.EJB_REF,
71         WebXmlTag.EJB_LOCAL_REF,
72     };
73     
74     // Instance Variables ------------------------------------------------------
75

76     /**
77      * The DOM representation of the deployment descriptor.
78      */

79     private final Document JavaDoc document;
80     
81     /**
82      * The root element of the descriptor.
83      */

84     private final Element JavaDoc rootElement;
85     
86     // Constructors ------------------------------------------------------------
87

88     /**
89      * Constructor.
90      *
91      * @param theDocument The DOM document representing the parsed deployment
92      * descriptor
93      */

94     public WebXml(Document JavaDoc theDocument)
95     {
96         this.document = theDocument;
97         this.rootElement = theDocument.getDocumentElement();
98     }
99     
100     // Public Methods ----------------------------------------------------------
101

102     /**
103      * Returns the DOM document representing the deployment descriptor. The
104      * document will contain any modifications made through this instance.
105      *
106      * @return The document representing the deploy descriptor
107      */

108     public final Document JavaDoc getDocument()
109     {
110         return this.document;
111     }
112     
113     /**
114      * Returns the servlet API version.
115      *
116      * @return The version
117      */

118     public final WebXmlVersion getVersion()
119     {
120         DocumentType JavaDoc docType = this.document.getDoctype();
121         if (docType != null)
122         {
123             return WebXmlVersion.valueOf(docType);
124         }
125         return null;
126     }
127
128     /**
129      * Adds a new servlet filter to the descriptor.
130      *
131      * @param theFilterName The name of the filter to add
132      * @param theFilterClass The name of the class implementing the filter
133      */

134     public final void addFilter(String JavaDoc theFilterName, String JavaDoc theFilterClass)
135     {
136         if (theFilterName == null)
137         {
138             throw new NullPointerException JavaDoc();
139         }
140         if (hasFilter(theFilterName))
141         {
142             throw new IllegalStateException JavaDoc("Filter '" + theFilterName
143                 + "' already defined");
144         }
145         Element JavaDoc filterElement =
146             this.document.createElement(WebXmlTag.FILTER.getTagName());
147         filterElement.appendChild(
148             createNestedText(WebXmlTag.FILTER_NAME, theFilterName));
149         filterElement.appendChild(
150             createNestedText(WebXmlTag.FILTER_CLASS, theFilterClass));
151         addElement(WebXmlTag.FILTER, filterElement);
152     }
153
154     /**
155      * Adds a new context-param element to the descriptor.
156      *
157      * @param theContextParam The element representing the context-param
158      * definition
159      */

160     public final void addContextParam(Element JavaDoc theContextParam)
161     {
162         checkElement(theContextParam, WebXmlTag.CONTEXT_PARAM);
163
164         String JavaDoc paramName =
165             getNestedText(theContextParam, WebXmlTag.PARAM_NAME);
166         if (paramName == null)
167         {
168             throw new IllegalArgumentException JavaDoc(
169                 "Not a valid context-param name element");
170         }
171
172         String JavaDoc paramValue =
173             getNestedText(theContextParam, WebXmlTag.PARAM_VALUE);
174         if (paramValue == null)
175         {
176             throw new IllegalArgumentException JavaDoc(
177                 "Not a valid context-param value element");
178         }
179
180         if (hasContextParam(paramName))
181         {
182             throw new IllegalStateException JavaDoc("Context param '" + paramName
183                 + "' already defined");
184         }
185         addElement(WebXmlTag.CONTEXT_PARAM, theContextParam);
186     }
187     
188     /**
189      * Adds a new servlet filter to the descriptor.
190      *
191      * @param theFilter The element representing the filter definition
192      */

193     public final void addFilter(Element JavaDoc theFilter)
194     {
195         checkElement(theFilter, WebXmlTag.FILTER);
196         String JavaDoc filterName = getNestedText(theFilter, WebXmlTag.FILTER_NAME);
197         if (filterName == null)
198         {
199             throw new IllegalArgumentException JavaDoc("Not a valid filter element");
200         }
201         if (hasFilter(filterName))
202         {
203             throw new IllegalStateException JavaDoc("Filter '" + filterName
204                 + "' already defined");
205         }
206         addElement(WebXmlTag.FILTER, theFilter);
207     }
208     
209     /**
210      * Adds an initialization parameter to the specified filter.
211      *
212      * @param theFilterName The name of the filter
213      * @param theParamName The name of the parameter
214      * @param theParamValue The parameter value
215      */

216     public final void addFilterInitParam(String JavaDoc theFilterName,
217         String JavaDoc theParamName, String JavaDoc theParamValue)
218     {
219         Element JavaDoc filterElement = getFilter(theFilterName);
220         if (filterElement == null)
221         {
222             throw new IllegalStateException JavaDoc("Filter '" + theFilterName
223                 + "' not defined");
224         }
225         addInitParam(filterElement, theParamName, theParamValue);
226     }
227     
228     /**
229      * Adds a filter mapping to the descriptor.
230      *
231      * @param theFilterName The name of the filter
232      * @param theUrlPattern The URL pattern the filter should be mapped to
233      */

234     public final void addFilterMapping(String JavaDoc theFilterName,
235         String JavaDoc theUrlPattern)
236     {
237         if (!hasFilter(theFilterName))
238         {
239             throw new IllegalStateException JavaDoc("Filter '" + theFilterName
240                 + "' not defined");
241         }
242         Element JavaDoc filterMappingElement =
243             this.document.createElement(WebXmlTag.FILTER_MAPPING.getTagName());
244         filterMappingElement.appendChild(
245             createNestedText(WebXmlTag.FILTER_NAME, theFilterName));
246         filterMappingElement.appendChild(
247             createNestedText(WebXmlTag.URL_PATTERN, theUrlPattern));
248         addElement(WebXmlTag.FILTER_MAPPING, filterMappingElement);
249     }
250     
251     /**
252      * Returns the element that contains the definition of a specific servlet
253      * filter, or <code>null</code> if a filter of the specified name is not
254      * defined in the descriptor.
255      *
256      * @param theFilterName The name of the servlet filter
257      * @return The DOM element representing the filter definition
258      */

259     public final Element JavaDoc getFilter(String JavaDoc theFilterName)
260     {
261         if (theFilterName == null)
262         {
263             throw new NullPointerException JavaDoc();
264         }
265         Iterator JavaDoc filterElements = getElements(WebXmlTag.FILTER);
266         while (filterElements.hasNext())
267         {
268             Element JavaDoc filterElement = (Element JavaDoc) filterElements.next();
269             if (theFilterName.equals(getNestedText(
270                 filterElement, WebXmlTag.FILTER_NAME)))
271             {
272                 return filterElement;
273             }
274         }
275         return null;
276     }
277
278     /**
279      * Returns the element that contains the definition of a specific context
280      * param, or <code>null</code> if a context param of the specified name
281      * is not defined in the descriptor.
282      *
283      * @param theParamName The context param name
284      * @return The DOM element representing the context param definition
285      */

286     public final Element JavaDoc getContextParam(String JavaDoc theParamName)
287     {
288         if (theParamName == null)
289         {
290             throw new NullPointerException JavaDoc();
291         }
292         Iterator JavaDoc contextParamElements = getElements(WebXmlTag.CONTEXT_PARAM);
293         while (contextParamElements.hasNext())
294         {
295             Element JavaDoc contextParamElement = (Element JavaDoc) contextParamElements.next();
296             if (theParamName.equals(getNestedText(
297                     contextParamElement, WebXmlTag.PARAM_NAME)))
298             {
299                 return contextParamElement;
300             }
301         }
302         return null;
303     }
304
305     /**
306      * @param theContextParam the context param element from which to extract
307      * the name
308      * @return the name of the passed context param element
309      */

310     public final String JavaDoc getContextParamName(Element JavaDoc theContextParam)
311     {
312         return getNestedText(theContextParam, WebXmlTag.PARAM_NAME);
313     }
314     
315     /**
316      * Returns a list of names of filters that are mapped to the specified
317      * class.
318      *
319      * @param theClassName The fully qualified name of the filter class
320      * @return An iterator over the names of the filters mapped to the class
321      */

322     public final Iterator JavaDoc getFilterNamesForClass(String JavaDoc theClassName)
323     {
324         if (theClassName == null)
325         {
326             throw new NullPointerException JavaDoc();
327         }
328         Iterator JavaDoc filterElements = getElements(WebXmlTag.FILTER);
329         List JavaDoc filterNames = new ArrayList JavaDoc();
330         while (filterElements.hasNext())
331         {
332             Element JavaDoc filterElement = (Element JavaDoc) filterElements.next();
333             if (theClassName.equals(getNestedText(
334                 filterElement, WebXmlTag.FILTER_CLASS)))
335             {
336                 filterNames.add(getNestedText(
337                     filterElement, WebXmlTag.FILTER_NAME));
338             }
339         }
340         return filterNames.iterator();
341     }
342     
343     /**
344      * Returns the value of an initialization parameter of the specified filter.
345      *
346      * @param theFilterName The name of the servlet filter
347      * @param theParamName The name of the initialization parameter
348      * @return The parameter value
349      */

350     public final String JavaDoc getFilterInitParam(String JavaDoc theFilterName,
351         String JavaDoc theParamName)
352     {
353         return getInitParam(getFilter(theFilterName), theParamName);
354     }
355     
356     /**
357      * Returns the names of the initialization parameters of the specified
358      * servlet filter.
359      *
360      * @param theFilterName The name of the servlet filter of which the
361      * parameter names should be retrieved
362      * @return An iterator over the ordered list of parameter names
363      */

364     public final Iterator JavaDoc getFilterInitParamNames(String JavaDoc theFilterName)
365     {
366         return getInitParamNames(getFilter(theFilterName));
367     }
368     
369     /**
370      * Returns the URL-patterns that the specified filter is mapped to in an
371      * ordered list. If there are no mappings for the specified filter, an
372      * iterator over an empty list is returned.
373      *
374      * @param theFilterName The name of the servlet filter of which the
375      * mappings should be retrieved
376      * @return An iterator over the ordered list of URL-patterns
377      */

378     public final Iterator JavaDoc getFilterMappings(String JavaDoc theFilterName)
379     {
380         if (theFilterName == null)
381         {
382             throw new NullPointerException JavaDoc();
383         }
384         List JavaDoc filterMappings = new ArrayList JavaDoc();
385         Iterator JavaDoc filterMappingElements = getElements(WebXmlTag.FILTER_MAPPING);
386         while (filterMappingElements.hasNext())
387         {
388             Element JavaDoc filterMappingElement = (Element JavaDoc)
389                 filterMappingElements.next();
390             if (theFilterName.equals(getNestedText(
391                 filterMappingElement, WebXmlTag.FILTER_NAME)))
392             {
393                 String JavaDoc urlPattern = getNestedText(
394                     filterMappingElement, WebXmlTag.URL_PATTERN);
395                 if (urlPattern != null)
396                 {
397                     filterMappings.add(urlPattern);
398                 }
399             }
400         }
401         return filterMappings.iterator();
402     }
403     
404     /**
405      * Returns the names of all filters defined in the deployment descriptor.
406      * The names are returned as an iterator over an ordered list.
407      *
408      * @return The filter names
409      */

410     public final Iterator JavaDoc getFilterNames()
411     {
412         List JavaDoc filterNames = new ArrayList JavaDoc();
413         Iterator JavaDoc filterElements = getElements(WebXmlTag.FILTER);
414         while (filterElements.hasNext())
415         {
416             Element JavaDoc filterElement = (Element JavaDoc) filterElements.next();
417             String JavaDoc filterName =
418                 getNestedText(filterElement, WebXmlTag.FILTER_NAME);
419             if (filterName != null)
420             {
421                 filterNames.add(filterName);
422             }
423         }
424         return filterNames.iterator();
425     }
426
427     /**
428      * Returns whether a context param by the specified name is defined in the
429      * deployment descriptor.
430      *
431      * @param theParamName The name of the context param
432      * @return <code>true</code> if the context param is defined,
433      * <code>false</code> otherwise
434      */

435     public final boolean hasContextParam(String JavaDoc theParamName)
436     {
437         return (getContextParam(theParamName) != null);
438     }
439     
440     /**
441      * Returns whether a servlet filter by the specified name is defined in the
442      * deployment descriptor.
443      *
444      * @param theFilterName The name of the filter
445      * @return <code>true</code> if the filter is defined, <code>false</code>
446      * otherwise
447      */

448     public final boolean hasFilter(String JavaDoc theFilterName)
449     {
450         return (getFilter(theFilterName) != null);
451     }
452     
453     /**
454      * Adds a mapped JSP file to the descriptor.
455      *
456      * @param theServletName The name of the servlet to add
457      * @param theJspFile The path to the JSP file relative to the root of the
458      * web application
459      */

460     public final void addJspFile(String JavaDoc theServletName, String JavaDoc theJspFile)
461     {
462         if (theServletName == null)
463         {
464             throw new NullPointerException JavaDoc();
465         }
466         if (hasFilter(theServletName))
467         {
468             throw new IllegalStateException JavaDoc("Servlet '" + theServletName
469                 + "' already defined");
470         }
471         Element JavaDoc servletElement =
472             this.document.createElement(WebXmlTag.SERVLET.getTagName());
473         servletElement.appendChild(
474             createNestedText(WebXmlTag.SERVLET_NAME, theServletName));
475         servletElement.appendChild(
476             createNestedText(WebXmlTag.JSP_FILE, theJspFile));
477         addElement(WebXmlTag.SERVLET, servletElement);
478     }
479     
480     /**
481      * Adds a new servlet to the descriptor.
482      *
483      * @param theServletName The name of the servlet to add
484      * @param theServletClass The name of the class implementing the servlet
485      */

486     public final void addServlet(String JavaDoc theServletName, String JavaDoc theServletClass)
487     {
488         if (theServletName == null)
489         {
490             throw new NullPointerException JavaDoc();
491         }
492         if (hasServlet(theServletName))
493         {
494             throw new IllegalStateException JavaDoc("Servlet '" + theServletName
495                 + "' already defined");
496         }
497         Element JavaDoc servletElement =
498             this.document.createElement(WebXmlTag.SERVLET.getTagName());
499         servletElement.appendChild(
500             createNestedText(WebXmlTag.SERVLET_NAME, theServletName));
501         servletElement.appendChild(
502             createNestedText(WebXmlTag.SERVLET_CLASS, theServletClass));
503         addElement(WebXmlTag.SERVLET, servletElement);
504     }
505     
506     /**
507      * Adds a new servlet to the descriptor.
508      *
509      * @param theServlet The element representing the servlet definition
510      */

511     public final void addServlet(Element JavaDoc theServlet)
512     {
513         checkElement(theServlet, WebXmlTag.SERVLET);
514         String JavaDoc servletName = getNestedText(theServlet, WebXmlTag.SERVLET_NAME);
515         if (servletName == null)
516         {
517             throw new IllegalArgumentException JavaDoc("Not a valid servlet element");
518         }
519         if (hasServlet(servletName))
520         {
521             throw new IllegalStateException JavaDoc("Servlet '" + servletName
522                 + "' already defined");
523         }
524         addElement(WebXmlTag.SERVLET, theServlet);
525     }
526     
527     /**
528      * Adds an initialization parameter to the specified servlet.
529      *
530      * @param theServletName The name of the filter
531      * @param theParamName The name of the parameter
532      * @param theParamValue The parameter value
533      */

534     public final void addServletInitParam(String JavaDoc theServletName,
535         String JavaDoc theParamName, String JavaDoc theParamValue)
536     {
537         Element JavaDoc servletElement = getServlet(theServletName);
538         if (servletElement == null)
539         {
540             throw new IllegalStateException JavaDoc("Servlet '" + theServletName
541                 + "' not defined");
542         }
543         addInitParam(servletElement, theParamName, theParamValue);
544     }
545     
546     /**
547      * Adds a servlet mapping to the descriptor.
548      *
549      * @param theServletName The name of the servlet
550      * @param theUrlPattern The URL pattern the servlet should be mapped to
551      */

552     public final void addServletMapping(String JavaDoc theServletName,
553         String JavaDoc theUrlPattern)
554     {
555         if (!hasServlet(theServletName))
556         {
557             throw new IllegalStateException JavaDoc("Servlet '" + theServletName
558                 + "' not defined");
559         }
560         Element JavaDoc servletMappingElement =
561             this.document.createElement(WebXmlTag.SERVLET_MAPPING.getTagName());
562         servletMappingElement.appendChild(
563             createNestedText(WebXmlTag.SERVLET_NAME, theServletName));
564         servletMappingElement.appendChild(
565             createNestedText(WebXmlTag.URL_PATTERN, theUrlPattern));
566         addElement(WebXmlTag.SERVLET_MAPPING, servletMappingElement);
567     }
568     
569     /**
570      * Returns the element that contains the definition of a specific servlet,
571      * or <code>null</code> if a servlet of the specified name is not defined
572      * in the descriptor.
573      *
574      * @param theServletName The name of the servlet
575      * @return The DOM element representing the servlet definition
576      */

577     public final Element JavaDoc getServlet(String JavaDoc theServletName)
578     {
579         if (theServletName == null)
580         {
581             throw new NullPointerException JavaDoc();
582         }
583         Iterator JavaDoc servletElements = getElements(WebXmlTag.SERVLET);
584         while (servletElements.hasNext())
585         {
586             Element JavaDoc servletElement = (Element JavaDoc) servletElements.next();
587             if (theServletName.equals(getNestedText(
588                 servletElement, WebXmlTag.SERVLET_NAME)))
589             {
590                 return servletElement;
591             }
592         }
593         return null;
594     }
595     
596     /**
597      * Returns the value of an initialization parameter of the specified
598      * servlet.
599      *
600      * @param theServletName The name of the servlet
601      * @param theParamName The name of the initialization parameter
602      * @return The parameter value
603      */

604     public final String JavaDoc getServletInitParam(String JavaDoc theServletName,
605         String JavaDoc theParamName)
606     {
607         return getInitParam(getServlet(theServletName), theParamName);
608     }
609     
610     /**
611      * Returns the names of the initialization parameters of the specified
612      * servlet.
613      *
614      * @param theServletName The name of the servlet of which the parameter
615      * names should be retrieved
616      * @return An iterator over the ordered list of parameter names
617      */

618     public final Iterator JavaDoc getServletInitParamNames(String JavaDoc theServletName)
619     {
620         return getInitParamNames(getServlet(theServletName));
621     }
622     
623     /**
624      * Returns the URL-patterns that the specified servlet is mapped to in an
625      * ordered list. If there are no mappings for the specified servlet, an
626      * iterator over an empty list is returned.
627      *
628      * @param theServletName The name of the servlet of which the mappings
629      * should be retrieved
630      * @return An iterator over the ordered list of URL-patterns
631      */

632     public final Iterator JavaDoc getServletMappings(String JavaDoc theServletName)
633     {
634         if (theServletName == null)
635         {
636             throw new NullPointerException JavaDoc();
637         }
638         List JavaDoc servletMappings = new ArrayList JavaDoc();
639         Iterator JavaDoc servletMappingElements =
640             getElements(WebXmlTag.SERVLET_MAPPING);
641         while (servletMappingElements.hasNext())
642         {
643             Element JavaDoc servletMappingElement = (Element JavaDoc)
644                 servletMappingElements.next();
645             if (theServletName.equals(getNestedText(
646                 servletMappingElement, WebXmlTag.SERVLET_NAME)))
647             {
648                 String JavaDoc urlPattern = getNestedText(
649                     servletMappingElement, WebXmlTag.URL_PATTERN);
650                 if (urlPattern != null)
651                 {
652                     servletMappings.add(urlPattern);
653                 }
654             }
655         }
656         return servletMappings.iterator();
657     }
658     
659     /**
660      * Returns the names of all servlets defined in the deployment descriptor.
661      * The names are returned as an iterator over an ordered list.
662      *
663      * @return The servlet names
664      */

665     public final Iterator JavaDoc getServletNames()
666     {
667         List JavaDoc servletNames = new ArrayList JavaDoc();
668         Iterator JavaDoc servletElements = getElements(WebXmlTag.SERVLET);
669         while (servletElements.hasNext())
670         {
671             Element JavaDoc servletElement = (Element JavaDoc) servletElements.next();
672             String JavaDoc servletName =
673                 getNestedText(servletElement, WebXmlTag.SERVLET_NAME);
674             if (servletName != null)
675             {
676                 servletNames.add(servletName);
677             }
678         }
679         return servletNames.iterator();
680     }
681     
682     /**
683      * Returns a list of names of servlets that are mapped to the specified
684      * class.
685      *
686      * @param theClassName The fully qualified name of the servlet class
687      * @return An iterator over the names of the servlets mapped to the class
688      */

689     public final Iterator JavaDoc getServletNamesForClass(String JavaDoc theClassName)
690     {
691         if (theClassName == null)
692         {
693             throw new NullPointerException JavaDoc();
694         }
695         Iterator JavaDoc servletElements = getElements(WebXmlTag.SERVLET);
696         List JavaDoc servletNames = new ArrayList JavaDoc();
697         while (servletElements.hasNext())
698         {
699             Element JavaDoc servletElement = (Element JavaDoc) servletElements.next();
700             if (theClassName.equals(getNestedText(
701                 servletElement, WebXmlTag.SERVLET_CLASS)))
702             {
703                 servletNames.add(getNestedText(
704                     servletElement, WebXmlTag.SERVLET_NAME));
705             }
706         }
707         return servletNames.iterator();
708     }
709     
710     /**
711      * Returns a list of names of servlets that are mapped to the specified
712      * JSP file.
713      *
714      * @param theJspFile The path to the JSP file, relative to the root of the
715      * web-application
716      * @return An iterator over the names of the servlets mapped to the JSP file
717      */

718     public final Iterator JavaDoc getServletNamesForJspFile(String JavaDoc theJspFile)
719     {
720         if (theJspFile == null)
721         {
722             throw new NullPointerException JavaDoc();
723         }
724         Iterator JavaDoc servletElements = getElements(WebXmlTag.SERVLET);
725         List JavaDoc servletNames = new ArrayList JavaDoc();
726         while (servletElements.hasNext())
727         {
728             Element JavaDoc servletElement = (Element JavaDoc) servletElements.next();
729             if (theJspFile.equals(getNestedText(
730                 servletElement, WebXmlTag.JSP_FILE)))
731             {
732                 servletNames.add(getNestedText(
733                     servletElement, WebXmlTag.SERVLET_NAME));
734             }
735         }
736         return servletNames.iterator();
737     }
738     
739     /**
740      * Returns whether a servlet by the specified name is defined in the
741      * deployment descriptor.
742      *
743      * @param theServletName The name of the servlet
744      * @return <code>true</code> if the servlet is defined, <code>false</code>
745      * otherwise
746      */

747     public final boolean hasServlet(String JavaDoc theServletName)
748     {
749         return (getServlet(theServletName) != null);
750     }
751     
752     /**
753      * Creates and adds a security-constraint to the descriptor.
754      *
755      * @param theWebResourceName The name of the web resource collection to
756      * protect
757      * @param theUrlPattern The URL pattern to apply the constraint to
758      * @param theRoles The list of authorized roles
759      */

760     public final void addSecurityConstraint(String JavaDoc theWebResourceName,
761         String JavaDoc theUrlPattern, List JavaDoc theRoles)
762     {
763         if ((theWebResourceName == null) || (theUrlPattern == null)
764          || (theRoles == null))
765         {
766             throw new NullPointerException JavaDoc();
767         }
768         if (hasSecurityConstraint(theUrlPattern))
769         {
770             throw new IllegalStateException JavaDoc("Security constraint for URL "
771                 + "pattern " + theUrlPattern + " already defined");
772         }
773         Element JavaDoc securityConstraintElement =
774             this.document.createElement(
775                 WebXmlTag.SECURITY_CONSTRAINT.getTagName());
776         Element JavaDoc webResourceCollectionElement =
777             this.document.createElement(
778                 WebXmlTag.WEB_RESOURCE_COLLECTION.getTagName());
779         webResourceCollectionElement.appendChild(
780             createNestedText(WebXmlTag.WEB_RESOURCE_NAME, theWebResourceName));
781         webResourceCollectionElement.appendChild(
782             createNestedText(WebXmlTag.URL_PATTERN, theUrlPattern));
783         securityConstraintElement.appendChild(webResourceCollectionElement);
784         Element JavaDoc authConstraintElement =
785             this.document.createElement(WebXmlTag.AUTH_CONSTRAINT.getTagName());
786         for (Iterator JavaDoc i = theRoles.iterator(); i.hasNext();)
787         {
788             authConstraintElement.appendChild(