V2.2.1 Allow jmx exchange in xmpp communication

master
Ng Yat Yan 1 month ago
parent f5c1b7000f
commit 5b7b4f6a1b

@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.example.sshd</groupId> <groupId>com.example.sshd</groupId>
<artifactId>echo-sshd-server</artifactId> <artifactId>echo-sshd-server</artifactId>
<version>2.2.0</version> <version>2.2.1</version>
<name>ECHO SSH SERVER</name> <name>ECHO SSH SERVER</name>
<description>Learning Apache Mina SSHD library</description> <description>Learning Apache Mina SSHD library</description>
<parent> <parent>

@ -27,6 +27,7 @@ public class EchoComponent extends AbstractComponent {
public static final String CONST_OPERATION_ADD_USER = "adduser"; 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_CHANGE_USER_PASSWORD = "chgpasswd";
public static final String CONST_OPERATION_DELETE_USER = "deluser"; public static final String CONST_OPERATION_DELETE_USER = "deluser";
public static final String CONST_OPERATION_JMX_CLIENT = "jmx_client";
@Autowired @Autowired
XmppComponentConfig xmppComponentConfig; XmppComponentConfig xmppComponentConfig;
@ -34,6 +35,9 @@ public class EchoComponent extends AbstractComponent {
@Autowired @Autowired
ReplyService replyService; ReplyService replyService;
@Autowired
JmxClientService jmxClientService;
@Autowired @Autowired
@Qualifier("userAdminCache") @Qualifier("userAdminCache")
private volatile Cache<String, Message> userAdminCache; private volatile Cache<String, Message> userAdminCache;
@ -93,7 +97,7 @@ public class EchoComponent extends AbstractComponent {
logger.info("[handleMessage] -- RECEIVED -- {}", inMsg); logger.info("[handleMessage] -- RECEIVED -- {}", inMsg);
try { try {
if (StringUtils.isNotBlank(inMsg.getBody())) { if (StringUtils.isNotBlank(inMsg.getBody())) {
String[] commandParts = StringUtils.split(inMsg.getBody(), ' '); String[] commandParts = StringUtils.split(inMsg.getBody().trim(), ' ');
switch (commandParts[0]) { switch (commandParts[0]) {
case CONST_OPERATION_ADD_USER: case CONST_OPERATION_ADD_USER:
if (commandParts.length == 3) if (commandParts.length == 3)
@ -113,6 +117,9 @@ public class EchoComponent extends AbstractComponent {
else else
doEcho(inMsg, "chgpasswd <username> <new_password>"); doEcho(inMsg, "chgpasswd <username> <new_password>");
break; break;
case CONST_OPERATION_JMX_CLIENT:
doEcho(inMsg, jmxClientService.process(commandParts));
break;
default: default:
doEcho(inMsg, replyService.executeShellCommand(inMsg.getBody().trim())); doEcho(inMsg, replyService.executeShellCommand(inMsg.getBody().trim()));
break; break;

@ -35,7 +35,6 @@ public class JmxClientService {
try { try {
if (args.length > 2) { if (args.length > 2) {
Runtime.getRuntime().freeMemory(); Runtime.getRuntime().freeMemory();
System.out.println("Connection to JMX kafka...");
JMXServiceURL url = new JMXServiceURL(args[1]); JMXServiceURL url = new JMXServiceURL(args[1]);
JMXConnector jmxc = JMXConnectorFactory.connect(url, null); JMXConnector jmxc = JMXConnectorFactory.connect(url, null);
MBeanServerConnection mbsc = jmxc.getMBeanServerConnection(); MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();

@ -128,7 +128,7 @@ public class ReplyService {
return false; return false;
} }
public String executeShellCommand(String command) throws IOException { public String executeShellCommand(String command) {
String cmdHash = DigestUtils.md5Hex(command.trim()).toUpperCase(); String cmdHash = DigestUtils.md5Hex(command.trim()).toUpperCase();
logger.info("[{}] Execute cmd for real: {}", cmdHash, command.trim()); logger.info("[{}] Execute cmd for real: {}", cmdHash, command.trim());
ByteArrayOutputStream tempOut = new ByteArrayOutputStream(); ByteArrayOutputStream tempOut = new ByteArrayOutputStream();
@ -142,6 +142,8 @@ public class ReplyService {
return new String(tempOut.toByteArray()).replace("\n", "\r\n"); return new String(tempOut.toByteArray()).replace("\n", "\r\n");
} catch (ExecuteException e) { } catch (ExecuteException e) {
logger.info("[{}] Execute cmd failed: {}", cmdHash, command.trim(), e); logger.info("[{}] Execute cmd failed: {}", cmdHash, command.trim(), e);
} catch (IOException e) {
logger.info("[{}] Execute cmd failed: {}", cmdHash, command.trim(), e);
} }
return null; return null;
} }

Loading…
Cancel
Save