Search in sources :

Example 31 with ObjectInstance

use of javax.management.ObjectInstance in project jdk8u_jdk by JetBrains.

the class DefaultMBeanServerInterceptor method objectInstancesFromFilteredNamedObjects.

/**
     * Applies the specified queries to the set of NamedObjects.
     */
private Set<ObjectInstance> objectInstancesFromFilteredNamedObjects(Set<NamedObject> list, QueryExp query) {
    Set<ObjectInstance> result = new HashSet<ObjectInstance>();
    // No query ...
    if (query == null) {
        for (NamedObject no : list) {
            final DynamicMBean obj = no.getObject();
            final String className = safeGetClassName(obj);
            result.add(new ObjectInstance(no.getName(), className));
        }
    } else {
        // Access the filter
        MBeanServer oldServer = QueryEval.getMBeanServer();
        query.setMBeanServer(server);
        try {
            for (NamedObject no : list) {
                final DynamicMBean obj = no.getObject();
                boolean res;
                try {
                    res = query.apply(no.getName());
                } catch (Exception e) {
                    res = false;
                }
                if (res) {
                    String className = safeGetClassName(obj);
                    result.add(new ObjectInstance(no.getName(), className));
                }
            }
        } finally {
            /*
                 * query.setMBeanServer is probably
                 * QueryEval.setMBeanServer so put back the old
                 * value.  Since that method uses a ThreadLocal
                 * variable, this code is only needed for the
                 * unusual case where the user creates a custom
                 * QueryExp that calls a nested query on another
                 * MBeanServer.
                 */
            query.setMBeanServer(oldServer);
        }
    }
    return result;
}
Also used : DynamicMBean(javax.management.DynamicMBean) ObjectInstance(javax.management.ObjectInstance) NamedObject(com.sun.jmx.mbeanserver.NamedObject) IntrospectionException(javax.management.IntrospectionException) OperationsException(javax.management.OperationsException) InstanceAlreadyExistsException(javax.management.InstanceAlreadyExistsException) ReflectionException(javax.management.ReflectionException) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) RuntimeMBeanException(javax.management.RuntimeMBeanException) RuntimeErrorException(javax.management.RuntimeErrorException) ListenerNotFoundException(javax.management.ListenerNotFoundException) RuntimeOperationsException(javax.management.RuntimeOperationsException) AttributeNotFoundException(javax.management.AttributeNotFoundException) MBeanRegistrationException(javax.management.MBeanRegistrationException) InstanceNotFoundException(javax.management.InstanceNotFoundException) JMRuntimeException(javax.management.JMRuntimeException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) MBeanException(javax.management.MBeanException) HashSet(java.util.HashSet) MBeanServer(javax.management.MBeanServer)

Example 32 with ObjectInstance

use of javax.management.ObjectInstance in project jdk8u_jdk by JetBrains.

the class ScanManager method register.

/**
     * Create and register a new singleton instance of the ScanManager
     * MBean in the given {@link MBeanServerConnection}.
     * @param mbs The MBeanServer in which the new singleton instance
     *         should be created.
     * @throws JMException The MBeanServer connection raised an exception
     *         while trying to instantiate and register the singleton MBean
     *         instance.
     * @throws IOException There was a connection problem while trying to
     *         communicate with the underlying MBeanServer.
     * @return A proxy for the registered MBean.
     **/
public static ScanManagerMXBean register(MBeanServerConnection mbs) throws IOException, JMException {
    final ObjectInstance moi = mbs.createMBean(ScanManager.class.getName(), SCAN_MANAGER_NAME);
    final ScanManagerMXBean proxy = JMX.newMXBeanProxy(mbs, moi.getObjectName(), ScanManagerMXBean.class, true);
    return proxy;
}
Also used : ObjectInstance(javax.management.ObjectInstance)

Example 33 with ObjectInstance

use of javax.management.ObjectInstance in project hbase by apache.

the class TestStochasticBalancerJmxMetrics method readJmxMetrics.

/**
   * Read the attributes from Hadoop->HBase->Master->Balancer in JMX
   * @throws IOException 
   */
