KickJava   Java API By Example, From Geeks To Geeks.

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


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.cmdui.util.Table;
23 import org.jmanage.core.services.MBeanService;
24 import org.jmanage.core.services.ServiceFactory;
25 import org.jmanage.core.management.ObjectInfo;
26 import org.jmanage.core.management.ObjectOperationInfo;
27 import org.jmanage.core.management.ObjectAttributeInfo;
28 import org.jmanage.core.management.ObjectParameterInfo;
29 import org.jmanage.core.util.Expression;
30
31 /**
32  * info <appName>/<mbeanName[configured name or object name]>
33  *
34  * date: Feb 23, 2005
35  * @author Rakesh Kalra
36  */

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

44     public boolean execute(HandlerContext context) {
45
46         String JavaDoc[] args = context.getCommand().getArgs();
47         if(args.length != 1){
48             usage();
49             return false;
50         }
51
52         Expression expression = new Expression(args[0]);
53         if(expression.getAppName() == null || expression.getMBeanName() == null){
54             usage();
55             return false;
56         }
57
58         MBeanService service = ServiceFactory.getMBeanService();
59         ObjectInfo objectInfo =
60                 service.getMBeanInfo(context.getServiceContext(
61                         expression.getAppName(), expression.getMBeanName()));
62         printObjectInfo(objectInfo);
63         return true;
64     }
65
66     public String JavaDoc getShortHelp() {
67         return "Display information about the mbean.";
68     }
69
70     public void help() {
71         Out.println(getShortHelp());
72         Out.println("Usage:");
73         Out.println(CommandConstants.INFO + " <application name>/<mbean name>");
74         Out.println("Examples:");
75         Out.println(CommandConstants.INFO + " myApp/myMBean");
76         Out.println(CommandConstants.INFO + " myApp/jmanage:name=TestMBean");
77     }
78
79     private void usage(){
80         Out.println("Invalid arguments");
81         help();
82     }
83
84     private void printObjectInfo(ObjectInfo objectInfo){
85         Out.println();
86         Out.println("Object Name: " + objectInfo.getObjectName());
87         Out.println("Class Name : " + objectInfo.getClassName());
88         Out.println("Description: " + objectInfo.getDescription());
89         printAttributes(objectInfo.getAttributes());
90         printOperations(objectInfo.getOperations());
91     }
92
93     private void printAttributes(ObjectAttributeInfo[] attributes) {
94         if(attributes.length == 0)
95             return;
96
97         Out.println();
98         Out.println("Attributes:");
99         Table table = new Table(4);
100         for(int i=0; i<attributes.length; i++){
101             table.add(attributes[i].getName(),
102                     attributes[i].getDisplayType(),
103                     attributes[i].getReadWrite(),
104                     attributes[i].getDescription());
105         }
106         table.print();
107     }
108
109     private void printOperations(ObjectOperationInfo[] operations) {
110         if(operations.length == 0)
111             return;
112
113         Out.println();
114         Out.println("Operations:");
115         Table table = new Table(3);
116         for(int i=0; i<operations.length; i++){
117             table.add(operations[i].getName()+ "(" +
118                     signature(operations[i].getSignature()) + ")",
119                     operations[i].getDisplayReturnType(),
120                     operations[i].getDescription());
121         }
122         table.print();
123     }
124
125     private String JavaDoc signature(ObjectParameterInfo[] signature){
126         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
127         for(int i=0; i<signature.length; i++){
128             if(i > 0){
129                 buff.append(", ");
130             }
131             buff.append(signature[i].getDisplayType() + " " + signature[i].getName());
132         }
133         return buff.toString();
134     }
135 }
136
Popular Tags