From 97995123f3a82ecf6ed4afe21d57db2ec330577c Mon Sep 17 00:00:00 2001 From: brett Date: Tue, 6 Jul 2004 14:42:49 +0000 Subject: [PATCH] PR: MPARTIFACT-22 git-svn-id: https://svn.apache.org/repos/asf/maven/maven-1/plugins/trunk@115676 13f79535-47bb-0310-9956-ffa450edef68 --- .../deploy/deployers/ScpExeDeployer.java | 207 ++++++++++++++++++ 1 file changed, 207 insertions(+) create mode 100755 artifact/src/main/org/apache/maven/deploy/deployers/ScpExeDeployer.java diff --git a/artifact/src/main/org/apache/maven/deploy/deployers/ScpExeDeployer.java b/artifact/src/main/org/apache/maven/deploy/deployers/ScpExeDeployer.java new file mode 100755 index 00000000..ea73d1e6 --- /dev/null +++ b/artifact/src/main/org/apache/maven/deploy/deployers/ScpExeDeployer.java @@ -0,0 +1,207 @@ +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 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(); + } + } + } + +}