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.core.services.MBeanService; 23 import org.jmanage.core.services.ServiceFactory; 24 import org.jmanage.core.services.ServiceContext; 25 import org.jmanage.core.data.OperationResultData; 26 import org.jmanage.core.util.Expression; 27 28 33 public class ExecuteHandler implements CommandHandler { 34 35 40 public boolean execute(HandlerContext context) { 41 42 String [] args = context.getCommand().getArgs(); 43 if(args.length == 0){ 44 usage(); 45 return false; 46 } 47 Expression expression = new Expression(args[0]); 48 if(expression.getAppName() == null || 49 expression.getMBeanName() == null || 50 expression.getTargetName() == null){ 51 usage(); 52 return false; 53 } 54 String [] params = new String [args.length - 1]; 55 for(int i=0; i<params.length; i++){ 56 params[i] = args[i+1]; 57 } 58 59 MBeanService service = ServiceFactory.getMBeanService(); 60 ServiceContext serviceContext = context.getServiceContext(expression.getAppName(), 61 expression.getMBeanName()); 62 OperationResultData[] resultData = 63 service.invoke(serviceContext, 64 expression.getTargetName(), 65 params); 66 67 for(int i=0; i<resultData.length; i++){ 68 Out.println(); 69 Out.print(resultData[i].getApplicationName() + " > "); 70 if(resultData[i].getResult() == OperationResultData.RESULT_ERROR){ 71 Out.println("Operation Failed."); 72 Out.println("Error:"); 73 Out.println(resultData[i].getErrorString()); 74 }else{ 75 Out.println("Operation Successful. "); 76 if(resultData[i].getOutput() != null){ 77 Out.println("Result: "); 78 String strOutput = resultData[i].getDisplayOutput(); 79 Out.println(strOutput); 80 }else{ 81 Out.println(); 82 } 83 } 84 } 85 return true; 86 } 87 88 public String getShortHelp() { 89 return "Executes given operation on mbean."; 90 } 91 92 public void help() { 93 Out.println(getShortHelp()); 94 Out.println(); 95 Out.println("Usage:"); 96 Out.println(CommandConstants.EXECUTE + 97 " <application name>/<mbean name>/<operation> [param1] [param2] ..."); 98 Out.println(); 99 Out.println("Examples:"); 100 Out.println(CommandConstants.EXECUTE + 101 " myApp/myMBean/myOperation param"); 102 } 103 104 private void usage(){ 105 Out.println("Invalid arguments"); 106 help(); 107 } 108 } 109 | Popular Tags |