001package org.apache.maven.wagon.providers.ssh; 002/* 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, 014 * software distributed under the License is distributed on an 015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 016 * KIND, either express or implied. See the License for the 017 * specific language governing permissions and limitations 018 * under the License. 019 */ 020 021import org.apache.sshd.server.Command; 022import org.apache.sshd.server.Environment; 023import org.apache.sshd.server.ExitCallback; 024import org.codehaus.plexus.util.FileUtils; 025import org.codehaus.plexus.util.cli.CommandLineUtils; 026import org.codehaus.plexus.util.cli.Commandline; 027 028import java.io.File; 029import java.io.IOException; 030import java.io.InputStream; 031import java.io.OutputStream; 032 033/** 034 * @author Olivier Lamy 035 */ 036public class ShellCommand implements Command 037{ 038protected static final int OK = 0; 039 040 protected static final int WARNING = 1; 041 042 protected static final int ERROR = 2; 043 044 private InputStream in; 045 046 private OutputStream out; 047 048 private OutputStream err; 049 050 private ExitCallback callback; 051 052 private Thread thread; 053 054 private String commandLine; 055 056 public ShellCommand( String commandLine ) 057 { 058 this.commandLine = commandLine; 059 } 060 061 public void setInputStream( InputStream in ) 062 { 063 this.in = in; 064 } 065 066 public void setOutputStream( OutputStream out ) 067 { 068 this.out = out; 069 } 070 071 public void setErrorStream( OutputStream err ) 072 { 073 this.err = err; 074 } 075 076 public void setExitCallback( ExitCallback callback ) 077 { 078 this.callback = callback; 079 } 080 081 public void start( Environment env ) 082 throws IOException 083 { 084 File tmpFile = File.createTempFile( "wagon", "test-sh" ); 085 tmpFile.deleteOnExit(); 086 int exitValue = 0; 087 CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer(); 088 CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer(); 089 try 090 { 091 092 // hackhish defaut commandline tools not support ; or && so write a file with the script 093 // and "/bin/sh -e " + tmpFile.getPath(); 094 FileUtils.fileWrite( tmpFile, commandLine ); 095 096 Commandline cl = new Commandline(); 097 cl.setExecutable( "/bin/sh" ); 098 //cl.createArg().setValue( "-e" ); 099 //cl.createArg().setValue( tmpFile.getPath() ); 100 cl.createArg().setFile( tmpFile ); 101 102 exitValue = CommandLineUtils.executeCommandLine( cl, stdout, stderr ); 103 System.out.println( "exit value " + exitValue ); 104 /* 105 if ( exitValue == 0 ) 106 { 107 out.write( stdout.getOutput().getBytes() ); 108 out.write( '\n' ); 109 out.flush(); 110 111 } 112 else 113 { 114 out.write( stderr.getOutput().getBytes() ); 115 out.write( '\n' ); 116 out.flush(); 117 118 }*/ 119 120 } 121 catch ( Exception e ) 122 { 123 exitValue = ERROR; 124 e.printStackTrace(); 125 } 126 finally 127 { 128 deleteQuietly( tmpFile ); 129 if ( exitValue != 0 ) 130 { 131 err.write( stderr.getOutput().getBytes() ); 132 err.write( '\n' ); 133 err.flush(); 134 callback.onExit( exitValue, stderr.getOutput() ); 135 } 136 else 137 { 138 out.write( stdout.getOutput().getBytes() ); 139 out.write( '\n' ); 140 out.flush(); 141 callback.onExit( exitValue, stdout.getOutput() ); 142 } 143 144 } 145 /* 146 out.write( exitValue ); 147 out.write( '\n' ); 148 149 */ 150 out.flush(); 151 } 152 153 public void destroy() 154 { 155 156 } 157 158 private void deleteQuietly( File f ) 159 { 160 161 try 162 { 163 f.delete(); 164 } 165 catch ( Exception e ) 166 { 167 // ignore 168 } 169 } 170}