mirror of
https://github.com/LamGC/Oracle-Sentry.git
synced 2025-04-30 06:37:42 +00:00
refactor: 调整类名和方法名, 添加 unused 关闭因编译器无法识别脚本调用而出现的无意义警告.
将 Channel 修改为 Session, execCommand 改为 createExecSession 可以防止用户误以为这是执行命令而不是创建命令执行会话(execCommand 并不是立即执行, 只是创建了执行会话).
This commit is contained in:
parent
ad33ff4795
commit
e0a9b933ca
@ -0,0 +1,103 @@
|
|||||||
|
package net.lamgc.oracle.sentry.oci.compute.ssh;
|
||||||
|
|
||||||
|
import net.lamgc.oracle.sentry.common.InputStreamWrapper;
|
||||||
|
import net.lamgc.oracle.sentry.common.OutputStreamWrapper;
|
||||||
|
import org.apache.sshd.client.channel.ChannelExec;
|
||||||
|
import org.apache.sshd.client.channel.ClientChannelEvent;
|
||||||
|
|
||||||
|
import java.io.Closeable;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.util.EnumSet;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SSH 执行会话.
|
||||||
|
* @author LamGC
|
||||||
|
*/
|
||||||
|
public final class CommandExecSession implements Closeable {
|
||||||
|
|
||||||
|
private final ChannelExec channelExec;
|
||||||
|
|
||||||
|
public CommandExecSession(ChannelExec channelExec) {
|
||||||
|
this.channelExec = channelExec;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行命令并等待程序执行完毕.
|
||||||
|
* @throws IOException 如果发送命令失败则抛出异常.
|
||||||
|
*/
|
||||||
|
public void exec() throws IOException {
|
||||||
|
exec(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行命令.
|
||||||
|
* @param async 是否需要异步, 如果为异步, 则本方法不等待命令执行完成就返回, 适用于需要向程序输入的时候使用.
|
||||||
|
* @throws IOException 如果发送命令时发生异常则抛出.
|
||||||
|
*/
|
||||||
|
public void exec(boolean async) throws IOException {
|
||||||
|
channelExec.open();
|
||||||
|
if (!async) {
|
||||||
|
waitFor();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 等待程序执行完毕.
|
||||||
|
* <p> 该方法等同于 {@code waitFor(0L)}.
|
||||||
|
*/
|
||||||
|
public void waitFor() {
|
||||||
|
waitFor(0L);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 等待程序执行完毕.
|
||||||
|
* @param timeout 超时时间, 0 为无限等待(单位: 毫秒).
|
||||||
|
*/
|
||||||
|
public void waitFor(long timeout) {
|
||||||
|
channelExec.waitFor(EnumSet.of(ClientChannelEvent.EXIT_STATUS, ClientChannelEvent.EXIT_SIGNAL), timeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取程序退出代码.
|
||||||
|
* <p> 如果程序未执行完毕, 本方法将无法获取退出代码.
|
||||||
|
* @return 如果程序执行完毕, 返回具体代码, 否则返回 {@code null}.
|
||||||
|
*/
|
||||||
|
public Integer exitCode() {
|
||||||
|
return channelExec.getExitStatus();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置输入流.
|
||||||
|
* <p> 设置待执行命令的输入流.
|
||||||
|
*/
|
||||||
|
public void setIn(InputStream in) {
|
||||||
|
channelExec.setIn(new InputStreamWrapper(in));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置标准输出流.
|
||||||
|
* <p> 对应待执行命令的 Stdout.
|
||||||
|
*/
|
||||||
|
public void setOut(OutputStream out) {
|
||||||
|
channelExec.setOut(new OutputStreamWrapper(out));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置错误输出流.
|
||||||
|
* <p> 如果命令使用到, 错误信息会从该输出流输出.
|
||||||
|
*/
|
||||||
|
public void setErr(OutputStream err) {
|
||||||
|
channelExec.setErr(new OutputStreamWrapper(err));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 关闭命令执行会话.
|
||||||
|
* @throws IOException 可能会引发的异常.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void close() throws IOException {
|
||||||
|
channelExec.close();
|
||||||
|
}
|
||||||
|
}
|
@ -1,47 +0,0 @@
|
|||||||
package net.lamgc.oracle.sentry.oci.compute.ssh;
|
|
||||||
|
|
||||||
import net.lamgc.oracle.sentry.common.InputStreamWrapper;
|
|
||||||
import net.lamgc.oracle.sentry.common.OutputStreamWrapper;
|
|
||||||
import org.apache.sshd.client.channel.ChannelExec;
|
|
||||||
import org.apache.sshd.client.channel.ClientChannelEvent;
|
|
||||||
|
|
||||||
import java.io.Closeable;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.OutputStream;
|
|
||||||
import java.util.EnumSet;
|
|
||||||
|
|
||||||
public final class SshExecChannel implements Closeable {
|
|
||||||
|
|
||||||
private final ChannelExec channelExec;
|
|
||||||
|
|
||||||
public SshExecChannel(ChannelExec channelExec) {
|
|
||||||
this.channelExec = channelExec;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void exec() throws IOException {
|
|
||||||
channelExec.open();
|
|
||||||
channelExec.waitFor(EnumSet.of(ClientChannelEvent.EXIT_STATUS, ClientChannelEvent.EXIT_SIGNAL), 0L);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Integer exitCode() {
|
|
||||||
return channelExec.getExitStatus();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setIn(InputStream in) {
|
|
||||||
channelExec.setIn(new InputStreamWrapper(in));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setOut(OutputStream out) {
|
|
||||||
channelExec.setOut(new OutputStreamWrapper(out));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setErr(OutputStream err) {
|
|
||||||
channelExec.setErr(new OutputStreamWrapper(err));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void close() throws IOException {
|
|
||||||
channelExec.close();
|
|
||||||
}
|
|
||||||
}
|
|
@ -7,6 +7,7 @@ import org.apache.sshd.sftp.client.SftpClientFactory;
|
|||||||
import java.io.Closeable;
|
import java.io.Closeable;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
public class SshSession implements Closeable {
|
public class SshSession implements Closeable {
|
||||||
|
|
||||||
private final ClientSession clientSession;
|
private final ClientSession clientSession;
|
||||||
@ -15,8 +16,8 @@ public class SshSession implements Closeable {
|
|||||||
this.clientSession = clientSession;
|
this.clientSession = clientSession;
|
||||||
}
|
}
|
||||||
|
|
||||||
public SshExecChannel execCommand(String command) throws IOException {
|
public CommandExecSession createExecSession(String command) throws IOException {
|
||||||
return new SshExecChannel(clientSession.createExecChannel(command));
|
return new CommandExecSession(clientSession.createExecChannel(command));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Loading…
Reference in New Issue
Block a user