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));
}
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));
}
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));
}
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);
}
}
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);
}
Aggregations