added FunctionProxyApplicationListener
added support for lambda compiling Supplier and Consumer proxies
This commit is contained in:
@@ -37,6 +37,10 @@
|
|||||||
<groupId>commons-collections</groupId>
|
<groupId>commons-collections</groupId>
|
||||||
<artifactId>commons-collections</artifactId>
|
<artifactId>commons-collections</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-context</artifactId>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-web</artifactId>
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
|||||||
@@ -0,0 +1,115 @@
|
|||||||
|
/*
|
||||||
|
* 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.cloud.function.compiler.config;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
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.cloud.function.compiler.ConsumerCompiler;
|
||||||
|
import org.springframework.cloud.function.compiler.FunctionCompiler;
|
||||||
|
import org.springframework.cloud.function.compiler.SupplierCompiler;
|
||||||
|
import org.springframework.cloud.function.compiler.proxy.ByteCodeLoadingConsumer;
|
||||||
|
import org.springframework.cloud.function.compiler.proxy.ByteCodeLoadingFunction;
|
||||||
|
import org.springframework.cloud.function.compiler.proxy.ByteCodeLoadingSupplier;
|
||||||
|
import org.springframework.cloud.function.compiler.proxy.LambdaCompilingConsumer;
|
||||||
|
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.core.io.ByteArrayResource;
|
||||||
|
import org.springframework.core.io.Resource;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Mark Fisher
|
||||||
|
*/
|
||||||
|
public class FunctionProxyApplicationListener implements ApplicationListener<ApplicationPreparedEvent> {
|
||||||
|
|
||||||
|
private final SupplierCompiler<?> supplierCompiler = new SupplierCompiler<>();
|
||||||
|
|
||||||
|
private final FunctionCompiler<?, ?> functionCompiler = new FunctionCompiler<>();
|
||||||
|
|
||||||
|
private final ConsumerCompiler<?> consumerCompiler = new ConsumerCompiler<>();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onApplicationEvent(ApplicationPreparedEvent event) {
|
||||||
|
ConfigurableApplicationContext context = event.getApplicationContext();
|
||||||
|
DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) context.getBeanFactory();
|
||||||
|
PropertySourcesBinder binder = new PropertySourcesBinder(context.getEnvironment());
|
||||||
|
Map<String, Object> extracted = binder.extractAll("spring.cloud.function.proxy");
|
||||||
|
for (Map.Entry<String, Object> entry : extracted.entrySet()) {
|
||||||
|
String name = entry.getKey();
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
Map<String, String> properties = (Map<String, String>) entry.getValue();
|
||||||
|
String type = (properties.get("type") != null) ? properties.get("type") : "function";
|
||||||
|
String resource = properties.get("resource");
|
||||||
|
String lambda = properties.get("lambda");
|
||||||
|
if (!(null == resource ^ null == lambda)) {
|
||||||
|
throw new IllegalArgumentException("Exactly one of 'resource' or 'lambda' is required for a Function proxy");
|
||||||
|
}
|
||||||
|
if (resource != null) {
|
||||||
|
registerByteCodeLoadingProxy(name, type, context.getResource(resource), beanFactory);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
registerLambdaCompilingProxy(name, type, lambda, beanFactory);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void registerByteCodeLoadingProxy(String name, String type, Resource resource, DefaultListableBeanFactory beanFactory) {
|
||||||
|
Class<?> proxyClass = null;
|
||||||
|
if ("supplier".equals(type.toLowerCase())) {
|
||||||
|
proxyClass = ByteCodeLoadingSupplier.class;
|
||||||
|
}
|
||||||
|
else if ("consumer".equals(type.toLowerCase())) {
|
||||||
|
proxyClass = ByteCodeLoadingConsumer.class;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
proxyClass = ByteCodeLoadingFunction.class;
|
||||||
|
}
|
||||||
|
RootBeanDefinition beanDefinition = new RootBeanDefinition(proxyClass);
|
||||||
|
ConstructorArgumentValues args = new ConstructorArgumentValues();
|
||||||
|
args.addGenericArgumentValue(resource);
|
||||||
|
beanDefinition.setConstructorArgumentValues(args);
|
||||||
|
beanFactory.registerBeanDefinition(name, beanDefinition);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void registerLambdaCompilingProxy(String name, String type, String lambda, DefaultListableBeanFactory beanFactory) {
|
||||||
|
Resource resource = new ByteArrayResource(lambda.getBytes());
|
||||||
|
ConstructorArgumentValues args = new ConstructorArgumentValues();
|
||||||
|
args.addGenericArgumentValue(resource);
|
||||||
|
Class<?> proxyClass = null;
|
||||||
|
if ("supplier".equals(type.toLowerCase())) {
|
||||||
|
proxyClass = LambdaCompilingSupplier.class;
|
||||||
|
args.addGenericArgumentValue(this.supplierCompiler);
|
||||||
|
}
|
||||||
|
else if ("consumer".equals(type.toLowerCase())) {
|
||||||
|
proxyClass = LambdaCompilingConsumer.class;
|
||||||
|
args.addGenericArgumentValue(this.consumerCompiler);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
proxyClass = LambdaCompilingFunction.class;
|
||||||
|
args.addGenericArgumentValue(this.functionCompiler);
|
||||||
|
}
|
||||||
|
RootBeanDefinition beanDefinition = new RootBeanDefinition(proxyClass);
|
||||||
|
beanDefinition.setConstructorArgumentValues(args);
|
||||||
|
beanFactory.registerBeanDefinition(name, beanDefinition);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
org.springframework.context.ApplicationListener=\
|
||||||
|
org.springframework.cloud.function.compiler.config.FunctionProxyApplicationListener
|
||||||
@@ -25,12 +25,6 @@
|
|||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
|
||||||
<!-- TODO: this shouldn't be needed (bug in thin launcher) -->
|
|
||||||
<groupId>io.projectreactor</groupId>
|
|
||||||
<artifactId>reactor-core</artifactId>
|
|
||||||
<version>${reactor.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.cloud</groupId>
|
<groupId>org.springframework.cloud</groupId>
|
||||||
<artifactId>spring-cloud-function-web</artifactId>
|
<artifactId>spring-cloud-function-web</artifactId>
|
||||||
@@ -41,11 +35,6 @@
|
|||||||
<artifactId>spring-cloud-function-compiler</artifactId>
|
<artifactId>spring-cloud-function-compiler</artifactId>
|
||||||
<version>${spring-cloud-function.version}</version>
|
<version>${spring-cloud-function.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
|
||||||
<optional>true</optional>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@@ -16,54 +16,13 @@
|
|||||||
|
|
||||||
package com.example;
|
package com.example;
|
||||||
|
|
||||||
import java.util.function.Function;
|
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
|
||||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
|
||||||
import org.springframework.cloud.function.compiler.FunctionCompiler;
|
|
||||||
import org.springframework.cloud.function.compiler.proxy.LambdaCompilingFunction;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.core.io.ByteArrayResource;
|
|
||||||
|
|
||||||
import reactor.core.publisher.Flux;
|
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
@EnableConfigurationProperties(FunctionProperties.class)
|
|
||||||
public class SampleApplication {
|
public class SampleApplication {
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private FunctionProperties properties;
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
|
||||||
public Function<Flux<String>, Flux<String>> function(FunctionCompiler<?, ?> compiler) {
|
|
||||||
ByteArrayResource resource = new ByteArrayResource(properties.getLambda().getBytes());
|
|
||||||
return new LambdaCompilingFunction(resource, compiler);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public FunctionCompiler<?, ?> compiler() {
|
|
||||||
return new FunctionCompiler<>();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
SpringApplication.run(SampleApplication.class, args);
|
SpringApplication.run(SampleApplication.class, args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ConfigurationProperties("function")
|
|
||||||
class FunctionProperties {
|
|
||||||
|
|
||||||
private String lambda;
|
|
||||||
|
|
||||||
public String getLambda() {
|
|
||||||
return lambda;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setLambda(String lambda) {
|
|
||||||
this.lambda = lambda;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||||||
*/
|
*/
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT,
|
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT,
|
||||||
properties = "function.lambda=f->f.map(s->s.toString().toLowerCase())")
|
properties = "spring.cloud.function.proxy.test.lambda=f->f.map(s->s+\"!!!\")")
|
||||||
public class SampleApplicationTests {
|
public class SampleApplicationTests {
|
||||||
|
|
||||||
@LocalServerPort
|
@LocalServerPort
|
||||||
@@ -41,8 +41,8 @@ public class SampleApplicationTests {
|
|||||||
@Test
|
@Test
|
||||||
public void lowercase() {
|
public void lowercase() {
|
||||||
assertThat(new TestRestTemplate().postForObject(
|
assertThat(new TestRestTemplate().postForObject(
|
||||||
"http://localhost:" + port + "/function", "{\"VALUE\":\"FOO\"}",
|
"http://localhost:" + port + "/test", "it works",
|
||||||
String.class)).isEqualTo("{\"value\":\"foo\"}");
|
String.class)).isEqualTo("it works!!!");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user