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:com.betfair.cougar.core.impl.jmx.HtmlAdaptorParser.java

private void appendMBean(MBeanServer server, ObjectName on, String attrName, String separator,
        StringBuilder buf) {//w  ww  .j a v a  2  s. c o  m
    StringBuilder local = new StringBuilder();
    try {

        MBeanInfo info = server.getMBeanInfo(on);
        local.append(on.toString());
        MBeanAttributeInfo[] attr = info.getAttributes();
        for (int i = 0; i < attr.length; i++) {
            if ((attrName == null || attrName.equals(attr[i].getName())) && attr[i].isReadable()) {
                local.append(separator);
                local.append(attr[i].getName());
                local.append(separator);
                local.append(server.getAttribute(on, attr[i].getName()));
            }
        }
    } catch (Exception e) {
        LOGGER.debug("Unable to retrieve Bean information for bean " + on, e);
        return;
    }
    buf.append(local);
}

From source file:net.sbbi.upnp.jmx.UPNPMBeanService.java

private String generateServiceId(ObjectName mbeanName) {
    try {/* ww  w .j  a va  2 s  .c  o m*/
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        // the uuid is based on the device type, the internal id
        // and the host name
        md5.update(mbeanName.toString().getBytes());
        StringBuffer hexString = new StringBuffer();
        byte[] digest = md5.digest();
        for (int i = 0; i < digest.length; i++) {
            hexString.append(Integer.toHexString(0xFF & digest[i]));
        }
        return hexString.toString().toUpperCase();
    } catch (Exception ex) {
        RuntimeException runTimeEx = new RuntimeException(
                "Unexpected error during MD5 hash creation, check your JRE");
        runTimeEx.initCause(ex);
        throw runTimeEx;
    }
}

From source file:com.googlecode.arit.mbeans.MBeanScanner.java

public void clean(ClassLoader classLoader) {
    ArrayList<MBeanServer> mbeanServers = MBeanServerFactory.findMBeanServer(null);
    for (MBeanServer mbs : mbeanServers) {
        MBeanAccessor mbeanAccessor = mbsInspector.inspect(mbs);
        if (mbeanAccessor == null) {
            String errorMsg = "Unable to inspect MBeanServer of type " + mbs.getClass().getName();
            LOG.error(errorMsg);//from w  w  w.  ja  v a 2s.  co m
            if (messages != null) {
                messages.addMessage(errorMsg);
            }
        } else {
            Set<ObjectName> mbeans;
            try {
                mbeans = mbs.queryNames(new ObjectName("*:*"), null);
            } catch (MalformedObjectNameException ex) {
                // We should never get here
                throw new Error(ex);
            }

            for (ObjectName name : mbeans) {
                Object mbean = mbeanAccessor.retrieve(name);
                if (mbean.getClass().getClassLoader().equals(classLoader)) {
                    try {
                        mbs.unregisterMBean(name);
                    } catch (JMException ex) {
                        String errorMsg = "Error during cleanup of mbean - unregistration of mbean "
                                + name.toString() + " failed";
                        LOG.error(errorMsg, ex);
                        if (messages != null) {
                            messages.addMessage(errorMsg);
                        }
                    }
                }
            }
        }
    }
}

From source file:org.apache.servicemix.nmr.management.ManagementAgent.java

public void register(Object obj, ObjectName name, boolean forceRegistration) throws JMException {
    try {/*from   www  . jav a2 s  .  c o m*/
        registerMBeanWithServer(obj, name, forceRegistration);
    } catch (NotCompliantMBeanException e) {
        // If this is not a "normal" MBean, then try to deploy it using JMX
        // annotations
        ModelMBeanInfo mbi = assembler.getMBeanInfo(obj, name.toString());
        RequiredModelMBean mbean = (RequiredModelMBean) mbeanServer
                .instantiate(RequiredModelMBean.class.getName());
        mbean.setModelMBeanInfo(mbi);
        try {
            mbean.setManagedResource(obj, "ObjectReference");
        } catch (InvalidTargetObjectTypeException itotex) {
            throw new JMException(itotex.getMessage());
        }
        registerMBeanWithServer(mbean, name, forceRegistration);
    }
}

From source file:org.apache.camel.management.JmxMBeanAssembler.java

public RequiredModelMBean assemble(Object obj, ObjectName name) throws JMException {
    ModelMBeanInfo mbi = null;//w  w w.j av a  2  s  .  c  o m

    // prefer to use the managed instance if it has been annotated with Spring JMX annotations
    if (obj instanceof ManagedInstance) {
        Object custom = ((ManagedInstance) obj).getInstance();
        if (custom != null
                && ObjectHelper.hasAnnotation(custom.getClass().getAnnotations(), ManagedResource.class)) {
            if (LOG.isTraceEnabled()) {
                LOG.trace("Assembling MBeanInfo for: " + name.toString()
                        + " from custom @ManagedResource object: " + custom);
            }
            // get the mbean info from the custom managed object
            mbi = assembler.getMBeanInfo(custom, name.toString());
            // and let the custom object be registered in JMX
            obj = custom;
        }
    }

    if (mbi == null) {
        // use the default provided mbean which has been annotated with Spring JMX annotations
        if (LOG.isTraceEnabled()) {
            LOG.trace("Assembling MBeanInfo for: " + name.toString() + " from @ManagedResource object: " + obj);
        }
        mbi = assembler.getMBeanInfo(obj, name.toString());
    }

    RequiredModelMBean mbean = (RequiredModelMBean) server.instantiate(RequiredModelMBean.class.getName());
    mbean.setModelMBeanInfo(mbi);

    try {
        mbean.setManagedResource(obj, "ObjectReference");
    } catch (InvalidTargetObjectTypeException e) {
        throw new JMException(e.getMessage());
    }

    return mbean;
}

