Example usage for javax.management MBeanServer getMBeanInfo

List of usage examples for javax.management MBeanServer getMBeanInfo

Introduction

In this page you can find the example usage for javax.management MBeanServer getMBeanInfo.

Prototype

public MBeanInfo getMBeanInfo(ObjectName name)
            throws InstanceNotFoundException, IntrospectionException, ReflectionException;

Source Link

Usage

From source file:org.apache.openejb.server.cli.command.LocalJMXCommand.java

private void set(final String cmd) {
    final String[] split = cmd.split(" ");
    if (split.length < 2) {
        streamManager.writeErr("you need to specify an attribute, an objectname and a value");
        return;//from  ww w .ja  va  2s  .  com
    }

    final MBeanServer mBeanServer = LocalMBeanServer.get();
    final String newValue = cmd.substring(split[0].length() + split[1].length() + 1).trim();
    try {
        final ObjectName oname = new ObjectName(split[1]);
        final MBeanInfo minfo = mBeanServer.getMBeanInfo(oname);
        final MBeanAttributeInfo attrs[] = minfo.getAttributes();

        String type = String.class.getName();
        for (int i = 0; i < attrs.length; i++) {
            if (attrs[i].getName().equals(split[0])) {
                type = attrs[i].getType();
                break;
            }
        }

        final Object valueObj = PropertyEditors.getValue(type, newValue,
                Thread.currentThread().getContextClassLoader());
        mBeanServer.setAttribute(oname, new Attribute(split[0], valueObj));
        streamManager.writeOut("done");
    } catch (Exception ex) {
        streamManager.writeOut("Error - " + ex.toString());
    }
}

From source file:org.apache.openejb.server.cli.command.LocalJMXCommand.java

private void listMBeans() {
    final MBeanServer mBeanServer = LocalMBeanServer.get();

    final Set<ObjectName> names;
    try {/*from  w  w  w .j ava 2  s  .  c  o  m*/
        names = mBeanServer.queryNames(null, null);
    } catch (Exception e) {
        streamManager.writeErr(e);
        return;
    }

    final Iterator<ObjectName> it = names.iterator();
    while (it.hasNext()) {
        ObjectName oname = it.next();
        streamManager.writeOut("Name: " + oname.toString());

        try {
            final MBeanInfo minfo = mBeanServer.getMBeanInfo(oname);
            String code = minfo.getClassName();
            if ("org.apache.commons.modeler.BaseModelMBean".equals(code)) {
                code = (String) mBeanServer.getAttribute(oname, "modelerType");
            }
            streamManager.writeOut("  + modelerType: " + code);

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

            for (int i = 0; i < attrs.length; i++) {
                if (!attrs[i].isReadable()) {
                    continue;
                }

                final 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 (RuntimeMBeanException uoe) {
                    // ignored
                } catch (Throwable t) {
                    streamManager.writeErr(new Exception(t));
                    continue;
                }

                try {
                    String valueString = stringify(value);
                    streamManager.writeOut("  + " + attName + ": " + valueString);
                } catch (Throwable t) {
                    streamManager.writeErr(new Exception(t));
                }
            }
        } catch (Throwable t) {
            streamManager.writeErr(new Exception(t));
        }
        streamManager.writeOut("");
    }
}

From source file:org.apache.synapse.commons.snmp.SNMPAgent.java

@Override
protected void registerManagedObjects() {
    log.info("Initializing Synapse SNMP MIB");
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    Set<ObjectInstance> instances = mbs.queryMBeans(null, null);

    try {/*from  ww  w  .  j  a  va  2s.  c om*/
        for (ObjectInstance instance : instances) {
            ObjectName objectName = instance.getObjectName();
            if (objectName.getDomain().equals("org.apache.synapse")) {
                String oidString = SynapseMIBUtils.getOID(objectName);
                if (oidString == null) {
                    continue;
                }

                MBeanInfo info = mbs.getMBeanInfo(objectName);
                MBeanAttributeInfo[] attributes = info.getAttributes();
                List<String> attributeNames = new ArrayList<String>();
                List<String> mapAttributes = new ArrayList<String>();
                for (MBeanAttributeInfo attributeInfo : attributes) {
                    attributeNames.add(attributeInfo.getName());
                    if (Map.class.getName().equals(attributeInfo.getType())) {
                        mapAttributes.add(attributeInfo.getName());
                    }
                }
                Collections.sort(attributeNames);

                doRegister(attributeNames, mapAttributes, oidString, objectName);
            }
        }
    } catch (Exception e) {
        log.error("Error while initializing the SNMP MIB", e);
    }
}

From source file:org.eclipse.virgo.repository.internal.remote.RemoteRepositoryTests.java

