diff --git a/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/function/FunctionCatalogWrapper.java b/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/function/FunctionCatalogWrapper.java new file mode 100644 index 000000000..ced49d860 --- /dev/null +++ b/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/function/FunctionCatalogWrapper.java @@ -0,0 +1,45 @@ +/* + * Copyright 2018 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.cloud.stream.function; + +import org.springframework.cloud.function.context.FunctionCatalog; +import org.springframework.util.Assert; + +/** + * @author David Turanski + **/ +public class FunctionCatalogWrapper { + + private final FunctionCatalog catalog; + + FunctionCatalogWrapper(FunctionCatalog catalog) { + this.catalog = catalog; + } + + T lookup(Class functionType, String name) { + T function = catalog.lookup(functionType, name); + Assert.notNull(function, functionType == null ? + String.format("User provided Function '%s' cannot be located.", name) : + String.format("User provided %s '%s' cannot be located.", functionType.getSimpleName(), name)); + return function; + } + + T lookup(String name) { + return lookup(null, name); + } + +} diff --git a/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/function/FunctionConfiguration.java b/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/function/FunctionConfiguration.java index ff9ee83a1..8d5ef48e5 100644 --- a/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/function/FunctionConfiguration.java +++ b/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/function/FunctionConfiguration.java @@ -34,7 +34,7 @@ import org.springframework.context.annotation.Configuration; public class FunctionConfiguration { @Bean - public IntegrationFlowFunctionSupport functionSupport(FunctionCatalog functionCatalog, + public IntegrationFlowFunctionSupport functionSupport(FunctionCatalogWrapper functionCatalog, FunctionInspector functionInspector, CompositeMessageConverterFactory messageConverterFactory, FunctionProperties functionProperties) { @@ -42,4 +42,9 @@ public class FunctionConfiguration { functionProperties); } + @Bean + public FunctionCatalogWrapper functionCatalogWrapper(FunctionCatalog catalog) { + return new FunctionCatalogWrapper(catalog); + } + } diff --git a/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/function/FunctionInvoker.java b/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/function/FunctionInvoker.java index a0484211d..f9c8b7135 100644 --- a/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/function/FunctionInvoker.java +++ b/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/function/FunctionInvoker.java @@ -24,7 +24,6 @@ import org.apache.commons.logging.LogFactory; import reactor.core.publisher.Flux; -import org.springframework.cloud.function.context.FunctionCatalog; import org.springframework.cloud.function.context.FunctionType; import org.springframework.cloud.function.context.catalog.FunctionInspector; import org.springframework.cloud.stream.converter.CompositeMessageConverterFactory; @@ -35,6 +34,7 @@ import org.springframework.util.Assert; /** * * @author Oleg Zhurakousky + * @author David Turanski * * @param the payload type of the input Message * @param the payload type of the output Message @@ -51,11 +51,10 @@ class FunctionInvoker implements Function>, Flux> messageSource = () -> MessageBuilder.withPayload("hello function") - .setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.TEXT_PLAIN).build(); + .setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.TEXT_PLAIN) + .build(); IntegrationFlowBuilder flowBuilder = functionSupport.integrationFlowFromProvidedSupplier(messageSource); @@ -221,10 +226,7 @@ public class SourceToFunctionsSupportTests { public IntegrationFlow messageSourceFlow(IntegrationFlowFunctionSupport functionSupport) { Assert.hasText(this.functionProperties.getName(), "Supplier name must be provided"); - return functionSupport - .integrationFlowFromNamedSupplier() - .channel(this.source.output()) - .get(); + return functionSupport.integrationFlowFromNamedSupplier().channel(this.source.output()).get(); } }