KickJava   Java API By Example, From Geeks To Geeks.

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


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.CommandConstants;
20 import org.jmanage.cmdui.HandlerContext;
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.management.ObjectAttribute;
25 import org.jmanage.core.data.AttributeListData;
26 import org.jmanage.core.util.Expression;
27 import org.jmanage.util.StringUtils;
28
29 import java.util.*;
30
31 /**
32  *
33  * date: Feb 26, 2005
34  * @author Rakesh Kalra
35  */

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

43     public boolean execute(HandlerContext context) {
44
45         String JavaDoc[] args = context.getCommand().getArgs();
46         if(args.length == 0){
47             usage();
48             return false;
49         }
50
51         /* parse the expression and get the hashmap of appname+mbeanname to
52             attribute list */

53         final Map appMBeanToAttributesMap = new HashMap();
54
55         Expression expression = null;
56         for(int i=0; i<args.length; i++){
57             expression = new Expression(args[i], expression);
58             if(expression.getAppName() == null ||
59                     expression.getMBeanName() == null ||
60                     expression.getTargetName() == null){
61                 usage();
62                 return false;
63             }
64             AppMBeanKey key = new AppMBeanKey(expression.getAppName(),
65                     expression.getMBeanName());
66             List attributes = (List)appMBeanToAttributesMap.get(key);
67             if(attributes == null){
68                 attributes = new LinkedList();
69                 appMBeanToAttributesMap.put(key, attributes);
70             }
71             attributes.add(expression.getTargetName());
72         }
73
74         /* now get the values */
75         MBeanService service = ServiceFactory.getMBeanService();
76         for(Iterator it=appMBeanToAttributesMap.keySet().iterator(); it.hasNext();){
77             AppMBeanKey key = (AppMBeanKey)it.next();
78             List attributes = (List)appMBeanToAttributesMap.get(key);
79             AttributeListData[] attributeValues =
80                     service.getAttributes(
81                             context.getServiceContext(key.appName, key.mbeanName),
82                             StringUtils.listToStringArray(attributes),
83                             false);
84             print(attributeValues);
85         }
86         Out.println();
87
88         return true;
89     }
90
91     public String JavaDoc getShortHelp() {
92         return "Prints tab delimited value(s) for given mbean attribute(s).";
93     }
94
95     public void help() {
96         Out.println(getShortHelp());
97         Out.println("Usage:");
98         Out.println(CommandConstants.PRINT +
99                 " <application name>/<mbean name>/<attribute1> [attribute2] ...");
100         Out.println();
101         Out.println("Examples:");
102         Out.println(CommandConstants.PRINT +
103                 " myApp/myMBean/testAttr1 testAttr2");
104         Out.println(CommandConstants.PRINT +
105                 " myApp/jmanage:name=TestMBean/testAttr1 jmanage:name=Configuration/testAttr");
106     }
107
108     private void print(AttributeListData[] attrValues){
109         assert attrValues.length == 1;
110         for(Iterator it=attrValues[0].getAttributeList().iterator(); it.hasNext(); ){
111             ObjectAttribute attribute= (ObjectAttribute)it.next();
112             Out.print(attribute.getDisplayValue() + "\t");
113         }
114     }
115
116     private void usage(){
117         Out.println("Invalid arguments");
118         help();
119     }
120
121     private class AppMBeanKey{
122         String JavaDoc appName;
123         String JavaDoc mbeanName;
124
125         AppMBeanKey(String JavaDoc appName, String JavaDoc mbeanName){
126             this.appName = appName;
127             this.mbeanName = mbeanName;
128         }
129
130         public int hashCode(){
131             return (appName.hashCode() * 31) + mbeanName.hashCode();
132         }
133
134         public boolean equals(Object JavaDoc obj){
135             if(obj instanceof AppMBeanKey){
136                 AppMBeanKey key = (AppMBeanKey)obj;
137                 return key.appName.equals(this.appName) &&
138                         key.mbeanName.equals(this.mbeanName);
139             }
140             return false;
141         }
142     }
143 }
144
Popular Tags