Skip to content

Commit

Permalink
Fix #27 Register MBeanProxy only if MBean can be registered
Browse files Browse the repository at this point in the history
  • Loading branch information
weissreto committed Apr 14, 2022
1 parent 94ca854 commit 77c1a22
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
10 changes: 5 additions & 5 deletions src/main/java/com/axonivy/jmx/internal/MBeanManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@
*/
public class MBeanManager
{
private ExecutionContextContainer executionContexts = new ExecutionContextContainer();
private final ExecutionContextContainer executionContexts = new ExecutionContextContainer();

private ConcurrentHashMap<Object, MBeanProxy> proxyRegistry = new ConcurrentHashMap<Object, MBeanProxy>();
private final ConcurrentHashMap<Object, MBeanProxy> proxyRegistry = new ConcurrentHashMap<Object, MBeanProxy>();

private ConcurrentHashMap<Class<?>, MBeanType> mBeanTypes = new ConcurrentHashMap<Class<?>, MBeanType>();
private final ConcurrentHashMap<Class<?>, MBeanType> mBeanTypes = new ConcurrentHashMap<Class<?>, MBeanType>();

private OpenTypeConverterStrategy[] openTypeConverterStrategies = {
private final OpenTypeConverterStrategy[] openTypeConverterStrategies = {
new SimpleTypeConverterStrategy(),
new EnumTypeConverterStrategy(),
new ListTypeConverterStrategy(this),
Expand Down Expand Up @@ -66,8 +66,8 @@ public void registerMBeanFor(Object object, ObjectName parentName)
ensureMBeanProxyIsNotYetRegistered(object);
MBeanType mBeanType = getMBeanTypeFor(object);
MBeanProxy mBean = new MBeanProxy(mBeanType, object, parentName);
registerMBeanProxy(object, mBean);
registerMBean(mBean);
registerMBeanProxy(object, mBean);
registerCompositionMBeans(mBean);
}
catch(Throwable error)
Expand Down
33 changes: 30 additions & 3 deletions src/test/java/com/axonivy/jmx/TestMBeans.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
public class TestMBeans extends BaseMTest<TestMBeans.TestBean>
{
private final LogTestAppender logAppender = new LogTestAppender(Level.ERROR);

public static class BaseTestBean
{
@SuppressWarnings("unused")
Expand All @@ -32,14 +32,14 @@ private String getName()
}

@SuppressWarnings("unused")
private String surname="Name";
private final String surname="Name";
}

@MBean(value="Test:type=TestType,name=#{name}#{surname},app=#{pmv.application.name}", description="Description of #{application.name}")
public static class TestBean extends BaseTestBean
{
@SuppressWarnings("unused")
private Application application = new Application();
private final Application application = new Application();


public Pmv getPmv()
Expand All @@ -48,6 +48,14 @@ public Pmv getPmv()
}
}

@MBean(value="Test:name=#{name}")
public static class ErrorBean {
@SuppressWarnings("unused")
private String getName() {
throw new IllegalStateException("Not allowed to call getName");
}
}

public static class Pmv
{
public Application getApplication()
Expand Down Expand Up @@ -181,4 +189,23 @@ public void testRegisterNotMBean()
MBeans.registerMBeanFor(new Object());
assertThat(logAppender.getRecording()).contains("Bean 'class java.lang.Object' must contain a @MBean annotation");
}

@Test
public void testRegisterMBeanThrowsException() {
Logger.getLogger(LogErrorStrategy.class).addAppender(logAppender);
assertThat(logAppender.getRecording()).isEmpty();
MBeans.registerMBeanFor(new ErrorBean());
assertThat(logAppender.getRecording()).contains("Cannot resolve 'name' on mBean 'com.axonivy.jmx.TestMBeans$ErrorBean");
}

@Test
public void testRegisterUnregisterMBeanThrowsException() {
Logger.getLogger(LogErrorStrategy.class).addAppender(logAppender);
assertThat(logAppender.getRecording()).isEmpty();
Object bean = new ErrorBean();
MBeans.registerMBeanFor(bean);
MBeans.unregisterMBeanFor(bean);
assertThat(logAppender.getRecording()).contains("Cannot resolve 'name' on mBean 'com.axonivy.jmx.TestMBeans$ErrorBean");
}

}

0 comments on commit 77c1a22

Please sign in to comment.