KickJava   Java API By Example, From Geeks To Geeks.

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


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.CommandUtils;
23 import org.jmanage.core.services.MBeanService;
24 import org.jmanage.core.services.ServiceFactory;
25 import org.jmanage.core.data.AttributeListData;
26 import org.jmanage.core.util.Expression;
27
28 import java.util.StringTokenizer JavaDoc;
29
30 /**
31  * Sets one more attributes. The attribute values are specified in the following
32  * manner:
33  * <p>
34  * attr1=value1 attr2=value2
35  * <p><p>
36  * This handler can't handle complex attribute values (e.g. with spaces). To set
37  * complex attribute values, "set" command shoule be used.
38  *
39  * Date: Mar 13, 2005
40  * @author Rakesh Kalra
41  */

42 public class SetAttributesHandler implements CommandHandler {
43
44     /**
45      *
46      * @param context
47      * @return true if the command was handled properly; false otherwise
48      */

49     public boolean execute(HandlerContext context) {
50
51         String JavaDoc[] args = context.getCommand().getArgs();
52         if(args.length == 0){
53             usage();
54             return false;
55         }
56
57         Expression expression = new Expression(args[0]);
58         /* set the values */
59         MBeanService service = ServiceFactory.getMBeanService();
60         String JavaDoc[][] attributes = getAttributes(args);
61         AttributeListData[] attrListData =
62                 service.setAttributes(
63                         context.getServiceContext(expression.getAppName(),
64                                 expression.getMBeanName()),
65                         attributes);
66         Out.println();
67         Out.println("Changed Attributes:");
68         CommandUtils.printAttributeLists(attrListData);
69         return true;
70     }
71
72     /**
73      * Starting from second argument, attribute and values are specified as
74      * <p>
75      * attr1=value1 attr2=value2 ...
76      *
77      * @param args
78      * @return
79      */

80     private String JavaDoc[][] getAttributes(String JavaDoc[] args) {
81         String JavaDoc[][] attributes = new String JavaDoc[args.length-1][2];
82         for(int i=0; i<args.length-1; i++){
83             StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(args[i+1], "=");
84             assert tokenizer.countTokens() == 2;
85             attributes[i][0] = tokenizer.nextToken();
86             attributes[i][1] = tokenizer.nextToken();
87         }
88         return attributes;
89     }
90
91     public String JavaDoc getShortHelp() {
92         return "Sets one or more attribute values.";
93     }
94
95     public void help() {
96         Out.println(getShortHelp());
97         Out.println("Usage:");
98         Out.println(CommandConstants.SET_ATTRS +
99                 " <application name>/<mbean name> attribute1=value1 [attribute2=value2] ...");
100         Out.println();
101         Out.println("Examples:");
102         Out.println(CommandConstants.SET_ATTRS +
103                 " myApp/myMBean testAtt1=value1");
104         Out.println(CommandConstants.SET_ATTRS +
105                 " myApp/jmanage:name=TestMBean testAttr1=value1 testAttr2=value2");
106
107     }
108
109     private void usage(){
110         Out.println("Invalid arguments");
111         help();
112     }
113 }
114
Popular Tags