You are viewing a plain text version of this content. The canonical link for it is here.
Posted to wagon-commits@maven.apache.org by br...@apache.org on 2005/02/18 07:28:55 UTC

cvs commit: maven-wagon/wagon-provider-api/src/main/java/org/apache/maven/wagon AbstractWagon.java

brett       2005/02/17 22:28:55

  Modified:    wagon-providers/wagon-ssh/src/main/java/org/apache/maven/wagon/providers/ssh
                        ScpWagon.java
               wagon-providers/wagon-ssh/src/main/resources/META-INF/plexus
                        components.xml
               wagon-provider-api/src/main/java/org/apache/maven/wagon
                        AbstractWagon.java
  Added:       wagon-providers/wagon-ssh/src/main/java/org/apache/maven/wagon/providers/ssh
                        SftpWagon.java
               wagon-providers/wagon-ssh/src/test/java/org/apache/maven/wagon/providers/ssh
                        SftpWagonTest.java
  Log:
  add the Sftp wagon
  
  Revision  Changes    Path
  1.14      +14 -15    maven-wagon/wagon-providers/wagon-ssh/src/main/java/org/apache/maven/wagon/providers/ssh/ScpWagon.java
  
  Index: ScpWagon.java
  ===================================================================
  RCS file: /home/cvs/maven-wagon/wagon-providers/wagon-ssh/src/main/java/org/apache/maven/wagon/providers/ssh/ScpWagon.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- ScpWagon.java	18 Feb 2005 01:18:51 -0000	1.13
  +++ ScpWagon.java	18 Feb 2005 06:28:55 -0000	1.14
  @@ -29,13 +29,12 @@
   import org.apache.maven.wagon.ResourceDoesNotExistException;
   import org.apache.maven.wagon.TransferFailedException;
   import org.apache.maven.wagon.WagonConstants;
  -import org.apache.maven.wagon.repository.RepositoryPermissions;
  -import org.apache.maven.wagon.resource.Resource;
  -import org.apache.maven.wagon.events.TransferEvent;
   import org.apache.maven.wagon.authentication.AuthenticationException;
   import org.apache.maven.wagon.authentication.AuthenticationInfo;
   import org.apache.maven.wagon.authorization.AuthorizationException;
  -import org.apache.maven.wagon.proxy.ProxyInfo;
  +import org.apache.maven.wagon.events.TransferEvent;
  +import org.apache.maven.wagon.repository.RepositoryPermissions;
  +import org.apache.maven.wagon.resource.Resource;
   import org.codehaus.plexus.util.StringUtils;
   
   import java.io.File;
  @@ -269,8 +268,6 @@
   
               channel.connect();
   
  -            byte[] tmp = new byte[ 1 ];
  -
               if ( checkAck( in ) != 0 )
               {
                   throw new TransferFailedException( "ACK check failed" );
  @@ -326,6 +323,15 @@
   
               throw new TransferFailedException( msg, e );
           }
  +        finally
  +        {
  +            if ( channel != null )
  +            {
  +                shutdownStream( out );
  +
  +                channel.disconnect();
  +            }
  +        }
   
           RepositoryPermissions permissions = getRepository().getPermissions();
   
  @@ -338,13 +344,6 @@
           {
               executeCommand( "chmod " + permissions.getFileMode() + " " + basedir + "/" + resourceName + "\n" );
           }
  -
  -        if ( channel != null )
  -        {
  -            shutdownStream( out );
  -
  -            channel.disconnect();
  -        }
       }
   
       public void get( String resourceName, File destination )
  @@ -520,7 +519,7 @@
                   }
               }
   
  -            String msg = "Error occured while deploying to remote repository:" + getRepository();
  +            String msg = "Error occured while downloading from the remote repository:" + getRepository();
   
               throw new TransferFailedException( msg, e );
           }
  
  
  
  1.1                  maven-wagon/wagon-providers/wagon-ssh/src/main/java/org/apache/maven/wagon/providers/ssh/SftpWagon.java
  
  Index: SftpWagon.java
  ===================================================================
  package org.apache.maven.wagon.providers.ssh;
  
  /*
   * Copyright 2001-2005 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.
   */
  
  import com.jcraft.jsch.ChannelSftp;
  import com.jcraft.jsch.SftpATTRS;
  import com.jcraft.jsch.SftpException;
  import org.apache.maven.wagon.PathUtils;
  import org.apache.maven.wagon.ResourceDoesNotExistException;
  import org.apache.maven.wagon.TransferFailedException;
  import org.apache.maven.wagon.authorization.AuthorizationException;
  import org.apache.maven.wagon.events.TransferEvent;
  import org.apache.maven.wagon.repository.RepositoryPermissions;
  import org.apache.maven.wagon.resource.Resource;
  import org.codehaus.plexus.util.StringUtils;
  
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.IOException;
  import java.io.InputStream;
  
  /**
   * SFTP protocol wagon.
   *
   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
   * @version $Id: SftpWagon.java,v 1.1 2005/02/18 06:28:55 brett Exp $
   * @todo [BP] add compression flag
   */
  public class SftpWagon
      extends ScpWagon
      implements SshCommandExecutor
  {
      private static final String SFTP_CHANNEL = "sftp";
  
      private static final int S_IFDIR = 0x4000;
  
      // ----------------------------------------------------------------------
      //
      // ----------------------------------------------------------------------
  
      public void put( File source, String resourceName )
          throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
      {
          String basedir = getRepository().getBasedir();
  
          resourceName = StringUtils.replace( resourceName, "\\", "/" );
          String dir = PathUtils.dirname( resourceName );
          dir = StringUtils.replace( dir, "\\", "/" );
  
          ChannelSftp channel = null;
  
          String filename;
          if ( resourceName.lastIndexOf( '/' ) > 0 )
          {
              filename = resourceName.substring( resourceName.lastIndexOf( '/' ) + 1 );
          }
          else
          {
              filename = resourceName;
          }
  
          try
          {
              channel = (ChannelSftp) session.openChannel( SFTP_CHANNEL );
  
              channel.connect();
  
              channel.cd( basedir );
  
              mkdirs( channel, dir );
  
              Resource resource = new Resource( resourceName );
  
              firePutStarted( resource, source );
  
              channel.put( source.getAbsolutePath(), filename );
  
              postProcessListeners( resource, source, TransferEvent.REQUEST_PUT );
  
              RepositoryPermissions permissions = getRepository().getPermissions();
  
              if ( permissions != null && permissions.getGroup() != null )
              {
                  int group;
                  try
                  {
                      group = Integer.valueOf( permissions.getGroup() ).intValue();
                      channel.chgrp( group, filename );
                  }
                  catch ( NumberFormatException e )
                  {
                      // TODO: warning level
                      fireTransferDebug( "Not setting group: must be a numerical GID for SFTP" );
                  }
              }
  
              if ( permissions != null && permissions.getFileMode() != null )
              {
                  int mode;
                  try
                  {
                      mode = Integer.valueOf( permissions.getFileMode() ).intValue();
                      channel.chmod( mode, filename );
                  }
                  catch ( NumberFormatException e )
                  {
                      // TODO: warning level
                      fireTransferDebug( "Not setting mode: must be a numerical mode for SFTP" );
                  }
              }
  
              firePutCompleted( resource, source );
  
              String[] dirs = PathUtils.dirnames( dir );
              for ( int i = 0; i < dirs.length; i++ )
              {
                  channel.cd( ".." );
              }
          }
          catch ( Exception e )
          {
              String msg = "Error occured while deploying '" + resourceName + "' to remote repository: " +
                  getRepository().getUrl();
  
              throw new TransferFailedException( msg, e );
          }
  
          if ( channel != null )
          {
              channel.disconnect();
          }
      }
  
      private void mkdirs( ChannelSftp channel, String dir )
          throws TransferFailedException, SftpException
      {
          String[] dirs = PathUtils.dirnames( dir );
          for ( int i = 0; i < dirs.length; i++ )
          {
              try
              {
                  SftpATTRS attrs = channel.stat( dirs[i] );
                  if ( ( attrs.getPermissions() & S_IFDIR ) == 0 )
                  {
                      throw new TransferFailedException( "Remote path is not a directory:" + dir );
                  }
              }
              catch ( SftpException e )
              {
                  // doesn't exist, make it and try again
                  channel.mkdir( dirs[i] );
              }
  
              channel.cd( dirs[i] );
          }
      }
  
      private void postProcessListeners( Resource resource, File source, int requestType )
          throws TransferFailedException
      {
          byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
  
          TransferEvent transferEvent = new TransferEvent( this, resource, TransferEvent.TRANSFER_PROGRESS, requestType );
  
          try
          {
              InputStream input = new FileInputStream( source );
  
              while ( true )
              {
                  int n = input.read( buffer );
  
                  if ( n == -1 )
                  {
                      break;
                  }
  
                  fireTransferProgress( transferEvent, buffer, n );
              }
          }
          catch ( IOException e )
          {
              throw new TransferFailedException( "Failed to post-process the source file", e );
          }
      }
  
      public void get( String resourceName, File destination )
          throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
      {
          createParentDirectories( destination );
  
          ChannelSftp channel;
  
          resourceName = StringUtils.replace( resourceName, "\\", "/" );
          String dir = PathUtils.dirname( resourceName );
          dir = StringUtils.replace( dir, "\\", "/" );
  
          Resource resource = new Resource( resourceName );
  
          String filename;
          if ( resourceName.lastIndexOf( '/' ) > 0 )
          {
              filename = resourceName.substring( resourceName.lastIndexOf( '/' ) + 1 );
          }
          else
          {
              filename = resourceName;
          }
  
          try
          {
              channel = (ChannelSftp) session.openChannel( SFTP_CHANNEL );
  
              channel.connect();
  
              channel.cd( repository.getBasedir() );
  
              channel.cd( dir );
  
              fireGetStarted( resource, destination );
  
              channel.get( filename, destination.getAbsolutePath() );
  
              postProcessListeners( resource, destination, TransferEvent.REQUEST_GET );
  
              fireGetCompleted( resource, destination );
  
              String[] dirs = PathUtils.dirnames( dir );
              for ( int i = 0; i < dirs.length; i++ )
              {
                  channel.cd( ".." );
              }
          }
          catch ( Exception e )
          {
              fireTransferError( resource, e );
  
              if ( destination.exists() )
              {
                  boolean deleted = destination.delete();
  
                  if ( !deleted )
                  {
                      destination.deleteOnExit();
                  }
              }
  
              String msg = "Error occured while downloading from the remote repository:" + getRepository();
  
              throw new TransferFailedException( msg, e );
          }
  
      }
  
      public boolean getIfNewer( String resourceName, File destination, long timestamp )
      {
          throw new UnsupportedOperationException( "getIfNewer is scp wagon must be still implemented" );
      }
  }
  
  
  
  1.2       +6 -0      maven-wagon/wagon-providers/wagon-ssh/src/main/resources/META-INF/plexus/components.xml
  
  Index: components.xml
  ===================================================================
  RCS file: /home/cvs/maven-wagon/wagon-providers/wagon-ssh/src/main/resources/META-INF/plexus/components.xml,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- components.xml	10 Aug 2004 17:49:15 -0000	1.1
  +++ components.xml	18 Feb 2005 06:28:55 -0000	1.2
  @@ -7,6 +7,12 @@
         <instantiation-strategy>per-lookup</instantiation-strategy>
       </component>
       <component>
  +      <role>org.apache.maven.wagon.Wagon</role>
  +      <role-hint>sftp</role-hint>
  +      <implementation>org.apache.maven.wagon.providers.ssh.SftpWagon</implementation>
  +      <instantiation-strategy>per-lookup</instantiation-strategy>
  +    </component>
  +    <component>
         <role>org.apache.maven.wagon.providers.ssh.SshCommandExecutor</role>
         <implementation>org.apache.maven.wagon.providers.ssh.ScpWagon</implementation>
         <instantiation-strategy>per-lookup</instantiation-strategy>
  
  
  
  1.1                  maven-wagon/wagon-providers/wagon-ssh/src/test/java/org/apache/maven/wagon/providers/ssh/SftpWagonTest.java
  
  Index: SftpWagonTest.java
  ===================================================================
  package org.apache.maven.wagon.providers.ssh;
  
  /*
   * Copyright 2001-2005 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.
   */
  
  import org.apache.maven.wagon.WagonTestCase;
  import org.apache.maven.wagon.authentication.AuthenticationInfo;
  
  import java.io.File;
  
  /**
   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
   * @version $Id: SftpWagonTest.java,v 1.1 2005/02/18 06:28:55 brett Exp $
   */
  public class SftpWagonTest
      extends WagonTestCase
  {
      public SftpWagonTest( String testName )
      {
          super( testName );    
      }
  
      protected String getProtocol()
      {
          return "sftp";
      }
  
      public String getTestRepositoryUrl()
      {
          return TestData.getTestRepositoryUrl();
      }
  
  
  
      protected AuthenticationInfo getAuthInfo()
      {
          AuthenticationInfo authInfo = new AuthenticationInfo();
  
          String userName = TestData.getUserName();
  
          authInfo.setUserName( userName );
  
          File privateKey = TestData.getPrivateKey() ;
  
          if ( privateKey.exists() )
          {
              authInfo.setPrivateKey( privateKey.getAbsolutePath() );
  
              authInfo.setPassphrase( "" );
          }
  
          return authInfo;
      }
  
  }
  
  
  
  1.15      +1 -1      maven-wagon/wagon-provider-api/src/main/java/org/apache/maven/wagon/AbstractWagon.java
  
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: wagon-cvs-unsubscribe@maven.apache.org
For additional commands, e-mail: wagon-cvs-help@maven.apache.org