Search in sources :

Example 46 with InstanceAlreadyExistsException

use of javax.management.InstanceAlreadyExistsException in project aries by apache.

the class MBeanHolderTest method testRegistrationException.

@Test
public void testRegistrationException() throws Exception {
    MBeanHolder mbh = new MBeanHolder(new Object(), new ObjectName("foo:bar=123"));
    Map<?, ?> registrations = (Map<?, ?>) getPrivateField(mbh, "registrations");
    Logger ml = Mockito.mock(Logger.class);
    setPrivateField(mbh, "log", ml);
    MBeanServer ms = Mockito.mock(MBeanServer.class);
    Mockito.when(ms.registerMBean(Mockito.any(), Mockito.eq(new ObjectName("foo:bar=123")))).thenThrow(new InstanceAlreadyExistsException());
    assertEquals(0, registrations.size());
    mbh.register(ms, new String[] {});
    assertEquals(0, registrations.size());
    Mockito.verify(ml, Mockito.times(1)).error(Mockito.anyString(), Mockito.isA(InstanceAlreadyExistsException.class));
}
Also used : InstanceAlreadyExistsException(javax.management.InstanceAlreadyExistsException) Logger(org.slf4j.Logger) Map(java.util.Map) ObjectName(javax.management.ObjectName) MBeanServer(javax.management.MBeanServer) Test(org.junit.Test)

Example 47 with InstanceAlreadyExistsException

use of javax.management.InstanceAlreadyExistsException in project aries by apache.

the class JmxWhiteboardSupportTest method testRegisterMBeanWarning.

@Test
public void testRegisterMBeanWarning() throws Exception {
    final Logger ml = Mockito.mock(Logger.class);
    JmxWhiteboardSupport s = new JmxWhiteboardSupport() {

        @Override
        MBeanHolder createMBeanHolder(Object mbean, ObjectName objectName) {
            MBeanHolder mbh = super.createMBeanHolder(mbean, objectName);
            setPrivateField(mbh, "log", ml);
            return mbh;
        }
    };
    final Dictionary<String, Object> props = new Hashtable<String, Object>();
    props.put("jmx.objectname", "test:key=val");
    props.put("warning.exceptions", new String[] { InstanceAlreadyExistsException.class.getName(), MBeanRegistrationException.class.getName() });
    ServiceReference<?> sref = Mockito.mock(ServiceReference.class);
    Mockito.when(sref.getProperty(Mockito.anyString())).then(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            return props.get(invocation.getArguments()[0].toString());
        }
    });
    MBeanServer ms = Mockito.mock(MBeanServer.class);
    Mockito.when(ms.registerMBean(Mockito.any(), Mockito.eq(new ObjectName("test:key=val")))).thenThrow(new InstanceAlreadyExistsException());
    s.addMBeanServer(ms);
    Mockito.verify(ml, Mockito.never()).error(Mockito.anyString(), Mockito.isA(Throwable.class));
    Mockito.verify(ml, Mockito.never()).warn(Mockito.anyString(), Mockito.isA(Throwable.class));
    s.registerMBean(Mockito.mock(DynamicMBean.class), sref);
    Mockito.verify(ml, Mockito.never()).error(Mockito.anyString(), Mockito.isA(Throwable.class));
    Mockito.verify(ml, Mockito.times(1)).warn(Mockito.anyString(), Mockito.isA(Throwable.class));
}
Also used : DynamicMBean(javax.management.DynamicMBean) Hashtable(java.util.Hashtable) InstanceAlreadyExistsException(javax.management.InstanceAlreadyExistsException) Logger(org.slf4j.Logger) ObjectName(javax.management.ObjectName) InvocationOnMock(org.mockito.invocation.InvocationOnMock) MBeanRegistrationException(javax.management.MBeanRegistrationException) MBeanServer(javax.management.MBeanServer) Test(org.junit.Test)

Example 48 with InstanceAlreadyExistsException

use of javax.management.InstanceAlreadyExistsException in project aries by apache.

the class JmxWhiteboardSupportTest method testRegisterMBeanError.

@Test
public void testRegisterMBeanError() throws Exception {
    final Logger ml = Mockito.mock(Logger.class);
    JmxWhiteboardSupport s = new JmxWhiteboardSupport() {

        @Override
        MBeanHolder createMBeanHolder(Object mbean, ObjectName objectName) {
            MBeanHolder mbh = super.createMBeanHolder(mbean, objectName);
            setPrivateField(mbh, "log", ml);
            return mbh;
        }
    };
    final Dictionary<String, Object> props = new Hashtable<String, Object>();
    props.put("jmx.objectname", "test:key=val");
    ServiceReference<?> sref = Mockito.mock(ServiceReference.class);
    Mockito.when(sref.getProperty(Mockito.anyString())).then(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            return props.get(invocation.getArguments()[0].toString());
        }
    });
    MBeanServer ms = Mockito.mock(MBeanServer.class);
    Mockito.when(ms.registerMBean(Mockito.any(), Mockito.eq(new ObjectName("test:key=val")))).thenThrow(new InstanceAlreadyExistsException());
    s.addMBeanServer(ms);
    Mockito.verify(ml, Mockito.never()).error(Mockito.anyString(), Mockito.isA(Throwable.class));
    Mockito.verify(ml, Mockito.never()).warn(Mockito.anyString(), Mockito.isA(Throwable.class));
    s.registerMBean(Mockito.mock(DynamicMBean.class), sref);
    Mockito.verify(ml, Mockito.times(1)).error(Mockito.anyString(), Mockito.isA(Throwable.class));
    Mockito.verify(ml, Mockito.never()).warn(Mockito.anyString(), Mockito.isA(Throwable.class));
}
Also used : DynamicMBean(javax.management.DynamicMBean) Hashtable(java.util.Hashtable) InstanceAlreadyExistsException(javax.management.InstanceAlreadyExistsException) Logger(org.slf4j.Logger) ObjectName(javax.management.ObjectName) InvocationOnMock(org.mockito.invocation.InvocationOnMock) MBeanServer(javax.management.MBeanServer) Test(org.junit.Test)