private Set<String> readJmxMetrics() throws IOException {
    JMXConnector connector = null;
    ObjectName target = null;
    MBeanServerConnection mb = null;
    try {
        connector = JMXConnectorFactory.connect(JMXListener.buildJMXServiceURL(connectorPort, connectorPort));
        mb = connector.getMBeanServerConnection();
        Hashtable<String, String> pairs = new Hashtable<>();
        pairs.put("service", "HBase");
        pairs.put("name", "Master");
        pairs.put("sub", "Balancer");
        target = new ObjectName("Hadoop", pairs);
        MBeanInfo beanInfo = mb.getMBeanInfo(target);
        Set<String> existingAttrs = new HashSet<>();
        for (MBeanAttributeInfo attrInfo : beanInfo.getAttributes()) {
            existingAttrs.add(attrInfo.getName());
        }
        return existingAttrs;
    } catch (Exception e) {
        LOG.warn("Failed to get bean!!! " + target, e);
        if (mb != null) {
            Set<ObjectInstance> instances = mb.queryMBeans(null, null);
            Iterator<ObjectInstance> iterator = instances.iterator();
            System.out.println("MBean Found:");
            while (iterator.hasNext()) {
                ObjectInstance instance = iterator.next();
                System.out.println("Class Name: " + instance.getClassName());
                System.out.println("Object Name: " + instance.getObjectName());
            }
        }
    } finally {
        if (connector != null) {
            try {
                connector.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    return null;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) MBeanInfo(javax.management.MBeanInfo) Hashtable(java.util.Hashtable) ObjectInstance(javax.management.ObjectInstance) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) IOException(java.io.IOException) ObjectName(javax.management.ObjectName) JMXConnector(javax.management.remote.JMXConnector) Iterator(java.util.Iterator) MBeanServerConnection(javax.management.MBeanServerConnection) HashSet(java.util.HashSet)

Example 34 with ObjectInstance

use of javax.management.ObjectInstance in project tomcat by apache.

the class StatusManagerServlet method init.

// --------------------------------------------------------- Public Methods
/**
     * Initialize this servlet.
     */
@Override
public void init() throws ServletException {
    // Retrieve the MBean server
    mBeanServer = Registry.getRegistry(null, null).getMBeanServer();
    try {
        // Query protocol handlers
        String onStr = "*:type=ProtocolHandler,*";
        ObjectName objectName = new ObjectName(onStr);
        Set<ObjectInstance> set = mBeanServer.queryMBeans(objectName, null);
        Iterator<ObjectInstance> iterator = set.iterator();
        while (iterator.hasNext()) {
            ObjectInstance oi = iterator.next();
            protocolHandlers.addElement(oi.getObjectName());
        }
        // Query Thread Pools
        onStr = "*:type=ThreadPool,*";
        objectName = new ObjectName(onStr);
        set = mBeanServer.queryMBeans(objectName, null);
        iterator = set.iterator();
        while (iterator.hasNext()) {
            ObjectInstance oi = iterator.next();
            threadPools.addElement(oi.getObjectName());
        }
        // Query Global Request Processors
        onStr = "*:type=GlobalRequestProcessor,*";
        objectName = new ObjectName(onStr);
        set = mBeanServer.queryMBeans(objectName, null);
        iterator = set.iterator();
        while (iterator.hasNext()) {
            ObjectInstance oi = iterator.next();
            globalRequestProcessors.addElement(oi.getObjectName());
        }
        // Query Request Processors
        onStr = "*:type=RequestProcessor,*";
        objectName = new ObjectName(onStr);
        set = mBeanServer.queryMBeans(objectName, null);
        iterator = set.iterator();
        while (iterator.hasNext()) {
            ObjectInstance oi = iterator.next();
            requestProcessors.addElement(oi.getObjectName());
        }
        // Register with MBean server
        onStr = "JMImplementation:type=MBeanServerDelegate";
        objectName = new ObjectName(onStr);
        mBeanServer.addNotificationListener(objectName, this, null, null);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : ObjectInstance(javax.management.ObjectInstance) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) ObjectName(javax.management.ObjectName)

Example 35 with ObjectInstance

use of javax.management.ObjectInstance in project tomcat by apache.

the class StatusTransformer method writeContext.

/**
     * Write context state.
     * @param writer The output writer
     * @param objectName The context MBean name
     * @param mBeanServer MBean server
     * @param mode Mode <code>0</code> will generate HTML.
     *   Mode <code>1</code> will generate XML.
     * @throws Exception Propagated JMX error
     */
protected static void writeContext(PrintWriter writer, ObjectName objectName, MBeanServer mBeanServer, int mode) throws Exception {
    if (mode == 0) {
        String webModuleName = objectName.getKeyProperty("name");
        String name = webModuleName;
        if (name == null) {
            return;
        }
        String hostName = null;
        String contextName = null;
        if (name.startsWith("//")) {
            name = name.substring(2);
        }
        int slash = name.indexOf('/');
        if (slash != -1) {
            hostName = name.substring(0, slash);
            contextName = name.substring(slash);
        } else {
            return;
        }
        ObjectName queryManager = new ObjectName(objectName.getDomain() + ":type=Manager,context=" + contextName + ",host=" + hostName + ",*");
        Set<ObjectName> managersON = mBeanServer.queryNames(queryManager, null);
        ObjectName managerON = null;
        Iterator<ObjectName> iterator2 = managersON.iterator();
        while (iterator2.hasNext()) {
            managerON = iterator2.next();
        }
        ObjectName queryJspMonitor = new ObjectName(objectName.getDomain() + ":type=JspMonitor,WebModule=" + webModuleName + ",*");
        Set<ObjectName> jspMonitorONs = mBeanServer.queryNames(queryJspMonitor, null);
        // Special case for the root context
        if (contextName.equals("/")) {
            contextName = "";
        }
        writer.print("<h1>");
        writer.print(filter(name));
        writer.print("</h1>");
        writer.print("</a>");
        writer.print("<p>");
        Object startTime = mBeanServer.getAttribute(objectName, "startTime");
        writer.print(" Start time: " + new Date(((Long) startTime).longValue()));
        writer.print(" Startup time: ");
        writer.print(formatTime(mBeanServer.getAttribute(objectName, "startupTime"), false));
        writer.print(" TLD scan time: ");
        writer.print(formatTime(mBeanServer.getAttribute(objectName, "tldScanTime"), false));
        if (managerON != null) {
            writeManager(writer, managerON, mBeanServer, mode);
        }
        if (jspMonitorONs != null) {
            writeJspMonitor(writer, jspMonitorONs, mBeanServer, mode);
        }
        writer.print("</p>");
        String onStr = objectName.getDomain() + ":j2eeType=Servlet,WebModule=" + webModuleName + ",*";
        ObjectName servletObjectName = new ObjectName(onStr);
        Set<ObjectInstance> set = mBeanServer.queryMBeans(servletObjectName, null);
        Iterator<ObjectInstance> iterator = set.iterator();
        while (iterator.hasNext()) {
            ObjectInstance oi = iterator.next();
            writeWrapper(writer, oi.getObjectName(), mBeanServer, mode);
        }
    } else if (mode == 1) {
    // for now we don't write out the context in XML
    }
}
Also used : ObjectInstance(javax.management.ObjectInstance) Date(java.util.Date) ObjectName(javax.management.ObjectName)

Aggregations

ObjectInstance (javax.management.ObjectInstance)75 ObjectName (javax.management.ObjectName)36 Test (org.junit.Test)27 InstanceNotFoundException (javax.management.InstanceNotFoundException)15 MBeanServer (javax.management.MBeanServer)12 InstanceAlreadyExistsException (javax.management.InstanceAlreadyExistsException)10 JmxResultProcessor (com.googlecode.jmxtrans.model.JmxResultProcessor)9 Result (com.googlecode.jmxtrans.model.Result)9 HashSet (java.util.HashSet)7 Attribute (javax.management.Attribute)7 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)5 AttributeNotFoundException (javax.management.AttributeNotFoundException)5 MBeanRegistrationException (javax.management.MBeanRegistrationException)5 NotCompliantMBeanException (javax.management.NotCompliantMBeanException)5 HashMap (java.util.HashMap)4 AttributeList (javax.management.AttributeList)4 MBeanException (javax.management.MBeanException)4 MBeanServerConnection (javax.management.MBeanServerConnection)4 ReflectionException (javax.management.ReflectionException)4