Example usage for javax.management MBeanServerConnection getObjectInstance

List of usage examples for javax.management MBeanServerConnection getObjectInstance

Introduction

In this page you can find the example usage for javax.management MBeanServerConnection getObjectInstance.

Prototype

public ObjectInstance getObjectInstance(ObjectName name) throws InstanceNotFoundException, IOException;

Source Link

Document

Gets the ObjectInstance for a given MBean registered with the MBean server.

Usage

From source file:com.googlecode.jmxtrans.model.Query.java

public Iterable<Result> fetchResults(MBeanServerConnection mbeanServer, ObjectName queryName)
        throws InstanceNotFoundException, IntrospectionException, ReflectionException, IOException {
    MBeanInfo info = mbeanServer.getMBeanInfo(queryName);
    ObjectInstance oi = mbeanServer.getObjectInstance(queryName);

    List<String> attributes;
    if (attr.isEmpty()) {
        attributes = new ArrayList<>();
        for (MBeanAttributeInfo attrInfo : info.getAttributes()) {
            attributes.add(attrInfo.getName());
        }// w w w .  jav a  2s . c  o  m
    } else {
        attributes = attr;
    }

    try {
        if (!attributes.isEmpty()) {
            logger.debug("Executing queryName [{}] from query [{}]", queryName.getCanonicalName(), this);

            AttributeList al = mbeanServer.getAttributes(queryName,
                    attributes.toArray(new String[attributes.size()]));

            return new JmxResultProcessor(this, oi, al.asList(), info.getClassName(), queryName.getDomain())
                    .getResults();
        }
    } catch (UnmarshalException ue) {
        if ((ue.getCause() != null) && (ue.getCause() instanceof ClassNotFoundException)) {
            logger.debug("Bad unmarshall, continuing. This is probably ok and due to something like this: "
                    + "http://ehcache.org/xref/net/sf/ehcache/distribution/RMICacheManagerPeerListener.html#52",
                    ue.getMessage());
        } else {
            throw ue;
        }
    }
    return ImmutableList.of();
}

From source file:org.helios.collector.jmx.connection.AbstractMBeanServerConnectionFactory.java

/**
 * Returns the ObjectInstance for the passed ObjectName
 * @param objectName/*from   ww  w  .  ja va  2  s .c  om*/
 * @return
 * @throws InstanceNotFoundException
 * @throws IOException
 */
@Override
@ManagedOperation
public ObjectInstance getObjectInstance(ObjectName objectName) throws InstanceNotFoundException, IOException {
    validateConn();
    MBeanServerConnection conn = null;
    try {
        conn = getPooledConnection();
        return conn.getObjectInstance(objectName);
    } catch (MBeanServerConnectionFactoryException e) {
        throw new RuntimeException("Failed to get pooled connection", e);
    } finally {
        try {
            this.returnPooledConnection(conn);
        } catch (Exception e) {
            log.debug(e.getMessage());
        }
    }
}

From source file:org.hyperic.hq.plugin.jboss.JBossUtil.java

static Object getRemoteMBeanValue(Metric metric)
        throws MetricNotFoundException, MetricInvalidException, MetricUnreachableException, PluginException {

    MBeanServerConnection mServer = null;
    boolean cached = true;
    String url = getServerURL(metric);

    synchronized (serverCache) {
        mServer = (MBeanServerConnection) serverCache.get(url);
    }//from w  ww . j a v a 2s  .com

    if (mServer == null) {
        cached = false;
        try {
            mServer = getMBeanServerConnection(metric); //jndi lookup
        } catch (NamingException e) {
            throw unreachable(metric, e);
        } catch (RemoteException e) {
            throw unreachable(metric, e);
        }

        determineJSR77Case(url, mServer);
        synchronized (serverCache) {
            serverCache.put(url, mServer);
        }
    }

    String attrName = metric.getAttributeName();
    boolean lc;

    Boolean jsr77Case;
    synchronized (lowerCaseURLMappings) {
        jsr77Case = (Boolean) lowerCaseURLMappings.get(url);
    }

    if (jsr77Case != null) {
        lc = jsr77Case.booleanValue();
    } else {
        lc = Character.isLowerCase(attrName.charAt(0));
    }

    String lcAttr;
    //another 3.2.8 hack
    if (lc && ((lcAttr = (String) jsr77LowerCase.get(attrName)) != null)) {
        attrName = lcAttr;
    }

    try {
        ObjectName objName = new ObjectName(metric.getObjectName());

        if (attrName.substring(1).startsWith(/*S*/"tatistic")) {
            return getJSR77Statistic(mServer, objName, metric, lc);
        } else if (attrName.equals("__INSTANCE__")) {
            //cheap hack for an avail metric for MBeans that dont
            //have anything better we can use, e.g. Hibernate.
            try {
                mServer.getObjectInstance(objName);
                return Boolean.TRUE;
            } catch (Exception e) {
                return Boolean.FALSE;
            }
        } else {
            return mServer.getAttribute(objName, attrName);
        }
    } catch (MalformedObjectNameException e) {
        throw invalid(metric, e);
    } catch (InstanceNotFoundException e) {
        throw notfound(metric, e);
    } catch (AttributeNotFoundException e) {
        //XXX not all MBeans have a reasonable attribute to
        //determine availability, so just assume if we get this far
        //the MBean exists and is alive.
        if (attrName.equals(Metric.ATTR_AVAIL)) {
            return new Double(Metric.AVAIL_UP);
        }
        throw notfound(metric, e);
    } catch (ReflectionException e) {
        throw error(metric, e);
    } catch (MBeanException e) {
        throw error(metric, e);
    } catch (RuntimeMBeanException e) {
        throw error(metric, e);
    } catch (Exception e) {
        //CommunicationException, NamingException, RemoteException, etc.
        if (cached) {
            //retry once, in the event the cached connection was stale
            synchronized (serverCache) {
                serverCache.remove(url);
            }
            log.debug("MBeanServerConnection cache cleared for " + url);
            return getRemoteMBeanValue(metric);
        } else {
            throw unreachable(metric, e);
        }
    }
}

