Example usage for org.springframework.jmx.export.naming ObjectNamingStrategy getObjectName

List of usage examples for org.springframework.jmx.export.naming ObjectNamingStrategy getObjectName

Introduction

In this page you can find the example usage for org.springframework.jmx.export.naming ObjectNamingStrategy getObjectName.

Prototype

ObjectName getObjectName(Object managedBean, @Nullable String beanKey) throws MalformedObjectNameException;

Source Link

Document

Obtain an ObjectName for the supplied bean.

Usage

From source file:com.brienwheeler.lib.jmx.MBeanRegistrationSupport.java

public static void registerMBean(Object mbean) {
    try {/*from www . ja  v a 2  s. c  o  m*/
        ObjectNamingStrategy namingStrategy = new IdentityNamingStrategy();
        ObjectName objectName = namingStrategy.getObjectName(mbean, null);

        MBeanRegistrationSupport registrar = new MBeanRegistrationSupport();
        registrar.setServer(JmxUtils.locateMBeanServer());

        // if item qualifies as MBean, export it directly
        if (JmxUtils.isMBean(mbean.getClass())) {
            registrar.doRegister(mbean, objectName);
            return;
        }

        // assemble MBean info (from annotations by default)
        ModelMBean modelMBean = new RequiredModelMBean();
        modelMBean.setManagedResource(mbean, MR_TYPE_OBJECT_REFERENCE);
        MBeanInfoAssembler mBeanInfoAssembler = new MetadataMBeanInfoAssembler(
                new AnnotationJmxAttributeSource());
        modelMBean.setModelMBeanInfo(mBeanInfoAssembler.getMBeanInfo(mbean, objectName.getCanonicalName()));
        registrar.doRegister(modelMBean, objectName);
    } catch (Exception e) {
        throw new JmxRegisterException("error registering MBean", e);
    }
}

From source file:com.mtgi.jmx.export.naming.AppendNamingStrategyTest.java

@Test
public void testPackage() throws MalformedObjectNameException {

    Object inst = new XmlBehaviorEventPersisterImpl();
    String beanName = "aName";

    ObjectNamingStrategy mock = createMock(ObjectNamingStrategy.class);
    expect(mock.getObjectName(inst, beanName))
            .andReturn(ObjectName.getInstance("topLevel:package=com.mtgi.analytics,group=stuff,name=foobar"))
            .once();//from  w w w  .jav a  2s  .c o m
    replay(mock);

    AppendNamingStrategy ans = new AppendNamingStrategy();
    ans.setDelegate(mock);
    ans.setValue("testPersister");
    ans.setDomain("testApplication");

    ObjectName name = ans.getObjectName(inst, beanName);
    assertNotNull("name is constructed", name);
    assertEquals("name has been transformed correctly",
            "testApplication:package=com.mtgi.analytics.topLevel,group=stuff.foobar,name=testPersister",
            name.toString());
}