From 0e2334b1549f72b1ee0bf17aab4c210d58664dbb Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Mon, 8 Nov 2021 17:20:49 +0100 Subject: [PATCH] Additional removals of deprecated classes from core --- .../context/message/MessageUtils.java | 81 ------------------- .../cloud/function/core/Isolated.java | 27 ------- .../cloud/function/core/IsolatedConsumer.java | 55 ------------- .../cloud/function/core/IsolatedFunction.java | 56 ------------- 4 files changed, 219 deletions(-) delete mode 100644 spring-cloud-function-core/src/main/java/org/springframework/cloud/function/core/Isolated.java delete mode 100644 spring-cloud-function-core/src/main/java/org/springframework/cloud/function/core/IsolatedConsumer.java delete mode 100644 spring-cloud-function-core/src/main/java/org/springframework/cloud/function/core/IsolatedFunction.java diff --git a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/message/MessageUtils.java b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/message/MessageUtils.java index 6b7aa1140..585793feb 100644 --- a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/message/MessageUtils.java +++ b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/message/MessageUtils.java @@ -16,17 +16,6 @@ package org.springframework.cloud.function.context.message; -import java.lang.reflect.Method; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; - -import org.springframework.cloud.function.core.Isolated; -import org.springframework.messaging.Message; -import org.springframework.messaging.support.MessageBuilder; -import org.springframework.util.ClassUtils; -import org.springframework.util.ReflectionUtils; - /** * @author Dave Syer * @author Oleg Zhurakousky @@ -47,74 +36,4 @@ public abstract class MessageUtils { * Value for 'target-protocol' typically use as header key. */ public static String SOURCE_TYPE = "source-type"; - - /** - * Create a message for the handler. If the handler is a wrapper for a function in an - * isolated class loader, then the message will be created with the target class - * loader (therefore the {@link Message} class must be on the classpath of the target - * class loader). - * @param handler the function that will be applied to the message - * @param payload the payload of the message - * @param headers the headers for the message - * @return a message with the correct class loader - */ - public static Object create(Object handler, Object payload, - Map headers) { - if (payload instanceof Message) { - headers = new HashMap<>(headers); - headers.putAll(((Message) payload).getHeaders()); - payload = ((Message) payload).getPayload(); - } - if (!(handler instanceof Isolated)) { - return MessageBuilder.withPayload(payload).copyHeaders(headers).build(); - } - ClassLoader classLoader = ((Isolated) handler).getClassLoader(); - Class builder = ClassUtils.resolveClassName(MessageBuilder.class.getName(), - classLoader); - Method withPayload = ClassUtils.getMethod(builder, "withPayload", Object.class); - Method copyHeaders = ClassUtils.getMethod(builder, "copyHeaders", Map.class); - Method build = ClassUtils.getMethod(builder, "build"); - Object instance = ReflectionUtils.invokeMethod(withPayload, null, payload); - ReflectionUtils.invokeMethod(copyHeaders, instance, headers); - return ReflectionUtils.invokeMethod(build, instance); - } - - /** - * Convert a message from the handler into one that is safe to consume in the caller's - * class loader. If the handler is a wrapper for a function in an isolated class - * loader, then the message will be created with the target class loader (therefore - * the {@link Message} class must be on the classpath of the target class loader). - * @param handler the function that generated the message - * @param message the message to convert - * @return a message with the correct class loader - */ - public static Message unpack(Object handler, Object message) { - if (!(handler instanceof Isolated)) { - if (message instanceof Message) { - return (Message) message; - } - return MessageBuilder.withPayload(message).build(); - } - ClassLoader classLoader = ((Isolated) handler).getClassLoader(); - Class type = ClassUtils.isPresent(Message.class.getName(), classLoader) - ? ClassUtils.resolveClassName(Message.class.getName(), classLoader) - : null; - Object payload; - Map headers; - if (type != null && type.isAssignableFrom(message.getClass())) { - Method getPayload = ClassUtils.getMethod(type, "getPayload"); - Method getHeaders = ClassUtils.getMethod(type, "getHeaders"); - payload = ReflectionUtils.invokeMethod(getPayload, message); - @SuppressWarnings("unchecked") - Map map = (Map) ReflectionUtils - .invokeMethod(getHeaders, message); - headers = map; - } - else { - payload = message; - headers = Collections.emptyMap(); - } - return MessageBuilder.withPayload(payload).copyHeaders(headers).build(); - } - } diff --git a/spring-cloud-function-core/src/main/java/org/springframework/cloud/function/core/Isolated.java b/spring-cloud-function-core/src/main/java/org/springframework/cloud/function/core/Isolated.java deleted file mode 100644 index 3b7d7779a..000000000 --- a/spring-cloud-function-core/src/main/java/org/springframework/cloud/function/core/Isolated.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright 2012-2019 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.cloud.function.core; - -/** - * @author Dave Syer - * - */ -public interface Isolated { - - ClassLoader getClassLoader(); - -} diff --git a/spring-cloud-function-core/src/main/java/org/springframework/cloud/function/core/IsolatedConsumer.java b/spring-cloud-function-core/src/main/java/org/springframework/cloud/function/core/IsolatedConsumer.java deleted file mode 100644 index 1eefb1bc0..000000000 --- a/spring-cloud-function-core/src/main/java/org/springframework/cloud/function/core/IsolatedConsumer.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright 2012-2019 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.cloud.function.core; - -import java.util.function.Consumer; - -import org.springframework.util.ClassUtils; - -/** - * @param type to consume - * @author Dave Syer - */ -public class IsolatedConsumer implements Consumer, Isolated { - - private final Consumer consumer; - - private final ClassLoader classLoader; - - public IsolatedConsumer(Consumer consumer) { - this.consumer = consumer; - this.classLoader = consumer.getClass().getClassLoader(); - } - - @Override - public ClassLoader getClassLoader() { - return this.classLoader; - } - - @Override - public void accept(T item) { - ClassLoader context = ClassUtils - .overrideThreadContextClassLoader(this.classLoader); - try { - this.consumer.accept(item); - } - finally { - ClassUtils.overrideThreadContextClassLoader(context); - } - } - -} diff --git a/spring-cloud-function-core/src/main/java/org/springframework/cloud/function/core/IsolatedFunction.java b/spring-cloud-function-core/src/main/java/org/springframework/cloud/function/core/IsolatedFunction.java deleted file mode 100644 index c75122327..000000000 --- a/spring-cloud-function-core/src/main/java/org/springframework/cloud/function/core/IsolatedFunction.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright 2012-2019 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.cloud.function.core; - -import java.util.function.Function; - -import org.springframework.util.ClassUtils; - -/** - * @param input type - * @param output type - * @author Dave Syer - */ -public class IsolatedFunction implements Function, Isolated { - - private final Function function; - - private final ClassLoader classLoader; - - public IsolatedFunction(Function function) { - this.function = function; - this.classLoader = function.getClass().getClassLoader(); - } - - @Override - public ClassLoader getClassLoader() { - return this.classLoader; - } - - @Override - public T apply(S item) { - ClassLoader context = ClassUtils - .overrideThreadContextClassLoader(this.classLoader); - try { - return this.function.apply(item); - } - finally { - ClassUtils.overrideThreadContextClassLoader(context); - } - } - -}