Permalink
Browse files

Merge remote-tracking branch 'fork/21'

  • Loading branch information...
rultor committed Jun 24, 2015
2 parents e00c35b + 63ca8a8 commit c5a07100d9e6a3044f2e71c1f3c2cb97d667b4a8
@@ -0,0 +1,171 @@
/**
* Copyright (c) 2014-2015, jcabi.com
* 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) Neither the name of the jcabi.com nor
* the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS 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 COPYRIGHT HOLDER OR 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.
*/
package com.jcabi.ssh;
import com.jcabi.log.Logger;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.concurrent.TimeUnit;
/**
* Execution of a single command.
* @author Georgy Vlasov (wlasowegor@gmail.com)
* @version $Id$
* @since 1.4
*/
interface Execution {
/**
* Executes some command.
* @return Return code of the command.
* @throws IOException If fails
*/
int exec() throws IOException;
/**
* Execution of a command in an SSH session.
* @author Georgy Vlasov (wlasowegor@gmail.com)
* @version $Id$
* @since 1.4
*/
final class Default implements Execution {
/**
* Command.
*/
private final transient String command;
/**
* Stdin.
*/
private final transient InputStream stdin;
/**
* Stdout.
*/
private final transient OutputStream stdout;
/**
* Stderr.
*/
private final transient OutputStream stderr;
/**
* Session.
*/
private final transient Session session;
/**
* Uses an SSH session to execute a single command and disconnect
* immediately.
* @param cmd Command
* @param input Stdin (will be closed)
* @param out Stdout (will be closed)
* @param err Stderr (will be closed)
* @param sess SSH session (will be disconnected)
* @checkstyle ParameterNumberCheck (6 lines)
*/
Default(final String cmd, final InputStream input,
final OutputStream out, final OutputStream err,
final Session sess) {
this.command = cmd;
this.stdin = input;
this.stdout = out;
this.stderr = err;
this.session = sess;
}
/**
* Execute {@link #command} in {@link #session}.
* @return Exit code
* @throws IOException If fails
*/
@Override
public int exec() throws IOException {
try {
final ChannelExec channel = ChannelExec.class.cast(
this.session.openChannel("exec")
);
channel.setErrStream(this.stderr, false);
channel.setOutputStream(this.stdout, false);
channel.setInputStream(this.stdin, false);
channel.setCommand(this.command);
channel.connect();
Logger.info(this, "$ %s", this.command);
return this.exec(channel);
} catch (final JSchException ex) {
throw new IOException(ex);
} finally {
this.session.disconnect();
}
}
/**
* Exec this channel and return its exit code.
* @param channel The channel to exec
* @return Exit code (zero in case of success)
* @throws IOException If fails
*/
private int exec(final ChannelExec channel) throws IOException {
try {
return this.code(channel);
} finally {
channel.disconnect();
}
}
/**
* Wait until it's done and return its code.
* @param exec The channel
* @return The exit code
* @throws IOException If some IO problem inside
*/
@SuppressWarnings("PMD.AvoidCatchingGenericException")
private int code(final ChannelExec exec) throws IOException {
while (!exec.isClosed()) {
try {
this.session.sendKeepAliveMsg();
// @checkstyle IllegalCatch (1 line)
} catch (final Exception ex) {
throw new IOException(ex);
}
try {
TimeUnit.SECONDS.sleep(1L);
} catch (final InterruptedException ex) {
Thread.currentThread().interrupt();
throw new IOException(ex);
}
}
return exec.getExitStatus();
}
}
}
@@ -32,7 +32,6 @@
import com.jcabi.aspects.RetryOnFailure;
import com.jcabi.aspects.Tv;
import com.jcabi.log.Logger;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
@@ -68,6 +67,9 @@
* <p>It is highly recommended to use classes from {@link Shell} interface,
* they will simplify operations.</p>
*
* @todo #21:30min This class has common data, implementation of exec() and
* constructor validations with SSHByPassword class. Common functionality
* should be extracted into a separate module.
* @author Yegor Bugayenko (yegor@teamed.io)
* @version $Id$
* @since 1.0
@@ -214,25 +216,9 @@ public SSH(final String adr, final int prt,
public int exec(final String command, final InputStream stdin,
final OutputStream stdout, final OutputStream stderr)
throws IOException {
try {
final Session session = this.session();
try {
final ChannelExec channel = ChannelExec.class.cast(
session.openChannel("exec")
);
channel.setErrStream(stderr, false);
channel.setOutputStream(stdout, false);
channel.setInputStream(stdin, false);
channel.setCommand(command);
channel.connect();
Logger.info(this, "$ %s", command);
return this.exec(channel, session);
} finally {
session.disconnect();
}
} catch (final JSchException ex) {
throw new IOException(ex);
}
return new Execution.Default(
command, stdin, stdout, stderr, this.session()
).exec();
}
/**
@@ -244,49 +230,6 @@ public static String escape(final String arg) {
return String.format("'%s'", arg.replace("'", "'\\''"));
}
/**
* Exec this channel and return its exit code.
* @param channel The channel to exec
* @param session The session
* @return Exit code (zero in case of success)
* @throws IOException If fails
*/
private int exec(final ChannelExec channel, final Session session)
throws IOException {
try {
return this.code(channel, session);
} finally {
channel.disconnect();
}
}
/**
* Wait until it's done and return its code.
* @param exec The channel
* @param session The session
* @return The exit code
* @throws IOException If some IO problem inside
*/
@SuppressWarnings("PMD.AvoidCatchingGenericException")
private int code(final ChannelExec exec, final Session session)
throws IOException {
while (!exec.isClosed()) {
try {
session.sendKeepAliveMsg();
// @checkstyle IllegalCatch (1 line)
} catch (final Exception ex) {
throw new IOException(ex);
}
try {
TimeUnit.SECONDS.sleep(1L);
} catch (final InterruptedException ex) {
Thread.currentThread().interrupt();
throw new IOException(ex);
}
}
return exec.getExitStatus();
}
/**
* Create and return a session, connected.
* @return JSch session
Oops, something went wrong.

0 comments on commit c5a0710

Please sign in to comment.