KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > console > twiddle > command > SetCommand


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.console.twiddle.command;
23
24 import gnu.getopt.Getopt;
25 import gnu.getopt.LongOpt;
26
27 import java.beans.PropertyEditor JavaDoc;
28 import java.io.PrintWriter JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.List JavaDoc;
31
32 import javax.management.Attribute JavaDoc;
33 import javax.management.MBeanAttributeInfo JavaDoc;
34 import javax.management.MBeanInfo JavaDoc;
35 import javax.management.MBeanServerConnection JavaDoc;
36 import javax.management.ObjectName JavaDoc;
37
38 import org.jboss.util.Strings;
39 import org.jboss.util.propertyeditor.PropertyEditors;
40
41
42 /**
43  * Set the values of one MBean attribute.
44  *
45  * @author heiko.rupp@cellent.de
46  * @version $Revision: 37459 $
47  */

48 public class SetCommand
49     extends MBeanServerCommand
50 {
51
52     private ObjectName JavaDoc objectName;
53     private List JavaDoc attributeNames = new ArrayList JavaDoc(5);
54     private boolean prefix = true;
55     private String JavaDoc query;
56
57     public SetCommand()
58     {
59         super("set", "Set the value of one MBean attribute");
60     }
61
62
63    public void displayHelp()
64    {
65         PrintWriter JavaDoc out = context.getWriter();
66
67         out.println(desc);
68         out.println();
69         out.println("usage: " + name + " [options] <name> <attr> <val>");
70         out.println("options:");
71         out.println(" --noprefix Do not display attribute name prefixes");
72         out.println(" -- Stop processing options");
73
74         out.flush();
75    }
76    
77     private boolean processArguments(final String JavaDoc[] args)
78         throws CommandException
79     {
80         log.debug("processing arguments: " + Strings.join(args, ","));
81
82         if (args.length == 0)
83         {
84             throw new CommandException("Command requires arguments");
85         }
86
87         String JavaDoc sopts = "-:";
88         LongOpt[] lopts =
89             {
90                 new LongOpt("noprefix", LongOpt.NO_ARGUMENT, null, 0x1000),
91             };
92
93         Getopt getopt = new Getopt(null, args, sopts, lopts);
94         getopt.setOpterr(false);
95
96         int code;
97         int argidx = 0;
98
99         while ((code = getopt.getopt()) != -1)
100         {
101             switch (code)
102             {
103                 case ':':
104                     throw new CommandException
105                         ("Option requires an argument: " + args[getopt.getOptind() - 1]);
106
107                 case '?':
108                     throw new CommandException
109                         ("Invalid (or ambiguous) option: " + args[getopt.getOptind() - 1]);
110
111                 case 0x1000:
112                     prefix = false;
113                     break;
114                   
115                     // non-option arguments
116
case 1:
117                     {
118                         String JavaDoc arg = getopt.getOptarg();
119
120                         switch (argidx++)
121                         {
122                             case 0:
123                                 objectName = createObjectName(arg);
124                                 log.debug("mbean name: " + objectName);
125                                 break;
126
127                             default:
128                                 log.debug("adding attribute name: " + arg);
129                                 attributeNames.add(arg);
130                                 break;
131                         }
132                         break;
133                     }
134             }
135         }
136
137         return true;
138     }
139    
140
141    public void execute(String JavaDoc[] args) throws Exception JavaDoc
142    {
143
144         processArguments(args);
145                 
146         String JavaDoc theAttr = (String JavaDoc)attributeNames.toArray()[0];
147         String JavaDoc theVal = (String JavaDoc)attributeNames.toArray()[1];
148
149         if (objectName == null)
150             throw new CommandException("Missing object name");
151
152         log.debug("attribute names: " + attributeNames);
153         if (attributeNames.size() != 2 )
154         {
155             throw new CommandException("Wrong number of arguments");
156         }
157         MBeanServerConnection JavaDoc server = getMBeanServer();
158
159         MBeanInfo JavaDoc info = server.getMBeanInfo(objectName);
160         
161         MBeanAttributeInfo JavaDoc[] attrs = info.getAttributes();
162         
163         MBeanAttributeInfo JavaDoc attr=null;
164
165         boolean found = false;
166         for (int i=0;i < attrs.length ; i++ )
167         {
168             if (attrs[i].getName().equals(theAttr) &&
169                 attrs[i].isWritable()) {
170                 
171                     found=true;
172                     attr= attrs[i];
173                     break;
174                 }
175         }
176         
177         if (found == false)
178         {
179             
180             throw new CommandException("No matching attribute found");
181         }
182         else
183         {
184             Object JavaDoc oVal = convert(theVal,attr.getType());
185             Attribute JavaDoc at = new Attribute JavaDoc(theAttr,oVal);
186             server.setAttribute(objectName,at);
187             
188             // read the attribute back from the server
189
if (!context.isQuiet())
190             {
191                 PrintWriter JavaDoc out = context.getWriter();
192                 Object JavaDoc nat = server.getAttribute(objectName,theAttr);
193                 if (nat==null)
194                     out.println("null");
195                 else
196                     if (prefix)
197                         out.print(theAttr+"=");
198                     out.println(nat.toString());
199             }
200         }
201         
202
203    }
204
205     /**
206      * Convert val into an Object of type type
207      * @param val The given value
208      * @param oType the wanted return type
209      * @return the value in the correct representation
210      * @throws Exception various :)
211      */

212     private Object JavaDoc convert (String JavaDoc val,String JavaDoc oType) throws Exception JavaDoc
213     {
214         PropertyEditor JavaDoc editor = PropertyEditors.getEditor(oType);
215         editor.setAsText(val);
216         return editor.getValue();
217     }
218 }
219
Popular Tags