KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > console > plugins > helpers > jmx > Server


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.plugins.helpers.jmx;
23
24 import org.jboss.logging.Logger;
25 import org.jboss.mx.util.MBeanServerLocator;
26
27 import javax.management.Attribute JavaDoc;
28 import javax.management.AttributeList JavaDoc;
29 import javax.management.IntrospectionException JavaDoc;
30 import javax.management.JMException JavaDoc;
31 import javax.management.MBeanAttributeInfo JavaDoc;
32 import javax.management.MBeanInfo JavaDoc;
33 import javax.management.MBeanOperationInfo JavaDoc;
34 import javax.management.MBeanParameterInfo JavaDoc;
35 import javax.management.MBeanServer JavaDoc;
36 import javax.management.ObjectName JavaDoc;
37 import javax.management.ReflectionException JavaDoc;
38 import java.beans.PropertyEditor JavaDoc;
39 import java.beans.PropertyEditorManager JavaDoc;
40 import java.util.HashMap JavaDoc;
41 import java.util.Iterator JavaDoc;
42 import java.util.Set JavaDoc;
43 import java.util.TreeMap JavaDoc;
44
45 /** Utility methods related to the MBeanServer interface
46  *
47  * @author Scott.Stark@jboss.org
48  * @version $Revision: 37459 $
49  */

