Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -39,13 +39,13 @@
import org.springframework.beans.factory.config.AbstractFactoryBean;
import org.springframework.context.Lifecycle;
import org.springframework.context.SmartLifecycle;
import org.springframework.integration.JavaUtils;
import org.springframework.integration.amqp.channel.AbstractAmqpChannel;
import org.springframework.integration.amqp.channel.PointToPointSubscribableAmqpChannel;
import org.springframework.integration.amqp.channel.PollableAmqpChannel;
import org.springframework.integration.amqp.channel.PublishSubscribeAmqpChannel;
import org.springframework.integration.amqp.support.AmqpHeaderMapper;
import org.springframework.integration.amqp.support.DefaultAmqpHeaderMapper;
import org.springframework.integration.util.JavaUtils;
import org.springframework.lang.Nullable;
import org.springframework.messaging.support.ChannelInterceptor;
import org.springframework.transaction.PlatformTransactionManager;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/*
* Copyright 2019-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.integration;

import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

/**
* Chained utility methods to simplify some Java repetitive code. Obtain a reference to
* the singleton {@link #INSTANCE} and then chain calls to the utility methods.
*
* @author Gary Russell
* @author Artem Bilan
*
* @since 5.1.3
*/
public final class JavaUtils {

/**
* The singleton instance of this utility class.
*/
public static final JavaUtils INSTANCE = new JavaUtils();

private JavaUtils() {
}

/**
* Invoke {@link Consumer#accept(Object)} with the value if the condition is true.
* @param condition the condition.
* @param value the value.
* @param consumer the consumer.
* @param <T> the value type.
* @return this.
*/
public <T> JavaUtils acceptIfCondition(boolean condition, T value, Consumer<T> consumer) {
if (condition) {
consumer.accept(value);
}
return this;
}

/**
* Invoke {@link Consumer#accept(Object)} with the value if it is not null.
* @param value the value.
* @param consumer the consumer.
* @param <T> the value type.
* @return this.
*/
public <T> JavaUtils acceptIfNotNull(T value, Consumer<T> consumer) {
if (value != null) {
consumer.accept(value);
}
return this;
}

/**
* Invoke {@link Consumer#accept(Object)} with the value if it is not null or empty.
* @param value the value.
* @param consumer the consumer.
* @return this.
* @since 5.2
*/
public JavaUtils acceptIfHasText(String value, Consumer<String> consumer) {
if (StringUtils.hasText(value)) {
consumer.accept(value);
}
return this;
}

/**
* Invoke {@link Consumer#accept(Object)} with the value if it is not null or empty.
* @param value the value.
* @param consumer the consumer.
* @param <T> the value type.
* @return this.
* @since 5.2
*/
public <T> JavaUtils acceptIfNotEmpty(List<T> value, Consumer<List<T>> consumer) {
if (!CollectionUtils.isEmpty(value)) {
consumer.accept(value);
}
return this;
}

/**
* Invoke {@link Consumer#accept(Object)} with the value if it is not null or empty.
* @param value the value.
* @param consumer the consumer.
* @param <T> the value type.
* @return this.
* @since 5.2
*/
public <T> JavaUtils acceptIfNotEmpty(T[] value, Consumer<T[]> consumer) {
if (!ObjectUtils.isEmpty(value)) {
consumer.accept(value);
}
return this;
}

/**
* Invoke {@link BiConsumer#accept(Object, Object)} with the arguments if the
* condition is true.
* @param condition the condition.
* @param t1 the first consumer argument
* @param t2 the second consumer argument
* @param consumer the consumer.
* @param <T1> the first argument type.
* @param <T2> the second argument type.
* @return this.
* @since 5.2
*/
public <T1, T2> JavaUtils acceptIfCondition(boolean condition, T1 t1, T2 t2, BiConsumer<T1, T2> consumer) {
if (condition) {
consumer.accept(t1, t2);
}
return this;
}

/**
* Invoke {@link BiConsumer#accept(Object, Object)} with the arguments if the t2
* argument is not null.
* @param t1 the first argument
* @param t2 the second consumer argument
* @param consumer the consumer.
* @param <T1> the first argument type.
* @param <T2> the second argument type.
* @return this.
* @since 5.2
*/
public <T1, T2> JavaUtils acceptIfNotNull(T1 t1, T2 t2, BiConsumer<T1, T2> consumer) {
if (t2 != null) {
consumer.accept(t1, t2);
}
return this;
}

/**
* Invoke {@link BiConsumer#accept(Object, Object)} with the arguments if the value
* argument is not null or empty.
* @param t1 the first consumer argument.
* @param value the second consumer argument
* @param <T> the first argument type.
* @param consumer the consumer.
* @return this.
* @since 5.2
*/
public <T> JavaUtils acceptIfHasText(T t1, String value, BiConsumer<T, String> consumer) {
if (StringUtils.hasText(value)) {
consumer.accept(t1, value);
}
return this;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.integration.JavaUtils;
import org.springframework.integration.context.IntegrationObjectSupport;
import org.springframework.integration.context.Orderable;
import org.springframework.integration.core.MessageProducer;
import org.springframework.integration.handler.AbstractMessageProducingHandler;
import org.springframework.integration.handler.AbstractReplyProducingMessageHandler;
import org.springframework.integration.support.context.NamedComponent;
import org.springframework.integration.util.JavaUtils;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.core.DestinationResolver;
Expand Down Expand Up @@ -199,19 +199,19 @@ protected final H createHandlerInternal() {
}
this.handler = createHandler();
JavaUtils.INSTANCE
.acceptIfCondition(this.handler instanceof ApplicationContextAware && this.applicationContext != null,
this.applicationContext,
context -> ((ApplicationContextAware) this.handler).setApplicationContext(this.applicationContext))
.acceptIfCondition(this.handler instanceof BeanFactoryAware && getBeanFactory() != null,
getBeanFactory(),
factory -> ((BeanFactoryAware) this.handler).setBeanFactory(factory))
.acceptIfCondition(this.handler instanceof BeanNameAware && this.beanName != null, this.beanName,
name -> ((BeanNameAware) this.handler).setBeanName(this.beanName))
.acceptIfCondition(this.handler instanceof ApplicationEventPublisherAware
&& this.applicationEventPublisher != null,
this.applicationEventPublisher,
publisher -> ((ApplicationEventPublisherAware) this.handler)
.setApplicationEventPublisher(publisher));
.acceptIfCondition(this.handler instanceof ApplicationContextAware && this.applicationContext != null,
this.applicationContext,
context -> ((ApplicationContextAware) this.handler).setApplicationContext(this.applicationContext))
.acceptIfCondition(this.handler instanceof BeanFactoryAware && getBeanFactory() != null,
getBeanFactory(),
factory -> ((BeanFactoryAware) this.handler).setBeanFactory(factory))
.acceptIfCondition(this.handler instanceof BeanNameAware && this.beanName != null, this.beanName,
name -> ((BeanNameAware) this.handler).setBeanName(this.beanName))
.acceptIfCondition(this.handler instanceof ApplicationEventPublisherAware
&& this.applicationEventPublisher != null,
this.applicationEventPublisher,
publisher -> ((ApplicationEventPublisherAware) this.handler)
.setApplicationEventPublisher(publisher));
configureOutputChannelIfAny();
Object actualHandler = extractTarget(this.handler);
if (actualHandler == null) {
Expand All @@ -221,11 +221,11 @@ protected final H createHandlerInternal() {
integrationObjectSupport(actualHandler, handlerToConfigure);
adviceChain(actualHandler);
JavaUtils.INSTANCE
.acceptIfCondition(this.async != null && actualHandler instanceof AbstractMessageProducingHandler,
this.async,
asyncValue -> ((AbstractMessageProducingHandler) handlerToConfigure).setAsync(asyncValue))
.acceptIfCondition(this.handler instanceof Orderable && this.order != null,
this.order, theOrder -> ((Orderable) this.handler).setOrder(theOrder));
.acceptIfCondition(this.async != null && actualHandler instanceof AbstractMessageProducingHandler,
this.async,
asyncValue -> ((AbstractMessageProducingHandler) handlerToConfigure).setAsync(asyncValue))
.acceptIfCondition(this.handler instanceof Orderable && this.order != null,
this.order, theOrder -> ((Orderable) this.handler).setOrder(theOrder));
this.initialized = true;
}
initializingBean();
Expand All @@ -235,10 +235,10 @@ protected final H createHandlerInternal() {
private void integrationObjectSupport(Object actualHandler, final Object handlerToConfigure) {
if (actualHandler instanceof IntegrationObjectSupport) {
JavaUtils.INSTANCE
.acceptIfNotNull(this.componentName,
name -> ((IntegrationObjectSupport) handlerToConfigure).setComponentName(name))
.acceptIfNotNull(this.channelResolver,
resolver -> ((IntegrationObjectSupport) handlerToConfigure).setChannelResolver(resolver));
.acceptIfNotNull(this.componentName,
name -> ((IntegrationObjectSupport) handlerToConfigure).setComponentName(name))
.acceptIfNotNull(this.channelResolver,
resolver -> ((IntegrationObjectSupport) handlerToConfigure).setChannelResolver(resolver));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.jetbrains.annotations.Nullable;

import org.springframework.expression.Expression;
import org.springframework.integration.JavaUtils;
import org.springframework.integration.aggregator.AbstractAggregatingMessageGroupProcessor;
import org.springframework.integration.aggregator.AggregatingMessageHandler;
import org.springframework.integration.aggregator.CorrelationStrategy;
Expand All @@ -36,7 +37,6 @@
import org.springframework.integration.store.MessageGroup;
import org.springframework.integration.store.MessageGroupStore;
import org.springframework.integration.support.locks.LockRegistry;
import org.springframework.integration.util.JavaUtils;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHandler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.ConversionServiceFactory;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.integration.json.JsonNodeWrapperToJsonNodeConverter;
import org.springframework.integration.support.json.JacksonPresent;
import org.springframework.integration.support.utils.IntegrationUtils;
import org.springframework.util.Assert;

Expand Down Expand Up @@ -74,6 +76,9 @@ public void afterPropertiesSet() {

private void registerConverters(GenericConversionService conversionService) {
this.converters.addAll(this.applicationContext.getBeansWithAnnotation(IntegrationConverter.class).values());
if (JacksonPresent.isJackson2Present()) {
this.converters.add(new JsonNodeWrapperToJsonNodeConverter());
}
ConversionServiceFactory.registerConverters(this.converters, conversionService);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ private void xpath(int registryId) throws LinkageError {
}
}

// TODO Remove in 6.0
private void jsonNodeToString(int registryId) {
if (!this.beanFactory.containsBean(
IntegrationContextUtils.JSON_NODE_WRAPPER_TO_JSON_NODE_CONVERTER) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import java.util.Arrays;
import java.util.Properties;

import org.springframework.integration.util.JavaUtils;
import org.springframework.integration.JavaUtils;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2020 the original author or authors.
* Copyright 2017-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -28,9 +28,9 @@
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.expression.Expression;
import org.springframework.expression.common.LiteralExpression;
import org.springframework.integration.JavaUtils;
import org.springframework.integration.annotation.AnnotationConstants;
import org.springframework.integration.annotation.MessagingGateway;
import org.springframework.integration.util.JavaUtils;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import org.springframework.expression.common.LiteralExpression;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.integration.IntegrationPatternType;
import org.springframework.integration.JavaUtils;
import org.springframework.integration.annotation.Gateway;
import org.springframework.integration.annotation.GatewayHeader;
import org.springframework.integration.context.IntegrationContextUtils;
Expand All @@ -68,7 +69,6 @@
import org.springframework.integration.support.management.IntegrationManagement;
import org.springframework.integration.support.management.TrackableComponent;
import org.springframework.integration.support.management.metrics.MetricsCaptor;
import org.springframework.integration.util.JavaUtils;
import org.springframework.lang.Nullable;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
Expand Down Expand Up @@ -865,7 +865,7 @@ private MethodInvocationGateway doCreateMethodInvocationGateway(Method method,

String gatewayMethodBeanName =
getComponentName() + '#' + method.getName() +
'(' + Arrays.stream(method.getParameterTypes())
'(' + Arrays.stream(method.getParameterTypes())
.map(Class::getSimpleName)
.collect(Collectors.joining(", ")) + ')';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.integration.config.IntegrationConverter;
import org.springframework.integration.json.JsonPropertyAccessor.JsonNodeWrapper;
import org.springframework.lang.Nullable;

Expand All @@ -38,7 +37,6 @@
*
* @since 5.5
*/
@IntegrationConverter
public class JsonNodeWrapperToJsonNodeConverter implements GenericConverter {

@Override
Expand Down
Loading