Package org.apache.ws.util.platform.axis.tool

Source Code of org.apache.ws.util.platform.axis.tool.WsddUpdater

/*=============================================================================*
*  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.platform.axis.tool;

import org.apache.axis.AxisEngine;
import org.apache.axis.MessageContext;
import org.apache.axis.configuration.FileProvider;
import org.apache.axis.server.AxisServer;
import org.apache.axis.utils.Admin;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.taskdefs.MatchingTask;
import org.apache.tools.ant.types.FileSet;
import org.apache.ws.util.JaxpUtils;
import org.w3c.dom.Element;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

/**
* Utility class that allows for easy deployment of new entries (e.g. deploy.wsdd)
* to an Axis WSDD. This class can be executed in any of the following ways:
* <ul>
<li>by calling {@link WsddUpdater#deploy(java.io.File)}</li>
<li>from the command-line: java org.apache.ws.platform.axis.tool.WsddUpdater path_to_deploy.wsdd</li>
<li>as an Ant task: &lt;wsddupdater deployWsdd="path_to_deploy.wsdd"&gt;</li>
* </ul>
*
* @author Ian P. Springer
*/
public class WsddUpdater
   extends MatchingTask
{
   /**
    * Below default can be overridden by setting the below sysprop.
    */
   public static final String SYSPROP_WSDD = "wsdd";

   /**
    * Default to looking for server-config.wsdd in current dir or classpath.
    */
   private static final String DEFAULT_CONFIG_WSDD = "server-config.wsdd";
   private String              m_configWsdd;
   private List                m_wsddFiles  = new ArrayList(  );
   private Admin               m_axisAdmin;
   private MessageContext      m_msgContext;

   /**
    * Creates a new {@link WsddUpdater} object.
    */
   public WsddUpdater(  )
   {
      this( (String) null );
   }

   /**
    * Creates a new {@link WsddUpdater} object for updating the specified WSDD file.
    *
    * @param configWsdd DOCUMENT_ME
    */
   public WsddUpdater( File configWsdd )
   {
      this( configWsdd.getPath(  ) );
   }

   /**
    * Creates a new {@link WsddUpdater} object for updating the specified WSDD, which may be a a file path or a location in
    * the classpath.
    *
    * @param configWsdd DOCUMENT_ME
    */
   public WsddUpdater( String configWsdd )
   {
      initContextClassLoader(  );
      if ( configWsdd != null )
      {
         m_configWsdd = configWsdd;
      }
      else
      {
         m_configWsdd =
            ( System.getProperty( SYSPROP_WSDD ) != null ) ? System.getProperty( SYSPROP_WSDD ) : DEFAULT_CONFIG_WSDD;
      }
   }

   /**
    * Sets the WSDD to be updated.
    *
    * @param configWsdd the WSDD to be updated
    */
   public void setConfigWsdd( String configWsdd )
   {
      m_configWsdd = configWsdd;
   }

   /**
    * Sets the WSDD (un)deployment fragment to be added to the WSDD.
    *
    * @param deployWsdd the WSDD (un)deployment fragment to be added to the WSDD
    */
   public void setDeployWsdd( File deployWsdd )
   {
      m_wsddFiles.add( deployWsdd );
   }

   /**
    * DOCUMENT_ME
    *
    * @param wsddPaths DOCUMENT_ME
    */
   public void addConfiguredWsddPaths( FileSet wsddPaths )
   {
      File             baseDir       = wsddPaths.getDir( getProject(  ) );
      DirectoryScanner dirScanner    = wsddPaths.getDirectoryScanner( getProject(  ) );
      String[]         includedFiles = dirScanner.getIncludedFiles(  );
      for ( int i = 0; i < includedFiles.length; i++ )
      {
         m_wsddFiles.add( new File( baseDir, includedFiles[i] ) );
      }
   }

   /**
    * DOCUMENT_ME
    *
    * @param deployWsdd DOCUMENT_ME
    * @throws Exception DOCUMENT_ME
    */
   public void deploy( File deployWsdd )
   throws Exception
   {
      setDeployWsdd( deployWsdd );
      execute(  );
   }

   /**
    * DOCUMENT_ME
    *
    * @throws org.apache.tools.ant.BuildException DOCUMENT_ME
    */
   public void execute(  )
   throws BuildException
   {
      if ( m_wsddFiles.isEmpty(  ) )
      {
         throw new BuildException( "No deploy WSDDs were specified!" );
      }

      try
      {
         AxisEngine axisEngine = new AxisServer( new FileProvider( m_configWsdd ) );
         m_msgContext    = new MessageContext( axisEngine );
         m_axisAdmin     = new Admin(  );
         for ( int i = 0; i < m_wsddFiles.size(  ); i++ )
         {
            deployWsdd( (File) m_wsddFiles.get( i ) );
         }
      }
      catch ( Exception e )
      {
         throw new BuildException( e );
      }
   }

   /**
    * Command-line invocation entry point.
    *
    * @param args command-line arguments
    * @throws Exception on fatal error
    */
   public static void main( String[] args )
   throws Exception
   {
      //System.setProperty( SYSPROP_WSDD, "C:/server-config.wsdd" );
      if ( args.length != 1 )
      {
         System.err.println( "Usage: " + WsddUpdater.class.getName(  ) + " deployWsddFile" );
         System.exit( 1 );
      }

      new WsddUpdater(  ).deploy( new File( args[0] ) );
   }

   private void deployWsdd( File deployWsdd )
   throws Exception
   {
      log( "Deploying WSDD " + deployWsdd + " to configuration WSDD " + m_configWsdd + "..." );
      Element deployWsddElem = JaxpUtils.loadDocument( new FileInputStream( deployWsdd ) ).getDocumentElement(  );
      m_axisAdmin.process( m_msgContext, deployWsddElem );
   }

   private void initContextClassLoader(  )
   {
      // this is done because for some reason, when run using Maven,
      // the ContextClassloader is null, which causes an issue with Axis.
      if ( Thread.currentThread(  ).getContextClassLoader(  ) == null )
      {
         Thread.currentThread(  ).setContextClassLoader( MatchingTask.class.getClassLoader(  ) );
      }
   }
}
TOP

Related Classes of org.apache.ws.util.platform.axis.tool.WsddUpdater

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.