From 709cb7fe4c97448f75a6f496b41b9dfdf5e9fbc6 Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Thu, 11 May 2017 11:30:11 -0400 Subject: [PATCH] Remove Jackson Imports from JacksonJsonUtils Since the classes load their imports explicitly we can't use them for classes which provides utilities to check classpath * Move imports to fully qualified names in the target utility method **Cherry-pick to 4.3.x** Move `JacksonJsonUtils.isPresent` methods to separate `JacksonPresent` class Polishing --- ...ConfigurableCompositeMessageConverter.java | 4 +- .../support/json/JacksonJsonUtils.java | 27 ++-------- .../support/json/JacksonPresent.java | 53 +++++++++++++++++++ .../json/JsonObjectMapperProvider.java | 6 +-- .../HttpRequestHandlingEndpointSupport.java | 6 +-- .../WebSocketInboundChannelAdapter.java | 6 +-- .../WebSocketOutboundMessageHandler.java | 6 +-- 7 files changed, 71 insertions(+), 37 deletions(-) create mode 100644 spring-integration-core/src/main/java/org/springframework/integration/support/json/JacksonPresent.java diff --git a/spring-integration-core/src/main/java/org/springframework/integration/support/converter/ConfigurableCompositeMessageConverter.java b/spring-integration-core/src/main/java/org/springframework/integration/support/converter/ConfigurableCompositeMessageConverter.java index 92036f09d8..de6deaa83e 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/support/converter/ConfigurableCompositeMessageConverter.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/support/converter/ConfigurableCompositeMessageConverter.java @@ -22,7 +22,7 @@ import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; -import org.springframework.integration.support.json.JacksonJsonUtils; +import org.springframework.integration.support.json.JacksonPresent; import org.springframework.messaging.converter.ByteArrayMessageConverter; import org.springframework.messaging.converter.CompositeMessageConverter; import org.springframework.messaging.converter.GenericMessageConverter; @@ -77,7 +77,7 @@ public class ConfigurableCompositeMessageConverter extends CompositeMessageConve private static Collection initDefaults() { List converters = new LinkedList<>(); - if (JacksonJsonUtils.isJackson2Present()) { + if (JacksonPresent.isJackson2Present()) { converters.add(new MappingJackson2MessageConverter()); } converters.add(new ByteArrayMessageConverter()); diff --git a/spring-integration-core/src/main/java/org/springframework/integration/support/json/JacksonJsonUtils.java b/spring-integration-core/src/main/java/org/springframework/integration/support/json/JacksonJsonUtils.java index 8cb72d953c..ec32c43b8d 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/support/json/JacksonJsonUtils.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/support/json/JacksonJsonUtils.java @@ -20,7 +20,6 @@ import org.springframework.integration.message.AdviceMessage; import org.springframework.integration.support.MutableMessage; import org.springframework.messaging.support.ErrorMessage; import org.springframework.messaging.support.GenericMessage; -import org.springframework.util.ClassUtils; import com.fasterxml.jackson.annotation.JsonTypeInfo; import com.fasterxml.jackson.databind.DeserializationFeature; @@ -43,35 +42,17 @@ public final class JacksonJsonUtils { super(); } - private static final ClassLoader classLoader = JacksonJsonUtils.class.getClassLoader(); - - private static final boolean jackson2Present = - ClassUtils.isPresent("com.fasterxml.jackson.databind.ObjectMapper", classLoader) && - ClassUtils.isPresent("com.fasterxml.jackson.core.JsonGenerator", classLoader); - - private static final boolean jacksonPresent = - ClassUtils.isPresent("org.codehaus.jackson.map.ObjectMapper", classLoader) && - ClassUtils.isPresent("org.codehaus.jackson.JsonGenerator", classLoader); - - public static boolean isJackson2Present() { - return jackson2Present; - } - - public static boolean isJacksonPresent() { - return jacksonPresent; - } - /** - * Return an {@link ObjectMapper} if available, + * Return an {@link com.fasterxml.jackson.databind.ObjectMapper} if available, * supplied with Message specific serializers and deserializers. * Also configured to store typo info in the {@code @class} property. * @return the mapper. * @throws IllegalStateException if an implementation is not available. * @since 4.3.10 */ - public static ObjectMapper messagingAwareMapper() { - if (jackson2Present) { - ObjectMapper mapper = new ObjectMapper(); + public static com.fasterxml.jackson.databind.ObjectMapper messagingAwareMapper() { + if (JacksonPresent.isJackson2Present()) { + ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper(); mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY); diff --git a/spring-integration-core/src/main/java/org/springframework/integration/support/json/JacksonPresent.java b/spring-integration-core/src/main/java/org/springframework/integration/support/json/JacksonPresent.java new file mode 100644 index 0000000000..c98ba54c0c --- /dev/null +++ b/spring-integration-core/src/main/java/org/springframework/integration/support/json/JacksonPresent.java @@ -0,0 +1,53 @@ +/* + * Copyright 2017 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 + * + * http://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.support.json; + +import org.springframework.util.ClassUtils; + +/** + * The utility to check if Jackson JSON processor is present in the classpath. + * + * @author Artem Bilan + * + * @since 4.3.10 + */ +public final class JacksonPresent { + + private static final ClassLoader classLoader = JacksonJsonUtils.class.getClassLoader(); + + private static final boolean jackson2Present = + ClassUtils.isPresent("com.fasterxml.jackson.databind.ObjectMapper", classLoader) && + ClassUtils.isPresent("com.fasterxml.jackson.core.JsonGenerator", classLoader); + + private static final boolean jacksonPresent = + ClassUtils.isPresent("org.codehaus.jackson.map.ObjectMapper", classLoader) && + ClassUtils.isPresent("org.codehaus.jackson.JsonGenerator", classLoader); + + public static boolean isJackson2Present() { + return jackson2Present; + } + + public static boolean isJacksonPresent() { + return jacksonPresent; + } + + + private JacksonPresent() { + super(); + } + +} diff --git a/spring-integration-core/src/main/java/org/springframework/integration/support/json/JsonObjectMapperProvider.java b/spring-integration-core/src/main/java/org/springframework/integration/support/json/JsonObjectMapperProvider.java index 7967a7c96c..8657e15368 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/support/json/JsonObjectMapperProvider.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/support/json/JsonObjectMapperProvider.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 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. @@ -49,7 +49,7 @@ public final class JsonObjectMapperProvider { * @throws IllegalStateException if an implementation is not available. */ public static JsonObjectMapper newInstance() { - if (JacksonJsonUtils.isJackson2Present()) { + if (JacksonPresent.isJackson2Present()) { return new Jackson2JsonObjectMapper(); } else if (boonPresent) { @@ -66,7 +66,7 @@ public final class JsonObjectMapperProvider { * @since 4.2.7 */ public static boolean jsonAvailable() { - return JacksonJsonUtils.isJackson2Present() || boonPresent; + return JacksonPresent.isJackson2Present() || boonPresent; } } diff --git a/spring-integration-http/src/main/java/org/springframework/integration/http/inbound/HttpRequestHandlingEndpointSupport.java b/spring-integration-http/src/main/java/org/springframework/integration/http/inbound/HttpRequestHandlingEndpointSupport.java index 755a66a570..cdfbdbe0b2 100644 --- a/spring-integration-http/src/main/java/org/springframework/integration/http/inbound/HttpRequestHandlingEndpointSupport.java +++ b/spring-integration-http/src/main/java/org/springframework/integration/http/inbound/HttpRequestHandlingEndpointSupport.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 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. @@ -60,7 +60,7 @@ import org.springframework.integration.http.multipart.MultipartHttpInputMessage; import org.springframework.integration.http.support.DefaultHttpHeaderMapper; import org.springframework.integration.mapping.HeaderMapper; import org.springframework.integration.support.AbstractIntegrationMessageBuilder; -import org.springframework.integration.support.json.JacksonJsonUtils; +import org.springframework.integration.support.json.JacksonPresent; import org.springframework.messaging.Message; import org.springframework.messaging.MessageHeaders; import org.springframework.messaging.MessagingException; @@ -194,7 +194,7 @@ public abstract class HttpRequestHandlingEndpointSupport extends MessagingGatewa logger.debug("'Jaxb2RootElementHttpMessageConverter' was added to the 'defaultMessageConverters'."); } } - if (JacksonJsonUtils.isJackson2Present()) { + if (JacksonPresent.isJackson2Present()) { this.defaultMessageConverters.add(new MappingJackson2HttpMessageConverter()); if (logger.isDebugEnabled()) { logger.debug("'MappingJackson2HttpMessageConverter' was added to the 'defaultMessageConverters'."); diff --git a/spring-integration-websocket/src/main/java/org/springframework/integration/websocket/inbound/WebSocketInboundChannelAdapter.java b/spring-integration-websocket/src/main/java/org/springframework/integration/websocket/inbound/WebSocketInboundChannelAdapter.java index 0aa9aed58e..ada3f6944e 100644 --- a/spring-integration-websocket/src/main/java/org/springframework/integration/websocket/inbound/WebSocketInboundChannelAdapter.java +++ b/spring-integration-websocket/src/main/java/org/springframework/integration/websocket/inbound/WebSocketInboundChannelAdapter.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2015 the original author or authors. + * Copyright 2014-2017 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. @@ -28,7 +28,7 @@ import org.springframework.context.ApplicationEventPublisherAware; import org.springframework.context.Lifecycle; import org.springframework.integration.channel.FixedSubscriberChannel; import org.springframework.integration.endpoint.MessageProducerSupport; -import org.springframework.integration.support.json.JacksonJsonUtils; +import org.springframework.integration.support.json.JacksonPresent; import org.springframework.integration.websocket.IntegrationWebSocketContainer; import org.springframework.integration.websocket.ServerWebSocketContainer; import org.springframework.integration.websocket.WebSocketListener; @@ -75,7 +75,7 @@ public class WebSocketInboundChannelAdapter extends MessageProducerSupport { this.defaultConverters.add(new StringMessageConverter()); this.defaultConverters.add(new ByteArrayMessageConverter()); - if (JacksonJsonUtils.isJackson2Present()) { + if (JacksonPresent.isJackson2Present()) { DefaultContentTypeResolver resolver = new DefaultContentTypeResolver(); resolver.setDefaultMimeType(MimeTypeUtils.APPLICATION_JSON); MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter(); diff --git a/spring-integration-websocket/src/main/java/org/springframework/integration/websocket/outbound/WebSocketOutboundMessageHandler.java b/spring-integration-websocket/src/main/java/org/springframework/integration/websocket/outbound/WebSocketOutboundMessageHandler.java index 2751eca4ed..0d83c5dc94 100644 --- a/spring-integration-websocket/src/main/java/org/springframework/integration/websocket/outbound/WebSocketOutboundMessageHandler.java +++ b/spring-integration-websocket/src/main/java/org/springframework/integration/websocket/outbound/WebSocketOutboundMessageHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 the original author or authors. + * Copyright 2014-2017 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. @@ -21,7 +21,7 @@ import java.util.List; import java.util.ListIterator; import org.springframework.integration.handler.AbstractMessageHandler; -import org.springframework.integration.support.json.JacksonJsonUtils; +import org.springframework.integration.support.json.JacksonPresent; import org.springframework.integration.websocket.ClientWebSocketContainer; import org.springframework.integration.websocket.IntegrationWebSocketContainer; import org.springframework.integration.websocket.support.PassThruSubProtocolHandler; @@ -52,7 +52,7 @@ public class WebSocketOutboundMessageHandler extends AbstractMessageHandler { { this.defaultConverters.add(new StringMessageConverter()); this.defaultConverters.add(new ByteArrayMessageConverter()); - if (JacksonJsonUtils.isJackson2Present()) { + if (JacksonPresent.isJackson2Present()) { DefaultContentTypeResolver resolver = new DefaultContentTypeResolver(); resolver.setDefaultMimeType(MimeTypeUtils.APPLICATION_JSON); MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();