KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.beans.PropertyEditor JavaDoc;
25 import java.io.PrintWriter JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29
30 import javax.management.Attribute JavaDoc;
31 import javax.management.AttributeList JavaDoc;
32 import javax.management.MBeanAttributeInfo JavaDoc;
33 import javax.management.MBeanInfo JavaDoc;
34 import javax.management.MBeanServerConnection JavaDoc;
35 import javax.management.ObjectName JavaDoc;
36
37 import gnu.getopt.Getopt;
38 import gnu.getopt.LongOpt;
39 import org.jboss.util.Strings;
40 import org.jboss.util.propertyeditor.PropertyEditors;
41
42 /**
43  * Sets one or more attributes in an MBean
44  *
45  * @author <a HREF="mailto:bela@jboss.com">Bela Ban</a>
46  * @version $Revision: 37459 $
47  */

48 public class SetAttrsCommand
49    extends MBeanServerCommand
50 {
51    private ObjectName JavaDoc objectName;
52
53    /** List<String>. Contains names and values */
54    private List JavaDoc attributeNames = new ArrayList JavaDoc(5);
55
56
57    public SetAttrsCommand()
58    {
59       super("setattrs", "Set the values of one or more MBean attributes");
60    }
61
62    public void displayHelp()
63    {
64       PrintWriter JavaDoc out = context.getWriter();
65
66       out.println(desc);
67       out.println();
68       out.println("usage: " + name + " [options] <name> [<attr value>+]");
69       out.println("options:");
70       out.println(" --noprefix Do not display attribute name prefixes");
71       out.println(" -- Stop processing options");
72
73       out.flush();
74    }
75
76    private boolean processArguments(final String JavaDoc[] args)
77       throws CommandException
78    {
79       log.debug("processing arguments: " + Strings.join(args, ","));
80
81       if (args.length == 0)
82       {
83          throw new CommandException("Command requires arguments");
84       }
85
86       String JavaDoc sopts = "-:";
87       LongOpt[] lopts =
88          {
89             new LongOpt("noprefix", LongOpt.NO_ARGUMENT, null, 0x1000),
90          };
91
92       Getopt getopt = new Getopt(null, args, sopts, lopts);
93       getopt.setOpterr(false);
94
95       int code;
96       int argidx = 0;
97
98       while ((code = getopt.getopt()) != -1)
99       {
100          switch (code)
101          {
102             case ':':
103                throw new CommandException
104                   ("Option requires an argument: " + args[getopt.getOptind() - 1]);
105
106             case '?':
107                throw new CommandException
108                   ("Invalid (or ambiguous) option: " + args[getopt.getOptind() - 1]);
109
110             case 0x1000:
111                break;
112                   
113                // non-option arguments
114
case 1:
115                {
116                   String JavaDoc arg = getopt.getOptarg();
117
118                   switch (argidx++)
119                   {
120                      case 0:
121                         objectName = createObjectName(arg);
122                         log.debug("mbean name: " + objectName);
123                         break;
124
125                      default:
126                         log.debug("adding attribute name: " + arg);
127                         attributeNames.add(arg);
128                         break;
129                   }
130                   break;
131                }
132          }
133       }
134
135       return true;
136    }
137
138    public void execute(String JavaDoc[] args) throws Exception JavaDoc
139    {
140       processArguments(args);
141
142       if (objectName == null)
143          throw new CommandException("Missing object name");
144
145       log.debug("attribute names: " + attributeNames);
146
147       MBeanServerConnection JavaDoc server = getMBeanServer();
148       if (attributeNames.size() == 0)
149       {
150          throw new CommandException("at least 1 attribute and value needs to be defined");
151       }
152
153       MBeanInfo JavaDoc info = server.getMBeanInfo(objectName);
154       MBeanAttributeInfo JavaDoc[] attribute_info = info.getAttributes();
155       String JavaDoc type;
156       AttributeList JavaDoc attrs = new AttributeList JavaDoc(attributeNames.size());
157       Attribute JavaDoc attr;
158       String JavaDoc attr_name;
159       Object JavaDoc attr_value, real_value;
160       MBeanAttributeInfo JavaDoc attr_info;
161
162
163       for (Iterator JavaDoc it = attributeNames.iterator(); it.hasNext();)
164       {
165          attr_name = (String JavaDoc) it.next();
166          attr_value = it.next();
167
168          attr_info = findAttribute(attr_name, attribute_info);
169          if (attr_info == null)
170             throw new CommandException("attribute " + attr_name + " not found");
171          type = attr_info.getType();
172
173          PropertyEditor JavaDoc editor = PropertyEditors.getEditor(type);
174          editor.setAsText((String JavaDoc) attr_value);
175          real_value = editor.getValue();
176
177          attr = new Attribute JavaDoc(attr_name, real_value);
178          attrs.add(attr);
179       }
180
181       AttributeList JavaDoc ret = server.setAttributes(objectName, attrs);
182       System.out.println("The following attributes were set successfuly:");
183       if (ret.size() > 0)
184       {
185          for (Iterator JavaDoc it = ret.iterator(); it.hasNext();)
186          {
187             Attribute JavaDoc a = (Attribute JavaDoc) it.next();
188             System.out.println(a.getName() + "=" + a.getValue());
189          }
190       }
191    }
192
193    private MBeanAttributeInfo JavaDoc findAttribute(String JavaDoc attr_name,
194       MBeanAttributeInfo JavaDoc[] attribute_info)
195    {
196       for (int i = 0; i < attribute_info.length; i++)
197       {
198          MBeanAttributeInfo JavaDoc mBeanAttributeInfo = attribute_info[i];
199          if (mBeanAttributeInfo.getName().equals(attr_name))
200             return mBeanAttributeInfo;
201       }
202       return null;
203    }
204 }
205
Popular Tags