KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Loggers;
26 import org.jmanage.core.util.Expression;
27 import org.jmanage.core.data.AttributeListData;
28
29 import java.util.logging.Logger JavaDoc;
30 import java.util.logging.Level JavaDoc;
31
32 /**
33  * Sets one attribute value at a time. This command can handle complex attribute
34  * values.
35  *
36  * Date: Mar 20, 2005
37  * @author Rakesh Kalra
38  */

39 public class SetHandler implements CommandHandler {
40
41     private static final Logger JavaDoc logger = Loggers.getLogger(SetHandler.class);
42
43     /**
44      *
45      * @param context
46      * @return true if the command was handled properly; false otherwise
47      */

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

83     private String JavaDoc[][] getAttributes(String JavaDoc[] args) {
84         String JavaDoc[][] attributes = new String JavaDoc[1][2];
85         attributes[0][0] = args[1];
86         /* The value may contain spaces, hence we need to concatenate remaining
87             args */

88         StringBuffer JavaDoc value = new StringBuffer JavaDoc();
89         for(int i=2; i<args.length; i++){
90             if(i>2)
91                 value.append(" ");
92             value.append(args[i]);
93         }
94         attributes[0][1] = value.toString();
95         return attributes;
96     }
97
98     public String JavaDoc getShortHelp() {
99         return "Sets attribute value.";
100     }
101
102     public void help() {
103         Out.println(getShortHelp() + " This command can handle complex " +
104                 "attribute values (e.g. with spaces)");
105         Out.println("Usage:");
106         Out.println(CommandConstants.SET +
107                 " <application name>/<mbean name> attribute value");
108         Out.println();
109         Out.println("Examples:");
110         Out.println(CommandConstants.SET +
111                 " myApp/myMBean testAttr value");
112         Out.println(CommandConstants.SET +
113                 " myApp/jmanage:name=TestMBean testAttr value");
114     }
115
116     private void usage(){
117         Out.println("Invalid arguments");
118         help();
119     }
120 }
121
122
Popular Tags