Example usage for javax.management ObjectInstance getObjectName

List of usage examples for javax.management ObjectInstance getObjectName

Introduction

In this page you can find the example usage for javax.management ObjectInstance getObjectName.

Prototype

public ObjectName getObjectName() 

Source Link

Document

Returns the object name part.

Usage

From source file:org.sakaiproject.status.StatusServlet.java

protected void reportAllMBeanDetails(HttpServletResponse response) throws Exception {
    PrintWriter pw = response.getWriter();

    Set<ObjectInstance> allBeans = mbs.queryMBeans(null, null);
    SortedSet sortedBeanNames = new TreeSet();
    for (ObjectInstance bean : allBeans) {
        sortedBeanNames.add(bean.getObjectName().toString());
    }/*w  w w . j  a  va 2 s.c o  m*/
    for (Object beanName : sortedBeanNames) {
        pw.print(beanName.toString() + "\n");
        ObjectName beanObjectName = new ObjectName(beanName.toString());
        for (MBeanAttributeInfo mbai : mbs.getMBeanInfo(beanObjectName).getAttributes()) {
            pw.print("  ");
            pw.print(mbai.getName() + ",");
            pw.print(mbai.getType() + ",");
            pw.print(mbai.getDescription() + ",");
            pw.print(mbs.getAttribute(beanObjectName, mbai.getName()) + "\n");
        }
        pw.print("\n");
        for (MBeanOperationInfo mboi : mbs.getMBeanInfo(beanObjectName).getOperations()) {
            pw.print("  ");
            pw.print(mboi.getReturnType() + ",");
            pw.print(mboi.getName() + "(");
            for (MBeanParameterInfo mbpi : mboi.getSignature()) {
                pw.print(mbpi.getType() + " " + mbpi.getName() + ",");
            }
            pw.print("),");
            pw.print(mboi.getDescription() + "\n");
        }
        pw.print("\n-----------------------------\n\n");
    }
}

From source file:org.springframework.integration.jmx.DefaultMBeanObjectConverter.java

@Override
public Object convert(MBeanServerConnection connection, ObjectInstance instance) {
    Map<String, Object> attributeMap = new HashMap<String, Object>();

    try {//  w w w .j ava  2  s  .  c  o  m
        ObjectName objName = instance.getObjectName();
        if (!connection.isRegistered(objName)) {
            return attributeMap;
        }

        MBeanInfo info = connection.getMBeanInfo(objName);
        MBeanAttributeInfo[] attributeInfos = info.getAttributes();

        for (MBeanAttributeInfo attrInfo : attributeInfos) {
            // we don't need to repeat name of this as an attribute
            if ("ObjectName".equals(attrInfo.getName()) || !this.filter.accept(objName, attrInfo.getName())) {
                continue;
            }

            Object value;
            try {
                value = connection.getAttribute(objName, attrInfo.getName());
            } catch (RuntimeMBeanException e) {
                // N.B. standard MemoryUsage MBeans will throw an exception when some
                // measurement is unsupported. Logging at trace rather than debug to
                // avoid confusion.
                if (log.isTraceEnabled()) {
                    log.trace("Error getting attribute '" + attrInfo.getName() + "' on '" + objName + "'", e);
                }

                // try to unwrap the exception somewhat; not sure this is ideal
                Throwable t = e;
                while (t.getCause() != null) {
                    t = t.getCause();
                }
                value = String.format("%s[%s]", t.getClass().getName(), t.getMessage());
            }

            attributeMap.put(attrInfo.getName(), checkAndConvert(value));
        }

    } catch (Exception e) {
        throw new IllegalArgumentException(e);
    }

    return attributeMap;
}

From source file:org.springframework.integration.jmx.NotificationListeningMessageProducer.java

protected Collection<ObjectName> retrieveMBeanNames() {
    List<ObjectName> objectNames = new ArrayList<ObjectName>();
    for (ObjectName pattern : this.objectNames) {
        Set<ObjectInstance> mBeanInfos;
        try {/*from  ww w .j a  v a  2 s .c  om*/
            mBeanInfos = this.server.queryMBeans(pattern, null);
        } catch (IOException e) {
            throw new IllegalStateException("IOException on MBeanServerConnection.", e);
        }
        if (mBeanInfos.size() == 0 && logger.isDebugEnabled()) {
            logger.debug("No MBeans found matching pattern:" + pattern);
        }
        for (ObjectInstance instance : mBeanInfos) {
            if (logger.isDebugEnabled()) {
                logger.debug("Found MBean:" + instance.getObjectName().toString());
            }
            objectNames.add(instance.getObjectName());
        }
    }
    return objectNames;
}

