Skip to content

Commit db239f8

Browse files
committed
Fix NPE Sonar report in the NotificationPublishMH
1 parent 9ab6779 commit db239f8

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

spring-integration-jmx/src/main/java/org/springframework/integration/jmx/NotificationPublishingMessageHandler.java

+10-7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import javax.management.Notification;
2323
import javax.management.ObjectName;
2424

25+
import org.springframework.beans.factory.BeanFactory;
2526
import org.springframework.beans.factory.BeanFactoryAware;
2627
import org.springframework.beans.factory.BeanFactoryUtils;
2728
import org.springframework.beans.factory.InitializingBean;
@@ -60,7 +61,6 @@ public class NotificationPublishingMessageHandler extends AbstractMessageHandler
6061

6162
private String defaultNotificationType;
6263

63-
@Nullable
6464
private OutboundMessageMapper<Notification> notificationMapper;
6565

6666

@@ -109,11 +109,12 @@ public String getComponentType() {
109109

110110
@Override
111111
public final void onInit() {
112-
Assert.isTrue(this.getBeanFactory() instanceof ListableBeanFactory, "A ListableBeanFactory is required.");
112+
BeanFactory beanFactory = getBeanFactory();
113+
Assert.isTrue(beanFactory instanceof ListableBeanFactory, "A ListableBeanFactory is required.");
113114
Map<String, MBeanExporter> exporters =
114-
BeanFactoryUtils.beansOfTypeIncludingAncestors((ListableBeanFactory) getBeanFactory(),
115+
BeanFactoryUtils.beansOfTypeIncludingAncestors((ListableBeanFactory) beanFactory,
115116
MBeanExporter.class);
116-
Assert.isTrue(exporters.size() > 0, "No MBeanExporter is available in the current context.");
117+
Assert.state(exporters.size() > 0, "No MBeanExporter is available in the current context.");
117118
MBeanExporter exporter = null;
118119
for (MBeanExporter exp : exporters.values()) {
119120
exporter = exp;
@@ -124,9 +125,11 @@ public final void onInit() {
124125
if (this.notificationMapper == null) {
125126
this.notificationMapper = new DefaultNotificationMapper(this.objectName, this.defaultNotificationType);
126127
}
127-
exporter.registerManagedResource(this.delegate, this.objectName);
128-
if (this.logger.isInfoEnabled()) {
129-
this.logger.info("Registered JMX notification publisher as MBean with ObjectName: " + this.objectName);
128+
if (exporter != null) {
129+
exporter.registerManagedResource(this.delegate, this.objectName);
130+
if (this.logger.isInfoEnabled()) {
131+
this.logger.info("Registered JMX notification publisher as MBean with ObjectName: " + this.objectName);
132+
}
130133
}
131134
}
132135

0 commit comments

Comments
 (0)