From 5b7b4f6a1b642fa3d157511ffc6ffcf5b9c1209f Mon Sep 17 00:00:00 2001 From: Ng Yat Yan Date: Fri, 6 Dec 2024 14:06:55 +0800 Subject: [PATCH] V2.2.1 Allow jmx exchange in xmpp communication --- pom.xml | 2 +- .../java/com/example/sshd/service/EchoComponent.java | 9 ++++++++- .../java/com/example/sshd/service/JmxClientService.java | 1 - src/main/java/com/example/sshd/service/ReplyService.java | 4 +++- 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/pom.xml b/pom.xml index 68b08b3..d9a0a9e 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 com.example.sshd echo-sshd-server - 2.2.0 + 2.2.1 ECHO SSH SERVER Learning Apache Mina SSHD library diff --git a/src/main/java/com/example/sshd/service/EchoComponent.java b/src/main/java/com/example/sshd/service/EchoComponent.java index 4cf7d41..80e54f5 100644 --- a/src/main/java/com/example/sshd/service/EchoComponent.java +++ b/src/main/java/com/example/sshd/service/EchoComponent.java @@ -27,6 +27,7 @@ public class EchoComponent extends AbstractComponent { public static final String CONST_OPERATION_ADD_USER = "adduser"; public static final String CONST_OPERATION_CHANGE_USER_PASSWORD = "chgpasswd"; public static final String CONST_OPERATION_DELETE_USER = "deluser"; + public static final String CONST_OPERATION_JMX_CLIENT = "jmx_client"; @Autowired XmppComponentConfig xmppComponentConfig; @@ -34,6 +35,9 @@ public class EchoComponent extends AbstractComponent { @Autowired ReplyService replyService; + @Autowired + JmxClientService jmxClientService; + @Autowired @Qualifier("userAdminCache") private volatile Cache userAdminCache; @@ -93,7 +97,7 @@ public class EchoComponent extends AbstractComponent { logger.info("[handleMessage] -- RECEIVED -- {}", inMsg); try { if (StringUtils.isNotBlank(inMsg.getBody())) { - String[] commandParts = StringUtils.split(inMsg.getBody(), ' '); + String[] commandParts = StringUtils.split(inMsg.getBody().trim(), ' '); switch (commandParts[0]) { case CONST_OPERATION_ADD_USER: if (commandParts.length == 3) @@ -113,6 +117,9 @@ public class EchoComponent extends AbstractComponent { else doEcho(inMsg, "chgpasswd "); break; + case CONST_OPERATION_JMX_CLIENT: + doEcho(inMsg, jmxClientService.process(commandParts)); + break; default: doEcho(inMsg, replyService.executeShellCommand(inMsg.getBody().trim())); break; diff --git a/src/main/java/com/example/sshd/service/JmxClientService.java b/src/main/java/com/example/sshd/service/JmxClientService.java index 688a96c..a832d46 100644 --- a/src/main/java/com/example/sshd/service/JmxClientService.java +++ b/src/main/java/com/example/sshd/service/JmxClientService.java @@ -35,7 +35,6 @@ public class JmxClientService { try { if (args.length > 2) { Runtime.getRuntime().freeMemory(); - System.out.println("Connection to JMX kafka..."); JMXServiceURL url = new JMXServiceURL(args[1]); JMXConnector jmxc = JMXConnectorFactory.connect(url, null); MBeanServerConnection mbsc = jmxc.getMBeanServerConnection(); diff --git a/src/main/java/com/example/sshd/service/ReplyService.java b/src/main/java/com/example/sshd/service/ReplyService.java index ac041a8..dfebcc4 100644 --- a/src/main/java/com/example/sshd/service/ReplyService.java +++ b/src/main/java/com/example/sshd/service/ReplyService.java @@ -128,7 +128,7 @@ public class ReplyService { return false; } - public String executeShellCommand(String command) throws IOException { + public String executeShellCommand(String command) { String cmdHash = DigestUtils.md5Hex(command.trim()).toUpperCase(); logger.info("[{}] Execute cmd for real: {}", cmdHash, command.trim()); ByteArrayOutputStream tempOut = new ByteArrayOutputStream(); @@ -142,6 +142,8 @@ public class ReplyService { return new String(tempOut.toByteArray()).replace("\n", "\r\n"); } catch (ExecuteException e) { logger.info("[{}] Execute cmd failed: {}", cmdHash, command.trim(), e); + } catch (IOException e) { + logger.info("[{}] Execute cmd failed: {}", cmdHash, command.trim(), e); } return null; }