diff --git a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/ContextFunctionCatalogAutoConfiguration.java b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/ContextFunctionCatalogAutoConfiguration.java index 76fa74aa6..9ba787ff4 100644 --- a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/ContextFunctionCatalogAutoConfiguration.java +++ b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/ContextFunctionCatalogAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 the original author or authors. + * Copyright 2016-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. @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.cloud.function.context; import java.lang.reflect.ParameterizedType; @@ -47,40 +48,44 @@ import com.fasterxml.jackson.databind.ObjectMapper; import reactor.core.publisher.Flux; +/** + * @author Dave Syer + * @author Mark Fisher + */ @Configuration -@ConditionalOnClass(ApplicationContextFunctionCatalog.class) +@ConditionalOnClass(InMemoryFunctionCatalog.class) @ConditionalOnMissingBean(FunctionCatalog.class) public class ContextFunctionCatalogAutoConfiguration { - @Autowired(required = false) - private Map> functions = Collections.emptyMap(); - @Autowired(required = false) - private Map> consumers = Collections.emptyMap(); @Autowired(required = false) private Map> suppliers = Collections.emptyMap(); + @Autowired(required = false) + private Map> functions = Collections.emptyMap(); + + @Autowired(required = false) + private Map> consumers = Collections.emptyMap(); + @Bean - public FunctionCatalog functionCatalog(ContextFunctionPostProcessor processor, - ObjectMapper mapper) { - return new ApplicationContextFunctionCatalog( + public FunctionCatalog functionCatalog(ContextFunctionPostProcessor processor, ObjectMapper mapper) { + return new InMemoryFunctionCatalog( + processor.wrapSuppliers(mapper, suppliers), processor.wrapFunctions(mapper, functions), - processor.wrapConsumers(mapper, consumers), - processor.wrapSuppliers(mapper, suppliers)); + processor.wrapConsumers(mapper, consumers)); } @Component public static class ContextFunctionPostProcessor implements BeanFactoryPostProcessor, BeanDefinitionRegistryPostProcessor { + private Set suppliers = new HashSet<>(); private Set functions = new HashSet<>(); private Set consumers = new HashSet<>(); - private Set suppliers = new HashSet<>(); private BeanDefinitionRegistry registry; @Override - public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) - throws BeansException { + public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) { this.registry = registry; } @@ -100,22 +105,6 @@ public class ContextFunctionCatalogAutoConfiguration { return result; } - public Map> wrapConsumers(ObjectMapper mapper, - Map> consumers) { - Map> result = new HashMap<>(); - for (String key : consumers.keySet()) { - if (this.consumers.contains(key)) { - @SuppressWarnings("unchecked") - Consumer> consumer = (Consumer>) consumers.get(key); - result.put(key, wrapConsumer(consumer, mapper, key)); - } - else { - result.put(key, consumers.get(key)); - } - } - return result; - } - public Map> wrapFunctions(ObjectMapper mapper, Map> functions) { Map> result = new HashMap<>(); @@ -133,22 +122,48 @@ public class ContextFunctionCatalogAutoConfiguration { return result; } + public Map> wrapConsumers(ObjectMapper mapper, + Map> consumers) { + Map> result = new HashMap<>(); + for (String key : consumers.keySet()) { + if (this.consumers.contains(key)) { + @SuppressWarnings("unchecked") + Consumer> consumer = (Consumer>) consumers.get(key); + result.put(key, wrapConsumer(consumer, mapper, key)); + } + else { + result.put(key, consumers.get(key)); + } + } + return result; + } + @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory factory) throws BeansException { for (String name : factory.getBeanDefinitionNames()) { - if (isGenericFunction(factory, name)) { - this.functions.add(name); - } - else if (isGenericSupplier(factory, name)) { + if (isGenericSupplier(factory, name)) { this.suppliers.add(name); } + else if (isGenericFunction(factory, name)) { + this.functions.add(name); + } else if (isGenericConsumer(factory, name)) { this.consumers.add(name); } } } + private boolean isGenericSupplier(ConfigurableListableBeanFactory factory, + String name) { + return factory.isTypeMatch(name, + ResolvableType.forClassWithGenerics(Supplier.class, Flux.class)) + && !factory.isTypeMatch(name, + ResolvableType.forClassWithGenerics(Supplier.class, + ResolvableType.forClassWithGenerics(Flux.class, + String.class))); + } + private boolean isGenericFunction(ConfigurableListableBeanFactory factory, String name) { return factory.isTypeMatch(name, @@ -162,16 +177,6 @@ public class ContextFunctionCatalogAutoConfiguration { String.class))); } - private boolean isGenericSupplier(ConfigurableListableBeanFactory factory, - String name) { - return factory.isTypeMatch(name, - ResolvableType.forClassWithGenerics(Supplier.class, Flux.class)) - && !factory.isTypeMatch(name, - ResolvableType.forClassWithGenerics(Supplier.class, - ResolvableType.forClassWithGenerics(Flux.class, - String.class))); - } - private boolean isGenericConsumer(ConfigurableListableBeanFactory factory, String name) { return factory.isTypeMatch(name, @@ -182,6 +187,13 @@ public class ContextFunctionCatalogAutoConfiguration { String.class))); } + private ProxySupplier wrapSupplier(Supplier> supplier, + ObjectMapper mapper) { + ProxySupplier wrapped = new ProxySupplier(mapper); + wrapped.setDelegate(supplier); + return wrapped; + } + private ProxyFunction wrapFunction(Function, Flux> function, ObjectMapper mapper, String name) { ProxyFunction wrapped = new ProxyFunction(mapper); @@ -190,13 +202,6 @@ public class ContextFunctionCatalogAutoConfiguration { return wrapped; } - private ProxySupplier wrapSupplier(Supplier> supplier, - ObjectMapper mapper) { - ProxySupplier wrapped = new ProxySupplier(mapper); - wrapped.setDelegate(supplier); - return wrapped; - } - private ProxyConsumer wrapConsumer(Consumer> consumer, ObjectMapper mapper, String name) { ProxyConsumer wrapped = new ProxyConsumer(mapper); @@ -283,21 +288,6 @@ abstract class ProxyWrapper { } -class ProxyFunction extends ProxyWrapper, Flux>> - implements Function, Flux> { - - @Autowired - public ProxyFunction(ObjectMapper mapper) { - super(mapper); - } - - @Override - public Flux apply(Flux input) { - return getDelegate().apply(input.map(this::fromJson)).map(this::toJson); - } - -} - class ProxySupplier extends ProxyWrapper>> implements Supplier> { @@ -310,7 +300,20 @@ class ProxySupplier extends ProxyWrapper>> public Flux get() { return getDelegate().get().map(this::toJson); } +} +class ProxyFunction extends ProxyWrapper, Flux>> + implements Function, Flux> { + + @Autowired + public ProxyFunction(ObjectMapper mapper) { + super(mapper); + } + + @Override + public Flux apply(Flux input) { + return getDelegate().apply(input.map(this::fromJson)).map(this::toJson); + } } class ProxyConsumer extends ProxyWrapper>> @@ -325,5 +328,4 @@ class ProxyConsumer extends ProxyWrapper>> public void accept(Flux input) { getDelegate().accept(input.map(this::fromJson)); } - } diff --git a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/ApplicationContextFunctionCatalog.java b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/InMemoryFunctionCatalog.java similarity index 82% rename from spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/ApplicationContextFunctionCatalog.java rename to spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/InMemoryFunctionCatalog.java index bc34c0a8a..8c65b5e6d 100644 --- a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/ApplicationContextFunctionCatalog.java +++ b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/InMemoryFunctionCatalog.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 the original author or authors. + * Copyright 2016-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. @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.cloud.function.context; import java.util.Map; @@ -22,35 +23,40 @@ import java.util.function.Supplier; import org.springframework.cloud.function.registry.FunctionCatalog; -public class ApplicationContextFunctionCatalog implements FunctionCatalog { +/** + * @author Dave Syer + * @author Mark Fisher + */ +public class InMemoryFunctionCatalog implements FunctionCatalog { private final Map> functions; + private final Map> consumers; + private final Map> suppliers; - public ApplicationContextFunctionCatalog(Map> functions, - Map> consumers, Map> suppliers) { + public InMemoryFunctionCatalog(Map> suppliers, + Map> functions, Map> consumers) { + this.suppliers = suppliers; this.functions = functions; this.consumers = consumers; - this.suppliers = suppliers; } - @SuppressWarnings("unchecked") @Override - public Consumer lookupConsumer(String name) { - return (Consumer) consumers.get(name); - } - @SuppressWarnings("unchecked") - @Override - public Function lookupFunction(String name) { - return (Function) functions.get(name); - } - - @SuppressWarnings("unchecked") - @Override public Supplier lookupSupplier(String name) { return (Supplier) suppliers.get(name); } + @Override + @SuppressWarnings("unchecked") + public Function lookupFunction(String name) { + return (Function) functions.get(name); + } + + @Override + @SuppressWarnings("unchecked") + public Consumer lookupConsumer(String name) { + return (Consumer) consumers.get(name); + } } diff --git a/spring-cloud-function-core/src/main/java/org/springframework/cloud/function/registry/FunctionCatalog.java b/spring-cloud-function-core/src/main/java/org/springframework/cloud/function/registry/FunctionCatalog.java index 00fddcf6c..8b0ed5aff 100644 --- a/spring-cloud-function-core/src/main/java/org/springframework/cloud/function/registry/FunctionCatalog.java +++ b/spring-cloud-function-core/src/main/java/org/springframework/cloud/function/registry/FunctionCatalog.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 the original author or authors. + * Copyright 2016-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. @@ -25,9 +25,9 @@ import java.util.function.Supplier; */ public interface FunctionCatalog { - Consumer lookupConsumer(String name); + Supplier lookupSupplier(String name); Function lookupFunction(String name); - Supplier lookupSupplier(String name); + Consumer lookupConsumer(String name); }