Example usage for javax.management ObjectName toString

List of usage examples for javax.management ObjectName toString

Introduction

In this page you can find the example usage for javax.management ObjectName toString.

Prototype

@Override
public String toString() 

Source Link

Document

Returns a string representation of the object name.

Usage

From source file:io.fabric8.mq.controller.util.BrokerJmxUtils.java

public static List<ObjectName> getDestinations(J4pClient client, ObjectName root, String type)
        throws Exception {
    List<ObjectName> list = new ArrayList<>();
    String objectNameStr = root.toString() + ",destinationType=" + type + ",destinationName=*";
    J4pResponse<J4pReadRequest> response = client
            .execute(new J4pReadRequest(new ObjectName(objectNameStr), "Name"));
    JSONObject value = response.getValue();
    for (Object key : value.keySet()) {
        ObjectName objectName = new ObjectName(key.toString());
        list.add(objectName);/* w w  w . j ava 2 s.c om*/
    }

    return list;
}

From source file:org.skfiy.typhon.util.MBeanUtils.java

/**
 * Bean.//  w w w.j a  va  2 s .c o  m
 *
 * @param obj 
 * @param objName {@code ObjectName }
 * @param type 
 */
public static void registerComponent(Object obj, ObjectName objName, String type) {
    try {
        REGISTRY.registerComponent(obj, objName, type);
    } catch (Exception ex) {
        throw new TyphonException(objName.toString(), ex);
    }
}

From source file:org.apache.catalina.mbeans.MBeanDumper.java

/**
 * The following code to dump MBeans has been copied from JMXProxyServlet.
 *
 *///ww  w.  ja  v  a  2s. c  om
public static String dumpBeans(MBeanServer mbeanServer, Set<ObjectName> names) {
    StringBuilder buf = new StringBuilder();
    Iterator<ObjectName> it = names.iterator();
    while (it.hasNext()) {
        ObjectName oname = it.next();
        buf.append("Name: ");
        buf.append(oname.toString());
        buf.append(CRLF);

        try {
            MBeanInfo minfo = mbeanServer.getMBeanInfo(oname);
            // can't be null - I think
            String code = minfo.getClassName();
            if ("org.apache.commons.modeler.BaseModelMBean".equals(code)) {
                code = (String) mbeanServer.getAttribute(oname, "modelerType");
            }
            buf.append("modelerType: ");
            buf.append(code);
            buf.append(CRLF);

            MBeanAttributeInfo attrs[] = minfo.getAttributes();
            Object value = null;

            for (int i = 0; i < attrs.length; i++) {
                if (!attrs[i].isReadable())
                    continue;
                String attName = attrs[i].getName();
                if ("modelerType".equals(attName))
                    continue;
                if (attName.indexOf("=") >= 0 || attName.indexOf(":") >= 0 || attName.indexOf(" ") >= 0) {
                    continue;
                }

                try {
                    value = mbeanServer.getAttribute(oname, attName);
                } catch (JMRuntimeException rme) {
                    Throwable cause = rme.getCause();
                    if (cause instanceof UnsupportedOperationException) {
                        if (log.isDebugEnabled()) {
                            log.debug("Error getting attribute " + oname + " " + attName, rme);
                        }
                    } else if (cause instanceof NullPointerException) {
                        if (log.isDebugEnabled()) {
                            log.debug("Error getting attribute " + oname + " " + attName, rme);
                        }
                    } else {
                        log.error("Error getting attribute " + oname + " " + attName, rme);
                    }
                    continue;
                } catch (Throwable t) {
                    ExceptionUtils.handleThrowable(t);
                    log.error("Error getting attribute " + oname + " " + attName, t);
                    continue;
                }
                if (value == null)
                    continue;
                String valueString;
                try {
                    Class<?> c = value.getClass();
                    if (c.isArray()) {
                        int len = Array.getLength(value);
                        StringBuilder sb = new StringBuilder(
                                "Array[" + c.getComponentType().getName() + "] of length " + len);
                        if (len > 0) {
                            sb.append(CRLF);
                        }
                        for (int j = 0; j < len; j++) {
                            sb.append("\t");
                            Object item = Array.get(value, j);
                            if (item == null) {
                                sb.append("NULL VALUE");
                            } else {
                                try {
                                    sb.append(escape(item.toString()));
                                } catch (Throwable t) {
                                    ExceptionUtils.handleThrowable(t);
                                    sb.append("NON-STRINGABLE VALUE");
                                }
                            }
                            if (j < len - 1) {
                                sb.append(CRLF);
                            }
                        }
                        valueString = sb.toString();
                    } else {
                        valueString = escape(value.toString());
                    }
                    buf.append(attName);
                    buf.append(": ");
                    buf.append(valueString);
                    buf.append(CRLF);
                } catch (Throwable t) {
                    ExceptionUtils.handleThrowable(t);
                }
            }
        } catch (Throwable t) {
            ExceptionUtils.handleThrowable(t);
        }
        buf.append(CRLF);
    }
    return buf.toString();

}

