001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.jmx.adaptor.control;
023:
024: import java.beans.IntrospectionException;
025: import java.beans.PropertyEditor;
026: import java.beans.PropertyEditorManager;
027: import java.util.HashMap;
028: import java.util.Iterator;
029: import java.util.Set;
030: import java.util.TreeMap;
031: import javax.management.Attribute;
032: import javax.management.AttributeList;
033: import javax.management.JMException;
034: import javax.management.MBeanInfo;
035: import javax.management.MBeanAttributeInfo;
036: import javax.management.MBeanOperationInfo;
037: import javax.management.MBeanParameterInfo;
038: import javax.management.MBeanServer;
039: import javax.management.ObjectName;
040: import javax.management.ReflectionException;
041:
042: import org.jboss.jmx.adaptor.model.DomainData;
043: import org.jboss.jmx.adaptor.model.MBeanData;
044: import org.jboss.logging.Logger;
045: import org.jboss.mx.util.MBeanServerLocator;
046: import org.jboss.util.Classes;
047: import org.jboss.util.propertyeditor.PropertyEditors;
048:
049: /** Utility methods related to the MBeanServer interface
050: *
051: * @author Scott.Stark@jboss.org
052: * @author <a href="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
053: * @version $Revision: 57210 $
054: */
055: public class Server {
056: static Logger log = Logger.getLogger(Server.class);
057:
058: public static MBeanServer getMBeanServer() {
059: return MBeanServerLocator.locateJBoss();
060: }
061:
062: public static Iterator getDomainData(String filter)
063: throws JMException {
064: MBeanServer server = getMBeanServer();
065: TreeMap domainData = new TreeMap();
066: if (server != null) {
067: ObjectName filterName = null;
068: if (filter != null)
069: filterName = new ObjectName(filter);
070: Set objectNames = server.queryNames(filterName, null);
071: Iterator objectNamesIter = objectNames.iterator();
072: while (objectNamesIter.hasNext()) {
073: ObjectName name = (ObjectName) objectNamesIter.next();
074: MBeanInfo info = server.getMBeanInfo(name);
075: String domainName = name.getDomain();
076: MBeanData mbeanData = new MBeanData(name, info);
077: DomainData data = (DomainData) domainData
078: .get(domainName);
079: if (data == null) {
080: data = new DomainData(domainName);
081: domainData.put(domainName, data);
082: }
083: data.addData(mbeanData);
084: }
085: }
086: Iterator domainDataIter = domainData.values().iterator();
087: return domainDataIter;
088: }
089:
090: public static MBeanData getMBeanData(String name)
091: throws JMException {
092: MBeanServer server = getMBeanServer();
093: ObjectName objName = new ObjectName(name);
094: MBeanInfo info = server.getMBeanInfo(objName);
095: MBeanData mbeanData = new MBeanData(objName, info);
096: return mbeanData;
097: }
098:
099: public static Object getMBeanAttributeObject(String name,
100: String attrName) throws JMException {
101: MBeanServer server = getMBeanServer();
102: ObjectName objName = new ObjectName(name);
103: Object value = server.getAttribute(objName, attrName);
104: return value;
105: }
106:
107: public static String getMBeanAttribute(String name, String attrName)
108: throws JMException {
109: MBeanServer server = getMBeanServer();
110: ObjectName objName = new ObjectName(name);
111: String value = null;
112: try {
113: Object attr = server.getAttribute(objName, attrName);
114: if (attr != null)
115: value = attr.toString();
116: } catch (JMException e) {
117: value = e.getMessage();
118: }
119: return value;
120: }
121:
122: public static AttrResultInfo getMBeanAttributeResultInfo(
123: String name, MBeanAttributeInfo attrInfo)
124: throws JMException {
125: ClassLoader loader = Thread.currentThread()
126: .getContextClassLoader();
127: MBeanServer server = getMBeanServer();
128: ObjectName objName = new ObjectName(name);
129: String attrName = attrInfo.getName();
130: String attrType = attrInfo.getType();
131: Object value = null;
132: Throwable throwable = null;
133: if (attrInfo.isReadable() == true) {
134: try {
135: value = server.getAttribute(objName, attrName);
136: } catch (Throwable t) {
137: throwable = t;
138: }
139: }
140: Class typeClass = null;
141: try {
142: typeClass = Classes.getPrimitiveTypeForName(attrType);
143: if (typeClass == null)
144: typeClass = loader.loadClass(attrType);
145: } catch (ClassNotFoundException ignore) {
146: }
147: PropertyEditor editor = null;
148: if (typeClass != null)
149: editor = PropertyEditorManager.findEditor(typeClass);
150:
151: return new AttrResultInfo(attrName, editor, value, throwable);
152: }
153:
154: public static AttributeList setAttributes(String name,
155: HashMap attributes) throws JMException {
156: MBeanServer server = getMBeanServer();
157: ObjectName objName = new ObjectName(name);
158: MBeanInfo info = server.getMBeanInfo(objName);
159: MBeanAttributeInfo[] attributesInfo = info.getAttributes();
160: AttributeList newAttributes = new AttributeList();
161: for (int a = 0; a < attributesInfo.length; a++) {
162: MBeanAttributeInfo attrInfo = attributesInfo[a];
163: String attrName = attrInfo.getName();
164: if (attributes.containsKey(attrName) == false)
165: continue;
166: String value = (String) attributes.get(attrName);
167: if (value.equals("null")
168: && server.getAttribute(objName, attrName) == null) {
169: log.trace("ignoring 'null' for " + attrName);
170: continue;
171: }
172: String attrType = attrInfo.getType();
173: Attribute attr = null;
174: try {
175: Object realValue = PropertyEditors.convertValue(value,
176: attrType);
177: attr = new Attribute(attrName, realValue);
178: } catch (ClassNotFoundException e) {
179: String s = (attr != null) ? attr.getName() : attrType;
180: log
181: .trace("Failed to load class for attribute: "
182: + s, e);
183: throw new ReflectionException(e,
184: "Failed to load class for attribute: " + s);
185: } catch (IntrospectionException e) {
186: log.trace("Skipped setting attribute: " + attrName
187: + ", cannot find PropertyEditor for type: "
188: + attrType);
189: continue;
190: }
191:
192: server.setAttribute(objName, attr);
193: newAttributes.add(attr);
194: }
195: return newAttributes;
196: }
197:
198: public static OpResultInfo invokeOp(String name, int index,
199: String[] args) throws JMException {
200: MBeanServer server = getMBeanServer();
201: ObjectName objName = new ObjectName(name);
202: MBeanInfo info = server.getMBeanInfo(objName);
203: MBeanOperationInfo[] opInfo = info.getOperations();
204: MBeanOperationInfo op = opInfo[index];
205: MBeanParameterInfo[] paramInfo = op.getSignature();
206: String[] argTypes = new String[paramInfo.length];
207: for (int p = 0; p < paramInfo.length; p++)
208: argTypes[p] = paramInfo[p].getType();
209: return invokeOpByName(name, op.getName(), argTypes, args);
210: }
211:
212: public static OpResultInfo invokeOpByName(String name,
213: String opName, String[] argTypes, String[] args)
214: throws JMException {
215: MBeanServer server = getMBeanServer();
216: ObjectName objName = new ObjectName(name);
217: int length = argTypes != null ? argTypes.length : 0;
218: Object[] typedArgs = new Object[length];
219: for (int p = 0; p < typedArgs.length; p++) {
220: String arg = args[p];
221: try {
222: Object argValue = PropertyEditors.convertValue(arg,
223: argTypes[p]);
224: typedArgs[p] = argValue;
225: } catch (ClassNotFoundException e) {
226: log.trace("Failed to load class for arg" + p, e);
227: throw new ReflectionException(e,
228: "Failed to load class for arg" + p);
229: } catch (java.beans.IntrospectionException e) {
230: // If the type is not java.lang.Object throw an exception
231: if (argTypes[p].equals("java.lang.Object") == false)
232: throw new javax.management.IntrospectionException(
233: "Failed to find PropertyEditor for type: "
234: + argTypes[p]);
235: // Just use the String arg
236: typedArgs[p] = arg;
237: continue;
238: }
239: }
240: Object opReturn = server.invoke(objName, opName, typedArgs,
241: argTypes);
242: return new OpResultInfo(opName, argTypes, args, opReturn);
243: }
244: }
|