From source file:org.springframework.jmx.export.MBeanExporter.java

/**
 * Actually registers the MBean with the server. The behavior when encountering
 * an existing MBean can be configured using the {@link #setRegistrationBehavior(int)}
 * and {@link #setRegistrationBehaviorName(String)} methods.
 *///from  w w w . ja v  a2 s  . com
private ObjectName doRegister(Object mbean, ObjectName objectName) throws JMException {
    ObjectInstance registeredBean = null;
    try {
        registeredBean = this.server.registerMBean(mbean, objectName);
    } catch (InstanceAlreadyExistsException ex) {
        if (this.registrationBehavior == REGISTRATION_IGNORE_EXISTING) {
            if (logger.isDebugEnabled()) {
                logger.debug("Ignoring existing MBean at [" + objectName + "]");
            }
        } else if (this.registrationBehavior == REGISTRATION_REPLACE_EXISTING) {
            try {
                if (logger.isDebugEnabled()) {
                    logger.debug("Replacing existing MBean at [" + objectName + "]");
                }
                this.server.unregisterMBean(objectName);
                registeredBean = this.server.registerMBean(mbean, objectName);
            } catch (InstanceNotFoundException ex2) {
                throw new IllegalStateException(
                        "Unable to replace existing MBean at [" + objectName + "]: " + ex.getMessage());
            }
        } else {
            throw ex;
        }
    }
    // Track registration and notify listeners.
    ObjectName actualObjectName = (registeredBean != null ? registeredBean.getObjectName() : null);
    if (actualObjectName == null) {
        actualObjectName = objectName;
    }
    return actualObjectName;
}

From source file:org.springframework.jmx.support.MBeanRegistrationSupport.java

/**
 * Actually register the MBean with the server. The behavior when encountering
 * an existing MBean can be configured using {@link #setRegistrationPolicy}.
 * @param mbean the MBean instance/*from  w w w  . j  a v a  2 s. c  o  m*/
 * @param objectName the suggested ObjectName for the MBean
 * @throws JMException if the registration failed
 */
protected void doRegister(Object mbean, ObjectName objectName) throws JMException {
    Assert.state(this.server != null, "No MBeanServer set");
    ObjectName actualObjectName;

    synchronized (this.registeredBeans) {
        ObjectInstance registeredBean = null;
        try {
            registeredBean = this.server.registerMBean(mbean, objectName);
        } catch (InstanceAlreadyExistsException ex) {
            if (this.registrationPolicy == RegistrationPolicy.IGNORE_EXISTING) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Ignoring existing MBean at [" + objectName + "]");
                }
            } else if (this.registrationPolicy == RegistrationPolicy.REPLACE_EXISTING) {
                try {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Replacing existing MBean at [" + objectName + "]");
                    }
                    this.server.unregisterMBean(objectName);
                    registeredBean = this.server.registerMBean(mbean, objectName);
                } catch (InstanceNotFoundException ex2) {
                    logger.error("Unable to replace existing MBean at [" + objectName + "]", ex2);
                    throw ex;
                }
            } else {
                throw ex;
            }
        }

        // Track registration and notify listeners.
        actualObjectName = (registeredBean != null ? registeredBean.getObjectName() : null);
        if (actualObjectName == null) {
            actualObjectName = objectName;
        }
        this.registeredBeans.add(actualObjectName);
    }

    onRegister(actualObjectName, mbean);
}

From source file:org.wso2.carbon.as.monitoring.collector.jmx.clients.MBeanClient.java

/**
 * Read the possible attribute values from the MBean
 *
 * @return List of Result objects//from  w w w  . j  av a 2 s  . co m
 */
