KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > jmx > adaptor > control > 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.jmx.adaptor.control;
23
24 import java.beans.IntrospectionException JavaDoc;
25 import java.beans.PropertyEditor JavaDoc;
26 import java.beans.PropertyEditorManager JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Set JavaDoc;
30 import java.util.TreeMap JavaDoc;
31 import javax.management.Attribute JavaDoc;
32 import javax.management.AttributeList JavaDoc;
33 import javax.management.JMException JavaDoc;
34 import javax.management.MBeanInfo JavaDoc;
35 import javax.management.MBeanAttributeInfo JavaDoc;
36 import javax.management.MBeanOperationInfo JavaDoc;
37 import javax.management.MBeanParameterInfo JavaDoc;
38 import javax.management.MBeanServer JavaDoc;
39 import javax.management.ObjectName JavaDoc;
40 import javax.management.ReflectionException JavaDoc;
41
42 import org.jboss.jmx.adaptor.model.DomainData;
43 import org.jboss.jmx.adaptor.model.MBeanData;
44 import org.jboss.logging.Logger;
45 import org.jboss.mx.util.MBeanServerLocator;
46 import org.jboss.util.Classes;
47 import org.jboss.util.propertyeditor.PropertyEditors;
48
49 /** Utility methods related to the MBeanServer interface
50  *
51  * @author Scott.Stark@jboss.org
52  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
53  * @version $Revision: 37459 $
54  */

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