From 8fdc3b85b7b90b9f9dd2459bac08c8846a7a9e16 Mon Sep 17 00:00:00 2001 From: lijiahang Date: Tue, 20 Sep 2022 18:03:04 +0800 Subject: [PATCH] review code. --- .../net/remote/channel/SessionHolder.java | 28 ++++--------------- .../net/remote/channel/SessionStore.java | 20 +++++++++++++ .../remote/channel/CommandExecutorTests.java | 4 +-- .../net/remote/channel/SftpExecutorTests.java | 2 +- .../remote/channel/ShellExecutorTests.java | 2 +- .../net/remote/connection/CommandTests.java | 4 +-- 6 files changed, 32 insertions(+), 28 deletions(-) diff --git a/orion-net/src/main/java/com/orion/net/remote/channel/SessionHolder.java b/orion-net/src/main/java/com/orion/net/remote/channel/SessionHolder.java index 0d87a088..e82e31c8 100644 --- a/orion-net/src/main/java/com/orion/net/remote/channel/SessionHolder.java +++ b/orion-net/src/main/java/com/orion/net/remote/channel/SessionHolder.java @@ -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; @@ -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; @@ -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); - } - /** * 添加公钥登陆文件 * diff --git a/orion-net/src/main/java/com/orion/net/remote/channel/SessionStore.java b/orion-net/src/main/java/com/orion/net/remote/channel/SessionStore.java index 5c193615..a75e7eb0 100644 --- a/orion-net/src/main/java/com/orion/net/remote/channel/SessionStore.java +++ b/orion-net/src/main/java/com/orion/net/remote/channel/SessionStore.java @@ -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); + } + /** * 建立连接 * diff --git a/orion-net/src/test/java/com/orion/net/remote/channel/CommandExecutorTests.java b/orion-net/src/test/java/com/orion/net/remote/channel/CommandExecutorTests.java index 7558b983..03ac7e7b 100644 --- a/orion-net/src/test/java/com/orion/net/remote/channel/CommandExecutorTests.java +++ b/orion-net/src/test/java/com/orion/net/remote/channel/CommandExecutorTests.java @@ -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...."); @@ -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()); diff --git a/orion-net/src/test/java/com/orion/net/remote/channel/SftpExecutorTests.java b/orion-net/src/test/java/com/orion/net/remote/channel/SftpExecutorTests.java index eef670cf..0829635b 100644 --- a/orion-net/src/test/java/com/orion/net/remote/channel/SftpExecutorTests.java +++ b/orion-net/src/test/java/com/orion/net/remote/channel/SftpExecutorTests.java @@ -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) diff --git a/orion-net/src/test/java/com/orion/net/remote/channel/ShellExecutorTests.java b/orion-net/src/test/java/com/orion/net/remote/channel/ShellExecutorTests.java index 77a1a670..169950ca 100644 --- a/orion-net/src/test/java/com/orion/net/remote/channel/ShellExecutorTests.java +++ b/orion-net/src/test/java/com/orion/net/remote/channel/ShellExecutorTests.java @@ -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") diff --git a/orion-net/src/test/java/com/orion/net/remote/connection/CommandTests.java b/orion-net/src/test/java/com/orion/net/remote/connection/CommandTests.java index 7abead01..3dc05d26 100644 --- a/orion-net/src/test/java/com/orion/net/remote/connection/CommandTests.java +++ b/orion-net/src/test/java/com/orion/net/remote/connection/CommandTests.java @@ -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...."); @@ -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());