Example 49 with InstanceAlreadyExistsException

use of javax.management.InstanceAlreadyExistsException in project aries by apache.

the class Activator method registerMBeans.

protected void registerMBeans(MBeanServer mbeanServer) {
    // register BlueprintStateMBean to MBean server
    LOGGER.debug("Registering bundle state monitor with MBeanServer: {} with name: {}", mbeanServer, blueprintStateName);
    try {
        StandardMBean blueprintState = new RegistrableStandardEmitterMBean(new BlueprintState(bundleContext), BlueprintStateMBean.class);
        mbeanServer.registerMBean(blueprintState, blueprintStateName);
    } catch (InstanceAlreadyExistsException e) {
        LOGGER.debug("Cannot register BlueprintStateMBean");
    } catch (MBeanRegistrationException e) {
        LOGGER.error("Cannot register BlueprintStateMBean", e);
    } catch (NotCompliantMBeanException e) {
        LOGGER.error("Cannot register BlueprintStateMBean", e);
    }
    // register BlueprintMetadataMBean to MBean server
    LOGGER.debug("Registering bundle metadata monitor with MBeanServer: {} with name: {}", mbeanServer, blueprintMetadataName);
    try {
        StandardMBean blueprintMetadata = new StandardMBean(new BlueprintMetadata(bundleContext), BlueprintMetadataMBean.class);
        mbeanServer.registerMBean(blueprintMetadata, blueprintMetadataName);
    } catch (InstanceAlreadyExistsException e) {
        LOGGER.debug("Cannot register BlueprintMetadataMBean");
    } catch (MBeanRegistrationException e) {
        LOGGER.error("Cannot register BlueprintMetadataMBean", e);
    } catch (NotCompliantMBeanException e) {
        LOGGER.error("Cannot register BlueprintMetadataMBean", e);
    }
}
Also used : StandardMBean(javax.management.StandardMBean) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) InstanceAlreadyExistsException(javax.management.InstanceAlreadyExistsException) MBeanRegistrationException(javax.management.MBeanRegistrationException)

Example 50 with InstanceAlreadyExistsException

use of javax.management.InstanceAlreadyExistsException in project spring-framework by spring-projects.

the class MBeanRegistrationSupport method doRegister.

/**
 * Actually register the MBean with the server. The behavior when encountering
 * an existing MBean can be configured using {@link #setRegistrationPolicy}.
 * @param mbean the MBean instance
 * @param objectName the suggested ObjectName for the MBean
 * @throws JMException if the registration failed
 */
protected void doRegister(Object mbean, ObjectName objectName) throws JMException {
    Assert.state(this.server != null, "No MBeanServer set");
    ObjectName actualObjectName;
    synchronized (this.registeredBeans) {
        ObjectInstance registeredBean = null;
        try {
            registeredBean = this.server.registerMBean(mbean, objectName);
        } catch (InstanceAlreadyExistsException ex) {
            if (this.registrationPolicy == RegistrationPolicy.IGNORE_EXISTING) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Ignoring existing MBean at [" + objectName + "]");
                }
            } else if (this.registrationPolicy == RegistrationPolicy.REPLACE_EXISTING) {
                try {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Replacing existing MBean at [" + objectName + "]");
                    }
                    this.server.unregisterMBean(objectName);
                    registeredBean = this.server.registerMBean(mbean, objectName);
                } catch (InstanceNotFoundException ex2) {
                    if (logger.isInfoEnabled()) {
                        logger.info("Unable to replace existing MBean at [" + objectName + "]", ex2);
                    }
                    throw ex;
                }
            } else {
                throw ex;
            }
        }
        // Track registration and notify listeners.
        actualObjectName = (registeredBean != null ? registeredBean.getObjectName() : null);
        if (actualObjectName == null) {
            actualObjectName = objectName;
        }
        this.registeredBeans.add(actualObjectName);
    }
    onRegister(actualObjectName, mbean);
}
Also used : InstanceAlreadyExistsException(javax.management.InstanceAlreadyExistsException) InstanceNotFoundException(javax.management.InstanceNotFoundException) ObjectInstance(javax.management.ObjectInstance) ObjectName(javax.management.ObjectName)

Aggregations

InstanceAlreadyExistsException (javax.management.InstanceAlreadyExistsException)116 ObjectName (javax.management.ObjectName)87 MBeanRegistrationException (javax.management.MBeanRegistrationException)73 NotCompliantMBeanException (javax.management.NotCompliantMBeanException)70 MalformedObjectNameException (javax.management.MalformedObjectNameException)64 MBeanServer (javax.management.MBeanServer)48 InstanceNotFoundException (javax.management.InstanceNotFoundException)37 IOException (java.io.IOException)13 ObjectInstance (javax.management.ObjectInstance)12 Test (org.junit.Test)12 StandardMBean (javax.management.StandardMBean)9 MBeanException (javax.management.MBeanException)7 ReflectionException (javax.management.ReflectionException)7 HashMap (java.util.HashMap)5 JMRuntimeException (javax.management.JMRuntimeException)4 RuntimeOperationsException (javax.management.RuntimeOperationsException)4 MalformedURLException (java.net.MalformedURLException)3 AttributeNotFoundException (javax.management.AttributeNotFoundException)3 DynamicMBean (javax.management.DynamicMBean)3 InvalidAttributeValueException (javax.management.InvalidAttributeValueException)3