Java examples for javax.management:MBean
Sets a list of MBean attributes.
/**/*from ww w . jav a 2 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.beans.BeanInfo; import java.beans.Introspector; import java.lang.management.ManagementFactory; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.Hashtable; import java.util.List; import java.util.Map; import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.management.Attribute; import javax.management.AttributeList; import javax.management.DynamicMBean; import javax.management.MBeanInfo; import javax.management.MBeanServer; import javax.management.MBeanServerConnection; import javax.management.MBeanServerFactory; import javax.management.MBeanServerInvocationHandler; import javax.management.ObjectName; import javax.management.openmbean.CompositeData; import javax.management.remote.JMXConnector; import javax.management.remote.JMXConnectorFactory; import javax.management.remote.JMXServiceURL; import java.util.regex.*; import javax.management.*; public class Main{ /** * Sets a list of MBean attributes. Throws an exception on any failure. Returns a map of successfully set values. * @param on the object name * @param server the mbean server * @param attributes The attributes to set * @return a map of successfully set values. */ public static Map<String, Object> setAttributes(CharSequence on, MBeanServerConnection server, Object... attributes) { Map<String, Object> returnValues = new HashMap<String, Object>( attributes.length); Collection<NVP> list = NVP.generate(attributes); for (NVP nvp : list) { setAttribute(on, server, nvp.getName(), nvp.getValue()); returnValues.put(nvp.getName(), nvp.getValue()); } return returnValues; } /** * Sets an MBean attribute. * @param on The object name * @param server The mbean server * @param name The attribute name * @param value The attribute value */ public static void setAttribute(CharSequence on, MBeanServerConnection server, String name, Object value) { try { server.setAttribute(objectName(on), new Attribute(name, value)); } catch (Exception e) { throw new RuntimeException("Failed to set Attribute", e); } } /** * 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().trim()); } catch (Exception e) { throw new RuntimeException("Failed to create Object Name", e); } } /** * Creates a new JMX object name. * @param on An object representing the ObjectName * @return an ObjectName the created ObjectName */ public static ObjectName objectName(Object on) { try { return new ObjectName(on.toString().trim()); } 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); } } }