From source file:org.apache.hadoop.hbase.util.JSONMetricUtil.java

public static Object getValueFromMBean(ObjectName bean, String attribute) {
    Object value = null;//from  w  ww  .  j  a v  a  2  s  . co  m
    try {
        value = mbServer.getAttribute(bean, attribute);
    } catch (Exception e) {
        LOG.error("Unable to get value from MBean= " + bean.toString() + "for attribute=" + attribute + " "
                + e.getMessage());
    }
    return value;
}

From source file:de.jgoldhammer.alfresco.jscript.jmx.JmxDumpUtil.java

/**
 * Dumps a local or remote MBeanServer's entire object tree for support
 * purposes. Nested arrays and CompositeData objects in MBean attribute
 * values are handled.//from  w  w w.  jav  a2 s . co  m
 * 
 * @param connection
 *            the server connection (or server itself)
 * @param out
 *            PrintWriter to write the output to
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static void dumpConnection(MBeanServerConnection connection, PrintWriter out) throws IOException {
    JmxDumpUtil.showStartBanner(out);

    // Get all the object names
    Set<ObjectName> objectNames = connection.queryNames(null, null);

    // Sort the names (don't assume ObjectName implements Comparable in JDK
    // 1.5)
    Set<ObjectName> newObjectNames = new TreeSet<ObjectName>(new Comparator<ObjectName>() {
        public int compare(ObjectName o1, ObjectName o2) {
            return o1.toString().compareTo(o2.toString());
        }
    });
    newObjectNames.addAll(objectNames);
    objectNames = newObjectNames;

    // Dump each MBean
    for (ObjectName objectName : objectNames) {
        try {
            printMBeanInfo(connection, objectName, out, null);
        } catch (JMException e) {
            // Sometimes beans can disappear while we are examining them
        }
    }
}

From source file:de.jgoldhammer.alfresco.jscript.jmx.JmxDumpUtil.java

/**
 * Dumps the details of a single MBean./* w ww  .  ja  va2s.c o  m*/
 * 
 * @param connection
 *            the server connection (or server itself)
 * @param objectName
 *            the object name
 * @param out
 *            PrintWriter to write the output to
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 * @throws JMException
 *             Signals a JMX error
 */
public static Map<Object, Object> getSimpleMBeanInfo(MBeanServerConnection connection, ObjectName objectName)
        throws IOException, JMException {
    Map<Object, Object> attributes = new TreeMap<Object, Object>();
    MBeanInfo info = connection.getMBeanInfo(objectName);
    attributes.put("** Object Name", objectName.toString());
    attributes.put("** Object Type", info.getClassName());

    for (MBeanAttributeInfo element : info.getAttributes()) {
        Object value;
        if (element.isReadable()) {
            try {
                value = connection.getAttribute(objectName, element.getName());
            } catch (Exception e) {
                value = JmxDumpUtil.UNREADABLE_VALUE;
            }
        } else {
            value = JmxDumpUtil.UNREADABLE_VALUE;
        }
        attributes.put(element.getName(), value);
    }
    return attributes;
}

