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 02:33:48 UTC

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

jvanzyl     2003/01/04 17:33:47

  Modified:    src/plugins-build/deploy/src/java/org/apache/maven/deploy
                        Deployer.java SshDeployer.java
  Added:       src/plugins-build/deploy/src/java/org/apache/maven/deploy
                        AbstractDeployer.java HttpDeployer.java
  Log:
  o trying to make it so that a deployer only has to implement a single
    method.
  o added an http deployer.
  
    will add a factory, clean up the exception handling (including making
    a DeploymentException and figure out the remaining confiration issues.
  
  Revision  Changes    Path
  1.2       +3 -5      jakarta-turbine-maven/src/plugins-build/deploy/src/java/org/apache/maven/deploy/Deployer.java
  
  Index: Deployer.java
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-maven/src/plugins-build/deploy/src/java/org/apache/maven/deploy/Deployer.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- Deployer.java	4 Jan 2003 23:52:32 -0000	1.1
  +++ Deployer.java	5 Jan 2003 01:33:47 -0000	1.2
  @@ -55,7 +55,6 @@
    *
    * ====================================================================
    */
  -
   import org.apache.maven.project.Project;
   import org.apache.maven.repository.Artifact;
   
  @@ -63,7 +62,6 @@
    * Interface for all Maven deployers.
    *
    * @author <a href="mailto:jason@zenplex.com">Jason van Zyl</a>
  - *
    * @version $Id$
    */
   public interface Deployer
  @@ -74,7 +72,7 @@
        * @param artifact The artifact to deploy.
        * @param project The project for which the artifact is being deployed.
        */
  -    public void projectDeploy( Artifact artifact, Project project );
  +    public void projectDeploy(Artifact artifact);
   
       /**
        * Deploy an artifact for a given project to the location that the POM
  @@ -83,5 +81,5 @@
        * @param artifact The artifact to deploy.
        * @param project The project for which the artifact is being deployed.
        */
  -    public void centralDeploy( Artifact artifact, Project project );
  +    public void centralDeploy(Artifact artifact);
   }
  
  
  
  1.2       +45 -69    jakarta-turbine-maven/src/plugins-build/deploy/src/java/org/apache/maven/deploy/SshDeployer.java
  
  Index: SshDeployer.java
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-maven/src/plugins-build/deploy/src/java/org/apache/maven/deploy/SshDeployer.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- SshDeployer.java	4 Jan 2003 23:52:32 -0000	1.1
  +++ SshDeployer.java	5 Jan 2003 01:33:47 -0000	1.2
  @@ -70,72 +70,37 @@
   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.
  + * 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$
  - *
  - * @todo still have to account for differing setups for people deploying
  - *       to their own sites and to the central repository.
  + * @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
  +     extends AbstractDeployer
   {
  -    /** @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 )
  +    /**
  +     * @see Deployer#project
  +     */
  +    protected void deploy(String host,
  +                          String localArtifactPath,
  +                          String remoteArtifactPath)
       {
           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" );
  +            File privateKey = new File(new File(System.getProperty("user.home"), ".ssh"), "id_dsa");
   
  -            if ( privateKey.exists() )
  +            if (privateKey.exists())
               {
  -                session.setIdentity( privateKey.getPath() );
  +                session.setIdentity(privateKey.getPath());
               }
   
               // username and password will be given via UserInfo interface.
  @@ -222,49 +187,60 @@
        * MavenUserInfo class which returns the necessary name and password from
        * the POM to secure an ssh connection.
        */
  -    public static class MavenUserInfo
  +    public class MavenUserInfo
            implements UserInfo
       {
  -        /** Maven project. */
  -        private Project project;
  -
  -        /** Gets the name attribute of the MavenUserInfo object */
  +        /**
  +         * Gets the name attribute of the MavenUserInfo object
  +         */
           public String getName()
           {
  -            return (String) project.getContext().getVariable("maven.ssh.username");
  +            return (String) getProject().getContext().getVariable("maven.ssh.username");
           }
   
  -        /** Gets the password attribute of the MavenUserInfo object */
  +        /**
  +         * Gets the password attribute of the MavenUserInfo object
  +         */
           public String getPassword()
           {
  -            return (String) project.getContext().getVariable("maven.ssh.password");
  +            return (String) getProject().getContext().getVariable("maven.ssh.password");
           }
   
  -        /** Gets the passphrase attribute of the MavenUserInfo object */
  +        /**
  +         * Gets the passphrase attribute of the MavenUserInfo object
  +         */
           public String getPassphrase(String message)
           {
  -            return (String) project.getContext().getVariable("maven.ssh.passphrase");
  +            return (String) getProject().getContext().getVariable("maven.ssh.passphrase");
           }
  -        
  -        /** Required to satisfy UserInfo interface. Not used currently. */
  +
  +        /**
  +         * Required to satisfy UserInfo interface. Not used currently.
  +         */
           public boolean promptNameAndPassphrase(String message)
           {
               return true;
           }
   
  -        /** Required to satisfy UserInfo interface. Not used currently. */
  +        /**
  +         * Required to satisfy UserInfo interface. Not used currently.
  +         */
           public boolean promptNameAndPassword(String message)
           {
               return true;
           }
  -        
  -        /** Required to satisfy UserInfo interface. Not used currently. */
  +
  +        /**
  +         * Required to satisfy UserInfo interface. Not used currently.
  +         */
           public boolean retry()
           {
               return true;
           }
   
  -        /** Required to satisfy UserInfo interface. Not used currently. */
  +        /**
  +         * Required to satisfy UserInfo interface. Not used currently.
  +         */
           public boolean prompt(String message)
           {
               return true;
  
  
  
  1.1                  jakarta-turbine-maven/src/plugins-build/deploy/src/java/org/apache/maven/deploy/AbstractDeployer.java
  
  Index: AbstractDeployer.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;
  
  /**
   * Base deployer from which all deployers are derived.
   *
   * @author <a href="mailto:jason@zenplex.com">Jason van Zyl</a>
   * @version $Id: AbstractDeployer.java,v 1.1 2003/01/05 01:33:47 jvanzyl Exp $
   */
  public abstract class AbstractDeployer
       implements Deployer
  {
      /**
       * Maven project.
       */
      private Project project;
  
      /**
       * Set the Maven project.
       */
      public void setProject(Project project)
      {
          this.project = project;
      }
  
      /**
       * Get the Maven project.
       */
      public Project getProject()
      {
          return project;
      }
  
      /**
       * @see Deployer#centralDeploy
       */
      public void centralDeploy(Artifact artifact)
      {
          deploy( getCentralHost(),
                  getLocalArtifactPath(artifact),
                  getCentralRemoteArtifactPath(artifact));
      }
  
      /**
       * @see Deployer#projectDeploy
       */
      public void projectDeploy(Artifact artifact)
      {
          deploy( getProjectHost(),
                  getLocalArtifactPath(artifact),
                  getProjectRemoteArtifactPath(artifact));
      }
  
  
      /**
       * Gets the projectHost attribute of the AbstractDeployer object
       */
      protected String getProjectHost()
      {
          return (String) getProject().getDistributionSite();
      }
  
      /**
       * Gets the centralHost attribute of the AbstractDeployer object
       */
      protected String getCentralHost()
      {
          return (String) getProject().getContext().getVariable( "maven.repo.central" );
      }
  
      /**
       * Gets the projectRemoteArtifactPath attribute of the AbstractDeployer
       * object
       */
      protected String getProjectRemoteArtifactPath(Artifact artifact)
      {
          return (String) getProject().getContext().getVariable( "maven.repo.central.directory" ) + artifact.generatePath();
      }
  
      /**
       * Gets the centralRemoteArtifactPath attribute of the AbstractDeployer
       * object
       */
      protected String getCentralRemoteArtifactPath(Artifact artifact)
      {
          return (String) getProject().getDistributionDirectory() + artifact.generatePath();
      }
  
      /**
       * Gets the localArtifactPath attribute of the AbstractDeployer object
       */
      protected String getLocalArtifactPath(Artifact artifact)
      {
          return artifact.getPath();
      }
  
  
      /**
       * Description of the Method
       */
      protected abstract void deploy(String host, String localPath, String remotePath);
  }
  
  
  
  1.1                  jakarta-turbine-maven/src/plugins-build/deploy/src/java/org/apache/maven/deploy/HttpDeployer.java
  
  Index: HttpDeployer.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 java.io.File;
  import java.io.IOException;
  import java.net.MalformedURLException;
  import java.net.URL;
  
  import org.apache.commons.httpclient.Credentials;
  import org.apache.commons.httpclient.Header;
  import org.apache.commons.httpclient.HostConfiguration;
  import org.apache.commons.httpclient.HttpClient;
  import org.apache.commons.httpclient.HttpException;
  import org.apache.commons.httpclient.UsernamePasswordCredentials;
  import org.apache.commons.httpclient.URI;
  import org.apache.commons.httpclient.URIException;
  import org.apache.commons.httpclient.methods.PutMethod;
  
  import org.apache.maven.project.Project;
  import org.apache.maven.repository.Artifact;
  
  /**
   * An HTTP deployer based the Commons HttpClient library.
   *
   * @author <a href="mailto:jason@zenplex.com">Jason van Zyl</a>
   * @version $Id: HttpDeployer.java,v 1.1 2003/01/05 01:33:47 jvanzyl Exp $
   * 
   * @todo still have to account for differing setups for people deploying to
   *       their own sites and to the central repository.
   *
   * @todo deal with proxies
   * @todo deal with authentication
   */
  public class HttpDeployer
       extends AbstractDeployer
  {
      /**
       * Description of the Method
       */
      protected void deploy(String host, String localPath, String remotePath)
      {
          URL url = null;
          try
          {
              url = new URL( remotePath );
          }
          catch (MalformedURLException murle)
          {
          }
  
          Credentials creds = null;
          
          //!! Need to deal with credentials.
          
          /*
          if (args.length >= 3)
          {
              creds = new UsernamePasswordCredentials(args[1], args[2]);
          }
          */
          
          //create a singular HttpClient object
          HttpClient client = new HttpClient();
  
          //establish a connection within 5 seconds
          client.setConnectionTimeout(5000);
  
          //set username / password
          if (creds != null)
          {
              client.getState().setCredentials(null, creds);
          }
  
          //
          HostConfiguration hc = new HostConfiguration();
          try
          {
              hc.setHost(new URI(url));
          }
          catch (URIException e)
          {
              throw new RuntimeException(e.toString());
          }
  
          //start a session with the webserver
          client.setHostConfiguration(hc);
  
          //create a method object
          PutMethod method = new PutMethod(url.getPath());
          
          try
          {
              method.setRequestBody( new File( localPath ) );
          }
          catch ( IOException e )
          {
              // we'll throw this up the stack.
          }
          
          //turn follow redirects off
          method.setFollowRedirects(false);
  
          //turn strict mode on
          method.setStrictMode(false);
  
          //execute the method
          try
          {
              client.executeMethod(method);
          }
          catch (HttpException he)
          {
              System.err.println("Http error connecting to '" + url + "'");
              System.err.println(he.getMessage());
              System.exit(-4);
          }
          catch (IOException ioe)
          {
              System.err.println("Unable to connect to '" + url + "'");
              System.exit(-3);
          }
  
          //get the request headers
          Header[] requestHeaders = method.getRequestHeaders();
  
          //get the response headers
          Header[] responseHeaders = method.getResponseHeaders();
  
          //get the response body
          byte[] responseBody = method.getResponseBody();
  
          method.releaseConnection();
      }
  }