From source file:org.fluentd.jvmwatcher.proxy.JvmClientProxy.java

/**
 * @param objName//from w w  w .j av  a2  s.c om
 * @param interfaceClass
 * @return
 * @throws IOException
 */
public <T> T getMXBean(ObjectName objName, Class<T> interfaceClass) throws IOException {
    return newPlatformMXBeanProxy(this.server_, objName.toString(), interfaceClass);
}

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  v a 2 s. c  om
    }
}

From source file:org.codice.ddf.spatial.admin.module.service.Geocoding.java

private void registerMbean() {
    ObjectName objectName = null;
    MBeanServer mBeanServer = null;
    try {/* ww w.j  av  a 2 s  .c  om*/
        objectName = new ObjectName(Geocoding.class.getName() + ":service=geocoding");
        mBeanServer = ManagementFactory.getPlatformMBeanServer();
    } catch (MalformedObjectNameException e) {
        LOGGER.error("Unable to create Geocoding Configuration MBean.", e);
    }
    if (mBeanServer != null) {
        try {
            try {
                mBeanServer.registerMBean(this, objectName);
                LOGGER.info("Registered Geocoding Configuration MBean under object name: {}",
                        objectName.toString());
            } catch (InstanceAlreadyExistsException e) {
                // Try to remove and re-register
                mBeanServer.unregisterMBean(objectName);
                mBeanServer.registerMBean(this, objectName);
                LOGGER.info("Re-registered Geocoding Configuration MBean");
            }
        } catch (Exception e) {
            LOGGER.error("Could not register MBean [{}].", objectName.toString(), e);
        }
    }
}

From source file:org.springframework.ldap.config.LdapTemplateNamespaceHandlerTest.java

@Test
public void verifyParsePool2SizeSet() {
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
            "/ldap-namespace-config-pool2-configured-poolsize.xml");

    ContextSource outerContextSource = ctx.getBean(ContextSource.class);
    assertThat(outerContextSource).isNotNull();

    ContextSource pooledContextSource = ((TransactionAwareContextSourceProxy) outerContextSource).getTarget();
    assertThat(pooledContextSource).isNotNull();

    org.apache.commons.pool2.impl.GenericKeyedObjectPool objectPool = (org.apache.commons.pool2.impl.GenericKeyedObjectPool) getInternalState(
            pooledContextSource, "keyedObjectPool");
    assertThat(objectPool.getMaxTotal()).isEqualTo(12);
    assertThat(objectPool.getMaxIdlePerKey()).isEqualTo(20);
    assertThat(objectPool.getMaxTotalPerKey()).isEqualTo(10);
    assertThat(objectPool.getMaxWaitMillis()).isEqualTo(13);
    assertThat(objectPool.getMinIdlePerKey()).isEqualTo(14);
    assertThat(objectPool.getBlockWhenExhausted()).isEqualTo(true);
    assertThat(objectPool.getEvictionPolicyClassName())
            .isEqualTo("org.springframework.ldap.pool2.DummyEvictionPolicy");
    assertThat(objectPool.getFairness()).isEqualTo(true);
    assertThat(objectPool.getLifo()).isEqualTo(false);

    // ensures the pool is registered
    ObjectName oname = objectPool.getJmxName();
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    Set<ObjectName> result = mbs.queryNames(oname, null);
    assertThat(result).hasSize(1);/*from   w  w  w.j a  va2  s. c o  m*/
    assertThat(oname.toString()).isEqualTo("org.springframework.ldap.pool2:type=ldap-pool,name=test-pool");
}

From source file:com.streamsets.datacollector.bundles.content.SdcInfoContentGenerator.java

private void writeJmx(BundleWriter writer) throws IOException {
    MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();

    try (JsonGenerator generator = writer.createGenerator("runtime/jmx.json")) {
        generator.useDefaultPrettyPrinter();
        generator.writeStartObject();/*from  w w w.  ja v a 2s .c o  m*/
        generator.writeArrayFieldStart("beans");

        for (ObjectName objectName : mBeanServer.queryNames(null, null)) {
            MBeanInfo info;
            try {
                info = mBeanServer.getMBeanInfo(objectName);
            } catch (InstanceNotFoundException | IntrospectionException | ReflectionException ex) {
                LOG.warn("Exception accessing MBeanInfo ", ex);
                continue;
            }

            generator.writeStartObject();
            generator.writeStringField("name", objectName.toString());
            generator.writeObjectFieldStart("attributes");

            for (MBeanAttributeInfo attr : info.getAttributes()) {
                try {
                    writeAttribute(generator, attr.getName(),
                            mBeanServer.getAttribute(objectName, attr.getName()));
                } catch (MBeanException | AttributeNotFoundException | InstanceNotFoundException
                        | ReflectionException | RuntimeMBeanException ex) {
                    generator.writeStringField(attr.getName(), "Exception: " + ex.toString());
                }
            }

            generator.writeEndObject();
            generator.writeEndObject();
            writer.writeLn("");
        }

        generator.writeEndArray();
        generator.writeEndObject();
    } finally {
        writer.markEndOfFile();
    }
}