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.util.Loggers; 26 import org.jmanage.core.util.Expression; 27 import org.jmanage.core.data.AttributeListData; 28 29 import java.util.logging.Logger ; 30 import java.util.logging.Level ; 31 32 39 public class SetHandler implements CommandHandler { 40 41 private static final Logger logger = Loggers.getLogger(SetHandler.class); 42 43 48 public boolean execute(HandlerContext context) { 49 50 String [] args = context.getCommand().getArgs(); 51 if(args.length < 3){ 52 usage(); 53 return false; 54 } 55 56 Expression expression = new Expression(args[0]); 57 58 MBeanService service = ServiceFactory.getMBeanService(); 59 String [][] attributes = getAttributes(args); 60 if(logger.isLoggable(Level.FINE)){ 61 logger.log(Level.FINE, "Setting attr. name=" + attributes[0][0] + 62 " value=" + attributes[0][1]); 63 } 64 AttributeListData[] attrListData = 65 service.setAttributes( 66 context.getServiceContext(expression.getAppName(), 67 expression.getMBeanName()), 68 attributes); 69 Out.println(); 70 Out.println("Changed Attributes:"); 71 CommandUtils.printAttributeLists(attrListData); 72 return true; 73 } 74 75 83 private String [][] getAttributes(String [] args) { 84 String [][] attributes = new String [1][2]; 85 attributes[0][0] = args[1]; 86 88 StringBuffer value = new StringBuffer (); 89 for(int i=2; i<args.length; i++){ 90 if(i>2) 91 value.append(" "); 92 value.append(args[i]); 93 } 94 attributes[0][1] = value.toString(); 95 return attributes; 96 } 97 98 public String getShortHelp() { 99 return "Sets attribute value."; 100 } 101 102 public void help() { 103 Out.println(getShortHelp() + " This command can handle complex " + 104 "attribute values (e.g. with spaces)"); 105 Out.println("Usage:"); 106 Out.println(CommandConstants.SET + 107 " <application name>/<mbean name> attribute value"); 108 Out.println(); 109 Out.println("Examples:"); 110 Out.println(CommandConstants.SET + 111 " myApp/myMBean testAttr value"); 112 Out.println(CommandConstants.SET + 113 " myApp/jmanage:name=TestMBean testAttr value"); 114 } 115 116 private void usage(){ 117 Out.println("Invalid arguments"); 118 help(); 119 } 120 } 121 122 | Popular Tags |