You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@maven.apache.org by jv...@apache.org on 2003/01/05 00:52:32 UTC

cvs commit: jakarta-turbine-maven/src/plugins-build/deploy/src/java/org/apache/maven/deploy Deployer.java SshDeployer.java

jvanzyl     2003/01/04 15:52:32

  Added:       src/plugins-build/deploy/src/java/org/apache/maven/deploy
                        Deployer.java SshDeployer.java
  Log:
  o Deployer interface and ssh2 deployer.
  
  Revision  Changes    Path
  1.1                  jakarta-turbine-maven/src/plugins-build/deploy/src/java/org/apache/maven/deploy/Deployer.java
  
  Index: Deployer.java
  ===================================================================
  package org.apache.maven.deploy;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache MavenSession" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache MavenSession", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * ====================================================================
   */
  
  import org.apache.maven.project.Project;
  import org.apache.maven.repository.Artifact;
  
  /**
   * Interface for all Maven deployers.
   *
   * @author <a href="mailto:jason@zenplex.com">Jason van Zyl</a>
   *
   * @version $Id: Deployer.java,v 1.1 2003/01/04 23:52:32 jvanzyl Exp $
   */
  public interface Deployer
  {
      /**
       * Deploy an artifact for a given project to the central repository.
       *
       * @param artifact The artifact to deploy.
       * @param project The project for which the artifact is being deployed.
       */
      public void projectDeploy( Artifact artifact, Project project );
  
      /**
       * Deploy an artifact for a given project to the location that the POM
       * specifies.
       *
       * @param artifact The artifact to deploy.
       * @param project The project for which the artifact is being deployed.
       */
      public void centralDeploy( Artifact artifact, Project project );
  }
  
  
  
  1.1                  jakarta-turbine-maven/src/plugins-build/deploy/src/java/org/apache/maven/deploy/SshDeployer.java
  
  Index: SshDeployer.java
  ===================================================================
  package org.apache.maven.deploy;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache MavenSession" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache MavenSession", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * ====================================================================
   */
  import com.jcraft.jsch.JSch;
  import com.jcraft.jsch.UserInfo;
  import com.jcraft.jsch.ChannelExec;
  import com.jcraft.jsch.Session;
  import com.jcraft.jsch.Channel;
  
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.PipedInputStream;
  import java.io.PipedOutputStream;
  
  import org.apache.maven.project.Project;
  import org.apache.maven.repository.Artifact;
  
  /**
   * An ssh2 deployer that uses the JSch library and the JCE.
   *
   * We will first try to use public keys for authentication and if
   * that doesn't work then we fall back to using the login and
   * password of the user in question.
   *
   * NOTE: We are assuming the standard port of 22.
   *
   * @author <a href="mailto:jason@zenplex.com">Jason van Zyl</a>
   *
   * @version $Id: SshDeployer.java,v 1.1 2003/01/04 23:52:32 jvanzyl Exp $
   *
   * @todo still have to account for differing setups for people deploying
   *       to their own sites and to the central repository.
   */
  public class SshDeployer
      implements Deployer
  {
      /** @see Deployer#projectDeploy */
      public void centralDeploy( Artifact artifact, Project project )
      {
          // Get the distribution site.
          String host = (String) project.getContext().getVariable("maven.repo.central");
          
          // Path to the local artifact to deploy.
          String localArtifactPath = artifact.getPath();
          
          // Path for the remote artifact.
          String remoteArtifactPath = (String) project.getContext()
              .getVariable("maven.repo.central.directory") + artifact.generatePath();
          
          deploy( host, localArtifactPath, remoteArtifactPath, project );
      }
  
      /** @see Deployer#projectDeploy */
      public void projectDeploy( Artifact artifact, Project project )
      {
          // Get the distribution site.
          String host = project.getDistributionSite();
          
          // Path to the local artifact to deploy.
          String localArtifactPath = artifact.getPath();
          
          // Path for the remote artifact.
          String remoteArtifactPath = project.getDistributionDirectory() + artifact.generatePath();
          
          deploy( host, localArtifactPath, remoteArtifactPath, project );
      }
  
      /** @see Deployer#projectDeploy */
      protected void deploy( String host,
                             String localArtifactPath, 
                             String remoteArtifactPath,
                             Project project )
      {
          try
          {
              JSch jsch = new JSch();
              Session session = jsch.getSession(host, 22);
              
              // Look for the private key and use it if available.
              File privateKey = new File( new File( System.getProperty("user.home"), ".ssh" ), "id_dsa" );
  
              if ( privateKey.exists() )
              {
                  session.setIdentity( privateKey.getPath() );
              }
  
              // username and password will be given via UserInfo interface.
              UserInfo ui = new MavenUserInfo();
              session.setUserInfo(ui);
              session.connect();
  
              // exec 'scp -t remoteArtifactPath' remotely
              String command = "scp -t " + remoteArtifactPath;
              Channel channel = session.openChannel("exec");
              ((ChannelExec) channel).setCommand(command);
  
              // plug I/O streams for remote scp
              PipedOutputStream out = new PipedOutputStream();
              channel.setInputStream(new PipedInputStream(out));
              PipedInputStream in = new PipedInputStream();
              channel.setOutputStream(new PipedOutputStream(in));
  
              channel.connect();
  
              byte[] tmp = new byte[1];
  
              // wait for '\0'
              do
              {
                  in.read(tmp, 0, 1);
              } while (tmp[0] != 0);
  
              // send "C0644 filesize filename", where filename should not include '/'
              int filesize = (int) (new File(localArtifactPath)).length();
              command = "C0644 " + filesize + " ";
              if (localArtifactPath.lastIndexOf('/') > 0)
              {
                  command += localArtifactPath.substring(localArtifactPath.lastIndexOf('/') + 1);
              }
              else
              {
                  command += localArtifactPath;
              }
              command += "\n";
              out.write(command.getBytes());
              out.flush();
  
              // wait for '\0'
              do
              {
                  in.read(tmp, 0, 1);
              } while (tmp[0] != 0);
  
              // send a content of localArtifactPath
              FileInputStream fis = new FileInputStream(localArtifactPath);
              byte[] buf = new byte[1024];
              while (true)
              {
                  int len = fis.read(buf, 0, buf.length);
                  if (len <= 0)
                  {
                      break;
                  }
                  out.write(buf, 0, len);
                  out.flush();
              }
  
              // send '\0'
              buf[0] = 0;
              out.write(buf, 0, 1);
              out.flush();
  
              // wait for '\0'
              do
              {
                  in.read(tmp, 0, 1);
              } while (tmp[0] != 0);
  
              System.exit(0);
          }
          catch (Exception e)
          {
              System.out.println(e);
          }
      }
  
      /**
       * MavenUserInfo class which returns the necessary name and password from
       * the POM to secure an ssh connection.
       */
      public static class MavenUserInfo
           implements UserInfo
      {
          /** Maven project. */
          private Project project;
  
          /** Gets the name attribute of the MavenUserInfo object */
          public String getName()
          {
              return (String) project.getContext().getVariable("maven.ssh.username");
          }
  
          /** Gets the password attribute of the MavenUserInfo object */
          public String getPassword()
          {
              return (String) project.getContext().getVariable("maven.ssh.password");
          }
  
          /** Gets the passphrase attribute of the MavenUserInfo object */
          public String getPassphrase(String message)
          {
              return (String) project.getContext().getVariable("maven.ssh.passphrase");
          }
          
          /** Required to satisfy UserInfo interface. Not used currently. */
          public boolean promptNameAndPassphrase(String message)
          {
              return true;
          }
  
          /** Required to satisfy UserInfo interface. Not used currently. */
          public boolean promptNameAndPassword(String message)
          {
              return true;
          }
          
          /** Required to satisfy UserInfo interface. Not used currently. */
          public boolean retry()
          {
              return true;
          }
  
          /** Required to satisfy UserInfo interface. Not used currently. */
          public boolean prompt(String message)
          {
              return true;
          }
      }
  }