Fix some Sonar smells

This commit is contained in:
Artem Bilan
2022-11-01 12:19:24 -04:00
parent 39bb09012a
commit ef63d90262
7 changed files with 97 additions and 107 deletions

View File

@@ -346,7 +346,7 @@ public class CassandraMessageHandler extends AbstractReplyProducingMessageHandle
}
private final class ReactiveWriteResult extends WriteResult {
private static final class ReactiveWriteResult extends WriteResult {
ReactiveWriteResult(ReactiveResultSet reactiveResultSet) {
super(reactiveResultSet.getAllExecutionInfo(),

View File

@@ -89,7 +89,7 @@ public class AnnotationConfigParser implements BeanDefinitionParser {
@Override
public Set<MethodMetadata> getDeclaredMethods() {
return null;
throw new UnsupportedOperationException("The class doesn't support this operation");
}
}

View File

@@ -55,7 +55,7 @@ public class MessageHistoryParser implements BeanDefinitionParser {
@Override
public Set<MethodMetadata> getDeclaredMethods() {
return null;
throw new UnsupportedOperationException("The class doesn't support this operation");
}
}, parserContext.getRegistry());

View File

@@ -161,8 +161,6 @@ public class MessagingMethodInvokerHelper extends AbstractExpressionEvaluator im
SPEL_COMPILERS.put(SpelCompilerMode.MIXED, EXPRESSION_PARSER_MIXED);
}
private MessageHandlerMethodFactory messageHandlerMethodFactory;
private final Object targetObject;
private final JsonObjectMapper<?, ?> jsonObjectMapper;
@@ -197,6 +195,8 @@ public class MessagingMethodInvokerHelper extends AbstractExpressionEvaluator im
private boolean useSpelInvoker;
private volatile MessageHandlerMethodFactory messageHandlerMethodFactory;
private volatile boolean initialized;
public MessagingMethodInvokerHelper(Object targetObject, Method method, Class<?> expectedType,
@@ -515,10 +515,26 @@ public class MessagingMethodInvokerHelper extends AbstractExpressionEvaluator im
: IntegrationContextUtils.MESSAGE_HANDLER_FACTORY_BEAN_NAME);
}
private void configureLocalMessageHandlerFactory() {
BeanFactory beanFactory = getBeanFactory();
ConfigurableCompositeMessageConverter messageConverter = new ConfigurableCompositeMessageConverter();
messageConverter.setBeanFactory(beanFactory);
messageConverter.afterPropertiesSet();
IntegrationMessageHandlerMethodFactory localHandlerMethodFactory =
new IntegrationMessageHandlerMethodFactory(this.canProcessMessageList);
localHandlerMethodFactory.setMessageConverter(messageConverter);
localHandlerMethodFactory.setBeanFactory(beanFactory);
localHandlerMethodFactory.afterPropertiesSet();
this.messageHandlerMethodFactory = localHandlerMethodFactory;
}
/*
* This should not be needed in production but we have many tests
* This should not be needed in production, but we have many tests
* that don't run in an application context.
*/
private void initializeHandler(HandlerMethod candidate) {
ExpressionParser parser;
if (candidate.useSpelInvoker == null) {
@@ -537,22 +553,6 @@ public class MessagingMethodInvokerHelper extends AbstractExpressionEvaluator im
candidate.initialized = true;
}
private void configureLocalMessageHandlerFactory() {
BeanFactory beanFactory = getBeanFactory();
ConfigurableCompositeMessageConverter messageConverter = new ConfigurableCompositeMessageConverter();
messageConverter.setBeanFactory(beanFactory);
messageConverter.afterPropertiesSet();
IntegrationMessageHandlerMethodFactory localHandlerMethodFactory =
new IntegrationMessageHandlerMethodFactory(this.canProcessMessageList);
localHandlerMethodFactory.setMessageConverter(messageConverter);
localHandlerMethodFactory.setBeanFactory(beanFactory);
localHandlerMethodFactory.afterPropertiesSet();
this.messageHandlerMethodFactory = localHandlerMethodFactory;
}
@Nullable
private Object invokeHandlerMethod(HandlerMethod handlerMethod, ParametersWrapper parameters) {
try {

View File

@@ -52,7 +52,7 @@ public class IntegrationGraphControllerParser implements BeanDefinitionParser {
@Override
public Set<MethodMetadata> getDeclaredMethods() {
return null;
throw new UnsupportedOperationException("The class doesn't support this operation");
}
}, parserContext.getRegistry());

View File

@@ -108,7 +108,7 @@ public final class PostgresChannelMessageTableSubscriber implements SmartLifecyc
* while running.
* @param executor The executor to use or {@code null} if an executor should be created by this class.
*/
public void setExecutor(@Nullable ExecutorService executor) {
public synchronized void setExecutor(@Nullable ExecutorService executor) {
this.executor = executor;
}

View File

@@ -110,46 +110,42 @@ public class ZookeeperMetadataStore implements ListenableMetadataStore, SmartLif
}
@Override
public String putIfAbsent(String key, String value) {
public synchronized String putIfAbsent(String key, String value) {
Assert.notNull(key, KEY_MUST_NOT_BE_NULL);
Assert.notNull(value, "'value' must not be null.");
synchronized (this.updateMap) {
try {
createNode(key, value);
return null;
}
catch (KeeperException.NodeExistsException ex) {
// so the data actually exists, we can read it
return get(key);
}
catch (Exception ex) {
throw new ZookeeperMetadataStoreException("Error while trying to set '" + key + "':", ex);
}
try {
createNode(key, value);
return null;
}
catch (KeeperException.NodeExistsException ex) {
// so the data actually exists, we can read it
return get(key);
}
catch (Exception ex) {
throw new ZookeeperMetadataStoreException("Error while trying to set '" + key + "':", ex);
}
}
@Override
public boolean replace(String key, String oldValue, String newValue) {
public synchronized boolean replace(String key, String oldValue, String newValue) {
Assert.notNull(key, KEY_MUST_NOT_BE_NULL);
Assert.notNull(oldValue, "'oldValue' must not be null.");
Assert.notNull(newValue, "'newValue' must not be null.");
synchronized (this.updateMap) {
Stat currentStat = new Stat();
try {
byte[] bytes = this.client.getData().storingStatIn(currentStat).forPath(getPath(key));
if (oldValue.equals(IntegrationUtils.bytesToString(bytes, this.encoding))) {
updateNode(key, newValue, currentStat.getVersion());
}
return true;
}
catch (KeeperException.NoNodeException | KeeperException.BadVersionException ex) {
// ignore, the node doesn't exist there's nothing to replace
return false;
}
// ignore
catch (Exception ex) {
throw new ZookeeperMetadataStoreException("Cannot replace value", ex);
Stat currentStat = new Stat();
try {
byte[] bytes = this.client.getData().storingStatIn(currentStat).forPath(getPath(key));
if (oldValue.equals(IntegrationUtils.bytesToString(bytes, this.encoding))) {
updateNode(key, newValue, currentStat.getVersion());
}
return true;
}
catch (KeeperException.NoNodeException | KeeperException.BadVersionException ex) {
// ignore, the node doesn't exist there's nothing to replace
return false;
}
// ignore
catch (Exception ex) {
throw new ZookeeperMetadataStoreException("Cannot replace value", ex);
}
}
@@ -165,77 +161,71 @@ public class ZookeeperMetadataStore implements ListenableMetadataStore, SmartLif
}
@Override
public void put(String key, String value) {
public synchronized void put(String key, String value) {
Assert.notNull(key, KEY_MUST_NOT_BE_NULL);
Assert.notNull(value, "'value' must not be null.");
synchronized (this.updateMap) {
try {
Stat currentNode = this.client.checkExists().forPath(getPath(key));
if (currentNode == null) {
try {
createNode(key, value);
}
catch (KeeperException.NodeExistsException e) {
updateNode(key, value, -1);
}
try {
Stat currentNode = this.client.checkExists().forPath(getPath(key));
if (currentNode == null) {
try {
createNode(key, value);
}
else {
catch (KeeperException.NodeExistsException e) {
updateNode(key, value, -1);
}
}
catch (Exception ex) {
throw new ZookeeperMetadataStoreException("Error while setting value for key '" + key + "':", ex);
else {
updateNode(key, value, -1);
}
}
catch (Exception ex) {
throw new ZookeeperMetadataStoreException("Error while setting value for key '" + key + "':", ex);
}
}
@Override
public String get(String key) {
public synchronized String get(String key) {
Assert.notNull(key, KEY_MUST_NOT_BE_NULL);
Assert.state(isRunning(), "ZookeeperMetadataStore has to be started before using.");
synchronized (this.updateMap) {
return this.cache.get(getPath(key))
.map(currentData -> {
// our version is more recent than the cache
if (this.updateMap.containsKey(key) &&
this.updateMap.get(key).version() >= currentData.getStat().getVersion()) {
return this.cache.get(getPath(key))
.map(currentData -> {
// our version is more recent than the cache
if (this.updateMap.containsKey(key) &&
this.updateMap.get(key).version() >= currentData.getStat().getVersion()) {
return this.updateMap.get(key).value();
}
return IntegrationUtils.bytesToString(currentData.getData(), this.encoding);
})
.orElseGet(() -> {
if (this.updateMap.containsKey(key)) {
// we have saved the value, but the cache hasn't updated yet
// if the value had changed via replication, we would have been notified by the listener
return this.updateMap.get(key).value();
}
else {
// the value just doesn't exist
return null;
}
});
}
return this.updateMap.get(key).value();
}
return IntegrationUtils.bytesToString(currentData.getData(), this.encoding);
})
.orElseGet(() -> {
if (this.updateMap.containsKey(key)) {
// we have saved the value, but the cache hasn't updated yet
// if the value had changed via replication, we would have been notified by the listener
return this.updateMap.get(key).value();
}
else {
// the value just doesn't exist
return null;
}
});
}
@Override
public String remove(String key) {
public synchronized String remove(String key) {
Assert.notNull(key, KEY_MUST_NOT_BE_NULL);
synchronized (this.updateMap) {
try {
byte[] bytes = this.client.getData().forPath(getPath(key));
this.client.delete().forPath(getPath(key));
// we guarantee that the deletion will supersede the existing data
this.updateMap.put(key, new LocalChildData(null, Integer.MAX_VALUE));
return IntegrationUtils.bytesToString(bytes, this.encoding);
}
catch (KeeperException.NoNodeException ex) {
// ignore - the node doesn't exist
return null;
}
catch (Exception ex) {
throw new ZookeeperMetadataStoreException("Exception while deleting key '" + key + "'", ex);
}
try {
byte[] bytes = this.client.getData().forPath(getPath(key));
this.client.delete().forPath(getPath(key));
// we guarantee that the deletion will supersede the existing data
this.updateMap.put(key, new LocalChildData(null, Integer.MAX_VALUE));
return IntegrationUtils.bytesToString(bytes, this.encoding);
}
catch (KeeperException.NoNodeException ex) {
// ignore - the node doesn't exist
return null;
}
catch (Exception ex) {
throw new ZookeeperMetadataStoreException("Exception while deleting key '" + key + "'", ex);
}
}