Skip to content

Commit 9ab6779

Browse files
artembilangaryrussell
authored andcommitted
Fix new Sonar smell and some other in JMX module (#2716)
* Fix new Sonar smell and some other in JMX module * * Fix issues after testing
1 parent bac3565 commit 9ab6779

File tree

7 files changed

+339
-266
lines changed

7 files changed

+339
-266
lines changed

spring-integration-core/src/main/java/org/springframework/integration/dsl/IntegrationComponentSpec.java

+15-5
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,28 @@ public Class<?> getObjectType() {
7979
}
8080

8181
@Override
82-
protected T createInstance() throws Exception {
82+
protected T createInstance() {
8383
T instance = get();
8484
if (instance instanceof InitializingBean) {
85-
((InitializingBean) instance).afterPropertiesSet();
85+
try {
86+
((InitializingBean) instance).afterPropertiesSet();
87+
}
88+
catch (Exception e) {
89+
throw new IllegalStateException("Cannot initialize bean: " + instance, e);
90+
}
8691
}
8792
return instance;
8893
}
8994

9095
@Override
91-
protected void destroyInstance(T instance) throws Exception {
96+
protected void destroyInstance(T instance) {
9297
if (instance instanceof DisposableBean) {
93-
((DisposableBean) instance).destroy();
98+
try {
99+
((DisposableBean) instance).destroy();
100+
}
101+
catch (Exception e) {
102+
throw new IllegalStateException("Cannot destroy bean: " + instance, e);
103+
}
94104
}
95105
}
96106

@@ -145,7 +155,7 @@ public int getPhase() {
145155
}
146156

147157
@SuppressWarnings("unchecked")
148-
protected final S _this() {
158+
protected final S _this() { // NOSONAR
149159
return (S) this;
150160
}
151161

Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2016 the original author or authors.
2+
* Copyright 2013-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -35,14 +35,18 @@
3535
import org.apache.commons.logging.Log;
3636
import org.apache.commons.logging.LogFactory;
3737

38+
import org.springframework.util.Assert;
39+
3840
/**
3941
* @author Stuart Williams
42+
* @author Artem Bilan
43+
*
4044
* @since 3.0
4145
*
4246
*/
4347
public class DefaultMBeanObjectConverter implements MBeanObjectConverter {
4448

45-
private static final Log log = LogFactory.getLog(DefaultMBeanObjectConverter.class);
49+
private static final Log LOGGER = LogFactory.getLog(DefaultMBeanObjectConverter.class);
4650

4751
private final MBeanAttributeFilter filter;
4852

@@ -51,12 +55,13 @@ public DefaultMBeanObjectConverter() {
5155
}
5256

5357
public DefaultMBeanObjectConverter(MBeanAttributeFilter filter) {
58+
Assert.notNull(filter, "'filter' must not be null.");
5459
this.filter = filter;
5560
}
5661

5762
@Override
5863
public Object convert(MBeanServerConnection connection, ObjectInstance instance) {
59-
Map<String, Object> attributeMap = new HashMap<String, Object>();
64+
Map<String, Object> attributeMap = new HashMap<>();
6065

6166
try {
6267
ObjectName objName = instance.getObjectName();
@@ -81,8 +86,8 @@ public Object convert(MBeanServerConnection connection, ObjectInstance instance)
8186
// N.B. standard MemoryUsage MBeans will throw an exception when some
8287
// measurement is unsupported. Logging at trace rather than debug to
8388
// avoid confusion.
84-
if (log.isTraceEnabled()) {
85-
log.trace("Error getting attribute '" + attrInfo.getName() + "' on '" + objName + "'", e);
89+
if (LOGGER.isTraceEnabled()) {
90+
LOGGER.trace("Error getting attribute '" + attrInfo.getName() + "' on '" + objName + "'", e);
8691
}
8792

8893
// try to unwrap the exception somewhat; not sure this is ideal
@@ -104,90 +109,95 @@ public Object convert(MBeanServerConnection connection, ObjectInstance instance)
104109
return attributeMap;
105110
}
106111

107-
/**
108-
* @param input
109-
* @return recursively mapped object
110-
*/
111112
private Object checkAndConvert(Object input) {
112-
if (input == null) {
113+
Object converted = null;
114+
if (input instanceof CompositeData) {
115+
converted = convertFromCompositeData((CompositeData) input);
116+
}
117+
else if (input instanceof TabularData) {
118+
converted = convertFromTabularData((TabularData) input);
119+
}
120+
else if (input != null && input.getClass().isArray()) {
121+
converted = convertFromArray(input);
122+
}
123+
124+
if (converted != null) {
125+
return converted;
126+
}
127+
else {
113128
return input;
114129
}
115-
else if (input.getClass().isArray()) {
116-
117-
if (CompositeData.class.isAssignableFrom(input.getClass().getComponentType())) {
118-
List<Object> converted = new ArrayList<Object>();
119-
int length = Array.getLength(input);
120-
for (int i = 0; i < length; i++) {
121-
Object value = checkAndConvert(Array.get(input, i));
122-
converted.add(value);
123-
}
124-
return converted;
125-
}
126-
if (TabularData.class.isAssignableFrom(input.getClass().getComponentType())) {
127-
// TODO haven't hit this yet, but expect to
128-
log.warn("TabularData.isAssignableFrom(getComponentType) for " + input.toString());
130+
}
131+
132+
private Object convertFromArray(Object input) {
133+
if (CompositeData.class.isAssignableFrom(input.getClass().getComponentType())) {
134+
List<Object> converted = new ArrayList<>();
135+
int length = Array.getLength(input);
136+
for (int i = 0; i < length; i++) {
137+
Object value = checkAndConvert(Array.get(input, i));
138+
converted.add(value);
129139
}
140+
return converted;
141+
}
142+
if (TabularData.class.isAssignableFrom(input.getClass().getComponentType())) {
143+
// TODO haven't hit this yet, but expect to
144+
LOGGER.warn("TabularData.isAssignableFrom(getComponentType) for " + input.toString());
130145
}
131-
else if (input instanceof CompositeData) {
132-
CompositeData data = (CompositeData) input;
146+
return null;
147+
}
133148

134-
if (data.getCompositeType().isArray()) {
135-
// TODO? I haven't found an example where this gets thrown - but need to test it on Tomcat/Jetty or
136-
// something
137-
log.warn("(data.getCompositeType().isArray for " + input.toString());
138-
}
139-
else {
140-
Map<String, Object> returnable = new HashMap<String, Object>();
141-
Set<String> keys = data.getCompositeType().keySet();
142-
for (String key : keys) {
143-
// we don't need to repeat name of this as an attribute
144-
if ("ObjectName".equals(key)) {
145-
continue;
146-
}
147-
Object value = checkAndConvert(data.get(key));
148-
returnable.put(key, value);
149+
private Object convertFromCompositeData(CompositeData data) {
150+
if (data.getCompositeType().isArray()) {
151+
// TODO? I haven't found an example where this gets thrown - but need to test it on Tomcat/Jetty or
152+
// something
153+
LOGGER.warn("(data.getCompositeType().isArray for " + data.toString());
154+
return null;
155+
}
156+
else {
157+
Map<String, Object> returnable = new HashMap<>();
158+
Set<String> keys = data.getCompositeType().keySet();
159+
for (String key : keys) {
160+
// we don't need to repeat name of this as an attribute
161+
if ("ObjectName".equals(key)) {
162+
continue;
149163
}
150-
return returnable;
164+
Object value = checkAndConvert(data.get(key));
165+
returnable.put(key, value);
151166
}
167+
return returnable;
152168
}
153-
else if (input instanceof TabularData) {
154-
TabularData data = (TabularData) input;
169+
}
155170

156-
if (data.getTabularType().isArray()) {
157-
// TODO? I haven't found an example where this gets thrown, so might not be required
158-
log.warn("TabularData.isArray for " + input.toString());
159-
}
160-
else {
161-
162-
Map<Object, Object> returnable = new HashMap<Object, Object>();
163-
@SuppressWarnings("unchecked")
164-
Set<List<?>> keySet = (Set<List<?>>) data.keySet();
165-
for (List<?> keys : keySet) {
166-
CompositeData cd = data.get(keys.toArray());
167-
Object value = checkAndConvert(cd);
168-
169-
if (keys.size() == 1 && (value instanceof Map) && ((Map<?, ?>) value).size() == 2) {
170-
171-
Object actualKey = keys.get(0);
172-
Map<?, ?> valueMap = (Map<?, ?>) value;
173-
174-
if (valueMap.containsKey("key") && valueMap.containsKey("value")
175-
&& actualKey.equals(valueMap.get("key"))) {
176-
returnable.put(valueMap.get("key"), valueMap.get("value"));
177-
}
178-
else {
179-
returnable.put(actualKey, value);
180-
}
171+
private Object convertFromTabularData(TabularData data) {
172+
if (data.getTabularType().isArray()) {
173+
// TODO? I haven't found an example where this gets thrown, so might not be required
174+
LOGGER.warn("TabularData.isArray for " + data.toString());
175+
return null;
176+
}
177+
else {
178+
Map<Object, Object> returnable = new HashMap<>();
179+
@SuppressWarnings("unchecked")
180+
Set<List<?>> keySet = (Set<List<?>>) data.keySet();
181+
for (List<?> keys : keySet) {
182+
CompositeData cd = data.get(keys.toArray());
183+
Object value = checkAndConvert(cd);
184+
if (keys.size() == 1 && (value instanceof Map) && ((Map<?, ?>) value).size() == 2) {
185+
Object actualKey = keys.get(0);
186+
Map<?, ?> valueMap = (Map<?, ?>) value;
187+
if (valueMap.containsKey("key") && valueMap.containsKey("value")
188+
&& actualKey.equals(valueMap.get("key"))) {
189+
returnable.put(valueMap.get("key"), valueMap.get("value"));
181190
}
182191
else {
183-
returnable.put(keys, value);
192+
returnable.put(actualKey, value);
184193
}
185194
}
186-
187-
return returnable;
195+
else {
196+
returnable.put(keys, value);
197+
}
188198
}
199+
return returnable;
189200
}
190-
191-
return input;
192201
}
202+
193203
}

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

+11-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@
2222
import javax.management.ObjectName;
2323

2424
import org.springframework.integration.mapping.OutboundMessageMapper;
25+
import org.springframework.lang.Nullable;
2526
import org.springframework.messaging.Message;
2627
import org.springframework.util.Assert;
2728

@@ -32,32 +33,34 @@
3233
* 'userData' of the Notification instance.
3334
*
3435
* @author Mark Fisher
36+
* @author Artem Bilan
37+
*
3538
* @since 2.0
3639
*/
3740
class DefaultNotificationMapper implements OutboundMessageMapper<Notification> {
3841

3942
private final ObjectName sourceObjectName;
4043

44+
@Nullable
4145
private final String defaultNotificationType;
4246

4347
private final AtomicLong sequence = new AtomicLong();
4448

4549

46-
DefaultNotificationMapper(ObjectName sourceObjectName, String defaultNotificationType) {
50+
DefaultNotificationMapper(ObjectName sourceObjectName, @Nullable String defaultNotificationType) {
4751
this.sourceObjectName = sourceObjectName;
4852
this.defaultNotificationType = defaultNotificationType;
4953
}
5054

5155

52-
public Notification fromMessage(Message<?> message) throws Exception {
53-
String type = this.resolveNotificationType(message);
54-
Assert.hasText(type,
55-
"No notification type header is available, and no default has been provided.");
56-
Object payload = (message != null) ? message.getPayload() : null;
56+
public Notification fromMessage(Message<?> message) {
57+
String type = resolveNotificationType(message);
58+
Assert.hasText(type, "No notification type header is available, and no default has been provided.");
59+
Object payload = message.getPayload();
5760
String notificationMessage = (payload instanceof String) ? (String) payload : null;
5861
Notification notification = new Notification(type, this.sourceObjectName,
5962
this.sequence.incrementAndGet(), System.currentTimeMillis(), notificationMessage);
60-
if (payload != null && !(payload instanceof String)) {
63+
if (!(payload instanceof String)) {
6164
notification.setUserData(payload);
6265
}
6366
return notification;

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

+12-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -35,6 +35,7 @@
3535
import org.springframework.jmx.export.notification.NotificationPublisher;
3636
import org.springframework.jmx.export.notification.NotificationPublisherAware;
3737
import org.springframework.jmx.support.ObjectNameManager;
38+
import org.springframework.lang.Nullable;
3839
import org.springframework.messaging.Message;
3940
import org.springframework.util.Assert;
4041

@@ -55,11 +56,12 @@ public class NotificationPublishingMessageHandler extends AbstractMessageHandler
5556

5657
private final PublisherDelegate delegate = new PublisherDelegate();
5758

58-
private volatile OutboundMessageMapper<Notification> notificationMapper;
59-
6059
private final ObjectName objectName;
6160

62-
private volatile String defaultNotificationType;
61+
private String defaultNotificationType;
62+
63+
@Nullable
64+
private OutboundMessageMapper<Notification> notificationMapper;
6365

6466

6567
public NotificationPublishingMessageHandler(ObjectName objectName) {
@@ -85,7 +87,7 @@ public NotificationPublishingMessageHandler(String objectName) {
8587
* will be passed as the 'userData' of the Notification.
8688
* @param notificationMapper The notification mapper.
8789
*/
88-
public void setNotificationMapper(OutboundMessageMapper<Notification> notificationMapper) {
90+
public void setNotificationMapper(@Nullable OutboundMessageMapper<Notification> notificationMapper) {
8991
this.notificationMapper = notificationMapper;
9092
}
9193

@@ -108,8 +110,9 @@ public String getComponentType() {
108110
@Override
109111
public final void onInit() {
110112
Assert.isTrue(this.getBeanFactory() instanceof ListableBeanFactory, "A ListableBeanFactory is required.");
111-
Map<String, MBeanExporter> exporters = BeanFactoryUtils.beansOfTypeIncludingAncestors(
112-
(ListableBeanFactory) this.getBeanFactory(), MBeanExporter.class);
113+
Map<String, MBeanExporter> exporters =
114+
BeanFactoryUtils.beansOfTypeIncludingAncestors((ListableBeanFactory) getBeanFactory(),
115+
MBeanExporter.class);
113116
Assert.isTrue(exporters.size() > 0, "No MBeanExporter is available in the current context.");
114117
MBeanExporter exporter = null;
115118
for (MBeanExporter exp : exporters.values()) {
@@ -128,7 +131,7 @@ public final void onInit() {
128131
}
129132

130133
@Override
131-
protected void handleMessageInternal(Message<?> message) throws Exception {
134+
protected void handleMessageInternal(Message<?> message) throws Exception { // NOSONAR
132135
this.delegate.publish(this.notificationMapper.fromMessage(message));
133136
}
134137

@@ -141,7 +144,7 @@ protected void handleMessageInternal(Message<?> message) throws Exception {
141144
@IntegrationManagedResource
142145
public static class PublisherDelegate implements NotificationPublisherAware {
143146

144-
private volatile NotificationPublisher notificationPublisher;
147+
private NotificationPublisher notificationPublisher;
145148

146149
@Override
147150
public void setNotificationPublisher(NotificationPublisher notificationPublisher) {

0 commit comments

Comments
 (0)