Skip to content

Commit

Permalink
Merge remote-tracking branch 'fork/21'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Jun 24, 2015
2 parents e00c35b + 63ca8a8 commit c5a0710
Show file tree
Hide file tree
Showing 6 changed files with 624 additions and 63 deletions.
171 changes: 171 additions & 0 deletions src/main/java/com/jcabi/ssh/Execution.java
Original file line number Diff line number Diff line change
@@ -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 ([email protected])
* @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 ([email protected])
* @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();
}
}
}
69 changes: 6 additions & 63 deletions src/main/java/com/jcabi/ssh/SSH.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 ([email protected])
* @version $Id$
* @since 1.0
Expand Down Expand Up @@ -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();
}

/**
Expand All @@ -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
Expand Down
Loading

0 comments on commit c5a0710

Please sign in to comment.