package jp.gr.java_conf.skrb.game.trivia.mclient; import java.io.IOException; import java.net.MalformedURLException; import java.util.Arrays; import java.util.Set; import javax.management.Attribute; import javax.management.AttributeList; import javax.management.MBeanAttributeInfo; import javax.management.MBeanConstructorInfo; import javax.management.MBeanInfo; import javax.management.MBeanNotificationInfo; import javax.management.MBeanOperationInfo; import javax.management.MBeanParameterInfo; import javax.management.MBeanServer; import javax.management.MBeanServerConnection; import javax.management.MBeanServerFactory; import javax.management.ObjectInstance; import javax.management.ObjectName; import javax.management.AttributeNotFoundException; import javax.management.InstanceAlreadyExistsException; import javax.management.InstanceNotFoundException; import javax.management.IntrospectionException; import javax.management.InvalidAttributeValueException; import javax.management.MalformedObjectNameException; import javax.management.MBeanException; import javax.management.MBeanRegistrationException; import javax.management.NotCompliantMBeanException; import javax.management.ReflectionException; import javax.management.remote.JMXServiceURL; import javax.management.remote.JMXConnector; import javax.management.remote.JMXConnectorFactory; import jp.gr.java_conf.skrb.game.trivia.mserver.TriviaMBeanStarter; public class TriviaMBeanRemoteTest1 { public static final String TRIVIA_SERVER_ADDRESS = "service:jmx:rmi:///jndi/rmi://localhost/trivia"; private MBeanServerConnection connection; public TriviaMBeanRemoteTest1() { try { JMXServiceURL url = new JMXServiceURL(TRIVIA_SERVER_ADDRESS); JMXConnector connector = JMXConnectorFactory.connect(url, null); connection = connector.getMBeanServerConnection(); queryMBeans(); } catch (MalformedURLException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } } private void queryMBeans() { try { Set mbeans = connection.queryMBeans(null, null); for (Object obj: mbeans) { ObjectInstance objInstance = (ObjectInstance)obj; System.out.println("Name: " + objInstance.getObjectName()); System.out.println("Class: " + objInstance.getClassName()); showMBeanInfo(objInstance.getObjectName()); } System.out.println(); } catch (IOException ex) { ex.printStackTrace(); } } private void showMBeanInfo(ObjectName name) { try { MBeanInfo info = connection.getMBeanInfo(name); String description = info.getDescription(); System.out.println("Description: " + description); showMBeanAttributeInfo(info); showMBeanConstructorInfo(info); showMBeanOperationInfo(info); showMBeanNotificationInfo(info); } catch (IntrospectionException ex) { ex.printStackTrace(); } catch (ReflectionException ex) { ex.printStackTrace(); } catch (InstanceNotFoundException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } } private void showMBeanAttributeInfo(MBeanInfo info) { MBeanAttributeInfo[] attributes = info.getAttributes(); if (attributes.length > 0) { System.out.println("Attributes:"); for (MBeanAttributeInfo attribute: attributes) { System.out.println(" Name: " + attribute.getName()); System.out.println(" Description: " + attribute.getDescription()); System.out.println(" Type: " + attribute.getType()); if (attribute.isReadable()) { if (attribute.isWritable()) { System.out.println(" Access: RW"); } else { System.out.println(" Access: RO"); } } else { if (attribute.isWritable()) { System.out.println(" Access: WO"); } } System.out.println(); } } } private void showMBeanConstructorInfo(MBeanInfo info) { MBeanConstructorInfo[] constructors = info.getConstructors(); if (constructors.length > 0) { System.out.println("Constructors:"); for (MBeanConstructorInfo constructor: constructors) { System.out.println(" Name: " + constructor.getName()); System.out.println(" Description: " + constructor.getDescription()); System.out.println(" Signature: "); MBeanParameterInfo[] arguments = constructor.getSignature(); for (MBeanParameterInfo argument: arguments) { System.out.println(" Name: " + argument.getName() + " Type: " + argument.getType()); } System.out.println(); } } } private void showMBeanOperationInfo(MBeanInfo info) { MBeanOperationInfo[] operations = info.getOperations(); if (operations.length > 0) { System.out.println("Operations:"); for (MBeanOperationInfo operation: operations) { System.out.println(" Name: " + operation.getName()); System.out.println(" Description: " + operation.getDescription()); System.out.println(" Signature: "); MBeanParameterInfo[] arguments = operation.getSignature(); for (MBeanParameterInfo argument: arguments) { System.out.println(" Name: " + argument.getName() + " Type: " + argument.getType()); } System.out.println(" Return Type: " + operation.getReturnType()); System.out.println(); } } } private void showMBeanNotificationInfo(MBeanInfo info) { MBeanNotificationInfo[] notifications = info.getNotifications(); if (notifications.length > 0) { System.out.println("Notifications:"); for (MBeanNotificationInfo notification: notifications) { System.out.println(" Name: " + notification.getName()); System.out.println(" Description: " + notification.getDescription()); System.out.println(" NotifType: "); String[] types = notification.getNotifTypes(); for (String type: types) { System.out.println(" Type: " + type); } System.out.println(); } } } public static void main(String[] args) { new TriviaMBeanRemoteTest1(); } }