Package org.apache.ws.util.j2ee

Source Code of org.apache.ws.util.j2ee.WebDDMergeTask

/*=============================================================================*
*  Copyright 2004 The Apache Software Foundation
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*=============================================================================*/
package org.apache.ws.util.j2ee;

import noNamespace.ServletMappingType;
import noNamespace.ServletType;
import noNamespace.WebAppDocument;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.types.FileSet;
import org.apache.xmlbeans.XmlOptions;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
* An Ant task that merges one or more source J2EE web.xml's into a target web.xml.
*/
public class WebDDMergeTask
   extends Task
{
   private List m_sourceFiles = new ArrayList(  );
   private File m_targetFile;

   /**
    * Creates a new {@link WebDDMergeTask} object.
    */
   public WebDDMergeTask(  )
   {
      initContextClassLoader(  );
   }

   /**
    * Adds the specified file to the list of source web.xml's.
    *
    * @param srcFile
    */
   public void setSourceFile( File srcFile )
   {
      m_sourceFiles.add( srcFile );
   }

   /**
    * Sets the target web.xml (i.e. the web.xml to be merged into).
    *
    * @param targetFile
    */
   public void setTargetFile( File targetFile )
   {
      m_targetFile = targetFile;
   }

   /**
    * Adds the files in the specified fileset to the list of source web.xml's.
    *
    * @param fileSet DOCUMENT_ME
    */
   public void addConfiguredSourceFiles( FileSet fileSet )
   {
      File             baseDir       = fileSet.getDir( getProject(  ) );
      DirectoryScanner dirScanner    = fileSet.getDirectoryScanner( getProject(  ) );
      String[]         includedFiles = dirScanner.getIncludedFiles(  );
      for ( int i = 0; i < includedFiles.length; i++ )
      {
         m_sourceFiles.add( new File( baseDir, includedFiles[i] ) );
      }
   }

   /**
    * DOCUMENT_ME
    *
    * @throws org.apache.tools.ant.BuildException
    *          DOCUMENT_ME
    */
   public void execute(  )
   throws BuildException
   {
      if ( m_sourceFiles.isEmpty(  ) )
      {
         throw new BuildException( "No source web.xml files were specified!" );
      }

      try
      {
         System.out.println( "Loading target web-app document from " + m_targetFile + " ..." );
         WebAppDocument        targetWebAppDoc = parseWebXmlFile( m_targetFile );
         WebAppDocument.WebApp targetWebApp = targetWebAppDoc.getWebApp(  );
         for ( int i = 0; i < m_sourceFiles.size(  ); i++ )
         {
            mergeWebXml( (File) m_sourceFiles.get( i ), targetWebApp );
         }

         saveTargetDocToFile( targetWebAppDoc );
      }
      catch ( Exception e )
      {
         throw new BuildException( e );
      }
   }

   private ServletType getServlet( ServletType[] servletArray,
                                   String        servletName )
   {
      ServletType servlet = null;
      for ( int j = 0; j < servletArray.length; j++ )
      {
         if ( servletArray[j].getServletName(  ).getStringValue(  ).equals( servletName ) )
         {
            servlet = servletArray[j];
            break;
         }
      }

      return servlet;
   }

   private ServletMappingType getServletMapping( ServletMappingType[] servletMappingArray,
                                                 String               servletName,
                                                 String               urlPattern )
   {
      ServletMappingType servletMapping = null;
      for ( int j = 0; j < servletMappingArray.length; j++ )
      {
         if ( servletMappingArray[j].getServletName(  ).getStringValue(  ).equals( servletName )
              && servletMappingArray[j].getUrlPattern(  ).getStringValue(  ).equals( urlPattern ) )
         {
            servletMapping = servletMappingArray[j];
            break;
         }
      }

      return servletMapping;
   }

   private void initContextClassLoader(  )
   {
      if ( Thread.currentThread(  ).getContextClassLoader(  ) == null )
      {
         Thread.currentThread(  ).setContextClassLoader( Task.class.getClassLoader(  ) );
      }
   }

   private void mergeServletElements( WebAppDocument.WebApp webApp,
                                      WebAppDocument.WebApp targetWebApp )
   {
      ServletType[] servletArray       = webApp.getServletArray(  );
      ServletType[] targetServletArray = targetWebApp.getServletArray(  );
      for ( int i = 0; i < servletArray.length; i++ )
      {
         ServletType servlet       = servletArray[i];
         String      servletName   = servlet.getServletName(  ).getStringValue(  );
         ServletType targetServlet = getServlet( targetServletArray, servletName );
         if ( targetServlet == null )
         {
            targetServlet = targetWebApp.addNewServlet(  );
         }

         targetServlet.set( servlet );
      }
   }

   private void mergeServletMappingElements( WebAppDocument.WebApp webApp,
                                             WebAppDocument.WebApp targetWebApp )
   {
      ServletMappingType[] servletMappingArray       = webApp.getServletMappingArray(  );
      ServletMappingType[] targetServletMappingArray = targetWebApp.getServletMappingArray(  );
      for ( int i = 0; i < servletMappingArray.length; i++ )
      {
         ServletMappingType servletMapping       = servletMappingArray[i];
         String             servletName          = servletMapping.getServletName(  ).getStringValue(  );
         String             urlPattern           = servletMapping.getUrlPattern(  ).getStringValue(  );
         ServletMappingType targetServletMapping =
            getServletMapping( targetServletMappingArray, servletName, urlPattern );
         if ( targetServletMapping == null )
         {
            targetServletMapping = targetWebApp.addNewServletMapping(  );
         }

         targetServletMapping.set( servletMapping );
      }
   }

   private void mergeWebXml( File                  srcFile,
                             WebAppDocument.WebApp targetWebApp )
   throws Exception
   {
      System.out.println( "Merging " + srcFile + " into target web-app document ..." );
      WebAppDocument        webAppDoc = parseWebXmlFile( srcFile );
      WebAppDocument.WebApp webApp = webAppDoc.getWebApp(  );
      mergeServletElements( webApp, targetWebApp );
      mergeServletMappingElements( webApp, targetWebApp );
   }

   private WebAppDocument parseWebXmlFile( File ddFile )
   throws Exception
   {
      XmlOptions loadOptions = new XmlOptions(  ).setLoadStripWhitespace(  );
      return WebAppDocument.Factory.parse( ddFile, loadOptions );
   }

   private void saveTargetDocToFile( WebAppDocument doc )
   throws IOException
   {
      System.out.println( "Saving updated target web-app document to " + m_targetFile + " ..." );
      XmlOptions saveOptions = new XmlOptions(  ).setSavePrettyPrint(  );
      doc.save( m_targetFile, saveOptions );
   }
}
TOP

Related Classes of org.apache.ws.util.j2ee.WebDDMergeTask

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.