@Test
public void mBeanPublication() throws Exception {
    URI repositoryUri = URI.create("http://localhost:8080/repository");
    RemoteRepositoryConfiguration configuration = new RemoteRepositoryConfiguration("remote-repo",
            this.proxyIndexLocation, repositoryUri, 1, MBEAN_DOMAIN_VIRGO_WEB_SERVER, this.cacheDirectory);
    RemoteRepository repository = new RemoteRepository(configuration, new MockEventLogger());
    ObjectName objectName = new ObjectName(MBEAN_DOMAIN_VIRGO_WEB_SERVER + ":type=Repository,name=remote-repo");

    MBeanServer platformMBeanServer = ManagementFactory.getPlatformMBeanServer();

    try {//from  ww  w. j  a  v  a 2s  .  c o m
        platformMBeanServer.getMBeanInfo(objectName);
        fail("MBean should not be present until repository has been started");
    } catch (InstanceNotFoundException infe) {
    }

    repository.start();

    MBeanInfo mBeanInfo = platformMBeanServer.getMBeanInfo(objectName);
    Object type = mBeanInfo.getDescriptor().getFieldValue("type");
    assertNotNull(type);
    assertEquals("remote", type);

    repository.stop();

    try {
        platformMBeanServer.getMBeanInfo(objectName);
        fail("MBean should not be present once repository has been stopped");
    } catch (InstanceNotFoundException infe) {
    }
}

From source file:org.eclipse.virgo.repository.internal.remote.RemoteRepositoryTests.java

@Test
public void mBeanNonPublication() throws Exception {
    URI repositoryUri = URI.create("http://localhost:8080/repository");
    RemoteRepositoryConfiguration configuration = new RemoteRepositoryConfiguration("remote-repo",
            this.proxyIndexLocation, repositoryUri, 1, null, this.cacheDirectory);
    RemoteRepository repository = new RemoteRepository(configuration, new MockEventLogger());
    ObjectName objectName = new ObjectName(MBEAN_DOMAIN_VIRGO_WEB_SERVER + ":type=Repository,name=remote-repo");
    MBeanServer platformMBeanServer = ManagementFactory.getPlatformMBeanServer();

    try {/*from ww  w. j a  va2  s  .c  o m*/
        platformMBeanServer.getMBeanInfo(objectName);
        fail("MBean should not be present before start");
    } catch (InstanceNotFoundException infe) {
    }

    repository.start();

    try {
        platformMBeanServer.getMBeanInfo(objectName);
        fail("MBean should not be present after start");
    } catch (InstanceNotFoundException infe) {
    }

    repository.stop();

    try {
        platformMBeanServer.getMBeanInfo(objectName);
        fail("MBean should not be present once repository has been stopped");
    } catch (InstanceNotFoundException infe) {
    }
}

From source file:org.fusesource.cloudmix.agent.jbi.AgentComponent.java

private ObjectName validateMbean(MBeanServer anMbeanServer, String anMbeanName) {
    try {/*from  ww  w .j  a v a2  s . c o m*/
        ObjectName oname = new ObjectName(anMbeanName);
        MBeanInfo info = anMbeanServer.getMBeanInfo(oname);
        if (info != null) {
            LOGGER.info("Successfully accesses Deployment Service mbean");
            LOGGER.info("Description: " + info.getDescription());
            return oname;
        }
    } catch (Exception e) {
        LOGGER.debug("Exception getting DeploymentService mbean " + e);
    }
    LOGGER.error("Cannot resolve DeploymentService MBean using name " + anMbeanName);
    return null;
}

From source file:org.wso2.carbon.kernel.jmx.MBeanRegistratorTest.java

@Test()
public void testRegisterMBean() {

    MBeanServer mBeanServer = MBeanManagementFactory.getMBeanServer();
    initialMBeanCount = mBeanServer.getMBeanCount();

    MBeanRegistrator.registerMBean(new TransportManager());
    Assert.assertTrue(mBeanServer.getMBeanCount() == initialMBeanCount + 1);

    String className = new TransportManager().getClass().getName();
    if (className.indexOf('.') != -1) {
        className = className.substring(className.lastIndexOf('.') + 1);
    }/* ww w .  j av a2  s  .c  o m*/

    String objectName = Constants.SERVER_PACKAGE + ":type=" + className;
    try {
        Assert.assertNotNull(mBeanServer.getMBeanInfo(new ObjectName(objectName)));
    } catch (MalformedObjectNameException | InstanceNotFoundException | IntrospectionException
            | ReflectionException e) {
        log.error("Error when retrieving mBean Inforation", e);
    }
}