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.CommandUtils; 23 import org.jmanage.core.services.MBeanService; 24 import org.jmanage.core.services.ServiceFactory; 25 import org.jmanage.core.data.AttributeListData; 26 import org.jmanage.core.util.Expression; 27 28 import java.util.StringTokenizer ; 29 30 42 public class SetAttributesHandler implements CommandHandler { 43 44 49 public boolean execute(HandlerContext context) { 50 51 String [] args = context.getCommand().getArgs(); 52 if(args.length == 0){ 53 usage(); 54 return false; 55 } 56 57 Expression expression = new Expression(args[0]); 58 59 MBeanService service = ServiceFactory.getMBeanService(); 60 String [][] attributes = getAttributes(args); 61 AttributeListData[] attrListData = 62 service.setAttributes( 63 context.getServiceContext(expression.getAppName(), 64 expression.getMBeanName()), 65 attributes); 66 Out.println(); 67 Out.println("Changed Attributes:"); 68 CommandUtils.printAttributeLists(attrListData); 69 return true; 70 } 71 72 80 private String [][] getAttributes(String [] args) { 81 String [][] attributes = new String [args.length-1][2]; 82 for(int i=0; i<args.length-1; i++){ 83 StringTokenizer tokenizer = new StringTokenizer (args[i+1], "="); 84 assert tokenizer.countTokens() == 2; 85 attributes[i][0] = tokenizer.nextToken(); 86 attributes[i][1] = tokenizer.nextToken(); 87 } 88 return attributes; 89 } 90 91 public String getShortHelp() { 92 return "Sets one or more attribute values."; 93 } 94 95 public void help() { 96 Out.println(getShortHelp()); 97 Out.println("Usage:"); 98 Out.println(CommandConstants.SET_ATTRS + 99 " <application name>/<mbean name> attribute1=value1 [attribute2=value2] ..."); 100 Out.println(); 101 Out.println("Examples:"); 102 Out.println(CommandConstants.SET_ATTRS + 103 " myApp/myMBean testAtt1=value1"); 104 Out.println(CommandConstants.SET_ATTRS + 105 " myApp/jmanage:name=TestMBean testAttr1=value1 testAttr2=value2"); 106 107 } 108 109 private void usage(){ 110 Out.println("Invalid arguments"); 111 help(); 112 } 113 } 114 | Popular Tags |