Here you can find the source of getAttribute(ObjectName on, MBeanServerConnection server, String name)
Parameter | Description |
---|---|
on | a parameter |
server | a parameter |
name | a parameter |
public static Object getAttribute(ObjectName on, MBeanServerConnection server, String name)
//package com.java2s; /**/*from www. j ava2 s .c om*/ * Helios, OpenSource Monitoring * Brought to you by the Helios Development Group * * Copyright 2007, Helios Development Group and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. * */ import java.util.Collection; import java.util.HashMap; import java.util.Hashtable; import java.util.Map; import javax.management.openmbean.CompositeData; public class Main { /** * An regex pattern to parse A[X/Y/Z...] */ public static final Pattern OBJECT_NAME_ATTR_PATTERN = Pattern.compile("(\\S+)\\[(\\S+)\\]"); /** * Gets an attribute value from an mbean. * @param on * @param server * @param name * @return */ public static Object getAttribute(ObjectName on, MBeanServerConnection server, String name) { try { return server.getAttribute(on, name); } catch (Exception e) { throw new RuntimeException("Failed to get attribute", e); } } /** * Retrieves the named attribute from the MBean with the passed ObjectName in the passed MBeanServerConnection. * If the retrieval results in an exception or a null, the default value is returned * @param conn The MBeanServerConnection to the MBeanServer where the target MBean is registered * @param objectName The ObjectName of the target MBean * @param attributeName The attribute name * @param defaultValue The default value * @return The attribute value or the defaut value */ @SuppressWarnings("unchecked") public static <T> T getAttribute(MBeanServerConnection conn, ObjectName objectName, String attributeName, T defaultValue) { try { T t = (T) conn.getAttribute(objectName, attributeName); return t == null ? defaultValue : t; } catch (Exception e) { return defaultValue; } } /** * Retrieves an attribute from an MBeanServer connection. * The compound name is in the format <b><code><ObjectName>[<Fragment<i>1</i>>/<Fragment<i>2</i>>/<Fragment<i>n</i>>]</code></b>. * The multiple fragment names represent support for nested fields in a composite type. * To retrieve a standard "flat" attribute, simply supply one fragment. * @param conn The MBeanServer connection * @param compoundName The compound name * @return the attribute value or null. */ public static Object getAttribute(MBeanServerConnection conn, CharSequence compoundName) { try { Matcher m = OBJECT_NAME_ATTR_PATTERN.matcher(compoundName); if (m.find()) { String objName = m.group(1); String[] fragments = m.group(2).split("/"); return getAttribute(conn, objName, fragments); } else { return null; } } catch (Exception e) { return null; } } /** * Retrieves an attribute from an MBeanServer connection. * @param conn The MBeanServer connection * @param objectName The ObjectName * @param attrs the compound attribute name in the format <b><code><Fragment<i>1</i>>/<Fragment<i>2</i>>/<Fragment<i>n</i>></code></b>. * @return the attribute value or null. */ public static Object getAttribute(MBeanServerConnection conn, String objectName, String... attrs) { try { if (objectName != null && attrs != null && attrs.length > 0) { ObjectName on = objectName(objectName); StringBuilder b = new StringBuilder(); for (String s : attrs) { b.append(s).append("/"); } b.deleteCharAt(b.length() - 1); String key = b.toString(); Map<ObjectName, Map<String, Object>> map = getMBeanAttributeMap(conn, on, "/", key); return map.get(on).get(key); } } catch (Exception e) { } return null; } /** * Creates a new JMX object name. * @param on A string type representing the ObjectName string. * @return an ObjectName the created ObjectName */ public static ObjectName objectName(CharSequence on) { try { return new ObjectName(on.toString()); } catch (Exception e) { throw new RuntimeException("Failed to create Object Name", e); } } /** * Creates a new JMX object name by appending properties on the end of an existing name * @param on An existing ObjectName * @param props Appended properties in the for {@code key=value} * @return an ObjectName the created ObjectName */ public static ObjectName objectName(ObjectName on, CharSequence... props) { StringBuilder b = new StringBuilder(on.toString()); try { if (props != null) { for (CharSequence prop : props) { b.append(",").append(prop); } } return new ObjectName(b.toString()); } catch (Exception e) { throw new RuntimeException("Failed to create Object Name from [" + b + "]", e); } } /** * Creates a new JMX object name. * @param domain A string type representing the ObjectName domain * @param properties A hash table of the Object name's properties * @return an ObjectName the created ObjectName */ public static ObjectName objectName(CharSequence domain, Hashtable<String, String> properties) { try { return new ObjectName(domain.toString(), properties); } catch (Exception e) { throw new RuntimeException("Failed to create Object Name", e); } } /** * Creates a new JMX object name. * @param domain The ObjectName domain * @param nameValuePairs an (even lengthed) array of name value pairs making up the key properties * @return an ObjectName the created ObjectName */ public static ObjectName objectName(CharSequence domain, CharSequence... nameValuePairs) { if (domain == null || domain.toString().length() < 1) throw new IllegalArgumentException("Null or zero length domain name"); if (nameValuePairs == null || nameValuePairs.length < 1 || nameValuePairs.length % 2 != 0) { throw new IllegalArgumentException("Invalid number of namevaluepairs [" + (nameValuePairs == null ? 0 : nameValuePairs.length) + "]"); } try { Hashtable<String, String> props = new Hashtable<String, String>(); for (int i = 0; i < nameValuePairs.length; i++) { if (nameValuePairs[i] == null || nameValuePairs[i].toString().length() < 1) { throw new IllegalArgumentException("Null or blank nameValuePair entry at index [" + i + "]"); } String key = nameValuePairs[i].toString(); i++; if (nameValuePairs[i] == null || nameValuePairs[i].toString().length() < 1) { throw new IllegalArgumentException("Null or blank nameValuePair entry at index [" + i + "]"); } String value = nameValuePairs[i].toString(); props.put(key, value); } return new ObjectName(domain.toString(), props); } catch (IllegalArgumentException iae) { throw iae; } catch (Exception e) { throw new RuntimeException("Failed to create Object Name", e); } } /** * Retrieves maps of attribute values keyed by attribute name, in turn keyed by the ObjectName of the MBean. * @param server An MBeanServerConnection * @param objectName An ObjectName which can be absolute or a wildcard. * @param delimeter The delimeter for composite type compound names * @param attributeNames An array of absolute or compound attribute names. * @return a map of results. * TODO: TabularData * TODO: Collections / Maps / Arrays --> ref by index */ public static Map<ObjectName, Map<String, Object>> getMBeanAttributeMap(MBeanServerConnection server, ObjectName objectName, String delimeter, String... attributeNames) { if (server == null) throw new RuntimeException("MBeanServerConnection was null", new Throwable()); if (objectName == null) throw new RuntimeException("ObjectName was null", new Throwable()); if (attributeNames == null || attributeNames.length < 1) throw new RuntimeException("Attribute names array was null or zero length", new Throwable()); String[] rootNames = new String[attributeNames.length]; Map<String, String> compoundNames = new HashMap<String, String>(); for (int i = 0; i < attributeNames.length; i++) { String rootKey = null; if (attributeNames[i].contains(delimeter)) { String[] fragments = attributeNames[i].split(Pattern.quote(delimeter)); rootKey = fragments[0]; compoundNames.put(rootKey, attributeNames[i]); } else { rootKey = attributeNames[i]; } rootNames[i] = rootKey; } Map<ObjectName, Map<String, Object>> map = new HashMap<ObjectName, Map<String, Object>>(); try { for (ObjectName on : server.queryNames(objectName, null)) { AttributeList attrs = null; try { attrs = server.getAttributes(on, rootNames); if (attrs.size() < 1) continue; } catch (Exception e) { continue; } Map<String, Object> attrMap = new HashMap<String, Object>(); map.put(on, attrMap); for (Attribute attr : attrs.asList()) { Object value = attr.getValue(); if (value == null) continue; String name = attr.getName(); if (value instanceof CompositeData && compoundNames.containsKey(name)) { try { name = compoundNames.get(name); value = extractCompositeData((CompositeData) value, delimeter, name); } catch (Exception e) { continue; } } attrMap.put(name, value); } } } catch (Exception e) { throw new RuntimeException("Failed to acquire attribute names for ObjectName [" + objectName + "] for MBeanServer [" + server + "]", e); } return map; } /** * Retrieves maps of attribute values keyed by attribute name, in turn keyed by the ObjectName of the MBean. * @param server An MBeanServerConnection * @param objectName An ObjectName which can be absolute or a wildcard. * @param delimeter The delimeter for composite type compound names * @param attributeNames An collection of absolute or compound attribute names. * @return a map of results. */ public static Map<ObjectName, Map<String, Object>> getMBeanAttributeMap(MBeanServerConnection server, ObjectName objectName, String delimeter, Collection<String> attributeNames) { if (attributeNames == null || attributeNames.size() < 1) throw new RuntimeException("Attribute names collection was null or zero size", new Throwable()); return getMBeanAttributeMap(server, objectName, delimeter, attributeNames.toArray(new String[attributeNames.size()])); } /** * Returns a String->Object Map of the named attributes from the Mbean. * @param on The object name of the MBean. * @param server The MBeanServerConnection the MBean is registered in. * @param attributes An array of attribute names to retrieve. * @return A name value map of the requested attributes. */ public static Map<String, Object> getAttributes(ObjectName on, MBeanServerConnection server, String... attributes) { try { Map<String, Object> attrs = new HashMap<String, Object>(attributes.length); AttributeList attributeList = server.getAttributes(on, attributes); for (int i = 0; i < attributeList.size(); i++) { Attribute at = (Attribute) attributeList.get(i); if (isIn(at.getName(), attributes)) { attrs.put(at.getName(), at.getValue()); } } return attrs; } catch (Exception e) { throw new RuntimeException("Failed to getAttributes on [" + on + "]", e); } } /** * Extracts a composite data field from a CompositeData instance using a compound name. * @param cd The composite data instance * @param name The compound attribute name */ public static Object extractCompositeData(final CompositeData cd, final String delimeter, final String name) { String[] fragments = name.split(Pattern.quote(delimeter)); CompositeData ref = cd; Object value = null; for (int i = 1; i < fragments.length; i++) { value = ref.get(fragments[i]); if (value instanceof CompositeData) { ref = (CompositeData) value; } else { break; } } return value; } /** * Inspects the array to see if it contains the passed string. * @param name * @param array * @return true if the array contains the passed string. */ public static boolean isIn(String name, String[] array) { if (array == null || name == null) return false; for (String s : array) { if (s.equals(name)) return true; } return false; } }