1 16 package org.jmanage.cmdui.commands; 17 18 import org.jmanage.cmdui.CommandHandler; 19 import org.jmanage.cmdui.CommandConstants; 20 import org.jmanage.cmdui.HandlerContext; 21 import org.jmanage.cmdui.util.Out; 22 import org.jmanage.core.services.MBeanService; 23 import org.jmanage.core.services.ServiceFactory; 24 import org.jmanage.core.management.ObjectAttribute; 25 import org.jmanage.core.data.AttributeListData; 26 import org.jmanage.core.util.Expression; 27 import org.jmanage.util.StringUtils; 28 29 import java.util.*; 30 31 36 public class PrintHandler implements CommandHandler { 37 38 43 public boolean execute(HandlerContext context) { 44 45 String [] args = context.getCommand().getArgs(); 46 if(args.length == 0){ 47 usage(); 48 return false; 49 } 50 51 53 final Map appMBeanToAttributesMap = new HashMap(); 54 55 Expression expression = null; 56 for(int i=0; i<args.length; i++){ 57 expression = new Expression(args[i], expression); 58 if(expression.getAppName() == null || 59 expression.getMBeanName() == null || 60 expression.getTargetName() == null){ 61 usage(); 62 return false; 63 } 64 AppMBeanKey key = new AppMBeanKey(expression.getAppName(), 65 expression.getMBeanName()); 66 List attributes = (List)appMBeanToAttributesMap.get(key); 67 if(attributes == null){ 68 attributes = new LinkedList(); 69 appMBeanToAttributesMap.put(key, attributes); 70 } 71 attributes.add(expression.getTargetName()); 72 } 73 74 75 MBeanService service = ServiceFactory.getMBeanService(); 76 for(Iterator it=appMBeanToAttributesMap.keySet().iterator(); it.hasNext();){ 77 AppMBeanKey key = (AppMBeanKey)it.next(); 78 List attributes = (List)appMBeanToAttributesMap.get(key); 79 AttributeListData[] attributeValues = 80 service.getAttributes( 81 context.getServiceContext(key.appName, key.mbeanName), 82 StringUtils.listToStringArray(attributes), 83 false); 84 print(attributeValues); 85 } 86 Out.println(); 87 88 return true; 89 } 90 91 public String getShortHelp() { 92 return "Prints tab delimited value(s) for given mbean attribute(s)."; 93 } 94 95 public void help() { 96 Out.println(getShortHelp()); 97 Out.println("Usage:"); 98 Out.println(CommandConstants.PRINT + 99 " <application name>/<mbean name>/<attribute1> [attribute2] ..."); 100 Out.println(); 101 Out.println("Examples:"); 102 Out.println(CommandConstants.PRINT + 103 " myApp/myMBean/testAttr1 testAttr2"); 104 Out.println(CommandConstants.PRINT + 105 " myApp/jmanage:name=TestMBean/testAttr1 jmanage:name=Configuration/testAttr"); 106 } 107 108 private void print(AttributeListData[] attrValues){ 109 assert attrValues.length == 1; 110 for(Iterator it=attrValues[0].getAttributeList().iterator(); it.hasNext(); ){ 111 ObjectAttribute attribute= (ObjectAttribute)it.next(); 112 Out.print(attribute.getDisplayValue() + "\t"); 113 } 114 } 115 116 private void usage(){ 117 Out.println("Invalid arguments"); 118 help(); 119 } 120 121 private class AppMBeanKey{ 122 String appName; 123 String mbeanName; 124 125 AppMBeanKey(String appName, String mbeanName){ 126 this.appName = appName; 127 this.mbeanName = mbeanName; 128 } 129 130 public int hashCode(){ 131 return (appName.hashCode() * 31) + mbeanName.hashCode(); 132 } 133 134 public boolean equals(Object obj){ 135 if(obj instanceof AppMBeanKey){ 136 AppMBeanKey key = (AppMBeanKey)obj; 137 return key.appName.equals(this.appName) && 138 key.mbeanName.equals(this.mbeanName); 139 } 140 return false; 141 } 142 } 143 } 144 | Popular Tags |