Clover coverage report - Cactus 1.5 for J2EE API 1.2
Coverage timestamp: Wed Feb 18 2004 09:04:33 EST
file stats: LOC: 447   Methods: 16
NCLOC: 227   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
WebXmlMerger.java 43.9% 55.9% 37.5% 49.7%
coverage coverage
 1   
 /*
 2   
  * ====================================================================
 3   
  *
 4   
  * The Apache Software License, Version 1.1
 5   
  *
 6   
  * Copyright (c) 2003 The Apache Software Foundation.  All rights
 7   
  * reserved.
 8   
  *
 9   
  * Redistribution and use in source and binary forms, with or without
 10   
  * modification, are permitted provided that the following conditions
 11   
  * are met:
 12   
  *
 13   
  * 1. Redistributions of source code must retain the above copyright
 14   
  *    notice, this list of conditions and the following disclaimer.
 15   
  *
 16   
  * 2. Redistributions in binary form must reproduce the above copyright
 17   
  *    notice, this list of conditions and the following disclaimer in
 18   
  *    the documentation and/or other materials provided with the
 19   
  *    distribution.
 20   
  *
 21   
  * 3. The end-user documentation included with the redistribution, if
 22   
  *    any, must include the following acknowlegement:
 23   
  *       "This product includes software developed by the
 24   
  *        Apache Software Foundation (http://www.apache.org/)."
 25   
  *    Alternately, this acknowlegement may appear in the software itself,
 26   
  *    if and wherever such third-party acknowlegements normally appear.
 27   
  *
 28   
  * 4. The names "The Jakarta Project", "Cactus" and "Apache Software
 29   
  *    Foundation" must not be used to endorse or promote products
 30   
  *    derived from this software without prior written permission. For
 31   
  *    written permission, please contact apache@apache.org.
 32   
  *
 33   
  * 5. Products derived from this software may not be called "Apache"
 34   
  *    nor may "Apache" appear in their names without prior written
 35   
  *    permission of the Apache Group.
 36   
  *
 37   
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 38   
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 39   
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 40   
  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 41   
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 42   
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 43   
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 44   
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 45   
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 46   
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 47   
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 48   
  * SUCH DAMAGE.
 49   
  * ====================================================================
 50   
  *
 51   
  * This software consists of voluntary contributions made by many
 52   
  * individuals on behalf of the Apache Software Foundation.  For more
 53   
  * information on the Apache Software Foundation, please see
 54   
  * <http://www.apache.org/>.
 55   
  *
 56   
  */
 57   
 package org.apache.cactus.integration.ant.deployment;
 58   
 
 59   
 import java.util.Iterator;
 60   
 
 61   
 import org.apache.cactus.integration.ant.util.AntLog;
 62   
 import org.apache.commons.logging.Log;
 63   
 import org.w3c.dom.Element;
 64   
 
 65   
 /**
 66   
  * Helper class that can merge two web deployment descriptors.
 67   
  *
 68   
  * @author <a href="mailto:cmlenz@apache.org">Christopher Lenz</a>
 69   
  *
 70   
  * @since Cactus 1.5
 71   
  * @version $Id: WebXmlMerger.java,v 1.4 2003/06/10 09:20:32 cmlenz Exp $
 72   
  */
 73   
 public class WebXmlMerger
 74   
 {
 75   
 
 76   
     // Instance Variables ------------------------------------------------------
 77   
 
 78   
     /**
 79   
      * The original, authorative descriptor onto which the merges are performed.
 80   
      */
 81   
     private WebXml webXml;
 82   
     
 83   
     /**
 84   
      * The log to use.
 85   
      */
 86   
     private Log log = AntLog.NULL;
 87   
 
 88   
     // Constructors ------------------------------------------------------------
 89   
 
 90   
     /**
 91   
      * Constructor.
 92   
      * 
 93   
      * @param theWebXml The original descriptor
 94   
      */
 95  20
     public WebXmlMerger(WebXml theWebXml)
 96   
     {
 97  20
         this.webXml = theWebXml;
 98   
     }
 99   
 
 100   
     // Public Methods ----------------------------------------------------------
 101   
 
 102   
     /**
 103   
      * Merges the merge descriptor with the original descriptor. 
 104   
      * 
 105   
      * @param theMergeWebXml The descriptor to merge in
 106   
      */
 107  0
     public final void merge(WebXml theMergeWebXml)
 108   
     {
 109  0
         checkServletVersions(theMergeWebXml);
 110  0
         if (WebXmlVersion.V2_3.compareTo(this.webXml.getVersion()) <= 0)
 111   
         {
 112  0
             mergeFilters(theMergeWebXml);
 113   
         }
 114  0
         mergeServlets(theMergeWebXml);
 115  0
         if (WebXmlVersion.V2_3.compareTo(this.webXml.getVersion()) <= 0)
 116   
         {
 117  0
             mergeResourceEnvironmentReferences(theMergeWebXml);
 118   
         }
 119  0
         mergeResourceReferences(theMergeWebXml);
 120  0
         mergeSecurityConstraints(theMergeWebXml);
 121  0
         mergeLoginConfig(theMergeWebXml);
 122  0
         mergeSecurityRoles(theMergeWebXml);
 123  0
         mergeEnvironmentEntries(theMergeWebXml);
 124  0
         mergeEjbRefs(theMergeWebXml);
 125  0
         if (WebXmlVersion.V2_3.compareTo(this.webXml.getVersion()) <= 0)
 126   
         {
 127  0
             mergeEjbLocalRefs(theMergeWebXml);
 128   
         }
 129   
     }
 130   
 
 131   
     /**
 132   
      * Sets the log to which events should be written. This method must be 
 133   
      * called before any of the other methods, because the class will rely on 
 134   
      * being able to log.
 135   
      * 
 136   
      * @param theLog The log to use
 137   
      */
 138  0
     public final void setLog(Log theLog)
 139   
     {
 140  0
         this.log = theLog;
 141   
     }
 142   
 
 143   
     // Protected Methods -------------------------------------------------------
 144   
 
 145   
     /**
 146   
      * Checks the versions of the servlet API in each descriptor, and logs
 147   
      * a warning if a mismatch might result in the loss of definitions.
 148   
      * 
 149   
      * @param theWebXml The descriptor that will be merged with the original
 150   
      */
 151  0
     protected final void checkServletVersions(WebXml theWebXml)
 152   
     {
 153  0
         if ((this.webXml.getVersion() != null)
 154   
          && (this.webXml.getVersion().compareTo(theWebXml.getVersion()) < 0))
 155   
         {
 156  0
             this.log.warn(
 157   
                 "Merging elements from a version " + theWebXml.getVersion()
 158   
                 + " descriptor into a version " + this.webXml.getVersion() 
 159   
                 + ", some elements may be skipped");
 160   
         }
 161   
     }
 162   
 
 163   
     /**
 164   
      * Merges the servlet definitions from the specified descriptor into the 
 165   
      * original descriptor.
 166   
      * 
 167   
      * @param theWebXml The descriptor that contains the filter definitions
 168   
      *        that are to be merged into the original descriptor
 169   
      */
 170  8
     protected final void mergeFilters(WebXml theWebXml)
 171   
     {
 172  8
         Iterator filterNames = theWebXml.getFilterNames();
 173  8
         int count = 0;
 174  8
         while (filterNames.hasNext())
 175   
         {
 176  10
             String filterName = (String) filterNames.next();
 177  10
             if (!webXml.hasFilter(filterName))
 178   
             {
 179  8
                 webXml.addFilter(theWebXml.getFilter(filterName));
 180   
             }
 181   
             else
 182   
             {
 183   
                 // merge the parameters
 184  2
                 Iterator filterInitParamNames =
 185   
                     theWebXml.getFilterInitParamNames(filterName);
 186  2
                 while (filterInitParamNames.hasNext())
 187   
                 {
 188  1
                     String paramName = (String) filterInitParamNames.next();
 189  1
                     String paramValue =
 190   
                         theWebXml.getFilterInitParam(filterName, paramName);
 191  1
                     webXml.addFilterInitParam(
 192   
                         filterName, paramName, paramValue);
 193   
                 }
 194   
             }
 195   
             // merge the mappings
 196  10
             Iterator filterMappings = theWebXml.getFilterMappings(filterName);
 197  10
             while (filterMappings.hasNext())
 198   
             {
 199  4
                 String urlPattern = (String) filterMappings.next();
 200  4
                 webXml.addFilterMapping(filterName, urlPattern);
 201   
             }
 202  10
             count++;
 203   
         }
 204  8
         this.log.trace("Merged " + count + " filter definition"
 205  8
             + (count != 1 ? "s " : " ") + "into the descriptor");
 206   
     }
 207   
 
 208   
     /**
 209   
      * Merges the servlet definitions from the specified descriptor into the 
 210   
      * original descriptor.
 211   
      * 
 212   
      * @param theWebXml The descriptor that contains the servlet definitions
 213   
      *        that are to be merged into the original descriptor
 214   
      */
 215  9
     protected final void mergeServlets(WebXml theWebXml)
 216   
     {
 217  9
         Iterator servletNames = theWebXml.getServletNames();
 218  9
         int count = 0;
 219  9
         while (servletNames.hasNext())
 220   
         {
 221  11
             String servletName = (String) servletNames.next();
 222  11
             if (!webXml.hasServlet(servletName))
 223   
             {
 224  8
                 webXml.addServlet(theWebXml.getServlet(servletName));
 225   
             }
 226   
             else
 227   
             {
 228   
                 // merge the parameters
 229  3
                 Iterator servletInitParamNames =
 230   
                     theWebXml.getServletInitParamNames(servletName);
 231  3
                 while (servletInitParamNames.hasNext())
 232   
                 {
 233  2
                     String paramName = (String) servletInitParamNames.next();
 234  2
                     String paramValue =
 235   
                         theWebXml.getServletInitParam(servletName, paramName);
 236  2
                     webXml.addServletInitParam(
 237   
                         servletName, paramName, paramValue);
 238   
                 }
 239   
             }
 240   
             // merge the mappings
 241  11
             Iterator servletMappings =
 242   
                 theWebXml.getServletMappings(servletName);
 243  11
             while (servletMappings.hasNext())
 244   
             {
 245  4
                 String urlPattern = (String) servletMappings.next();
 246  4
                 webXml.addServletMapping(servletName, urlPattern);
 247   
             }
 248  11
             count++;
 249   
         }
 250  9
         this.log.trace("Merged " + count + " servlet definition"
 251  9
             + (count != 1 ? "s " : " ") + "into the descriptor");
 252   
     }
 253   
 
 254   
     /**
 255   
      * Merges the resource environment references from the provided descriptor
 256   
      * into the original descriptor.
 257   
      * 
 258   
      * @param theWebXml The descriptor that contains the references that are to
 259   
      *        be merged into the original descriptor
 260   
      */
 261  0
     protected final void mergeResourceEnvironmentReferences(WebXml theWebXml)
 262   
     {
 263  0
         int count = insertElements(theWebXml, WebXmlTag.RESOURCE_ENV_REF);
 264  0
         if (count > 0)
 265   
         {
 266  0
             this.log.trace("Merged " + count + " resource environment "
 267  0
                 + "reference" + (count != 1 ? "s " : " ") + "into the "
 268   
                 + "descriptor");
 269   
         }
 270   
     }
 271   
 
 272   
     /**
 273   
      * Merges the resource references from the provided descriptor into the
 274   
      * original descriptor.
 275   
      * 
 276   
      * @param theWebXml The descriptor that contains the resource refs that
 277   
      *        are to be merged into the original descriptor
 278   
      */
 279  0
     protected final void mergeResourceReferences(WebXml theWebXml)
 280   
     {
 281  0
         int count = insertElements(theWebXml, WebXmlTag.RESOURCE_REF);
 282  0
         if (count > 0)
 283   
         {
 284  0
             this.log.trace("Merged " + count + " resource reference"
 285  0
                 + (count != 1 ? "s " : " ") + "into the descriptor");
 286   
         }
 287   
     }
 288   
 
 289   
     /**
 290   
      * Merges the 
 291   
      * 
 292   
      * @param theWebXml The descriptor that contains the security constraints
 293   
      *        that are to be merged into the original descriptor
 294   
      */
 295  0
     protected final void mergeSecurityConstraints(WebXml theWebXml)
 296   
     {
 297  0
         int count = insertElements(theWebXml, WebXmlTag.SECURITY_CONSTRAINT);
 298  0
         if (count > 0)
 299   
         {
 300  0
             this.log.trace("Merged " + count + " security constraint"
 301  0
                 + (count != 1 ? "s " : " ") + "into the descriptor");
 302   
         }
 303   
     }
 304   
 
 305   
     /**
 306   
      * Merges the login configuration from the provided descriptor into the
 307   
      * original descriptor, thereby eventually replacing the existing login 
 308   
      * config.
 309   
      * 
 310   
      * @param theWebXml The descriptor that contains the login config that
 311   
      *        is to be merged into the original descriptor
 312   
      */
 313  0
     protected final void mergeLoginConfig(WebXml theWebXml)
 314   
     {
 315  0
         boolean replaced = replaceElement(theWebXml, WebXmlTag.LOGIN_CONFIG);
 316  0
         if (replaced)
 317   
         {
 318  0
             this.log.trace(
 319   
                 "Merged the login configuration into the descriptor");
 320   
         }
 321   
     }
 322   
 
 323   
     /**
 324   
      * Merges the security roles from the provided descriptor into the original
 325   
      * descriptor.
 326   
      * 
 327   
      * @param theWebXml The descriptor that contains the security roles that
 328   
      *        are to be merged into the original descriptor
 329   
      */
 330  2
     protected final void mergeSecurityRoles(WebXml theWebXml)
 331   
     {
 332  2
         Iterator securityRoleNames = theWebXml.getSecurityRoleNames();
 333  2
         int count = 0;
 334  2
         while (securityRoleNames.hasNext())
 335   
         {
 336  2
             String securityRoleName = (String) securityRoleNames.next();
 337  2
             if (!this.webXml.hasSecurityRole(securityRoleName))
 338   
             {
 339  1
                 this.webXml.addSecurityRole(securityRoleName);
 340   
             }
 341   
         }
 342  2
         if (count > 0)
 343   
         {
 344  0
             this.log.trace("Merged " + count + " security role"
 345  0
                 + (count != 1 ? "s " : " ") + "into the descriptor");
 346   
         }
 347   
     }
 348   
 
 349   
     /**
 350   
      * Merges the environment entries from the provided descriptor into the
 351   
      * original descriptor.
 352   
      * 
 353   
      * @param theWebXml The descriptor that contains the environment entries
 354   
      *        that are to be merged into the original descriptor
 355   
      */
 356  0
     protected final void mergeEnvironmentEntries(WebXml theWebXml)
 357   
     {
 358  0
         int count = insertElements(theWebXml, WebXmlTag.ENV_ENTRY);
 359  0
         if (count > 0)
 360   
         {
 361  0
             this.log.trace("Merged " + count + " environment entr"
 362  0
                 + (count != 1 ? "ies " : "y ") + "into the descriptor");
 363   
         }
 364   
     }
 365   
 
 366   
     /**
 367   
      * Merges the EJB references from the provided descriptor into the original
 368   
      * descriptor.
 369   
      * 
 370   
      * @param theWebXml The descriptor that contains the EJB refs that are to be
 371   
      *        merged into the original descriptor
 372   
      */
 373  1
     protected final void mergeEjbRefs(WebXml theWebXml)
 374   
     {
 375  1
         int count = insertElements(theWebXml, WebXmlTag.EJB_REF);
 376  1
         if (count > 0)
 377   
         {
 378  1
             this.log.trace("Merged " + count + " EJB reference"
 379  1
                 + (count != 1 ? "s " : "y ") + "into the descriptor");
 380   
         }
 381   
     }
 382   
 
 383   
     /**
 384   
      * Merges the EJB local references from the provided descriptor into the
 385   
      * original descriptor.
 386   
      * 
 387   
      * @param theWebXml The descriptor that contains the EJB local refs that are
 388   
      *        to be merged into the original descriptor
 389   
      */
 390  0
     protected final void mergeEjbLocalRefs(WebXml theWebXml)
 391   
     {
 392  0
         int count = insertElements(theWebXml, WebXmlTag.EJB_LOCAL_REF);
 393  0
         if (count > 0)
 394   
         {
 395  0
             this.log.trace("Merged " + count + " EJB local reference"
 396  0
                 + (count != 1 ? "s " : "y ") + "into the descriptor");
 397   
         }
 398   
     }
 399   
 
 400   
     // Private Methods ---------------------------------------------------------
 401   
 
 402   
     /**
 403   
      * Insert all elements of the specified tag from the given descriptor into
 404   
      * the original descriptor, and returns the number of elements that were
 405   
      * added.
 406   
      * 
 407   
      * @param theWebXml The descriptor that contains the elements that are to be
 408   
      *        merged into the original descriptor
 409   
      * @param theTag Defines which elements will get merged
 410   
      * @return The number of elements inserted into the original descriptor
 411   
      */
 412  1
     private int insertElements(WebXml theWebXml, WebXmlTag theTag)
 413   
     {
 414  1
         Iterator elements = theWebXml.getElements(theTag);
 415  1
         int count = 0;
 416  1
         while (elements.hasNext())
 417   
         {
 418  1
             Element element = (Element) elements.next();
 419  1
             webXml.addElement(theTag, element);
 420  1
             count++;
 421   
         }
 422  1
         return count;
 423   
     }
 424   
 
 425   
     /**
 426   
      * Replaces the element of the specified tag in the original descriptor with
 427   
      * the equivalent element in the specified descriptor.
 428   
      * 
 429   
      * @param theWebXml The descriptor that contains the element that is to be
 430   
      *        added to the original descriptor, replacing the original
 431   
      *        definition
 432   
      * @param theTag Defines which element will get replaced
 433   
      * @return Whether the element was replaced
 434   
      */
 435  0
     private boolean replaceElement(WebXml theWebXml, WebXmlTag theTag)
 436   
     {
 437  0
         Iterator elements = theWebXml.getElements(theTag);
 438  0
         if (elements.hasNext())
 439   
         {
 440  0
             webXml.replaceElement(theTag, (Element) elements.next());
 441  0
             return true;
 442   
         }
 443  0
         return false;
 444   
     }
 445   
 
 446   
 }
 447