From source file:de.jgoldhammer.alfresco.jscript.jmx.JmxDumpUtil.java

/**
 * Dumps the details of a single MBean.//from ww w  .jav a2  s.c om
 * 
 * @param connection
 *            the server connection (or server itself)
 * @param objectName
 *            the object name
 * @param out
 *            PrintWriter to write the output to
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 * @throws JMException
 *             Signals a JMX error
 */
public static void printMBeanInfo(MBeanServerConnection connection, ObjectName objectName, PrintWriter out,
        String attributeName) throws IOException, JMException {
    Map<String, Object> attributes = new TreeMap<String, Object>();
    MBeanInfo info = connection.getMBeanInfo(objectName);
    attributes.put("** Object Name", objectName.toString());
    attributes.put("** Object Type", info.getClassName());

    if (StringUtils.isNotBlank(attributeName)) {
        Object value;
        try {
            value = connection.getAttribute(objectName, attributeName);
        } catch (Exception e) {
            value = JmxDumpUtil.UNREADABLE_VALUE;
        }
        outputValue(out, value, 10);
    } else {

        for (MBeanAttributeInfo element : info.getAttributes()) {
            Object value;
            if (element.isReadable()) {
                try {
                    value = connection.getAttribute(objectName, element.getName());
                } catch (Exception e) {
                    value = JmxDumpUtil.UNREADABLE_VALUE;
                }
            } else {
                value = JmxDumpUtil.UNREADABLE_VALUE;
            }
            attributes.put(element.getName(), value);
        }
        tabulate(JmxDumpUtil.NAME_HEADER, JmxDumpUtil.VALUE_HEADER, attributes, out, 0);
    }
}

From source file:org.hyperic.hq.plugin.websphere.WebsphereUtil.java

public static double getMBeanCount(AdminClient mServer, ObjectName query, String attr)
        throws MetricUnreachableException, MetricNotFoundException {

    double count = 0;

    try {//from  w  ww .j  ava2s.c  o  m
        Set beansSet = mServer.queryNames(query, null);
        for (Iterator it = beansSet.iterator(); it.hasNext();) {
            ObjectName name = (ObjectName) it.next();
            try {
                if (attr != null) {
                    mServer.getAttribute(name, attr);
                }
                count++;
            } catch (AttributeNotFoundException e) {
                throw new MetricNotFoundException(name.toString());
            } catch (InstanceNotFoundException e) {
                throw new MetricNotFoundException(name.toString());
            } catch (Exception e) {
                //e.g. unauthorized
                throw new MetricUnreachableException(name + ": " + e.getMessage(), e);
            }
        }
    } catch (ConnectorException e) {
        throw new MetricUnreachableException(query.toString(), e);
    }

    if (count == 0) {
        throw new MetricNotFoundException(query + " (Invalid node name?)");
    }

    return count;
}

From source file:com.continuent.tungsten.common.jmx.JmxManager.java

public static ObjectName generateMBeanObjectName(Class<?> mbeanClass) throws Exception {
    String className = mbeanClass.getName();
    String type = className;/*  w  w w.jav a2s. c om*/
    String domain = "default";

    int lastPeriod = className.lastIndexOf('.');
    if (lastPeriod != -1) {
        domain = className.substring(0, lastPeriod);
        type = className.substring(className.lastIndexOf('.') + 1);
    }

    String name = String.format("%s:type=%s", domain, type);
    ObjectName objName = new ObjectName(name);

    if (logger.isDebugEnabled()) {
        logger.debug("ObjectName is: " + objName.toString());
    }
    return objName;
}

From source file:com.continuent.tungsten.common.jmx.JmxManager.java

public static ObjectName generateMBeanObjectName(String mbeanName, String typeName) throws Exception {
    ObjectName name = new ObjectName(mbeanName + ":type=" + typeName);
    if (logger.isDebugEnabled()) {
        logger.debug("ObjectName is: " + name.toString());
    }/*from   w w  w. j a v a2s . c o  m*/
    return name;
}