You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cactus-dev@jakarta.apache.org by cm...@apache.org on 2003/05/15 22:22:43 UTC

cvs commit: jakarta-cactus/documentation/docs/xdocs/integration/ant task_webxmlmerge.xml

cmlenz      2003/05/15 13:22:43

  Modified:    integration/ant/src/java/org/apache/cactus/integration/ant/deployment
                        WebXmlMerger.java
               integration/ant/src/test/org/apache/cactus/integration/ant/deployment
                        TestWebXmlMerger.java
               documentation/docs/xdocs/integration/ant
                        task_webxmlmerge.xml
  Log:
  WebXmlMerger now also merges:
   - resource-ref
   - resource-env-ref (Servlet 2.3 only)
   - env-entry
   - ejb-ref
   - ejb-local-ref (Servlet 2.3 only)
  
  Note: this needs more tests, but they are boring to write :-P
  
  Revision  Changes    Path
  1.2       +166 -31   jakarta-cactus/integration/ant/src/java/org/apache/cactus/integration/ant/deployment/WebXmlMerger.java
  
  Index: WebXmlMerger.java
  ===================================================================
  RCS file: /home/cvs/jakarta-cactus/integration/ant/src/java/org/apache/cactus/integration/ant/deployment/WebXmlMerger.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- WebXmlMerger.java	14 May 2003 10:43:36 -0000	1.1
  +++ WebXmlMerger.java	15 May 2003 20:22:42 -0000	1.2
  @@ -107,14 +107,28 @@
       public final void merge(WebXml theMergeWebXml)
       {
           checkServletVersions(theMergeWebXml);
  -        if (this.webXml.getVersion() == WebXmlVersion.V2_3)
  +        if ((this.webXml.getVersion() != null)
  +         && (this.webXml.getVersion().compareTo(WebXmlVersion.V2_3) >= 0))
           {
               mergeFilters(theMergeWebXml);
           }
           mergeServlets(theMergeWebXml);
  +        if ((this.webXml.getVersion() != null)
  +         && (this.webXml.getVersion().compareTo(WebXmlVersion.V2_3) >= 0))
  +        {
  +            mergeResourceEnvironmentReferences(theMergeWebXml);
  +        }
  +        mergeResourceReferences(theMergeWebXml);
           mergeSecurityConstraints(theMergeWebXml);
           mergeLoginConfig(theMergeWebXml);
           mergeSecurityRoles(theMergeWebXml);
  +        mergeEnvironmentEntries(theMergeWebXml);
  +        mergeEjbRefs(theMergeWebXml);
  +        if ((this.webXml.getVersion() != null)
  +         && (this.webXml.getVersion().compareTo(WebXmlVersion.V2_3) >= 0))
  +        {
  +            mergeEjbLocalRefs(theMergeWebXml);
  +        }
       }
   
       /**
  @@ -154,7 +168,7 @@
        * original descriptor.
        * 
        * @param theWebXml The descriptor that contains the filter definitions
  -     *         that are to be merged into the original descriptor
  +     *        that are to be merged into the original descriptor
        */
       protected final void mergeFilters(WebXml theWebXml)
       {
  @@ -199,7 +213,7 @@
        * original descriptor.
        * 
        * @param theWebXml The descriptor that contains the servlet definitions
  -     *         that are to be merged into the original descriptor
  +     *        that are to be merged into the original descriptor
        */
       protected final void mergeServlets(WebXml theWebXml)
       {
  @@ -241,65 +255,186 @@
       }
   
       /**
  +     * Merges the resource environment references from the provided descriptor
  +     * into the original descriptor.
  +     * 
  +     * @param theWebXml The descriptor that contains the references that are to
  +     *        be merged into the original descriptor
  +     */
  +    protected final void mergeResourceEnvironmentReferences(WebXml theWebXml)
  +    {
  +        int count = insertElements(theWebXml, WebXmlTag.RESOURCE_ENV_REF);
  +        if (count > 0)
  +        {
  +            this.log.trace("Merged " + count + " resource environment "
  +                + "reference" + (count != 1 ? "s " : " ") + "into the "
  +                + "descriptor");
  +        }
  +    }
  +
  +    /**
  +     * Merges the resource references from the provided descriptor into the
  +     * original descriptor.
        * 
  +     * @param theWebXml The descriptor that contains the resource refs that
  +     *        are to be merged into the original descriptor
  +     */
  +    protected final void mergeResourceReferences(WebXml theWebXml)
  +    {
  +        int count = insertElements(theWebXml, WebXmlTag.RESOURCE_REF);
  +        if (count > 0)
  +        {
  +            this.log.trace("Merged " + count + " resource reference"
  +                + (count != 1 ? "s " : " ") + "into the descriptor");
  +        }
  +    }
  +
  +    /**
  +     * Merges the 
        * 
        * @param theWebXml The descriptor that contains the security constraints
  -     *         that are to be merged into the original descriptor
  +     *        that are to be merged into the original descriptor
        */
       protected final void mergeSecurityConstraints(WebXml theWebXml)
       {
  -        Iterator securityConstraints =
  -            theWebXml.getElements(WebXmlTag.SECURITY_CONSTRAINT);
  -        int count = 0;
  -        while (securityConstraints.hasNext())
  +        int count = insertElements(theWebXml, WebXmlTag.SECURITY_CONSTRAINT);
  +        if (count > 0)
           {
  -            Element securityConstraint = (Element) securityConstraints.next();
  -            webXml.addElement(WebXmlTag.SECURITY_CONSTRAINT,
  -                securityConstraint);
  -            count++;
  +            this.log.trace("Merged " + count + " security constraint"
  +                + (count != 1 ? "s " : " ") + "into the descriptor");
           }
  -        this.log.trace("Merged " + count + " security constraint"
  -            + (count != 1 ? "s " : " ") + "into the descriptor");
       }
   
       /**
  -     * 
  +     * Merges the login configuration from the provided descriptor into the
  +     * original descriptor, thereby eventually replacing the existing login 
  +     * config.
        * 
        * @param theWebXml The descriptor that contains the login config that
  -     *         is to be merged into the original descriptor
  +     *        is to be merged into the original descriptor
        */
       protected final void mergeLoginConfig(WebXml theWebXml)
       {
  -        Iterator loginConfigs = theWebXml.getElements(WebXmlTag.LOGIN_CONFIG);
  -        if (loginConfigs.hasNext())
  +        boolean replaced = replaceElement(theWebXml, WebXmlTag.LOGIN_CONFIG);
  +        if (replaced)
           {
  -            webXml.replaceElement(WebXmlTag.LOGIN_CONFIG,
  -                (Element) loginConfigs.next());
               this.log.trace(
                   "Merged the login configuration into the descriptor");
           }
       }
   
       /**
  -     * 
  +     * Merges the security roles from the provided descriptor into the original
  +     * descriptor.
        * 
        * @param theWebXml The descriptor that contains the security roles that
  -     *         are to be merged into the original descriptor
  +     *        are to be merged into the original descriptor
        */
       protected final void mergeSecurityRoles(WebXml theWebXml)
       {
  -        Iterator securityRoles =
  -            theWebXml.getElements(WebXmlTag.SECURITY_ROLE);
  +        int count = insertElements(theWebXml, WebXmlTag.SECURITY_ROLE);
  +        if (count > 0)
  +        {
  +            this.log.trace("Merged " + count + " security role"
  +                + (count != 1 ? "s " : " ") + "into the descriptor");
  +        }
  +    }
  +
  +    /**
  +     * Merges the environment entries from the provided descriptor into the
  +     * original descriptor.
  +     * 
  +     * @param theWebXml The descriptor that contains the environment entries
  +     *        that are to be merged into the original descriptor
  +     */
  +    protected final void mergeEnvironmentEntries(WebXml theWebXml)
  +    {
  +        int count = insertElements(theWebXml, WebXmlTag.ENV_ENTRY);
  +        if (count > 0)
  +        {
  +            this.log.trace("Merged " + count + " environment entr"
  +                + (count != 1 ? "ies " : "y ") + "into the descriptor");
  +        }
  +    }
  +
  +    /**
  +     * Merges the EJB references from the provided descriptor into the original
  +     * descriptor.
  +     * 
  +     * @param theWebXml The descriptor that contains the EJB refs that are to be
  +     *        merged into the original descriptor
  +     */
  +    protected final void mergeEjbRefs(WebXml theWebXml)
  +    {
  +        int count = insertElements(theWebXml, WebXmlTag.EJB_REF);
  +        if (count > 0)
  +        {
  +            this.log.trace("Merged " + count + " EJB reference"
  +                + (count != 1 ? "s " : "y ") + "into the descriptor");
  +        }
  +    }
  +
  +    /**
  +     * Merges the EJB local references from the provided descriptor into the
  +     * original descriptor.
  +     * 
  +     * @param theWebXml The descriptor that contains the EJB local refs that are
  +     *        to be merged into the original descriptor
  +     */
  +    protected final void mergeEjbLocalRefs(WebXml theWebXml)
  +    {
  +        int count = insertElements(theWebXml, WebXmlTag.EJB_LOCAL_REF);
  +        if (count > 0)
  +        {
  +            this.log.trace("Merged " + count + " EJB local reference"
  +                + (count != 1 ? "s " : "y ") + "into the descriptor");
  +        }
  +    }
  +
  +    // Private Methods ---------------------------------------------------------
  +
  +    /**
  +     * Insert all elements of the specified tag from the given descriptor into
  +     * the original descriptor, and returns the number of elements that were
  +     * added.
  +     * 
  +     * @param theWebXml The descriptor that contains the elements that are to be
  +     *        merged into the original descriptor
  +     * @param theTag Defines which elements will get merged
  +     * @return The number of elements inserted into the original descriptor
  +     */
  +    private int insertElements(WebXml theWebXml, WebXmlTag theTag)
  +    {
  +        Iterator elements = theWebXml.getElements(theTag);
           int count = 0;
  -        while (securityRoles.hasNext())
  +        while (elements.hasNext())
           {
  -            Element securityRole = (Element) securityRoles.next();
  -            webXml.addElement(WebXmlTag.SECURITY_ROLE,
  -                securityRole);
  +            Element element = (Element) elements.next();
  +            webXml.addElement(theTag, element);
               count++;
           }
  -        this.log.trace("Merged " + count + " security role"
  -            + (count != 1 ? "s " : " ") + "into the descriptor");
  +        return count;
  +    }
  +
  +    /**
  +     * Replaces the element of the specified tag in the original descriptor with
  +     * the equivalent element in the specified descriptor.
  +     * 
  +     * @param theWebXml The descriptor that contains the element that is to be
  +     *        added to the original descriptor, replacing the original
  +     *        definition
  +     * @param theTag Defines which element will get replaced
  +     * @return Whether the element was replaced
  +     */
  +    private boolean replaceElement(WebXml theWebXml, WebXmlTag theTag)
  +    {
  +        Iterator elements = theWebXml.getElements(theTag);
  +        if (elements.hasNext())
  +        {
  +            webXml.replaceElement(theTag, (Element) elements.next());
  +            return true;
  +        }
  +        return false;
       }
   
   }
  
  
  
  1.2       +34 -2     jakarta-cactus/integration/ant/src/test/org/apache/cactus/integration/ant/deployment/TestWebXmlMerger.java
  
  Index: TestWebXmlMerger.java
  ===================================================================
  RCS file: /home/cvs/jakarta-cactus/integration/ant/src/test/org/apache/cactus/integration/ant/deployment/TestWebXmlMerger.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TestWebXmlMerger.java	14 May 2003 10:46:20 -0000	1.1
  +++ TestWebXmlMerger.java	15 May 2003 20:22:43 -0000	1.2
  @@ -69,7 +69,9 @@
   
   /**
    * Unit tests for {@link WebXmlMerger}.
  - *
  + * 
  + * TODO: we need more tests for the security sections and the various references
  + * 
    * @author <a href="mailto:cmlenz@apache.org">Christopher Lenz</a>
    *
    * @version $Id$
  @@ -689,6 +691,36 @@
           assertEquals("/s1mapping2", servletMappings.next());
           assertEquals("/s1mapping3", servletMappings.next());
           assertTrue(!servletMappings.hasNext());
  +    }
  +
  +    /**
  +     * Tests whether a single EJB reference is correctly inserted into an empty
  +     * descriptor.
  +     * 
  +     * @throws Exception If an unexpected error occurs
  +     */
  +    public void testMergeOneEjbRefIntoEmptyDocument()
  +        throws Exception
  +    {
  +        String srcXml = "<web-app></web-app>";
  +        Document srcDoc =
  +            builder.parse(new ByteArrayInputStream(srcXml.getBytes()));
  +        WebXml srcWebXml = new WebXml(srcDoc);
  +        String mergeXml = "<web-app>"
  +            + "  <ejb-ref>"
  +            + "    <ejb-ref-name>ejbref1</ejb-ref-name>"
  +            + "    <ejb-ref-type>ejbref1.type</ejb-ref-type>"
  +            + "    <home>ejbref1.homeInterface</home>"
  +            + "    <remote>ejbref1.remoteInterface</remote>"
  +            + "  </ejb-ref>"
  +            + "</web-app>";
  +        Document mergeDoc =
  +            builder.parse(new ByteArrayInputStream(mergeXml.getBytes()));
  +        WebXml mergeWebXml = new WebXml(mergeDoc);
  +        WebXmlMerger merger = new WebXmlMerger(srcWebXml);
  +        merger.mergeEjbRefs(mergeWebXml);
  +        Iterator ejbRefs = srcWebXml.getElements(WebXmlTag.EJB_REF); 
  +        assertTrue(ejbRefs.hasNext());
       }
   
   }
  
  
  
  1.3       +15 -2     jakarta-cactus/documentation/docs/xdocs/integration/ant/task_webxmlmerge.xml
  
  Index: task_webxmlmerge.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-cactus/documentation/docs/xdocs/integration/ant/task_webxmlmerge.xml,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- task_webxmlmerge.xml	17 Mar 2003 20:07:51 -0000	1.2
  +++ task_webxmlmerge.xml	15 May 2003 20:22:43 -0000	1.3
  @@ -32,6 +32,11 @@
             source descriptor, the initialization parameters are merged.
           </li>
           <li>
  +          <emphasis>Resource References and Resource Environment References
  +          </emphasis>: Any &lt;resource-ref&gt; or &lt;resource-env-ref&gt;
  +          elements get inserted into the resulting descriptor verbatim.
  +        </li>
  +        <li>
             <emphasis>Security Constraints</emphasis>: Any
             &lt;security-constraint&gt; elements get inserted into the resulting
             descriptor verbatim.
  @@ -42,8 +47,16 @@
             source descriptor.
           </li>
           <li>
  -          <emphasis>Security Roles</emphasis>: Any
  -          &lt;security-role&gt; elements get inserted into the resulting
  +          <emphasis>Security Roles</emphasis>: Any &lt;security-role&gt;
  +          elements get inserted into the resulting descriptor verbatim.
  +        </li>
  +        <li>
  +          <emphasis>Environment Entries</emphasis>: Any &lt;env-entry&gt;
  +          elements get inserted into the resulting descriptor verbatim.
  +        </li>
  +        <li>
  +          <emphasis>EJB References</emphasis>: Any &lt;ejb-ref&gt; or
  +          &lt;ejb-local-ref&gt; elements get inserted into the resulting
             descriptor verbatim.
           </li>
         </ul>
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: cactus-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: cactus-dev-help@jakarta.apache.org