1 16 package org.jmanage.cmdui.commands; 17 18 import org.jmanage.cmdui.CommandHandler; 19 import org.jmanage.cmdui.HandlerContext; 20 import org.jmanage.cmdui.CommandConstants; 21 import org.jmanage.cmdui.util.Out; 22 import org.jmanage.cmdui.util.Table; 23 import org.jmanage.core.services.MBeanService; 24 import org.jmanage.core.services.ServiceFactory; 25 import org.jmanage.core.management.ObjectInfo; 26 import org.jmanage.core.management.ObjectOperationInfo; 27 import org.jmanage.core.management.ObjectAttributeInfo; 28 import org.jmanage.core.management.ObjectParameterInfo; 29 import org.jmanage.core.util.Expression; 30 31 37 public class InfoHandler implements CommandHandler { 38 39 44 public boolean execute(HandlerContext context) { 45 46 String [] args = context.getCommand().getArgs(); 47 if(args.length != 1){ 48 usage(); 49 return false; 50 } 51 52 Expression expression = new Expression(args[0]); 53 if(expression.getAppName() == null || expression.getMBeanName() == null){ 54 usage(); 55 return false; 56 } 57 58 MBeanService service = ServiceFactory.getMBeanService(); 59 ObjectInfo objectInfo = 60 service.getMBeanInfo(context.getServiceContext( 61 expression.getAppName(), expression.getMBeanName())); 62 printObjectInfo(objectInfo); 63 return true; 64 } 65 66 public String getShortHelp() { 67 return "Display information about the mbean."; 68 } 69 70 public void help() { 71 Out.println(getShortHelp()); 72 Out.println("Usage:"); 73 Out.println(CommandConstants.INFO + " <application name>/<mbean name>"); 74 Out.println("Examples:"); 75 Out.println(CommandConstants.INFO + " myApp/myMBean"); 76 Out.println(CommandConstants.INFO + " myApp/jmanage:name=TestMBean"); 77 } 78 79 private void usage(){ 80 Out.println("Invalid arguments"); 81 help(); 82 } 83 84 private void printObjectInfo(ObjectInfo objectInfo){ 85 Out.println(); 86 Out.println("Object Name: " + objectInfo.getObjectName()); 87 Out.println("Class Name : " + objectInfo.getClassName()); 88 Out.println("Description: " + objectInfo.getDescription()); 89 printAttributes(objectInfo.getAttributes()); 90 printOperations(objectInfo.getOperations()); 91 } 92 93 private void printAttributes(ObjectAttributeInfo[] attributes) { 94 if(attributes.length == 0) 95 return; 96 97 Out.println(); 98 Out.println("Attributes:"); 99 Table table = new Table(4); 100 for(int i=0; i<attributes.length; i++){ 101 table.add(attributes[i].getName(), 102 attributes[i].getDisplayType(), 103 attributes[i].getReadWrite(), 104 attributes[i].getDescription()); 105 } 106 table.print(); 107 } 108 109 private void printOperations(ObjectOperationInfo[] operations) { 110 if(operations.length == 0) 111 return; 112 113 Out.println(); 114 Out.println("Operations:"); 115 Table table = new Table(3); 116 for(int i=0; i<operations.length; i++){ 117 table.add(operations[i].getName()+ "(" + 118 signature(operations[i].getSignature()) + ")", 119 operations[i].getDisplayReturnType(), 120 operations[i].getDescription()); 121 } 122 table.print(); 123 } 124 125 private String signature(ObjectParameterInfo[] signature){ 126 StringBuffer buff = new StringBuffer (); 127 for(int i=0; i<signature.length; i++){ 128 if(i > 0){ 129 buff.append(", "); 130 } 131 buff.append(signature[i].getDisplayType() + " " + signature[i].getName()); 132 } 133 return buff.toString(); 134 } 135 } 136 | Popular Tags |