Skip to content

Commit 4900ac7

Browse files
authored
GH-10083: Apply Nullability to TCP/IP module (#10306)
Related to: #10083 * Add `@NullMarked` to every package * Assume `BeanFactory` and `ApplicationEventPublisher` are not null in TCP/IP components. Therefore, fix all the failing tests to ensure those properties and their propagation via `afterPropertiesSet()` * Fix `DatagramPacketMessageMapperTests` to use `assertThatExceptionOfType` instead of `try..catch` * Fix Syslog module for changes in IP module * Remove `SyslogdTests.java` which are not tests. * Improve `TcpInboundGateway` logic to check for `connectionFactory` is set in the `onInit()` * Improve `UnicastSendingMessageHandler.restartAckThread()` to reflect the proper state of the `UnicastSendingMessageHandler`
1 parent 00c812f commit 4900ac7

File tree

94 files changed

+1068
-926
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+1068
-926
lines changed

spring-integration-core/src/main/java/org/springframework/integration/context/IntegrationObjectSupport.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,7 @@ public void setChannelResolver(DestinationResolver<MessageChannel> channelResolv
211211
}
212212

213213
@Override
214-
@Nullable
215-
public Expression getExpression() {
214+
public @Nullable Expression getExpression() {
216215
return this.expression;
217216
}
218217

spring-integration-ip/src/main/java/org/springframework/integration/ip/AbstractInternetProtocolReceivingChannelAdapter.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import java.util.concurrent.ExecutorService;
2121
import java.util.concurrent.Executors;
2222

23+
import org.jspecify.annotations.Nullable;
24+
2325
import org.springframework.context.ApplicationEventPublisher;
2426
import org.springframework.context.ApplicationEventPublisherAware;
2527
import org.springframework.integration.endpoint.MessageProducerSupport;
@@ -41,6 +43,7 @@ public abstract class AbstractInternetProtocolReceivingChannelAdapter
4143

4244
private final int port;
4345

46+
@SuppressWarnings("NullAway.Init")
4447
private ApplicationEventPublisher applicationEventPublisher;
4548

4649
private int soTimeout = 0;
@@ -49,9 +52,9 @@ public abstract class AbstractInternetProtocolReceivingChannelAdapter
4952

5053
private int receiveBufferSize = 2048; // NOSONAR magic number
5154

52-
private String localAddress;
55+
private @Nullable String localAddress;
5356

54-
private Executor taskExecutor;
57+
private @Nullable Executor taskExecutor;
5558

5659
private boolean taskExecutorSet;
5760

@@ -117,7 +120,7 @@ public void setListening(boolean listening) {
117120
this.listening = listening;
118121
}
119122

120-
public String getLocalAddress() {
123+
public @Nullable String getLocalAddress() {
121124
return this.localAddress;
122125
}
123126

@@ -139,7 +142,7 @@ public void setTaskExecutor(Executor taskExecutor) {
139142
/**
140143
* @return the taskExecutor
141144
*/
142-
public Executor getTaskExecutor() {
145+
public @Nullable Executor getTaskExecutor() {
143146
return this.taskExecutor;
144147
}
145148

@@ -153,6 +156,7 @@ public void setApplicationEventPublisher(ApplicationEventPublisher applicationEv
153156
}
154157

155158
@Override
159+
@SuppressWarnings("NullAway") // Dataflow analysis limitation
156160
protected void doStart() {
157161
String beanName = getComponentName();
158162
checkTaskExecutor((beanName == null ? "" : beanName + "-") + getComponentType());
@@ -179,8 +183,9 @@ protected void checkTaskExecutor(final String threadName) {
179183

180184
@Override
181185
protected void doStop() {
182-
if (!this.taskExecutorSet && this.taskExecutor != null) {
183-
((ExecutorService) this.taskExecutor).shutdown();
186+
Executor taskExecutorToShutdown = this.taskExecutor;
187+
if (!this.taskExecutorSet && taskExecutorToShutdown != null) {
188+
((ExecutorService) taskExecutorToShutdown).shutdown();
184189
this.taskExecutor = null;
185190
}
186191
}

spring-integration-ip/src/main/java/org/springframework/integration/ip/config/TcpConnectionFactoryFactoryBean.java

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import java.util.concurrent.Executor;
2020

21+
import org.jspecify.annotations.Nullable;
22+
2123
import org.springframework.beans.BeansException;
2224
import org.springframework.beans.factory.BeanFactory;
2325
import org.springframework.beans.factory.BeanNameAware;
@@ -62,11 +64,12 @@
6264
public class TcpConnectionFactoryFactoryBean extends AbstractFactoryBean<AbstractConnectionFactory>
6365
implements Lifecycle, BeanNameAware, ApplicationEventPublisherAware, ApplicationContextAware {
6466

67+
@SuppressWarnings("NullAway.Init")
6568
private AbstractConnectionFactory connectionFactory;
6669

67-
private String type;
70+
private @Nullable String type;
6871

69-
private String host;
72+
private @Nullable String host;
7073

7174
private int port;
7275

@@ -84,7 +87,7 @@ public class TcpConnectionFactoryFactoryBean extends AbstractFactoryBean<Abstrac
8487

8588
private int soTrafficClass = -1; // don't set by default
8689

87-
private Executor taskExecutor;
90+
private @Nullable Executor taskExecutor;
8891

8992
private Deserializer<?> deserializer = new ByteArrayCrLfSerializer();
9093

@@ -98,38 +101,41 @@ public class TcpConnectionFactoryFactoryBean extends AbstractFactoryBean<Abstrac
98101

99102
private int backlog = 5; // NOSONAR magic number
100103

101-
private TcpConnectionInterceptorFactoryChain interceptorFactoryChain;
104+
private @Nullable TcpConnectionInterceptorFactoryChain interceptorFactoryChain;
102105

103106
private boolean lookupHost = true;
104107

105-
private String localAddress;
108+
private @Nullable String localAddress;
106109

107110
private boolean usingNio;
108111

109112
private boolean usingDirectBuffers;
110113

114+
@SuppressWarnings("NullAway.Init")
111115
private String beanName;
112116

113117
private boolean applySequence;
114118

115-
private Long readDelay;
119+
private @Nullable Long readDelay;
116120

117-
private TcpSSLContextSupport sslContextSupport;
121+
private @Nullable TcpSSLContextSupport sslContextSupport;
118122

119-
private Integer sslHandshakeTimeout;
123+
private @Nullable Integer sslHandshakeTimeout;
120124

121125
private TcpSocketSupport socketSupport = new DefaultTcpSocketSupport();
122126

123-
private TcpNioConnectionSupport nioConnectionSupport;
127+
private @Nullable TcpNioConnectionSupport nioConnectionSupport;
124128

125-
private TcpNetConnectionSupport netConnectionSupport;
129+
private @Nullable TcpNetConnectionSupport netConnectionSupport;
126130

127-
private TcpSocketFactorySupport socketFactorySupport;
131+
private @Nullable TcpSocketFactorySupport socketFactorySupport;
128132

133+
@SuppressWarnings("NullAway.Init")
129134
private ApplicationEventPublisher applicationEventPublisher;
130135

131-
private Integer connectTimeout;
136+
private @Nullable Integer connectTimeout;
132137

138+
@SuppressWarnings("NullAway.Init")
133139
private ApplicationContext applicationContext;
134140

135141
public TcpConnectionFactoryFactoryBean() {
@@ -169,6 +175,7 @@ protected AbstractConnectionFactory createInstance() {
169175
this.connectionFactory = factory;
170176
}
171177
else {
178+
Assert.notNull(this.host, "The 'host' must be provided for client factory.");
172179
TcpNioClientConnectionFactory factory = new TcpNioClientConnectionFactory(this.host, this.port);
173180
this.setCommonAttributes(factory);
174181
factory.setUsingDirectBuffers(this.usingDirectBuffers);
@@ -189,8 +196,8 @@ protected AbstractConnectionFactory createInstance() {
189196
this.connectionFactory = factory;
190197
}
191198
else {
192-
TcpNetClientConnectionFactory factory = new TcpNetClientConnectionFactory(
193-
this.host, this.port);
199+
Assert.notNull(this.host, "The 'host' must be provided for client factory.");
200+
TcpNetClientConnectionFactory factory = new TcpNetClientConnectionFactory(this.host, this.port);
194201
this.setCommonAttributes(factory);
195202
factory.setTcpSocketFactorySupport(this.obtainSocketFactorySupport());
196203
factory.setTcpNetConnectionSupport(this.obtainNetConnectionSupport());
@@ -203,16 +210,16 @@ protected AbstractConnectionFactory createInstance() {
203210
if (beanFactory != null) {
204211
this.connectionFactory.setBeanFactory(beanFactory);
205212
}
206-
if (this.applicationContext != null) {
207-
this.connectionFactory.setApplicationContext(this.applicationContext);
208-
}
213+
this.connectionFactory.setApplicationContext(this.applicationContext);
209214
this.connectionFactory.afterPropertiesSet();
210215
return this.connectionFactory;
211216
}
212217

213218
private void setCommonAttributes(AbstractConnectionFactory factory) {
214219
factory.setDeserializer(this.deserializer);
215-
factory.setInterceptorFactoryChain(this.interceptorFactoryChain);
220+
if (this.interceptorFactoryChain != null) {
221+
factory.setInterceptorFactoryChain(this.interceptorFactoryChain);
222+
}
216223
factory.setLookupHost(this.lookupHost);
217224
this.mapper.setApplySequence(this.applySequence);
218225
factory.setMapper(this.mapper);
@@ -225,7 +232,9 @@ private void setCommonAttributes(AbstractConnectionFactory factory) {
225232
factory.setSoTcpNoDelay(this.soTcpNoDelay);
226233
factory.setSoTimeout(this.soTimeout);
227234
factory.setSoTrafficClass(this.soTrafficClass);
228-
factory.setTaskExecutor(this.taskExecutor);
235+
if (this.taskExecutor != null) {
236+
factory.setTaskExecutor(this.taskExecutor);
237+
}
229238
factory.setBeanName(this.beanName);
230239
factory.setTcpSocketSupport(this.socketSupport);
231240
factory.setApplicationEventPublisher(this.applicationEventPublisher);
@@ -235,7 +244,9 @@ private void setCommonAttributes(AbstractConnectionFactory factory) {
235244
}
236245

237246
private void setServerAttributes(AbstractServerConnectionFactory factory) {
238-
factory.setLocalAddress(this.localAddress);
247+
if (this.localAddress != null) {
248+
factory.setLocalAddress(this.localAddress);
249+
}
239250
factory.setBacklog(this.backlog);
240251
}
241252

@@ -264,12 +275,7 @@ private TcpNioConnectionSupport obtainNioConnectionSupport() {
264275
}
265276

266277
private TcpNetConnectionSupport obtainNetConnectionSupport() {
267-
if (this.netConnectionSupport != null) {
268-
return this.netConnectionSupport;
269-
}
270-
else {
271-
return new DefaultTcpNetConnectionSupport();
272-
}
278+
return this.netConnectionSupport != null ? this.netConnectionSupport : new DefaultTcpNetConnectionSupport();
273279
}
274280

275281
/**
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/**
22
* Provides classes for configuration - parsers, namespace handlers, factory beans.
33
*/
4+
@org.jspecify.annotations.NullMarked
45
package org.springframework.integration.ip.config;

spring-integration-ip/src/main/java/org/springframework/integration/ip/dsl/TcpInboundChannelAdapterSpec.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import java.util.Collections;
2020
import java.util.Map;
2121

22+
import org.jspecify.annotations.Nullable;
23+
2224
import org.springframework.integration.dsl.ComponentsRegistration;
2325
import org.springframework.integration.dsl.MessageProducerSpec;
2426
import org.springframework.integration.ip.tcp.TcpReceivingChannelAdapter;
@@ -38,7 +40,7 @@ public class TcpInboundChannelAdapterSpec
3840
extends MessageProducerSpec<TcpInboundChannelAdapterSpec, TcpReceivingChannelAdapter>
3941
implements ComponentsRegistration {
4042

41-
protected final AbstractConnectionFactory connectionFactory; // NOSONAR - final
43+
protected final @Nullable AbstractConnectionFactory connectionFactory; // NOSONAR - final
4244

4345
/**
4446
* Construct an instance using an existing spring-managed connection factory.
@@ -91,10 +93,10 @@ public TcpInboundChannelAdapterSpec taskScheduler(TaskScheduler taskScheduler) {
9193
}
9294

9395
@Override
94-
public Map<Object, String> getComponentsToRegister() {
96+
public Map<Object, @Nullable String> getComponentsToRegister() {
9597
return this.connectionFactory != null
96-
? Collections.singletonMap(this.connectionFactory, this.connectionFactory.getComponentName())
97-
: Collections.emptyMap();
98+
? Collections.<Object, @Nullable String>singletonMap(this.connectionFactory, this.connectionFactory.getComponentName())
99+
: Collections.<Object, @Nullable String>emptyMap();
98100
}
99101

100102
}

spring-integration-ip/src/main/java/org/springframework/integration/ip/dsl/TcpInboundGatewaySpec.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import java.util.Collections;
2020
import java.util.Map;
2121

22+
import org.jspecify.annotations.Nullable;
23+
2224
import org.springframework.integration.dsl.ComponentsRegistration;
2325
import org.springframework.integration.dsl.MessagingGatewaySpec;
2426
import org.springframework.integration.ip.tcp.TcpInboundGateway;
@@ -37,7 +39,7 @@
3739
public class TcpInboundGatewaySpec extends MessagingGatewaySpec<TcpInboundGatewaySpec, TcpInboundGateway>
3840
implements ComponentsRegistration {
3941

40-
protected final AbstractConnectionFactory connectionFactory; // NOSONAR - final
42+
protected final @Nullable AbstractConnectionFactory connectionFactory; // NOSONAR - final
4143

4244
/**
4345
* Construct an instance using an existing spring-managed connection factory.
@@ -90,10 +92,10 @@ public TcpInboundGatewaySpec taskScheduler(TaskScheduler taskScheduler) {
9092
}
9193

9294
@Override
93-
public Map<Object, String> getComponentsToRegister() {
95+
public Map<Object, @Nullable String> getComponentsToRegister() {
9496
return this.connectionFactory != null
95-
? Collections.singletonMap(this.connectionFactory, this.connectionFactory.getComponentName())
96-
: Collections.emptyMap();
97+
? Collections.<Object, @Nullable String>singletonMap(this.connectionFactory, this.connectionFactory.getComponentName())
98+
: Collections.<Object, @Nullable String>emptyMap();
9799
}
98100

99101
}

spring-integration-ip/src/main/java/org/springframework/integration/ip/dsl/TcpOutboundChannelAdapterSpec.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import java.util.Collections;
2020
import java.util.Map;
2121

22+
import org.jspecify.annotations.Nullable;
23+
2224
import org.springframework.integration.dsl.ComponentsRegistration;
2325
import org.springframework.integration.dsl.MessageHandlerSpec;
2426
import org.springframework.integration.ip.tcp.TcpSendingMessageHandler;
@@ -38,7 +40,7 @@ public class TcpOutboundChannelAdapterSpec
3840
extends MessageHandlerSpec<TcpOutboundChannelAdapterSpec, TcpSendingMessageHandler>
3941
implements ComponentsRegistration {
4042

41-
protected final AbstractConnectionFactory connectionFactory; // NOSONAR - final
43+
protected final @Nullable AbstractConnectionFactory connectionFactory; // NOSONAR - final
4244

4345
/**
4446
* Construct an instance using an existing spring-managed connection factory.
@@ -91,10 +93,10 @@ public TcpOutboundChannelAdapterSpec taskScheduler(TaskScheduler taskScheduler)
9193
}
9294

9395
@Override
94-
public Map<Object, String> getComponentsToRegister() {
96+
public Map<Object, @Nullable String> getComponentsToRegister() {
9597
return this.connectionFactory != null
96-
? Collections.singletonMap(this.connectionFactory, this.connectionFactory.getComponentName())
97-
: Collections.emptyMap();
98+
? Collections.<Object, @Nullable String>singletonMap(this.connectionFactory, this.connectionFactory.getComponentName())
99+
: Collections.<Object, @Nullable String>emptyMap();
98100
}
99101

100102
}

spring-integration-ip/src/main/java/org/springframework/integration/ip/dsl/TcpOutboundGatewaySpec.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import java.util.Map;
2121
import java.util.function.Function;
2222

23+
import org.jspecify.annotations.Nullable;
24+
2325
import org.springframework.integration.dsl.ComponentsRegistration;
2426
import org.springframework.integration.dsl.MessageHandlerSpec;
2527
import org.springframework.integration.expression.FunctionExpression;
@@ -40,7 +42,7 @@
4042
public class TcpOutboundGatewaySpec extends MessageHandlerSpec<TcpOutboundGatewaySpec, TcpOutboundGateway>
4143
implements ComponentsRegistration {
4244

43-
protected final AbstractClientConnectionFactory connectionFactory; // NOSONAR - final
45+
protected final @Nullable AbstractClientConnectionFactory connectionFactory; // NOSONAR - final
4446

4547
/**
4648
* Construct an instance using an existing spring-managed connection factory.
@@ -138,10 +140,10 @@ public TcpOutboundGatewaySpec unsolicitedMessageChannel(MessageChannel channel)
138140
}
139141

140142
@Override
141-
public Map<Object, String> getComponentsToRegister() {
143+
public Map<Object, @Nullable String> getComponentsToRegister() {
142144
return this.connectionFactory != null
143-
? Collections.singletonMap(this.connectionFactory, this.connectionFactory.getComponentName())
144-
: Collections.emptyMap();
145+
? Collections.<Object, @Nullable String>singletonMap(this.connectionFactory, this.connectionFactory.getComponentName())
146+
: Collections.<Object, @Nullable String>emptyMap();
145147
}
146148

147149
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
22
* Provides TCP/UDP Component support for the Java DSL.
33
*/
4-
@org.springframework.lang.NonNullApi
4+
@org.jspecify.annotations.NullMarked
55
package org.springframework.integration.ip.dsl;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/**
22
* ApplicationEvents generated by the ip module.
33
*/
4+
@org.jspecify.annotations.NullMarked
45
package org.springframework.integration.ip.event;

0 commit comments

Comments
 (0)