From source file:org.hyperic.hq.plugin.tomcat.JBossUtil.java

static Object getRemoteMBeanValue(Metric metric)
        throws MetricNotFoundException, MetricInvalidException, MetricUnreachableException, PluginException {

    MBeanServerConnection mServer = null;
    boolean cached = true;
    String url = getServerURL(metric);

    synchronized (serverCache) {
        mServer = (MBeanServerConnection) serverCache.get(url);
    }/*from w w  w  .j  a v  a2  s.  c  o  m*/

    if (mServer == null) {
        cached = false;
        try {
            mServer = getMBeanServerConnection(metric); //jndi lookup
        } catch (NamingException e) {
            throw unreachable(metric, e);
        } catch (RemoteException e) {
            throw unreachable(metric, e);
        }

        //            determineJSR77Case(url, mServer);
        synchronized (serverCache) {
            serverCache.put(url, mServer);
        }
    }

    String attrName = metric.getAttributeName();
    boolean lc;

    Boolean jsr77Case;
    synchronized (lowerCaseURLMappings) {
        jsr77Case = (Boolean) lowerCaseURLMappings.get(url);
    }

    if (jsr77Case != null) {
        lc = jsr77Case.booleanValue();
    } else {
        lc = Character.isLowerCase(attrName.charAt(0));
    }

    String lcAttr;
    //another 3.2.8 hack
    if (lc && ((lcAttr = (String) jsr77LowerCase.get(attrName)) != null)) {
        attrName = lcAttr;
    }

    try {
        ObjectName objName = new ObjectName(metric.getObjectName());

        if (attrName.substring(1).startsWith(/*S*/"tatistic")) {
            return getJSR77Statistic(mServer, objName, metric, lc);
        } else if (attrName.equals("__INSTANCE__")) {
            //cheap hack for an avail metric for MBeans that dont
            //have anything better we can use, e.g. Hibernate.
            try {
                mServer.getObjectInstance(objName);
                return Boolean.TRUE;
            } catch (Exception e) {
                return Boolean.FALSE;
            }
        } else {
            return mServer.getAttribute(objName, attrName);
        }
    } catch (MalformedObjectNameException e) {
        throw invalid(metric, e);
    } catch (InstanceNotFoundException e) {
        throw notfound(metric, e);
    } catch (AttributeNotFoundException e) {
        //XXX not all MBeans have a reasonable attribute to
        //determine availability, so just assume if we get this far
        //the MBean exists and is alive.
        if (attrName.equals(Metric.ATTR_AVAIL)) {
            return new Double(Metric.AVAIL_UP);
        }
        throw notfound(metric, e);
    } catch (ReflectionException e) {
        throw error(metric, e);
    } catch (MBeanException e) {
        throw error(metric, e);
    } catch (RuntimeMBeanException e) {
        throw error(metric, e);
    } catch (Exception e) {
        //CommunicationException, NamingException, RemoteException, etc.
        if (cached) {
            //retry once, in the event the cached connection was stale
            synchronized (serverCache) {
                serverCache.remove(url);
            }
            log.debug("MBeanServerConnection cache cleared for " + url);
            return getRemoteMBeanValue(metric);
        } else {
            throw unreachable(metric, e);
        }
    }
}