Skip to content

Commit 2711812

Browse files
artembilangaryrussell
authored andcommitted
Fix code smell in test and syslog modules (#2667)
* Fix code smell in test and syslog modules * * Fix code smell in the scripting module
1 parent 12dd73d commit 2711812

File tree

4 files changed

+51
-26
lines changed

4 files changed

+51
-26
lines changed

spring-integration-scripting/src/main/java/org/springframework/integration/scripting/dsl/DslScriptExecutingMessageProcessor.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016 the original author or authors.
2+
* Copyright 2016-2018 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.
@@ -17,6 +17,7 @@
1717
package org.springframework.integration.scripting.dsl;
1818

1919
import org.springframework.beans.BeansException;
20+
import org.springframework.beans.factory.BeanClassLoaderAware;
2021
import org.springframework.beans.factory.BeanCreationException;
2122
import org.springframework.beans.factory.InitializingBean;
2223
import org.springframework.context.ApplicationContext;
@@ -44,7 +45,7 @@
4445
* @since 5.0
4546
*/
4647
class DslScriptExecutingMessageProcessor
47-
implements MessageProcessor<Object>, InitializingBean, ApplicationContextAware {
48+
implements MessageProcessor<Object>, InitializingBean, ApplicationContextAware, BeanClassLoaderAware {
4849

4950
private Resource script;
5051

@@ -60,6 +61,8 @@ class DslScriptExecutingMessageProcessor
6061

6162
private AbstractScriptExecutingMessageProcessor<?> delegate;
6263

64+
private ClassLoader classLoader;
65+
6366
DslScriptExecutingMessageProcessor(Resource script) {
6467
this.script = script;
6568
}
@@ -86,7 +89,12 @@ public void setApplicationContext(ApplicationContext applicationContext) throws
8689
}
8790

8891
@Override
89-
public void afterPropertiesSet() throws Exception {
92+
public void setBeanClassLoader(ClassLoader classLoader) {
93+
this.classLoader = classLoader;
94+
}
95+
96+
@Override
97+
public void afterPropertiesSet() {
9098
if (StringUtils.hasText(this.location)) {
9199
this.script = this.applicationContext.getResource(this.location);
92100
}
@@ -95,10 +103,13 @@ public void afterPropertiesSet() throws Exception {
95103

96104
if (!StringUtils.hasText(this.lang)) {
97105
String filename = this.script.getFilename();
98-
int index = filename.lastIndexOf(".") + 1;
106+
int index =
107+
filename != null
108+
? filename.lastIndexOf(".") + 1
109+
: -1;
99110
if (index < 1) {
100-
throw new BeanCreationException("'lang' isn't provided and there is 'file extension' for script " +
101-
"resource: " + this.script);
111+
throw new BeanCreationException(
112+
"'lang' isn't provided and there is no 'file extension' for script resource: " + this.script);
102113
}
103114
this.lang = filename.substring(index);
104115
}
@@ -115,7 +126,7 @@ public void afterPropertiesSet() throws Exception {
115126
}
116127

117128
this.delegate.setBeanFactory(this.applicationContext);
118-
this.delegate.setBeanClassLoader(this.applicationContext.getClassLoader());
129+
this.delegate.setBeanClassLoader(this.classLoader);
119130
}
120131

121132
@Override

spring-integration-syslog/src/main/java/org/springframework/integration/syslog/config/SyslogReceivingChannelAdapterFactoryBean.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.integration.syslog.config;
1818

19+
import org.springframework.beans.factory.BeanFactory;
1920
import org.springframework.beans.factory.BeanNameAware;
2021
import org.springframework.beans.factory.config.AbstractFactoryBean;
2122
import org.springframework.context.ApplicationEventPublisher;
@@ -173,12 +174,13 @@ public void stop(Runnable callback) {
173174

174175
@Override
175176
public Class<?> getObjectType() {
176-
return this.adapter == null ? SyslogReceivingChannelAdapterSupport.class :
177-
this.adapter.getClass();
177+
return this.adapter == null
178+
? SyslogReceivingChannelAdapterSupport.class
179+
: this.adapter.getClass();
178180
}
179181

180182
@Override
181-
protected SyslogReceivingChannelAdapterSupport createInstance() throws Exception {
183+
protected SyslogReceivingChannelAdapterSupport createInstance() {
182184
SyslogReceivingChannelAdapterSupport adapter;
183185
if (this.protocol == Protocol.tcp) {
184186
adapter = new TcpSyslogReceivingChannelAdapter();
@@ -187,7 +189,8 @@ protected SyslogReceivingChannelAdapterSupport createInstance() throws Exception
187189
((TcpSyslogReceivingChannelAdapter) adapter).setConnectionFactory(this.connectionFactory);
188190
}
189191
else if (this.applicationEventPublisher != null) {
190-
((TcpSyslogReceivingChannelAdapter) adapter).setApplicationEventPublisher(this.applicationEventPublisher);
192+
((TcpSyslogReceivingChannelAdapter) adapter)
193+
.setApplicationEventPublisher(this.applicationEventPublisher);
191194
}
192195
Assert.isNull(this.udpAdapter, "Cannot specify 'udp-attributes' when the protocol is 'tcp'");
193196
}
@@ -222,12 +225,13 @@ else if (this.protocol == Protocol.udp) {
222225
if (this.beanName != null) {
223226
adapter.setBeanName(this.beanName);
224227
}
225-
if (this.getBeanFactory() != null) {
226-
adapter.setBeanFactory(this.getBeanFactory());
228+
BeanFactory beanFactory = getBeanFactory();
229+
if (beanFactory != null) {
230+
adapter.setBeanFactory(beanFactory);
227231
}
228232
adapter.afterPropertiesSet();
229233
this.adapter = adapter;
230-
return adapter;
234+
return this.adapter;
231235
}
232236

233237
}

spring-integration-test-support/src/main/java/org/springframework/integration/test/util/TestUtils.java

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public void registerBean(String beanName, Object bean) {
174174

175175
private String getComponentNameIfNamed(final MessageChannel channel) {
176176
Set<Class<?>> interfaces = ClassUtils.getAllInterfacesAsSet(channel);
177-
final AtomicReference<String> componentName = new AtomicReference<String>();
177+
final AtomicReference<String> componentName = new AtomicReference<>();
178178
for (Class<?> intface : interfaces) {
179179
if ("org.springframework.integration.support.context.NamedComponent".equals(intface.getName())) {
180180
ReflectionUtils.doWithMethods(channel.getClass(), method -> {
@@ -235,12 +235,12 @@ private static class MessagePublishingErrorHandler implements ErrorHandler {
235235
}
236236

237237
@Override
238-
public void handleError(Throwable t) {
239-
MessageChannel errorChannel = this.resolveErrorChannel(t);
238+
public void handleError(Throwable throwable) {
239+
MessageChannel errorChannel = resolveErrorChannel(throwable);
240240
boolean sent = false;
241241
if (errorChannel != null) {
242242
try {
243-
sent = errorChannel.send(new ErrorMessage(t), 10000);
243+
sent = errorChannel.send(new ErrorMessage(throwable), 10000);
244244
}
245245
catch (Throwable errorDeliveryError) { //NOSONAR
246246
// message will be logged only
@@ -253,13 +253,15 @@ public void handleError(Throwable t) {
253253
}
254254
}
255255
if (!sent && logger.isErrorEnabled()) {
256-
Message<?> failedMessage = (t instanceof MessagingException) ?
257-
((MessagingException) t).getFailedMessage() : null;
256+
Message<?> failedMessage =
257+
throwable instanceof MessagingException
258+
? ((MessagingException) throwable).getFailedMessage()
259+
: null;
258260
if (failedMessage != null) {
259-
logger.error("failure occurred in messaging task with message: " + failedMessage, t);
261+
logger.error("failure occurred in messaging task with message: " + failedMessage, throwable);
260262
}
261263
else {
262-
logger.error("failure occurred in messaging task", t);
264+
logger.error("failure occurred in messaging task", throwable);
263265
}
264266
}
265267

@@ -268,14 +270,20 @@ public void handleError(Throwable t) {
268270
private MessageChannel resolveErrorChannel(Throwable t) {
269271
if (t instanceof MessagingException) {
270272
Message<?> failedMessage = ((MessagingException) t).getFailedMessage();
273+
if (failedMessage == null) {
274+
return null;
275+
}
271276
Object errorChannelHeader = failedMessage.getHeaders().getErrorChannel();
272277
if (errorChannelHeader instanceof MessageChannel) {
273278
return (MessageChannel) errorChannelHeader;
274279
}
275-
Assert.isInstanceOf(String.class, errorChannelHeader, "Unsupported error channel header type. " +
276-
"Expected MessageChannel or String, but actual type is [" +
277-
errorChannelHeader.getClass() + "]");
278-
return this.context.getBean((String) errorChannelHeader, MessageChannel.class);
280+
else if (errorChannelHeader instanceof String) {
281+
return this.context.getBean((String) errorChannelHeader, MessageChannel.class);
282+
}
283+
else {
284+
throw new IllegalStateException("Unsupported error channel header type. " +
285+
"Expected MessageChannel or String, but actual header is [" + errorChannelHeader + "]");
286+
}
279287
}
280288
else {
281289
return null;

spring-integration-test/src/main/java/org/springframework/integration/test/context/MockIntegrationContext.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,10 @@ public void substituteMessageHandlerFor(String consumerEndpointId, MessageHandle
148148
}
149149
DirectFieldAccessor directFieldAccessor = new DirectFieldAccessor(endpoint);
150150
Object targetMessageHandler = directFieldAccessor.getPropertyValue("handler");
151+
Assert.notNull(targetMessageHandler, () -> "'handler' must not be null in the: " + endpoint);
151152
if (endpoint instanceof ReactiveStreamsConsumer) {
152153
Object targetSubscriber = directFieldAccessor.getPropertyValue("subscriber");
154+
Assert.notNull(targetSubscriber, () -> "'subscriber' must not be null in the: " + endpoint);
153155
this.beans.put(consumerEndpointId, Tuples.of(targetMessageHandler, targetSubscriber));
154156
}
155157
else {

0 commit comments

Comments
 (0)