KickJava   Java API By Example, From Geeks To Geeks.

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


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.CommandConstants;
19 import org.jmanage.cmdui.CommandHandler;
20 import org.jmanage.cmdui.HandlerContext;
21 import org.jmanage.cmdui.util.CommandUtils;
22 import org.jmanage.cmdui.util.Out;
23 import org.jmanage.core.data.AttributeListData;
24 import org.jmanage.core.services.MBeanService;
25 import org.jmanage.core.services.ServiceContext;
26 import org.jmanage.core.services.ServiceFactory;
27 import org.jmanage.core.util.Expression;
28
29 /**
30  * Gets attribute values for given mbean.
31  *
32  * Date: Mar 13, 2005
33  * @author Rakesh Kalra
34  */

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

42     public boolean execute(HandlerContext context) {
43
44         String JavaDoc[] args = context.getCommand().getArgs();
45         if(args.length == 0){
46             usage();
47             return false;
48         }
49
50         Expression expression = new Expression(args[0]);
51         /* get the values */
52         MBeanService service = ServiceFactory.getMBeanService();
53
54         String JavaDoc[] attributeNames = null;
55         if(args.length > 1){
56             attributeNames = new String JavaDoc[args.length - 1];
57             for(int i=0; i< attributeNames.length; i++){
58                 attributeNames[i] = args[i+1];
59             }
60         }
61         ServiceContext serviceContext =
62                 context.getServiceContext(expression.getAppName(),
63                             expression.getMBeanName());
64         AttributeListData[] attributeValues = null;
65         if(attributeNames != null){
66             attributeValues = service.getAttributes(serviceContext,
67                     attributeNames, true);
68         }else{
69             attributeValues = service.getAttributes(serviceContext);
70         }
71         CommandUtils.printAttributeLists(attributeValues);
72         return true;
73     }
74
75     public String JavaDoc getShortHelp() {
76         return "Gets attribute values for given mbean.";
77     }
78
79     public void help() {
80         Out.println(getShortHelp());
81         Out.println("Usage:");
82         Out.println(CommandConstants.GET +
83                 " <application name>/<mbean name> [attribute1] [attribute2] ...");
84         Out.println();
85         Out.println("Examples:");
86         Out.println(CommandConstants.GET +
87                 " myApp/myMBean");
88         Out.println(CommandConstants.GET +
89                 " myApp/jmanage:name=TestMBean testAttr1 testAttr2");
90     }
91
92     /* TODO: we should probably move this to base class -rk */
93     private void usage(){
94         Out.println("Invalid arguments");
95         help();
96     }
97 }
98
Popular Tags