KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmanage > cmdui > commands > ExecuteHandler


1 /**
2  * Copyright 2004-2005 jManage.org
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 /**
29  *
30  * date: Feb 26, 2005
31  * @author Rakesh Kalra
32  */

33 public class ExecuteHandler implements CommandHandler {
34
35     /**
36      *
37      * @param context
38      * @return true if the command was handled properly; false otherwise
39      */

40     public boolean execute(HandlerContext context) {
41
42         String JavaDoc[] 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 JavaDoc[] params = new String JavaDoc[args.length - 1];
55         for(int i=0; i<params.length; i++){
56             params[i] = args[i+1];
57         }
58         /* execute the operation */
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 JavaDoc strOutput = resultData[i].getDisplayOutput();
79                     Out.println(strOutput);
80                 }else{
81                     Out.println();
82                 }
83             }
84         }
85         return true;
86     }
87
88     public String JavaDoc 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