KickJava   Java API By Example, From Geeks To Geeks.

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


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.CommandUtils;
22 import org.jmanage.cmdui.util.Out;
23 import org.jmanage.core.services.MBeanService;
24 import org.jmanage.core.services.ServiceFactory;
25
26 import java.util.List JavaDoc;
27
28 /**
29  *
30  * date: Feb 23, 2005
31  * @author Rakesh Kalra
32  */

33 public class QueryMBeansHandler implements CommandHandler {
34
35     /**
36      * Lists mbeans for the given application based on the filter (optional)
37      *
38      * @param context
39      * @return true if the command was handled properly; false otherwise
40      */

41     public boolean execute(HandlerContext context) {
42         String JavaDoc[] args = context.getCommand().getArgs();
43         if(args.length != 1 && args.length != 2){
44             usage();
45             return false;
46         }
47
48         String JavaDoc appName = args[0];
49         String JavaDoc filter = null;
50         if(args.length > 1){
51             filter = args[1];
52         }
53
54         MBeanService mbeanService = ServiceFactory.getMBeanService();
55         List JavaDoc mbeanDataList =
56                 mbeanService.queryMBeans(context.getServiceContext(appName),
57                         filter);
58         assert mbeanDataList != null;
59         CommandUtils.printMBeans(mbeanDataList);
60         return true;
61     }
62
63     public String JavaDoc getShortHelp() {
64         return "Queries mbeans for the given application";
65     }
66
67     public void help() {
68         Out.println(getShortHelp());
69         Out.println("Usage:");
70         Out.println(CommandConstants.QUERY_MBEANS + " <application name> [filter]");
71     }
72
73     private void usage(){
74         Out.println("Invalid arguments");
75         help();
76     }
77 }
78
Popular Tags