KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
23
24 import org.apache.cactus.integration.ant.util.AntLog;
25 import org.apache.commons.logging.Log;
26 import org.w3c.dom.Element JavaDoc;
27
28 /**
29  * Helper class that can merge two web deployment descriptors.
30  *
31  * @since Cactus 1.5
32  * @version $Id: WebXmlMerger.java,v 1.1 2004/05/31 20:05:23 vmassol Exp $
33  */

34 public class WebXmlMerger
35 {
36
37     // Instance Variables ------------------------------------------------------
38

39     /**
40      * The original, authorative descriptor onto which the merges are performed.
41      */

42     private WebXml webXml;
43     
44     /**
45      * The log to use.
46      */

47     private Log log = AntLog.NULL;
48
49     // Constructors ------------------------------------------------------------
50

51     /**
52      * Constructor.
53      *
54      * @param theWebXml The original descriptor
55      */

56     public WebXmlMerger(WebXml theWebXml)
57     {
58         this.webXml = theWebXml;
59     }
60
61     // Public Methods ----------------------------------------------------------
62

63     /**
64      * Merges the merge descriptor with the original descriptor.
65      *
66      * @param theMergeWebXml The descriptor to merge in
67      */

68     public final void merge(WebXml theMergeWebXml)
69     {
70         checkServletVersions(theMergeWebXml);
71         mergeContextParams(theMergeWebXml);
72         if (WebXmlVersion.V2_3.compareTo(this.webXml.getVersion()) <= 0)
73         {
74             mergeFilters(theMergeWebXml);
75         }
76         mergeServlets(theMergeWebXml);
77         if (WebXmlVersion.V2_3.compareTo(this.webXml.getVersion()) <= 0)
78         {
79             mergeResourceEnvironmentReferences(theMergeWebXml);
80         }
81         mergeResourceReferences(theMergeWebXml);
82         mergeSecurityConstraints(theMergeWebXml);
83         mergeLoginConfig(theMergeWebXml);
84         mergeSecurityRoles(theMergeWebXml);
85         mergeEnvironmentEntries(theMergeWebXml);
86         mergeEjbRefs(theMergeWebXml);
87         if (WebXmlVersion.V2_3.compareTo(this.webXml.getVersion()) <= 0)
88         {
89             mergeEjbLocalRefs(theMergeWebXml);
90         }
91     }
92
93     /**
94      * Sets the log to which events should be written. This method must be
95      * called before any of the other methods, because the class will rely on
96      * being able to log.
97      *
98      * @param theLog The log to use
99      */

100     public final void setLog(Log theLog)
101     {
102         this.log = theLog;
103     }
104
105     // Protected Methods -------------------------------------------------------
106

107     /**
108      * Checks the versions of the servlet API in each descriptor, and logs
109      * a warning if a mismatch might result in the loss of definitions.
110      *
111      * @param theWebXml The descriptor that will be merged with the original
112      */

113     protected final void checkServletVersions(WebXml theWebXml)
114     {
115         if ((this.webXml.getVersion() != null)
116          && (this.webXml.getVersion().compareTo(theWebXml.getVersion()) < 0))
117         {
118             this.log.warn(
119                 "Merging elements from a version " + theWebXml.getVersion()
120                 + " descriptor into a version " + this.webXml.getVersion()
121                 + ", some elements may be skipped");
122         }
123     }
124
125     /**
126      * Merges the context-param definitions from the specified descriptor into
127      * the original descriptor.
128      *
129      * @param theWebXml The descriptor that contains the context-params
130      * definitions that are to be merged into the original descriptor
131      */

132     protected final void mergeContextParams(WebXml theWebXml)
133     {
134         Iterator JavaDoc contextParams = theWebXml.getElements(WebXmlTag.CONTEXT_PARAM);
135         int count = 0;
136         while (contextParams.hasNext())
137         {
138             String JavaDoc paramName = theWebXml.getContextParamName(
139                 (Element JavaDoc) contextParams.next());
140             if (!webXml.hasContextParam(paramName))
141             {
142                 webXml.addContextParam(theWebXml.getContextParam(paramName));
143             }
144             count++;
145         }
146         this.log.trace("Merged " + count + " context-param definition"
147             + (count != 1 ? "s " : " ") + "into the descriptor");
148     }
149     
150     /**
151      * Merges the servlet definitions from the specified descriptor into the
152      * original descriptor.
153      *
154      * @param theWebXml The descriptor that contains the filter definitions
155      * that are to be merged into the original descriptor
156      */

157     protected final void mergeFilters(WebXml theWebXml)
158     {
159         Iterator JavaDoc filterNames = theWebXml.getFilterNames();
160         int count = 0;
161         while (filterNames.hasNext())
162         {
163             String JavaDoc filterName = (String JavaDoc) filterNames.next();
164             if (!webXml.hasFilter(filterName))
165             {
166                 webXml.addFilter(theWebXml.getFilter(filterName));
167             }
168             else
169             {
170                 // merge the parameters
171
Iterator JavaDoc filterInitParamNames =
172                     theWebXml.getFilterInitParamNames(filterName);
173                 while (filterInitParamNames.hasNext())
174                 {
175                     String JavaDoc paramName = (String JavaDoc) filterInitParamNames.next();
176                     String JavaDoc paramValue =
177                         theWebXml.getFilterInitParam(filterName, paramName);
178                     webXml.addFilterInitParam(
179                         filterName, paramName, paramValue);
180                 }
181             }
182             // merge the mappings
183
Iterator JavaDoc filterMappings = theWebXml.getFilterMappings(filterName);
184             while (filterMappings.hasNext())
185             {
186                 String JavaDoc urlPattern = (String JavaDoc) filterMappings.next();
187                 webXml.addFilterMapping(filterName, urlPattern);
188             }
189             count++;
190         }
191         this.log.trace("Merged " + count + " filter definition"
192             + (count != 1 ? "s " : " ") + "into the descriptor");
193     }
194
195     /**
196      * Merges the servlet definitions from the specified descriptor into the
197      * original descriptor.
198      *
199      * @param theWebXml The descriptor that contains the servlet definitions
200      * that are to be merged into the original descriptor
201      */

202     protected final void mergeServlets(WebXml theWebXml)
203     {
204         Iterator JavaDoc servletNames = theWebXml.getServletNames();
205         int count = 0;
206         while (servletNames.hasNext())
207         {
208             String JavaDoc servletName = (String JavaDoc) servletNames.next();
209             if (!webXml.hasServlet(servletName))
210             {
211                 webXml.addServlet(theWebXml.getServlet(servletName));
212             }
213             else
214             {
215                 // merge the parameters
216
Iterator JavaDoc servletInitParamNames =
217                     theWebXml.getServletInitParamNames(servletName);
218                 while (servletInitParamNames.hasNext())
219                 {
220                     String JavaDoc paramName = (String JavaDoc) servletInitParamNames.next();
221                     String JavaDoc paramValue =
222                         theWebXml.getServletInitParam(servletName, paramName);
223                     webXml.addServletInitParam(
224                         servletName, paramName, paramValue);
225                 }
226             }
227             // merge the mappings
228
Iterator JavaDoc servletMappings =
229                 theWebXml.getServletMappings(servletName);
230             while (servletMappings.hasNext())
231             {
232                 String JavaDoc urlPattern = (String JavaDoc) servletMappings.next();
233                 webXml.addServletMapping(servletName, urlPattern);
234             }
235             count++;
236         }
237         this.log.trace("Merged " + count + " servlet definition"
238             + (count != 1 ? "s " : " ") + "into the descriptor");
239     }
240
241     /**
242      * Merges the resource environment references from the provided descriptor
243      * into the original descriptor.
244      *
245      * @param theWebXml The descriptor that contains the references that are to
246      * be merged into the original descriptor
247      */

248     protected final void mergeResourceEnvironmentReferences(WebXml theWebXml)
249     {
250         int count = insertElements(theWebXml, WebXmlTag.RESOURCE_ENV_REF);
251         if (count > 0)
252         {
253             this.log.trace("Merged " + count + " resource environment "
254                 + "reference" + (count != 1 ? "s " : " ") + "into the "
255                 + "descriptor");
256         }
257     }
258
259     /**
260      * Merges the resource references from the provided descriptor into the
261      * original descriptor.
262      *
263      * @param theWebXml The descriptor that contains the resource refs that
264      * are to be merged into the original descriptor
265      */

266     protected final void mergeResourceReferences(WebXml theWebXml)
267     {
268         int count = insertElements(theWebXml, WebXmlTag.RESOURCE_REF);
269         if (count > 0)
270         {
271             this.log.trace("Merged " + count + " resource reference"
272                 + (count != 1 ? "s " : " ") + "into the descriptor");
273         }
274     }
275
276     /**
277      * Merges the
278      *
279      * @param theWebXml The descriptor that contains the security constraints
280      * that are to be merged into the original descriptor
281      */

282     protected final void mergeSecurityConstraints(WebXml theWebXml)
283     {
284         int count = insertElements(theWebXml, WebXmlTag.SECURITY_CONSTRAINT);
285         if (count > 0)
286         {
287             this.log.trace("Merged " + count + " security constraint"
288                 + (count != 1 ? "s " : " ") + "into the descriptor");
289         }
290     }
291
292     /**
293      * Merges the login configuration from the provided descriptor into the
294      * original descriptor, thereby eventually replacing the existing login
295      * config.
296      *
297      * @param theWebXml The descriptor that contains the login config that
298      * is to be merged into the original descriptor
299      */

300     protected final void mergeLoginConfig(WebXml theWebXml)
301     {
302         boolean replaced = replaceElement(theWebXml, WebXmlTag.LOGIN_CONFIG);
303         if (replaced)
304         {
305             this.log.trace(
306                 "Merged the login configuration into the descriptor");
307         }
308     }
309
310     /**
311      * Merges the security roles from the provided descriptor into the original
312      * descriptor.
313      *
314      * @param theWebXml The descriptor that contains the security roles that
315      * are to be merged into the original descriptor
316      */

317     protected final void mergeSecurityRoles(WebXml theWebXml)
318     {
319         Iterator JavaDoc securityRoleNames = theWebXml.getSecurityRoleNames();
320         int count = 0;
321         while (securityRoleNames.hasNext())
322         {
323             String JavaDoc securityRoleName = (String JavaDoc) securityRoleNames.next();
324             if (!this.webXml.hasSecurityRole(securityRoleName))
325             {
326                 this.webXml.addSecurityRole(securityRoleName);
327             }
328         }
329         if (count > 0)
330         {
331             this.log.trace("Merged " + count + " security role"
332                 + (count != 1 ? "s " : " ") + "into the descriptor");
333         }
334     }
335
336     /**
337      * Merges the environment entries from the provided descriptor into the
338      * original descriptor.
339      *
340      * @param theWebXml The descriptor that contains the environment entries
341      * that are to be merged into the original descriptor
342      */

343     protected final void mergeEnvironmentEntries(WebXml theWebXml)
344     {
345         int count = insertElements(theWebXml, WebXmlTag.ENV_ENTRY);
346         if (count > 0)
347         {
348             this.log.trace("Merged " + count + " environment entr"
349                 + (count != 1 ? "ies " : "y ") + "into the descriptor");
350         }
351     }
352
353     /**
354      * Merges the EJB references from the provided descriptor into the original
355      * descriptor.
356      *
357      * @param theWebXml The descriptor that contains the EJB refs that are to be
358      * merged into the original descriptor
359      */

360     protected final void mergeEjbRefs(WebXml theWebXml)
361     {
362         int count = insertElements(theWebXml, WebXmlTag.EJB_REF);
363         if (count > 0)
364         {
365             this.log.trace("Merged " + count + " EJB reference"
366                 + (count != 1 ? "s " : "y ") + "into the descriptor");
367         }
368     }
369
370     /**
371      * Merges the EJB local references from the provided descriptor into the
372      * original descriptor.
373      *
374      * @param theWebXml The descriptor that contains the EJB local refs that are
375      * to be merged into the original descriptor
376      */

377     protected final void mergeEjbLocalRefs(WebXml theWebXml)
378     {
379         int count = insertElements(theWebXml, WebXmlTag.EJB_LOCAL_REF);
380         if (count > 0)
381         {
382             this.log.trace("Merged " + count + " EJB local reference"
383                 + (count != 1 ? "s " : "y ") + "into the descriptor");
384         }
385     }
386
387     // Private Methods ---------------------------------------------------------
388

389     /**
390      * Insert all elements of the specified tag from the given descriptor into
391      * the original descriptor, and returns the number of elements that were
392      * added.
393      *
394      * @param theWebXml The descriptor that contains the elements that are to be
395      * merged into the original descriptor
396      * @param theTag Defines which elements will get merged
397      * @return The number of elements inserted into the original descriptor
398      */

399     private int insertElements(WebXml theWebXml, WebXmlTag theTag)
400     {
401         Iterator JavaDoc elements = theWebXml.getElements(theTag);
402         int count = 0;
403         while (elements.hasNext())
404         {
405             Element JavaDoc element = (Element JavaDoc) elements.next();
406             webXml.addElement(theTag, element);
407             count++;
408         }
409         return count;
410     }
411
412     /**
413      * Replaces the element of the specified tag in the original descriptor with
414      * the equivalent element in the specified descriptor.
415      *
416      * @param theWebXml The descriptor that contains the element that is to be
417      * added to the original descriptor, replacing the original
418      * definition
419      * @param theTag Defines which element will get replaced
420      * @return Whether the element was replaced
421      */

422     private boolean replaceElement(WebXml theWebXml, WebXmlTag theTag)
423     {
424         Iterator JavaDoc elements = theWebXml.getElements(theTag);
425         if (elements.hasNext())
426         {
427             webXml.replaceElement(theTag, (Element JavaDoc) elements.next());
428             return true;
429         }
430         return false;
431     }
432
433 }
434
Popular Tags