brett       2004/07/06 07:42:49

  Added:       artifact/src/main/org/apache/maven/deploy/deployers
                        ScpExeDeployer.java
  Log:
  PR: MPARTIFACT-22
  
  Revision  Changes    Path
  1.1                  
maven-plugins/artifact/src/main/org/apache/maven/deploy/deployers/ScpExeDeployer.java
  
  Index: ScpExeDeployer.java
  ===================================================================
  package org.apache.maven.deploy.deployers;
  
  
  /* ====================================================================
   *   Copyright 2001-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.
   * ====================================================================
   */
  
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.apache.maven.deploy.DeployRequest;
  import org.apache.maven.deploy.RepositoryInfo;
  import org.apache.maven.deploy.exceptions.AuthenticationException;
  import org.apache.maven.deploy.exceptions.TransferFailedException;
  
  
  /**
   * SCP deployer using "external" scp program.  To allow for
   * ssh-agent type behavior.  Also provides backwards compatible 
   * support for those using the 
   * maven.scp.executable/maven.scp.args
   * and maven.ssh.executable/maven.ssh.args
   * 
   * 
   */
  public class ScpExeDeployer extends AbstractDeployer
  {
  
      public final static String PROTOCOL = "scpexe://";
      private RepositoryInfo repositoryInfo = null;
  
      private String cmd = null;
  
      private static final Log LOG = LogFactory.getLog(ScpExeDeployer.class);
  
      /* (non-Javadoc)
       * @see 
org.apache.maven.deploy.deployers.Deployer#init(org.apache.maven.deploy.HostInfo)
       */
      public void init(RepositoryInfo repoInfo) throws AuthenticationException
      {
        repositoryInfo = repoInfo;
      }
  
      private RepositoryInfo getRepositoryInfo() {
        return repositoryInfo;
      }
      
      /**
       * @see org.apache.maven.deploy.deployers.Deployer#release()
       */
      public void release()
      {
      }
  
      /**
       * @see 
org.apache.maven.deploy.deployers.Deployer#deploy(org.apache.maven.deploy.DeployRequest)
       */
      public void deploy(DeployRequest request) throws TransferFailedException
      {
          String mkdirCmd =
              "mkdir -p "
                  + getRepositoryInfo().getBasedir()
                  + "/"
                  + request.dirname()
                  + "\n";
  
          executeSimpleCommand(mkdirCmd);
  
          doCopy(request);
  
          if (getRepositoryInfo().getGroup() != null)
          {
              String chgrpCmd =
                  "chgrp "
                      + getRepositoryInfo().getGroup()
                      + " "
                      + getRepositoryInfo().getBasedir()
                      + "/"
                      + request.getDestFile()
                      + "\n";
  
              executeSimpleCommand(chgrpCmd);
          }
      }
      
      private String arrayToString(String[] array) {
        StringBuffer sb = new StringBuffer();
        for (int i=0; i<array.length; i++) {
                sb.append(array[i]);
                sb.append(" ");
        }
        return sb.toString();
      }
      
      private void executeSimpleCommand(String cmd) throws TransferFailedException {
          String args = getRepositoryInfo().getSshArgs();
          if (args == null) {
                args = "";
          }
        String[] sshCmd = { getRepositoryInfo().getSshExe(), 
                args,
                        getRepositoryInfo().getUserName() + "@" + 
getRepositoryInfo().getHost(),
                        cmd };
        try {
                if (LOG.isDebugEnabled()) {
                        LOG.debug("Executing command: " + arrayToString(sshCmd));
                }
                        Process p = Runtime.getRuntime().exec(sshCmd);
                        p.waitFor();
                } catch (IOException e) {
                        LOG.error("Error executing command: " + cmd);
                        throw new TransferFailedException("Error executing command: ", 
e);
                } catch (InterruptedException e) {
                        LOG.error("Error executing command: " + cmd);
                        throw new TransferFailedException("Error executing command: ", 
e);
                }
      }
      
      private void doCopy(DeployRequest request) throws TransferFailedException {
         String srcFile = request.getSrcFile();
           String destFile = getRepositoryInfo().getBasedir() + "/" + 
request.getDestFile();
           String dest = getRepositoryInfo().getUserName() + "@" + 
getRepositoryInfo().getHost() + ":" + destFile;
           String args = getRepositoryInfo().getScpArgs();
           if (args == null) {
                args = "";
           }
  
           if (destFile.indexOf("SNAPSHOT") > 0) {
                // If an old SNAPSHOT exists, remove it
                executeSimpleCommand("rm -r " + destFile);
           }
  
           String[] scpCmd = { getRepositoryInfo().getScpExe(), 
                        args,
                        srcFile,
                                dest };
                try {
                        if (LOG.isDebugEnabled()) {
                                LOG.debug("Executing command: " + 
arrayToString(scpCmd));
                        }
                        Process p = Runtime.getRuntime().exec(scpCmd);
                    // any error message?
                    StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(), 
"ERROR");
  
                    // any output?
                    StreamGobbler outputGobbler = new 
StreamGobbler(p.getInputStream(), "OUTPUT");
  
                    // kick them off
                    errorGobbler.start();
                    outputGobbler.start();
  
                        p.waitFor();
                } catch (IOException e) {
                        LOG.error("Error executing command: " + cmd);
                        throw new TransferFailedException("Error executing command: ", 
e);
                } catch (InterruptedException e) {
                        LOG.error("Error executing command: " + cmd);
                        throw new TransferFailedException("Error executing command: ", 
e);
                }
        
      }
      
      private class StreamGobbler extends Thread
        {
            private InputStream is;
            private String type;
            
                public StreamGobbler(InputStream is, String type) {
                    this.is = is;
                    this.type = type;
                }       
        
                public void run() {
                    try {
                        InputStreamReader isr = new InputStreamReader(is);
                        BufferedReader br = new BufferedReader(isr);
                        String line = null;
                        while ((line = br.readLine()) != null) {
                            LOG.debug(type + ">" + line);
                        }
                        br.close();
                        isr.close();
        
                    } catch (IOException ioe) {
                        ioe.printStackTrace();
                    }
                }
        }
      
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to