Skip to content

Commit

Permalink
review code.
Browse files Browse the repository at this point in the history
  • Loading branch information
lijiahangmax committed Sep 20, 2022
1 parent d9000d0 commit 8fdc3b8
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public class SessionHolder {

public final JSch ch;

public SessionHolder() {
this(new JSch());
}

public SessionHolder(JSch ch) {
Valid.notNull(ch, "jsch is null");
this.ch = ch;
Expand All @@ -45,9 +49,9 @@ public static SessionHolder getHolder() {
/**
* 设置日志等级
*/
public static void setLogger(SessionLogger logger) {
public void setLogger(SessionLogger logger) {
int loggerLevel = logger.getLevel();
JSch.setLogger(new Logger() {
ch.setInstanceLogger(new Logger() {
@Override
public boolean isEnabled(int level) {
return loggerLevel <= level;
Expand All @@ -60,26 +64,6 @@ public void log(int level, String message) {
});
}

/**
* 获取配置信息
*
* @param key key
* @return value
*/
public static String getConfig(String key) {
return JSch.getConfig(key);
}

/**
* 设置配置信息
*
* @param key key
* @param value value
*/
public static void setConfig(String key, String value) {
JSch.setConfig(key, value);
}

/**
* 添加公钥登陆文件
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,26 @@ public SessionStore daemonThread(boolean daemon) {
return this;
}

/**
* 获取配置信息
*
* @param key key
* @return value
*/
public String getConfig(String key) {
return session.getConfig(key);
}

/**
* 设置配置信息
*
* @param key key
* @param value value
*/
public void setConfig(String key, String value) {
session.setConfig(key, value);
}

/**
* 建立连接
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void init() {

@Test
public void ls() {
SessionHolder.setLogger(SessionLogger.ERROR);
SessionHolder.HOLDER.setLogger(SessionLogger.ERROR);
CommandExecutor e = s.getCommandExecutor("ls -la /root");
e.callback(exe -> {
System.out.println("end....");
Expand All @@ -45,7 +45,7 @@ public void ls() {

@Test
public void echo() {
SessionHolder.setLogger(SessionLogger.INFO);
SessionHolder.HOLDER.setLogger(SessionLogger.INFO);
CommandExecutor e = s.getCommandExecutor("echo $PATH");
e.inherit();
e.streamHandler(ReaderLineConsumer.getDefaultPrint());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class SftpExecutorTests {

@Before
public void before() {
SessionHolder.setLogger(SessionLogger.ERROR);
SessionHolder.HOLDER.setLogger(SessionLogger.ERROR);
this.e = SessionHolder.HOLDER.getSession("192.168.146.230", "root")
.password("admin123")
.timeout(20000)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
public class ShellExecutorTests {

public static void main(String[] args) {
SessionHolder.setLogger(SessionLogger.INFO);
SessionHolder.HOLDER.setLogger(SessionLogger.INFO);
ShellExecutor e = SessionHolder.HOLDER.getSession("192.168.146.230", "root")
.password("admin123")
// ShellExecutor e = SessionHolder.getSession("192.168.146.230", "root")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void before() {

@Test
public void ls() {
SessionHolder.setLogger(SessionLogger.ERROR);
SessionHolder.HOLDER.setLogger(SessionLogger.ERROR);
CommandExecutor e = c.getCommandExecutor("ls -la /root");
e.callback(exe -> {
System.out.println("end....");
Expand All @@ -45,7 +45,7 @@ public void ls() {

@Test
public void echo() {
SessionHolder.setLogger(SessionLogger.INFO);
SessionHolder.HOLDER.setLogger(SessionLogger.INFO);
CommandExecutor e = c.getCommandExecutor("echo $PATH");
e.inherit();
e.streamHandler(ReaderLineConsumer.getDefaultPrint());
Expand Down

0 comments on commit 8fdc3b8

Please sign in to comment.