Skip to content

Commit d2a4987

Browse files
committed
Fix new Sonar smells
1 parent 737b939 commit d2a4987

File tree

2 files changed

+32
-25
lines changed

2 files changed

+32
-25
lines changed

spring-integration-rsocket/src/main/java/org/springframework/integration/rsocket/ServerRSocketConnector.java

+7-5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.integration.rsocket;
1818

19+
import java.lang.reflect.Method;
1920
import java.util.Collections;
2021
import java.util.HashMap;
2122
import java.util.Map;
@@ -38,7 +39,6 @@
3839
import org.springframework.util.RouteMatcher;
3940

4041
import io.rsocket.RSocketFactory;
41-
import io.rsocket.SocketAcceptor;
4242
import io.rsocket.transport.ServerTransport;
4343
import io.rsocket.transport.netty.server.CloseableChannel;
4444
import io.rsocket.transport.netty.server.TcpServerTransport;
@@ -50,7 +50,7 @@
5050
/**
5151
* A server {@link AbstractRSocketConnector} extension to accept and manage client RSocket connections.
5252
* <p>
53-
* Note: the {@link RSocketFactory.ServerRSocketFactory#acceptor(SocketAcceptor)}
53+
* Note: the {@link RSocketFactory.ServerRSocketFactory#acceptor(io.rsocket.SocketAcceptor)}
5454
* in the provided {@link #factoryConfigurer} is overridden with an internal
5555
* {@link ServerRSocketMessageHandler#serverAcceptor()}
5656
* for the proper Spring Integration channel adapter mappings.
@@ -175,16 +175,18 @@ public void afterSingletonsInstantiated() {
175175

176176
private static class ServerRSocketMessageHandler extends IntegrationRSocketMessageHandler {
177177

178+
private static final Method HANDLE_CONNECTION_SETUP_METHOD =
179+
ReflectionUtils.findMethod(ServerRSocketMessageHandler.class, "handleConnectionSetup", Message.class);
180+
181+
178182
private final Map<Object, RSocketRequester> clientRSocketRequesters = new HashMap<>();
179183

180184
private BiFunction<String, DataBuffer, Object> clientRSocketKeyStrategy = (destination, data) -> destination;
181185

182186
private ApplicationEventPublisher applicationEventPublisher;
183187

184188
private void registerHandleConnectionSetupMethod() {
185-
registerHandlerMethod(this,
186-
ReflectionUtils.findMethod(ServerRSocketMessageHandler.class, "handleConnectionSetup", // NOSONAR
187-
Message.class),
189+
registerHandlerMethod(this, HANDLE_CONNECTION_SETUP_METHOD,
188190
new CompositeMessageCondition(
189191
RSocketFrameTypeMessageCondition.CONNECT_CONDITION,
190192
new DestinationPatternsMessageCondition(new String[] { "*" }, getRouteMatcher())));

spring-integration-rsocket/src/main/java/org/springframework/integration/rsocket/inbound/ChannelSendOperator.java

+25-20
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ private class WriteBarrier implements CoreSubscriber<T>, Subscription, Publisher
157157
private long demandBeforeReadyToWrite;
158158

159159
/** Current state. */
160-
private State state = State.NEW;
160+
private volatile State state = State.NEW;
161161

162162
/** The actual writeSubscriber from the HTTP server adapter. */
163163
@Nullable
@@ -198,7 +198,7 @@ else if (this.state == State.NEW) {
198198
try {
199199
result = ChannelSendOperator.this.writeFunction.apply(this);
200200
}
201-
catch (Throwable ex) {
201+
catch (Throwable ex) { // NOSONAR
202202
this.writeCompletionBarrier.onError(ex);
203203
return;
204204
}
@@ -214,8 +214,9 @@ else if (this.state == State.NEW) {
214214
}
215215

216216
private Subscriber<? super T> requiredWriteSubscriber() {
217-
Assert.state(this.writeSubscriber != null, "No write subscriber");
218-
return this.writeSubscriber;
217+
Subscriber<? super T> writeSubscriberToReturn = this.writeSubscriber;
218+
Assert.state(writeSubscriberToReturn != null, "No write subscriber");
219+
return writeSubscriberToReturn;
219220
}
220221

221222
@Override
@@ -255,7 +256,7 @@ else if (this.state == State.NEW) {
255256
try {
256257
result = ChannelSendOperator.this.writeFunction.apply(this);
257258
}
258-
catch (Throwable ex) {
259+
catch (Throwable ex) { // NOSONAR
259260
this.writeCompletionBarrier.onError(ex);
260261
return;
261262
}
@@ -277,27 +278,28 @@ public Context currentContext() {
277278

278279
@Override
279280
public void request(long n) {
281+
long requests = n;
280282
Subscription s = this.subscription;
281283
if (s == null) {
282284
return;
283285
}
284286
if (this.state == State.READY_TO_WRITE) {
285-
s.request(n);
287+
s.request(requests);
286288
return;
287289
}
288290
synchronized (this) {
289291
if (this.writeSubscriber != null) {
290292
if (this.state == State.EMITTING_CACHED_SIGNALS) {
291-
this.demandBeforeReadyToWrite = n;
293+
this.demandBeforeReadyToWrite = requests;
292294
return;
293295
}
294296
try {
295297
this.state = State.EMITTING_CACHED_SIGNALS;
296298
if (emitCachedSignals()) {
297299
return;
298300
}
299-
n = n + this.demandBeforeReadyToWrite - 1;
300-
if (n == 0) {
301+
requests = requests + this.demandBeforeReadyToWrite - 1;
302+
if (requests == 0) {
301303
return;
302304
}
303305
}
@@ -306,7 +308,7 @@ public void request(long n) {
306308
}
307309
}
308310
}
309-
s.request(n);
311+
s.request(requests);
310312
}
311313

312314
private boolean emitCachedSignals() {
@@ -319,10 +321,13 @@ private boolean emitCachedSignals() {
319321
}
320322
return true;
321323
}
322-
T item = this.item;
323-
this.item = null;
324-
if (item != null) {
325-
requiredWriteSubscriber().onNext(item);
324+
T itemToUse;
325+
synchronized (this) {
326+
itemToUse = this.item;
327+
this.item = null;
328+
}
329+
if (itemToUse != null) {
330+
requiredWriteSubscriber().onNext(itemToUse);
326331
}
327332
if (this.completed) {
328333
requiredWriteSubscriber().onComplete();
@@ -347,9 +352,9 @@ public void cancel() {
347352

348353
private void releaseCachedItem() {
349354
synchronized (this) {
350-
Object item = this.item;
351-
if (item instanceof DataBuffer) {
352-
DataBufferUtils.release((DataBuffer) item);
355+
Object itemToRelease = this.item;
356+
if (itemToRelease instanceof DataBuffer) {
357+
DataBufferUtils.release((DataBuffer) itemToRelease);
353358
}
354359
this.item = null;
355360
}
@@ -451,9 +456,9 @@ public void request(long n) {
451456
@Override
452457
public void cancel() {
453458
this.writeBarrier.cancel();
454-
Subscription subscription = this.subscription;
455-
if (subscription != null) {
456-
subscription.cancel();
459+
Subscription subscriptionToCancel = this.subscription;
460+
if (subscriptionToCancel != null) {
461+
subscriptionToCancel.cancel();
457462
}
458463
}
459464

0 commit comments

Comments
 (0)