package org.ngbw.utils; import java.util.Scanner; import javax.management.JMX; import javax.management.MBeanServerConnection; import javax.management.ObjectName; import javax.management.remote.JMXConnector; import javax.management.remote.JMXConnectorFactory; import javax.management.remote.JMXServiceURL; public class JMXClient { public static void main ( String[] args ) throws Exception { if (args.length == 0) { echo("Useage: JMXClient port_number"); echo("port_number is the port that this JMXClient is going to connet to"); System.exit(1); } int portNum = 9999; try { portNum = Integer.parseInt(args[0]); } catch ( NumberFormatException e ) { echo("Argument " + args[0] + " must be an integer."); System.exit(2); } echo("Connecting to port: " + portNum + " on local host...."); JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://:" + portNum + "/jmxrmi"); JMXConnector jmxc = JMXConnectorFactory.connect(url, null); // Create listener // //ClientListener listener = new ClientListener(); MBeanServerConnection mbsc = jmxc.getMBeanServerConnection(); ObjectName mxbeanName = new ObjectName("org.ngbw.utils:type=ToolRegistryControl"); ToolRegistryControlMXBean mxbeanProxy = JMX.newMXBeanProxy(mbsc, mxbeanName, ToolRegistryControlMXBean.class); Scanner scanner = new Scanner(System.in); while (true) { echo("**********************************"); echo("Please choose below options:"); echo("1 - check whether a specific tool is cloud eligible or not"); echo("2 - turn on/off cloud eligibility of a specific tool"); echo("3 - exit"); echo("----------------------------------\n"); //while (!scanner.hasNext()) //{ //} int choice = 0; try { String choiceStr = scanner.nextLine(); choice = Integer.parseInt(choiceStr); } catch ( NumberFormatException e ) { scanner.reset(); } if (choice == 3) { System.exit(0); } else if (choice == 1) { echo("Please enter tool name"); String toolName = null; while (!scanner.hasNext()) { } toolName = scanner.nextLine(); while (toolName == null || toolName.trim().isEmpty()) { toolName = scanner.nextLine(); } echo("Tool name is: " + toolName); try { echo(mxbeanProxy.isCloudEligible(toolName)); } catch ( Exception e ) { echo(e.getMessage()); } } else if (choice == 2) { echo("Please enter tool name"); //String toolName = scanner.nextLine(); String toolName = null; while (!scanner.hasNext()) { } toolName = scanner.nextLine(); if (toolName == null || toolName.trim().isEmpty()) { toolName = scanner.nextLine(); } echo("Tool name is: " + toolName); echo("Please enter true to turn on cloud eligibility or false to turn it off"); String trueFalse = scanner.nextLine(); while (!trueFalse.equals("true") && !trueFalse.equals("false")) { echo("The valid choise is true or false. Please enter true to turn on cloud eligibility or false to turn if off"); trueFalse = scanner.nextLine(); } boolean eligible = Boolean.parseBoolean(trueFalse); try { echo(mxbeanProxy.setCloudEligible(toolName, eligible)); } catch ( Exception e ) { echo(e.getMessage()); } } else { echo("Invalid choice.Please try again\n"); } } } private static void echo ( String msg ) { System.out.println(msg); } /* * public static class ClientListener implements NotificationListener { * public void handleNotification(Notification notification, * Object handback) { * echo("\nReceived notification:"); * echo("\tClassName: " + notification.getClass().getName()); * echo("\tSource: " + notification.getSource()); * echo("\tType: " + notification.getType()); * echo("\tMessage: " + notification.getMessage()); * if (notification instanceof AttributeChangeNotification) { * AttributeChangeNotification acn = * (AttributeChangeNotification) notification; * echo("\tAttributeName: " + acn.getAttributeName()); * echo("\tAttributeType: " + acn.getAttributeType()); * echo("\tNewValue: " + acn.getNewValue()); * echo("\tOldValue: " + acn.getOldValue()); * } * } * } */ }