Java examples for javax.management:ObjectName
Creates a new JMX object name.
//package com.java2s; import java.lang.reflect.AccessibleObject; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.Hashtable; public class Main { /**//from w w w .j a va 2s. c o m * 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 divined from the passed class * @param clazz The class to create an ObjectName from * @return an ObjectName the created ObjectName */ public static ObjectName objectName(Class<?> clazz) { try { return new ObjectName(new StringBuilder(clazz.getPackage() .getName()).append(":service=") .append(clazz.getSimpleName()).toString()); } catch (Exception e) { throw new RuntimeException("Failed to create Object Name", e); } } /** * Creates a new JMX object name. * @param format The string format template * @param args The arguments to populate the template with * @return an ObjectName the created ObjectName */ public static ObjectName objectName(String format, Object... args) { try { return new ObjectName(String.format(format.trim(), args)); } catch (MalformedObjectNameException moex) { if (moex.getMessage().startsWith("Invalid character")) { for (int i = 0; i < args.length; i++) { args[i] = ObjectName.quote(args[i].toString()); } return objectName(String.format(format.trim(), args)); } throw new RuntimeException("Failed to create Object Name", moex); } catch (Exception e) { throw new RuntimeException("Failed to create Object Name", e); } } /** * Creates a new JMX ObjectName from the passed AccessibleObject * @param ao The AccessibleObject to create an ObjectName for * @return the ObjectName */ public static ObjectName objectName(AccessibleObject ao) { try { Class<?> clazz = getDeclaringClass(ao); StringBuilder b = new StringBuilder(clazz.getPackage() .getName()).append(":"); b.append("class=").append(clazz.getSimpleName()).append(","); b.append("method=").append(getName(ao)); return new ObjectName(b.toString()); } catch (Exception e) { throw new RuntimeException("Failed to create Object Name [" + getGenericString(ao) + "]", 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 [" + on + "]", e); } } /** * Creates a new JMX ObjectName from the passed class and method name * @param clazz The class * @param methodName The method name to create an ObjectName for * @return the ObjectName */ public static ObjectName objectName(Class<?> clazz, String methodName) { try { StringBuilder b = new StringBuilder(clazz.getPackage() .getName()).append(":"); b.append("class=").append(clazz.getSimpleName()).append(","); b.append("method=").append(methodName); return new ObjectName(b.toString()); } catch (Exception e) { throw new RuntimeException("Failed to create Object Name [" + clazz.getName() + "/" + methodName + "]", 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); } } /** * Gets the name of the passed AccessibleObject * @param ao the AccessibleObject to get the name for * @return the name of the Accessible Object */ public static String getName(AccessibleObject ao) { if (ao == null) return null; if (ao instanceof Method) { return ((Method) ao).getName(); } else if (ao instanceof Constructor) { return ((Constructor<?>) ao).getDeclaringClass() .getSimpleName(); } else if (ao instanceof Field) { return ((Field) ao).getName(); } else { throw new RuntimeException("Unknow AccessibleObject type [" + ao.getClass().getName() + "]"); } } /** * Gets the declaring class of the passed AccessibleObject * @param ao the AccessibleObject to get the declaring class for * @return the declaring class */ public static Class<?> getDeclaringClass(AccessibleObject ao) { if (ao == null) return null; if (ao instanceof Method) { return ((Method) ao).getDeclaringClass(); } else if (ao instanceof Constructor) { return ((Constructor<?>) ao).getDeclaringClass(); } else if (ao instanceof Field) { return ((Field) ao).getDeclaringClass(); } else { throw new RuntimeException("Unknow AccessibleObject type [" + ao.getClass().getName() + "]"); } } /** * Gets the generic string of the passed AccessibleObject * @param ao the AccessibleObject to get the generic string for * @return the generic string of the Accessible Object */ public static String getGenericString(AccessibleObject ao) { if (ao == null) return null; if (ao instanceof Method) { return ((Method) ao).toGenericString(); } else if (ao instanceof Constructor) { return ((Constructor<?>) ao).toGenericString(); } else if (ao instanceof Field) { Field f = (Field) ao; return String.format("%s:%s(%s)", f.getDeclaringClass() .getName(), f.getName(), f.getType().getName()); } else { throw new RuntimeException("Unknow AccessibleObject type [" + ao.getClass().getName() + "]"); } } }