Skip to content

Commit 7569d0a

Browse files
garyrussellartembilan
authored andcommitted
Sonar complexity issues
1 parent 8de53cc commit 7569d0a

10 files changed

+371
-255
lines changed

spring-integration-amqp/src/main/java/org/springframework/integration/amqp/support/DefaultAmqpHeaderMapper.java

+10-6
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,16 @@ else if (allHeaders != null) {
245245
.acceptIfNotNull(getHeaderIfAvailable(headers, AmqpHeaders.USER_ID, String.class),
246246
amqpMessageProperties::setUserId);
247247

248+
mapJsonHeaders(headers, amqpMessageProperties);
249+
250+
JavaUtils.INSTANCE
251+
.acceptIfHasText(getHeaderIfAvailable(headers, AmqpHeaders.SPRING_REPLY_CORRELATION, String.class),
252+
replyCorrelation -> amqpMessageProperties.setHeader("spring_reply_correlation", replyCorrelation))
253+
.acceptIfHasText(getHeaderIfAvailable(headers, AmqpHeaders.SPRING_REPLY_TO_STACK, String.class),
254+
replyToStack -> amqpMessageProperties.setHeader("spring_reply_to", replyToStack));
255+
}
256+
257+
private void mapJsonHeaders(Map<String, Object> headers, MessageProperties amqpMessageProperties) {
248258
Map<String, String> jsonHeaders = new HashMap<String, String>();
249259

250260
for (String jsonHeader : JsonHeaders.HEADERS) {
@@ -265,12 +275,6 @@ else if (allHeaders != null) {
265275
if (!amqpMessageProperties.getHeaders().containsKey(JsonHeaders.TYPE_ID.replaceFirst(JsonHeaders.PREFIX, ""))) {
266276
amqpMessageProperties.getHeaders().putAll(jsonHeaders);
267277
}
268-
269-
JavaUtils.INSTANCE
270-
.acceptIfHasText(getHeaderIfAvailable(headers, AmqpHeaders.SPRING_REPLY_CORRELATION, String.class),
271-
replyCorrelation -> amqpMessageProperties.setHeader("spring_reply_correlation", replyCorrelation))
272-
.acceptIfHasText(getHeaderIfAvailable(headers, AmqpHeaders.SPRING_REPLY_TO_STACK, String.class),
273-
replyToStack -> amqpMessageProperties.setHeader("spring_reply_to", replyToStack));
274278
}
275279

276280
@Override

spring-integration-core/src/main/java/org/springframework/integration/config/AbstractEvaluationContextFactoryBean.java

+46-33
Original file line numberDiff line numberDiff line change
@@ -92,52 +92,65 @@ public Map<String, Method> getFunctions() {
9292

9393
protected void initialize(String beanName) {
9494
if (this.applicationContext != null) {
95-
ConversionService conversionService = IntegrationUtils.getConversionService(getApplicationContext());
96-
if (conversionService != null) {
97-
this.typeConverter = new StandardTypeConverter(conversionService);
98-
}
95+
conversionService();
96+
functions();
97+
propertyAccessors();
98+
processParentIfPresent(beanName);
99+
}
100+
this.initialized = true;
101+
}
99102

100-
Map<String, SpelFunctionFactoryBean> functionFactoryBeanMap = BeanFactoryUtils
101-
.beansOfTypeIncludingAncestors(this.applicationContext, SpelFunctionFactoryBean.class);
102-
for (SpelFunctionFactoryBean spelFunctionFactoryBean : functionFactoryBeanMap.values()) {
103-
if (!getFunctions().containsKey(spelFunctionFactoryBean.getFunctionName())) {
104-
getFunctions().put(spelFunctionFactoryBean.getFunctionName(), spelFunctionFactoryBean.getObject());
105-
}
103+
private void conversionService() {
104+
ConversionService conversionService = IntegrationUtils.getConversionService(getApplicationContext());
105+
if (conversionService != null) {
106+
this.typeConverter = new StandardTypeConverter(conversionService);
107+
}
108+
}
109+
110+
private void functions() {
111+
Map<String, SpelFunctionFactoryBean> functionFactoryBeanMap = BeanFactoryUtils
112+
.beansOfTypeIncludingAncestors(this.applicationContext, SpelFunctionFactoryBean.class);
113+
for (SpelFunctionFactoryBean spelFunctionFactoryBean : functionFactoryBeanMap.values()) {
114+
if (!getFunctions().containsKey(spelFunctionFactoryBean.getFunctionName())) {
115+
getFunctions().put(spelFunctionFactoryBean.getFunctionName(), spelFunctionFactoryBean.getObject());
106116
}
117+
}
118+
}
107119

108-
try {
109-
SpelPropertyAccessorRegistrar propertyAccessorRegistrar =
110-
this.applicationContext.getBean(SpelPropertyAccessorRegistrar.class);
111-
for (Entry<String, PropertyAccessor> entry : propertyAccessorRegistrar.getPropertyAccessors()
112-
.entrySet()) {
113-
if (!getPropertyAccessors().containsKey(entry.getKey())) {
114-
getPropertyAccessors().put(entry.getKey(), entry.getValue());
115-
}
120+
private void propertyAccessors() {
121+
try {
122+
SpelPropertyAccessorRegistrar propertyAccessorRegistrar =
123+
this.applicationContext.getBean(SpelPropertyAccessorRegistrar.class);
124+
for (Entry<String, PropertyAccessor> entry : propertyAccessorRegistrar.getPropertyAccessors()
125+
.entrySet()) {
126+
if (!getPropertyAccessors().containsKey(entry.getKey())) {
127+
getPropertyAccessors().put(entry.getKey(), entry.getValue());
116128
}
117129
}
118-
catch (@SuppressWarnings("unused") NoSuchBeanDefinitionException e) {
119-
// There is no 'SpelPropertyAccessorRegistrar' bean in the application context.
120-
}
130+
}
131+
catch (@SuppressWarnings("unused") NoSuchBeanDefinitionException e) {
132+
// There is no 'SpelPropertyAccessorRegistrar' bean in the application context.
133+
}
134+
}
121135

122-
ApplicationContext parent = this.applicationContext.getParent();
136+
private void processParentIfPresent(String beanName) {
137+
ApplicationContext parent = this.applicationContext.getParent();
123138

124-
if (parent != null && parent.containsBean(beanName)) {
125-
AbstractEvaluationContextFactoryBean parentFactoryBean = parent.getBean("&" + beanName, getClass());
139+
if (parent != null && parent.containsBean(beanName)) {
140+
AbstractEvaluationContextFactoryBean parentFactoryBean = parent.getBean("&" + beanName, getClass());
126141

127-
for (Entry<String, PropertyAccessor> entry : parentFactoryBean.getPropertyAccessors().entrySet()) {
128-
if (!getPropertyAccessors().containsKey(entry.getKey())) {
129-
getPropertyAccessors().put(entry.getKey(), entry.getValue());
130-
}
142+
for (Entry<String, PropertyAccessor> entry : parentFactoryBean.getPropertyAccessors().entrySet()) {
143+
if (!getPropertyAccessors().containsKey(entry.getKey())) {
144+
getPropertyAccessors().put(entry.getKey(), entry.getValue());
131145
}
146+
}
132147

133-
for (Entry<String, Method> entry : parentFactoryBean.getFunctions().entrySet()) {
134-
if (!getFunctions().containsKey(entry.getKey())) {
135-
getFunctions().put(entry.getKey(), entry.getValue());
136-
}
148+
for (Entry<String, Method> entry : parentFactoryBean.getFunctions().entrySet()) {
149+
if (!getFunctions().containsKey(entry.getKey())) {
150+
getFunctions().put(entry.getKey(), entry.getValue());
137151
}
138152
}
139153
}
140-
this.initialized = true;
141154
}
142155

143156
}

spring-integration-core/src/main/java/org/springframework/integration/config/AbstractSimpleMessageHandlerFactoryBean.java

+34-22
Original file line numberDiff line numberDiff line change
@@ -215,27 +215,9 @@ protected final H createHandlerInternal() {
215215
if (actualHandler == null) {
216216
actualHandler = this.handler;
217217
}
218-
final Object handlerToConfigure = actualHandler; // must final for lambdas
219-
if (actualHandler instanceof IntegrationObjectSupport) {
220-
JavaUtils.INSTANCE
221-
.acceptIfNotNull(this.componentName,
222-
name -> ((IntegrationObjectSupport) handlerToConfigure).setComponentName(name))
223-
.acceptIfNotNull(this.channelResolver,
224-
resolver -> ((IntegrationObjectSupport) handlerToConfigure).setChannelResolver(resolver));
225-
}
226-
if (!CollectionUtils.isEmpty(this.adviceChain)) {
227-
if (actualHandler instanceof AbstractReplyProducingMessageHandler) {
228-
((AbstractReplyProducingMessageHandler) actualHandler).setAdviceChain(this.adviceChain);
229-
}
230-
else if (this.logger.isDebugEnabled()) {
231-
String name = this.componentName;
232-
if (name == null && actualHandler instanceof NamedComponent) {
233-
name = ((NamedComponent) actualHandler).getBeanName();
234-
}
235-
this.logger.debug("adviceChain can only be set on an AbstractReplyProducingMessageHandler"
236-
+ (name == null ? "" : (", " + name)) + ".");
237-
}
238-
}
218+
final Object handlerToConfigure = actualHandler; // must be final for lambdas
219+
integrationObjectSupport(actualHandler, handlerToConfigure);
220+
adviceChain(actualHandler);
239221
JavaUtils.INSTANCE
240222
.acceptIfCondition(this.async != null && actualHandler instanceof AbstractMessageProducingHandler,
241223
this.async,
@@ -244,6 +226,37 @@ else if (this.logger.isDebugEnabled()) {
244226
this.order, theOrder -> ((Orderable) this.handler).setOrder(theOrder));
245227
this.initialized = true;
246228
}
229+
initializingBean();
230+
return this.handler;
231+
}
232+
233+
private void integrationObjectSupport(Object actualHandler, final Object handlerToConfigure) {
234+
if (actualHandler instanceof IntegrationObjectSupport) {
235+
JavaUtils.INSTANCE
236+
.acceptIfNotNull(this.componentName,
237+
name -> ((IntegrationObjectSupport) handlerToConfigure).setComponentName(name))
238+
.acceptIfNotNull(this.channelResolver,
239+
resolver -> ((IntegrationObjectSupport) handlerToConfigure).setChannelResolver(resolver));
240+
}
241+
}
242+
243+
private void adviceChain(Object actualHandler) {
244+
if (!CollectionUtils.isEmpty(this.adviceChain)) {
245+
if (actualHandler instanceof AbstractReplyProducingMessageHandler) {
246+
((AbstractReplyProducingMessageHandler) actualHandler).setAdviceChain(this.adviceChain);
247+
}
248+
else if (this.logger.isDebugEnabled()) {
249+
String name = this.componentName;
250+
if (name == null && actualHandler instanceof NamedComponent) {
251+
name = ((NamedComponent) actualHandler).getBeanName();
252+
}
253+
this.logger.debug("adviceChain can only be set on an AbstractReplyProducingMessageHandler"
254+
+ (name == null ? "" : (", " + name)) + ".");
255+
}
256+
}
257+
}
258+
259+
private void initializingBean() {
247260
if (this.handler instanceof InitializingBean) {
248261
try {
249262
((InitializingBean) this.handler).afterPropertiesSet();
@@ -252,7 +265,6 @@ else if (this.logger.isDebugEnabled()) {
252265
throw new BeanInitializationException("failed to initialize MessageHandler", e);
253266
}
254267
}
255-
return this.handler;
256268
}
257269

258270
private void configureOutputChannelIfAny() {

spring-integration-core/src/main/java/org/springframework/integration/config/ConsumerEndpointFactoryBean.java

+61-45
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,14 @@ public void afterPropertiesSet() {
210210
}
211211
}
212212

213+
adviceChain();
214+
if (this.channelResolver == null) {
215+
this.channelResolver = ChannelResolverUtils.getChannelResolver(this.beanFactory);
216+
}
217+
initializeEndpoint();
218+
}
219+
220+
private void adviceChain() {
213221
if (!CollectionUtils.isEmpty(this.adviceChain)) {
214222
/*
215223
* ARPMHs advise the handleRequestMessage method internally and already have the advice chain injected.
@@ -236,10 +244,6 @@ public void afterPropertiesSet() {
236244
}
237245
}
238246
}
239-
if (this.channelResolver == null) {
240-
this.channelResolver = ChannelResolverUtils.getChannelResolver(this.beanFactory);
241-
}
242-
initializeEndpoint();
243247
}
244248

245249
@Override
@@ -277,55 +281,17 @@ private void initializeEndpoint() {
277281
}
278282
Assert.state(channel != null, "one of inputChannelName or inputChannel is required");
279283
if (channel instanceof SubscribableChannel) {
280-
Assert.isNull(this.pollerMetadata, "A poller should not be specified for endpoint '" + this.beanName
281-
+ "', since '" + channel + "' is a SubscribableChannel (not pollable).");
282-
this.endpoint = new EventDrivenConsumer((SubscribableChannel) channel, this.handler);
283-
if (logger.isWarnEnabled()
284-
&& Boolean.FALSE.equals(this.autoStartup)
285-
&& channel instanceof FixedSubscriberChannel) {
286-
logger.warn("'autoStartup=\"false\"' has no effect when using a FixedSubscriberChannel");
287-
}
284+
eventDrivenConsumer(channel);
288285
}
289286
else if (channel instanceof PollableChannel) {
290-
PollingConsumer pollingConsumer = new PollingConsumer((PollableChannel) channel, this.handler);
291-
if (this.pollerMetadata == null) {
292-
this.pollerMetadata = PollerMetadata.getDefaultPollerMetadata(this.beanFactory);
293-
Assert.notNull(this.pollerMetadata, "No poller has been defined for endpoint '" + this.beanName
294-
+ "', and no default poller is available within the context.");
295-
}
296-
pollingConsumer.setTaskExecutor(this.pollerMetadata.getTaskExecutor());
297-
pollingConsumer.setTrigger(this.pollerMetadata.getTrigger());
298-
pollingConsumer.setAdviceChain(this.pollerMetadata.getAdviceChain());
299-
pollingConsumer.setMaxMessagesPerPoll(this.pollerMetadata.getMaxMessagesPerPoll());
300-
301-
pollingConsumer.setErrorHandler(this.pollerMetadata.getErrorHandler());
302-
303-
pollingConsumer.setReceiveTimeout(this.pollerMetadata.getReceiveTimeout());
304-
pollingConsumer.setTransactionSynchronizationFactory(
305-
this.pollerMetadata.getTransactionSynchronizationFactory());
306-
pollingConsumer.setBeanClassLoader(this.beanClassLoader);
307-
pollingConsumer.setBeanFactory(this.beanFactory);
308-
this.endpoint = pollingConsumer;
287+
pollingConsumer(channel);
309288
}
310289
else {
311290
this.endpoint = new ReactiveStreamsConsumer(channel, this.handler);
312291
}
313292
this.endpoint.setBeanName(this.beanName);
314293
this.endpoint.setBeanFactory(this.beanFactory);
315-
if (this.autoStartup != null) {
316-
this.endpoint.setAutoStartup(this.autoStartup);
317-
}
318-
int phaseToSet = this.phase;
319-
if (!this.isPhaseSet) {
320-
if (this.endpoint instanceof PollingConsumer) {
321-
phaseToSet = Integer.MAX_VALUE / 2;
322-
}
323-
else {
324-
phaseToSet = Integer.MIN_VALUE;
325-
}
326-
}
327-
328-
this.endpoint.setPhase(phaseToSet);
294+
smartLifecycle();
329295
this.endpoint.setRole(this.role);
330296
if (this.taskScheduler != null) {
331297
this.endpoint.setTaskScheduler(this.taskScheduler);
@@ -335,6 +301,56 @@ else if (channel instanceof PollableChannel) {
335301
}
336302
}
337303

304+
private void eventDrivenConsumer(MessageChannel channel) {
305+
Assert.isNull(this.pollerMetadata, "A poller should not be specified for endpoint '" + this.beanName
306+
+ "', since '" + channel + "' is a SubscribableChannel (not pollable).");
307+
this.endpoint = new EventDrivenConsumer((SubscribableChannel) channel, this.handler);
308+
if (logger.isWarnEnabled()
309+
&& Boolean.FALSE.equals(this.autoStartup)
310+
&& channel instanceof FixedSubscriberChannel) {
311+
logger.warn("'autoStartup=\"false\"' has no effect when using a FixedSubscriberChannel");
312+
}
313+
}
314+
315+
private void pollingConsumer(MessageChannel channel) {
316+
PollingConsumer pollingConsumer = new PollingConsumer((PollableChannel) channel, this.handler);
317+
if (this.pollerMetadata == null) {
318+
this.pollerMetadata = PollerMetadata.getDefaultPollerMetadata(this.beanFactory);
319+
Assert.notNull(this.pollerMetadata, "No poller has been defined for endpoint '" + this.beanName
320+
+ "', and no default poller is available within the context.");
321+
}
322+
pollingConsumer.setTaskExecutor(this.pollerMetadata.getTaskExecutor());
323+
pollingConsumer.setTrigger(this.pollerMetadata.getTrigger());
324+
pollingConsumer.setAdviceChain(this.pollerMetadata.getAdviceChain());
325+
pollingConsumer.setMaxMessagesPerPoll(this.pollerMetadata.getMaxMessagesPerPoll());
326+
327+
pollingConsumer.setErrorHandler(this.pollerMetadata.getErrorHandler());
328+
329+
pollingConsumer.setReceiveTimeout(this.pollerMetadata.getReceiveTimeout());
330+
pollingConsumer.setTransactionSynchronizationFactory(
331+
this.pollerMetadata.getTransactionSynchronizationFactory());
332+
pollingConsumer.setBeanClassLoader(this.beanClassLoader);
333+
pollingConsumer.setBeanFactory(this.beanFactory);
334+
this.endpoint = pollingConsumer;
335+
}
336+
337+
private void smartLifecycle() {
338+
if (this.autoStartup != null) {
339+
this.endpoint.setAutoStartup(this.autoStartup);
340+
}
341+
int phaseToSet = this.phase;
342+
if (!this.isPhaseSet) {
343+
if (this.endpoint instanceof PollingConsumer) {
344+
phaseToSet = Integer.MAX_VALUE / 2;
345+
}
346+
else {
347+
phaseToSet = Integer.MIN_VALUE;
348+
}
349+
}
350+
351+
this.endpoint.setPhase(phaseToSet);
352+
}
353+
338354

339355
/*
340356
* SmartLifecycle implementation (delegates to the created endpoint)

0 commit comments

Comments
 (0)