public List<Result> readPossibleAttributeValues() throws MalformedObjectNameException {
    ObjectName name = new ObjectName(getObjectNameQuery());
    Set<ObjectInstance> instances = server.queryMBeans(name, null);

    List<Result> results = new ArrayList<Result>();
    for (ObjectInstance instance : instances) {
        ObjectName objectName = instance.getObjectName();

        AttributeList attributes = null;
        try {
            attributes = server.getAttributes(objectName, getAttributeNames());
        } catch (InstanceNotFoundException ignored) {
            // Here we put best-effort to grab any attributes.
            // Missing objects are ignored. We need whatever possible.
            if (LOG.isDebugEnabled()) {
                LOG.debug(objectName + " MBean not found : " + ignored.getMessage(), ignored);
            }
        } catch (ReflectionException ignored) {
            // Here we put best-effort to grab any attributes.
            // erroneous attributes are ignored. We need whatever possible.
            if (LOG.isDebugEnabled()) {
                LOG.debug("Exception occurred while reading the attributes from " + objectName, ignored);
            }
        }

        if (attributes != null) {
            attributes.addAll(getPropertiesFromKey(objectName));
        }

        Result result = new Result(getCorrelationKey(objectName), attributes);
        results.add(result);
    }

    return results;
}

From source file:org.xmatthew.spy2servers.component.spy.jmx.ActiveMQJmxSpyComponent.java

/**
 * @param objectInstance/*  ww  w . ja v  a 2s  .  c  o m*/
 * @param mbsc
 * @throws Exception
 */
private void inspectConnectionMBean(ObjectInstance objectInstance, MBeanServerConnection mbsc)
        throws Exception {
    Map<String, Object> beansMap = getAttributesAsMap(objectInstance.getObjectName().toString(), mbsc);
    if (beansMap != null) {
        if (CollectionUtils.isBlankCollection(llegalIps)) {
            return;
        }

        String address = String.valueOf(beansMap.get(REMOTEADDRESS));
        if (StringUtils.isBlank(address)) {
            return;
        }
        address = StringUtils.removeStart(address, ADDRESS_PREFIX);
        address = StringUtils.substringBefore(address, COLON_SIGN);

        if (!llegalIps.contains(address)) {
            if (invalidIps == null) {
                invalidIps = new LinkedHashSet<String>();
            }
            if (!invalidIps.contains(address)) {
                onSpy(createMessage(address, ILLEGAL_ADDRESS, Message.LV_ERROR, IP_INSPECT_TYPE, beansMap));
                invalidIps.add(address);
            }
        }

    }
}

From source file:org.xmatthew.spy2servers.component.spy.jmx.ActiveMQJmxSpyComponent.java

/**
 * @param objectInstance// w w  w. ja va 2s.  co m
 * @param mbsc
 * @throws Exception
 */
private void inspectQueueMBean(ObjectInstance objectInstance, MBeanServerConnection mbsc) throws Exception {
    Map<String, Object> beansMap = getAttributesAsMap(objectInstance.getObjectName().toString(), mbsc);
    if (beansMap != null) {
        String queueName = String.valueOf(beansMap.get(NAME));
        if (destinationNamesToWatch != null && !destinationNamesToWatch.contains(queueName)) {
            return;
        }

        DestinationStatus destinationStatus = null;
        if (StringConstant.ZORE.equals(String.valueOf(beansMap.get(CONSUMER_COUNT_NAME)))) {

            boolean isNeedSpy = true;
            if (!destinationStatusMap.containsKey(queueName)) {
                destinationStatus = new DestinationStatus(beansMap, new Date());
                destinationStatusMap.put(queueName, destinationStatus);
            } else {
                destinationStatus = destinationStatusMap.get(queueName);
                if (!destinationStatus.isConsumerZero()) {
                    destinationStatus.setStatusStartDate(new Date());
                }
            }

            if (destinationStatus.isNotified()
                    || destinationStatus.getStatusKeepTime() < queueSuspendNotifyTime) {
                isNeedSpy = false;
            }

            if (isNeedSpy && destinationStatus != null) {
                //DestinationStatus status = new DestinationStatus(beansMap, new Date());
                String description = QUEUE_CONSUMER_SUSPEND;
                destinationStatus.setBeansMap(beansMap);
                String body = beansMap.get(NAME).toString();
                onSpy(createMessage(body, description, QUEUE_INSPECT_TYPE, Message.LV_ERROR,
                        destinationStatus));
                destinationStatus.doNotifyStatus();
                //destinationStatus.setStatusStartDate(new Date());

                //destinationStatusMap.put(queueName, destinationStatus);
            }
        } else {
            if (destinationStatusMap.containsKey(queueName)) {
                destinationStatus = destinationStatusMap.get(queueName);
                if (destinationStatus.isConsumerZero()) {
                    if (destinationStatus.isNotified()) {
                        String description = QUEUE_CONSUMER_ACTIVE;
                        String body = beansMap.get(NAME).toString();
                        destinationStatus.setBeansMap(beansMap);
                        onSpy(createMessage(body, description, QUEUE_INSPECT_TYPE, Message.LV_INFO,
                                destinationStatus));
                        destinationStatus.setStatusStartDate(new Date());
                    }
                    destinationStatus.cancelNotifyStatus();
                }
            } else {
                destinationStatus = new DestinationStatus(beansMap, new Date());
            }
        }

        if (destinationStatus != null) {
            destinationStatus.setBeansMap(beansMap);
            destinationStatusMap.put(queueName, destinationStatus);
        }
    }
}

