Skip to content
This repository was archived by the owner on Aug 13, 2020. It is now read-only.

Commit ecfcb98

Browse files
author
amckenzie
committed
Give a better error message if the JMX bean name is incorrect
1 parent f8874bb commit ecfcb98

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ on [Keep a CHANGELOG](http://keepachangelog.com/). This project adheres to
55

66

77
## [Unreleased]
8+
## [6.0.8] - 2019-08-21
9+
### Fixed
10+
- Better error message if the name of the JMX bean is incorrect when called by the client
811

912
## [6.0.7] - 2019-08-20
1013
### Added

jmx/jmx-command-client/src/main/java/uk/gov/justice/services/jmx/system/command/client/MBeanClientException.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
public class MBeanClientException extends RuntimeException {
44

5+
public MBeanClientException(final String message) {
6+
super(message);
7+
}
8+
59
public MBeanClientException(final String message, final Throwable cause) {
610
super(message, cause);
711
}

jmx/jmx-command-client/src/main/java/uk/gov/justice/services/jmx/system/command/client/connection/MBeanConnector.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@ public <T> T connect(final String contextName, final Class<T> mBeanInterface, fi
2525
final ObjectName objectName = commandMBeanNameProvider.create(contextName);
2626
final MBeanServerConnection connection = jmxConnector.getMBeanServerConnection();
2727

28-
return remoteMBeanFactory.createRemote(connection, objectName, mBeanInterface);
28+
if(connection.isRegistered(objectName)) {
29+
return remoteMBeanFactory.createRemote(connection, objectName, mBeanInterface);
30+
}
31+
32+
throw new MBeanClientException(format("No JMX bean found with name '%s'. Is your context name of '%s' correct?", objectName.getKeyProperty("type"), contextName));
33+
2934
} catch (final IOException e) {
3035
throw new MBeanClientException(format("Failed to get remote connection to MBean '%s'", mBeanInterface.getSimpleName()), e);
3136
}

jmx/jmx-command-client/src/test/java/uk/gov/justice/services/jmx/system/command/client/connection/MBeanConnectorTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
import static org.junit.Assert.fail;
66
import static org.mockito.Mockito.mock;
77
import static org.mockito.Mockito.when;
8+
import static uk.gov.justice.services.test.utils.core.reflection.ReflectionUtil.setField;
89

910
import uk.gov.justice.services.jmx.api.mbean.SystemCommanderMBean;
1011
import uk.gov.justice.services.jmx.api.name.CommandMBeanNameProvider;
12+
import uk.gov.justice.services.jmx.api.name.ObjectNameFactory;
1113
import uk.gov.justice.services.jmx.system.command.client.MBeanClientException;
14+
import uk.gov.justice.services.test.utils.core.reflection.ReflectionUtil;
1215

1316
import java.io.IOException;
1417

@@ -47,6 +50,7 @@ public void shouldConnectToARemoteInstanceOfTheJmxBean() throws Exception {
4750

4851
when(commandMBeanNameProvider.create(contextName)).thenReturn(objectName);
4952
when(jmxConnector.getMBeanServerConnection()).thenReturn(connection);
53+
when(connection.isRegistered(objectName)).thenReturn(true);
5054
when(remoteMBeanFactory.createRemote(connection, objectName, mBeanInterface)).thenReturn(systemCommanderMBean);
5155

5256
assertThat(mBeanConnector.connect(contextName, mBeanInterface, jmxConnector), is(systemCommanderMBean));
@@ -74,4 +78,34 @@ public void shouldThrowExceptionIfConnectingToTheMBeanFails() throws Exception {
7478
assertThat(expected.getMessage(), is("Failed to get remote connection to MBean 'SystemCommanderMBean'"));
7579
}
7680
}
81+
82+
@Test
83+
public void shouldThrowExceptionIfMBeanNameNotCorrect() throws Exception {
84+
85+
final String contextName = "people";
86+
final ObjectName realObjectName = createARealObjectName(contextName);
87+
88+
final Class<SystemCommanderMBean> mBeanInterface = SystemCommanderMBean.class;
89+
final JMXConnector jmxConnector = mock(JMXConnector.class);
90+
final MBeanServerConnection connection = mock(MBeanServerConnection.class);
91+
92+
when(commandMBeanNameProvider.create(contextName)).thenReturn(realObjectName);
93+
when(jmxConnector.getMBeanServerConnection()).thenReturn(connection);
94+
when(connection.isRegistered(realObjectName)).thenReturn(false);
95+
96+
97+
try {
98+
mBeanConnector.connect(contextName, mBeanInterface, jmxConnector);
99+
fail();
100+
} catch (final MBeanClientException expected) {
101+
assertThat(expected.getMessage(), is("No JMX bean found with name 'people-system-command-handler-mbean'. Is your context name of 'people' correct?"));
102+
}
103+
}
104+
105+
private ObjectName createARealObjectName(final String contextName) {
106+
final CommandMBeanNameProvider commandMBeanNameProvider = new CommandMBeanNameProvider();
107+
setField(commandMBeanNameProvider, "objectNameFactory", new ObjectNameFactory());
108+
109+
return commandMBeanNameProvider.create(contextName);
110+
}
77111
}

0 commit comments

Comments
 (0)