From 7c29aa34c23a1304948ae9d22c35acd8329a561b Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Tue, 9 Jan 2018 12:40:51 +0000 Subject: [PATCH] Avoid use of Spring Boot 1.5.x native APIs --- .../FunctionProxyApplicationListener.java | 79 ++++++++++++++----- 1 file changed, 60 insertions(+), 19 deletions(-) diff --git a/spring-cloud-function-compiler/src/main/java/org/springframework/cloud/function/compiler/config/FunctionProxyApplicationListener.java b/spring-cloud-function-compiler/src/main/java/org/springframework/cloud/function/compiler/config/FunctionProxyApplicationListener.java index b748806b5..75683a581 100644 --- a/spring-cloud-function-compiler/src/main/java/org/springframework/cloud/function/compiler/config/FunctionProxyApplicationListener.java +++ b/spring-cloud-function-compiler/src/main/java/org/springframework/cloud/function/compiler/config/FunctionProxyApplicationListener.java @@ -16,14 +16,16 @@ package org.springframework.cloud.function.compiler.config; +import java.util.HashMap; import java.util.Map; import org.springframework.beans.MutablePropertyValues; import org.springframework.beans.factory.config.ConstructorArgumentValues; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.RootBeanDefinition; -import org.springframework.boot.bind.PropertySourcesBinder; import org.springframework.boot.context.event.ApplicationPreparedEvent; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor; import org.springframework.cloud.function.compiler.ConsumerCompiler; import org.springframework.cloud.function.compiler.FunctionCompiler; import org.springframework.cloud.function.compiler.SupplierCompiler; @@ -35,6 +37,7 @@ import org.springframework.cloud.function.compiler.proxy.LambdaCompilingFunction import org.springframework.cloud.function.compiler.proxy.LambdaCompilingSupplier; import org.springframework.context.ApplicationListener; import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.support.StaticApplicationContext; import org.springframework.core.io.ByteArrayResource; import org.springframework.core.io.Resource; import org.springframework.util.Assert; @@ -42,7 +45,9 @@ import org.springframework.util.Assert; /** * @author Mark Fisher */ -public class FunctionProxyApplicationListener implements ApplicationListener { +@ConfigurationProperties("spring.cloud.function") +public class FunctionProxyApplicationListener + implements ApplicationListener { private final SupplierCompiler supplierCompiler = new SupplierCompiler<>(); @@ -50,38 +55,70 @@ public class FunctionProxyApplicationListener implements ApplicationListener consumerCompiler = new ConsumerCompiler<>(); + private final Map toCompile = new HashMap<>(); + + private final Map toImport = new HashMap<>(); + + public Map getCompile() { + return toCompile; + } + + public Map getImport() { + return toImport; + } + @Override public void onApplicationEvent(ApplicationPreparedEvent event) { ConfigurableApplicationContext context = event.getApplicationContext(); - DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) context.getBeanFactory(); - PropertySourcesBinder binder = new PropertySourcesBinder(context.getEnvironment()); - Map toCompile = binder.extractAll("spring.cloud.function.compile"); + DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) context + .getBeanFactory(); + bind(context); for (Map.Entry entry : toCompile.entrySet()) { String name = entry.getKey(); @SuppressWarnings("unchecked") Map properties = (Map) entry.getValue(); - String type = (properties.get("type") != null) ? properties.get("type") : "function"; + String type = (properties.get("type") != null) ? properties.get("type") + : "function"; String lambda = properties.get("lambda"); - Assert.notNull(lambda, - String.format("The 'lambda' property is required for compiling Function: %s", name)); + Assert.notNull(lambda, String.format( + "The 'lambda' property is required for compiling Function: %s", + name)); String inputType = properties.get("inputType"); String outputType = properties.get("outputType"); - registerLambdaCompilingProxy(name, type, inputType, outputType, lambda, beanFactory); + registerLambdaCompilingProxy(name, type, inputType, outputType, lambda, + beanFactory); } - Map toImport = binder.extractAll("spring.cloud.function.import"); for (Map.Entry entry : toImport.entrySet()) { String name = entry.getKey(); @SuppressWarnings("unchecked") Map properties = (Map) entry.getValue(); - String type = (properties.get("type") != null) ? properties.get("type") : "function"; + String type = (properties.get("type") != null) ? properties.get("type") + : "function"; String location = properties.get("location"); - Assert.notNull(location, - String.format("The 'location' property is required for importing Function: %s", name)); - registerByteCodeLoadingProxy(name, type, context.getResource(location), beanFactory); + Assert.notNull(location, String.format( + "The 'location' property is required for importing Function: %s", + name)); + registerByteCodeLoadingProxy(name, type, context.getResource(location), + beanFactory); } } - private void registerByteCodeLoadingProxy(String name, String type, Resource resource, DefaultListableBeanFactory beanFactory) { + private void bind(ConfigurableApplicationContext context) { + ConfigurationPropertiesBindingPostProcessor post = new ConfigurationPropertiesBindingPostProcessor(); + post.setBeanFactory(new DefaultListableBeanFactory()); + post.setEnvironment(context.getEnvironment()); + post.setApplicationContext(new StaticApplicationContext()); + try { + post.afterPropertiesSet(); + } + catch (Exception e) { + throw new IllegalStateException("Cannot bind properties", e); + } + post.postProcessBeforeInitialization(this, getClass().getName()); + } + + private void registerByteCodeLoadingProxy(String name, String type, Resource resource, + DefaultListableBeanFactory beanFactory) { Class proxyClass = null; if ("supplier".equals(type.toLowerCase())) { proxyClass = ByteCodeLoadingSupplier.class; @@ -99,7 +136,8 @@ public class FunctionProxyApplicationListener implements ApplicationListener