From source file:org.xmatthew.spy2servers.component.spy.jmx.TomcatJmxSpyComponent.java

@Override
protected void inspectMBean(ObjectInstance objectInstance, MBeanServerConnection mbsc) throws Exception {
    super.inspectMBean(objectInstance, mbsc);

    String mBeanClass = objectInstance.getClassName();
    ObjectName objectName = objectInstance.getObjectName();

    if (BASEMODELBEAN_CLASS.equals(mBeanClass) || BASEMODELBAEN_CLASS_CHANGED.equals(mBeanClass)) {
        String type = objectName.getKeyProperty(JmxSpySupportComponent.TYPE);
        String name = objectName.getKeyProperty(JmxSpySupportComponent.NAME);
        name = StringUtils.remove(name, "\"");
        //is a DataSource mbean
        if (DATASOURCE.equals(type)) {
            dataSourceMBeanSpy(name, getAttributesAsMap(objectName.toString(), mbsc, getDataSourceKeys()));

        } else if (MANAGER.equals(type)) { //is a web module mbean 

        }/*from w w  w  .  ja va  2s . c  om*/
    }
}

From source file:psiprobe.beans.JvmMemoryInfoAccessorBean.java

/**
 * Gets the pools./* ww w.  ja  v a  2  s.  com*/
 *
 * @return the pools
 * @throws Exception the exception
 */
public List<MemoryPool> getPools() throws Exception {

    List<MemoryPool> memoryPools = new LinkedList<>();
    MBeanServer mbeanServer = new Registry().getMBeanServer();
    Set<ObjectInstance> memoryOPools = mbeanServer.queryMBeans(new ObjectName("java.lang:type=MemoryPool,*"),
            null);

    // totals
    long totalInit = 0;
    long totalMax = 0;
    long totalUsed = 0;
    long totalCommitted = 0;

    for (ObjectInstance oi : memoryOPools) {
        ObjectName objName = oi.getObjectName();
        MemoryPool memoryPool = new MemoryPool();
        memoryPool.setName(JmxTools.getStringAttr(mbeanServer, objName, "Name"));
        memoryPool.setType(JmxTools.getStringAttr(mbeanServer, objName, "Type"));

        CompositeDataSupport cd = (CompositeDataSupport) mbeanServer.getAttribute(objName, "Usage");
        /*
         * It seems that "Usage" attribute of one of the pools may turn into null intermittently. We
         * better have a dip in the graph then an NPE though.
         */
        if (cd != null) {
            memoryPool.setMax(JmxTools.getLongAttr(cd, "max"));
            memoryPool.setUsed(JmxTools.getLongAttr(cd, "used"));
            memoryPool.setInit(JmxTools.getLongAttr(cd, "init"));
            memoryPool.setCommitted(JmxTools.getLongAttr(cd, "committed"));
        } else {
            logger.error("Oops, JVM problem? {} 'Usage' attribute is NULL!", objName);
        }

        totalInit += memoryPool.getInit();
        totalMax += memoryPool.getMax();
        totalUsed += memoryPool.getUsed();
        totalCommitted += memoryPool.getCommitted();

        memoryPools.add(memoryPool);
    }

    if (!memoryPools.isEmpty()) {
        MemoryPool pool = new MemoryPool();
        pool.setName("Total");
        pool.setType("TOTAL");
        pool.setInit(totalInit);
        pool.setUsed(totalUsed);
        pool.setMax(totalMax);
        pool.setCommitted(totalCommitted);
        memoryPools.add(pool);
    }

    return memoryPools;

}