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