50 public class Server
51 {
52    static Logger log = Logger.getLogger(Server.class);
53
54    public static MBeanServer JavaDoc getMBeanServer() throws JMException JavaDoc
55    {
56       return MBeanServerLocator.locateJBoss();
57    }
58
59    public static Iterator JavaDoc getDomainData(String JavaDoc filter) throws JMException JavaDoc
60    {
61       MBeanServer JavaDoc server = getMBeanServer();
62       TreeMap JavaDoc domainData = new TreeMap JavaDoc();
63       if( server != null )
64       {
65          ObjectName JavaDoc filterName = null;
66          if( filter != null )
67             filterName = new ObjectName JavaDoc(filter);
68          Set JavaDoc objectNames = server.queryNames(filterName, null);
69          Iterator JavaDoc objectNamesIter = objectNames.iterator();
70          while( objectNamesIter.hasNext() )
71          {
72             ObjectName JavaDoc name = (ObjectName JavaDoc) objectNamesIter.next();
73             MBeanInfo JavaDoc info = server.getMBeanInfo(name);
74             String JavaDoc domainName = name.getDomain();
75             MBeanData mbeanData = new MBeanData(name, info);
76             DomainData data = (DomainData) domainData.get(domainName);
77             if( data == null )
78             {
79                data = new DomainData(domainName);
80                domainData.put(domainName, data);
81             }
82             data.addData(mbeanData);
83          }
84       }
85       Iterator JavaDoc domainDataIter = domainData.values().iterator();
86       return domainDataIter;
87    }
88
89    public static MBeanData getMBeanData(String JavaDoc name) throws JMException JavaDoc
90    {
91       MBeanServer JavaDoc server = getMBeanServer();
92       ObjectName JavaDoc objName = new ObjectName JavaDoc(name);
93       MBeanInfo JavaDoc info = server.getMBeanInfo(objName);
94       MBeanData mbeanData = new MBeanData(objName, info);
95       return mbeanData;
96    }
97
98    public static Object JavaDoc getMBeanAttributeObject(String JavaDoc name, String JavaDoc attrName)
99       throws JMException JavaDoc
100    {
101       MBeanServer JavaDoc server = getMBeanServer();
102       ObjectName JavaDoc objName = new ObjectName JavaDoc(name);
103       Object JavaDoc value = server.getAttribute(objName, attrName);
104       return value;
105    }
106
107    public static String JavaDoc getMBeanAttribute(String JavaDoc name, String JavaDoc attrName) throws JMException JavaDoc
108    {
109       MBeanServer JavaDoc server = getMBeanServer();
110       ObjectName JavaDoc objName = new ObjectName JavaDoc(name);
111       String JavaDoc value = null;
112       try
113       {
114          Object JavaDoc attr = server.getAttribute(objName, attrName);
115          if( attr != null )
116             value = attr.toString();
117       }
118       catch(JMException JavaDoc e)
119       {
120          value = e.getMessage();
121       }
122       return value;
123    }
124
125    public static AttrResultInfo getMBeanAttributeResultInfo(String JavaDoc name, MBeanAttributeInfo JavaDoc attrInfo)
126       throws JMException JavaDoc
127    {
128       ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
129       MBeanServer JavaDoc server = getMBeanServer();
130       ObjectName JavaDoc objName = new ObjectName JavaDoc(name);
131       String JavaDoc attrName = attrInfo.getName();
132       String JavaDoc attrType = attrInfo.getType();
133       Object JavaDoc value = null;
134       Throwable JavaDoc throwable = null;
135       if( attrInfo.isReadable() == true )
136       {
137          try
138          {
139             value = server.getAttribute(objName, attrName);
140          }
141          catch (Throwable JavaDoc t)
142          {
143             throwable = t;
144          }
145       }
146       Class JavaDoc typeClass = null;
147       try
148       {
149          typeClass = getPrimativeClass(attrType);
150          if( typeClass == null )
151             typeClass = loader.loadClass(attrType);
152       }
153       catch(ClassNotFoundException JavaDoc ignore)
154       {
155       }
156       PropertyEditor JavaDoc editor = null;
157       if( typeClass != null )
158          editor = PropertyEditorManager.findEditor(typeClass);
159
160       return new AttrResultInfo(attrName, editor, value, throwable);
161    }
162
163    public static AttributeList JavaDoc setAttributes(String JavaDoc name, HashMap JavaDoc attributes) throws JMException JavaDoc
164    {
165       ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
166       MBeanServer JavaDoc server = getMBeanServer();
167       ObjectName JavaDoc objName = new ObjectName JavaDoc(name);
168       MBeanInfo JavaDoc info = server.getMBeanInfo(objName);
169       MBeanAttributeInfo JavaDoc[] attributesInfo = info.getAttributes();
170       AttributeList JavaDoc newAttributes = new AttributeList JavaDoc();
171       for(int a = 0; a < attributesInfo.length; a ++)
172       {
173          MBeanAttributeInfo JavaDoc attrInfo = attributesInfo[a];
174          String JavaDoc attrName = attrInfo.getName();
175          if( attributes.containsKey(attrName) == false )
176             continue;
177          String JavaDoc value = (String JavaDoc) attributes.get(attrName);
178          String JavaDoc attrType = attrInfo.getType();
179          Attribute JavaDoc attr = null;
180          try
181          {
182             Class JavaDoc argType = getPrimativeClass(attrType);
183             if( argType == null )
184                argType = loader.loadClass(attrType);
185             PropertyEditor JavaDoc editor = PropertyEditorManager.findEditor(argType);
186             // Ignore attributes without valid editors
187
if( editor == null )
188             {
189                log.trace("Failed to find PropertyEditor for type: "+attrType);
190                continue;
191             }
192             editor.setAsText(value);
193             attr = new Attribute JavaDoc(attrName, editor.getValue());
194          }
195          catch(ClassNotFoundException JavaDoc e)
196          {
197             log.trace("Failed to load class for attribute: "+ (attr==null?"null":attr.getName()), e);
198             throw new ReflectionException JavaDoc(e, "Failed to load class for attribute: " + (attr==null?"null":attr.getName()));
199          }
200
201          server.setAttribute(objName, attr);
202          newAttributes.add(attr);
203       }
204       return newAttributes;
205    }
206
207    public static OpResultInfo invokeOp(String JavaDoc name, int index, String JavaDoc[] args) throws JMException JavaDoc
208    {
209       MBeanServer JavaDoc server = getMBeanServer();
210       ObjectName JavaDoc objName = new ObjectName JavaDoc(name);
211       MBeanInfo JavaDoc info = server.getMBeanInfo(objName);
212       MBeanOperationInfo JavaDoc[] opInfo = info.getOperations();
213       MBeanOperationInfo JavaDoc op = opInfo[index];
214       MBeanParameterInfo JavaDoc[] paramInfo = op.getSignature();
215       String JavaDoc[] argTypes = new String JavaDoc[paramInfo.length];
216       for(int p = 0; p < paramInfo.length; p ++)
217          argTypes[p] = paramInfo[p].getType();
218       return invokeOpByName(name, op.getName(), argTypes, args);
219    }
220
221    public static OpResultInfo invokeOpByName(String JavaDoc name, String JavaDoc opName, String JavaDoc[] argTypes, String JavaDoc[] args)
222       throws JMException JavaDoc
223    {
224       ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
225       MBeanServer JavaDoc server = getMBeanServer();
226       ObjectName JavaDoc objName = new ObjectName JavaDoc(name);
227       int length = argTypes != null ? argTypes.length : 0;
228       Object JavaDoc[] typedArgs = new Object JavaDoc[length];
229       for(int p = 0; p < typedArgs.length; p ++)
230       {
231          String JavaDoc arg = args[p];
232          try
233          {
234             Class JavaDoc argType = getPrimativeClass(argTypes[p]);
235             if( argType == null )
236                argType = loader.loadClass(argTypes[p]);
237             PropertyEditor JavaDoc editor = PropertyEditorManager.findEditor(argType);
238             if( editor == null )
239                throw new IntrospectionException JavaDoc("Failed to find PropertyEditor for type: "+argTypes[p]);
240             editor.setAsText(arg);
241             typedArgs[p] = editor.getValue();
242          }
243          catch(ClassNotFoundException JavaDoc e)
244          {
245             log.trace("Failed to load class for arg"+p, e);
246             throw new ReflectionException JavaDoc(e, "Failed to load class for arg"+p);
247          }
248       }
249       Object JavaDoc opReturn = server.invoke(objName, opName, typedArgs, argTypes);
250       return new OpResultInfo(opName, argTypes, opReturn);
251    }
252
253    static Class JavaDoc getPrimativeClass(String JavaDoc type)
254    {
255       String JavaDoc[] primatives = {
256             "boolean", "byte", "char", "int", "short",
257             "float", "double", "long"
258          };
259       Class JavaDoc[] primativeTypes = {
260          boolean.class, byte.class, char.class, int.class, short.class,
261          float.class, double.class, long.class
262       };
263       Class JavaDoc c = null;
264       for(int p = 0; p < primatives.length; p ++)
265       {
266          if( type.equals(primatives[p]) )
267             c = primativeTypes[p];
268       }
269       return c;
270    }
271 }
272
Popular Tags