diff --git a/spring-cloud-function-core/src/main/java/org/springframework/cloud/function/support/LambdaCompilingFunction.java b/spring-cloud-function-core/src/main/java/org/springframework/cloud/function/support/LambdaCompilingFunction.java new file mode 100644 index 000000000..5e01d1a7d --- /dev/null +++ b/spring-cloud-function-core/src/main/java/org/springframework/cloud/function/support/LambdaCompilingFunction.java @@ -0,0 +1,66 @@ +/* + * 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.support; + +import java.io.InputStreamReader; +import java.util.function.Function; + +import org.springframework.beans.factory.BeanNameAware; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.cloud.function.compiler.CompiledFunctionFactory; +import org.springframework.cloud.function.compiler.FunctionCompiler; +import org.springframework.core.io.Resource; +import org.springframework.util.Assert; +import org.springframework.util.FileCopyUtils; + +/** + * @author Mark Fisher + */ +public class LambdaCompilingFunction implements Function, InitializingBean, BeanNameAware { + + private final Resource resource; + + private final FunctionCompiler compiler; + + private String beanName; + + private Function compiledFunction; + + public LambdaCompilingFunction(Resource resource, FunctionCompiler compiler) { + Assert.notNull(resource, "Resource must not be null"); + Assert.notNull(compiler, "FunctionCompiler must not be null"); + this.resource = resource; + this.compiler = compiler; + } + + @Override + public void setBeanName(String beanName) { + this.beanName = beanName; + } + + @Override + public void afterPropertiesSet() throws Exception { + String lambda = FileCopyUtils.copyToString(new InputStreamReader(this.resource.getInputStream())); + CompiledFunctionFactory> factory = this.compiler.compile(this.beanName, lambda); + this.compiledFunction = factory.getResult(); + } + + @Override + public R apply(T input) { + return this.compiledFunction.apply(input); + } +} diff --git a/spring-cloud-function-samples/spring-cloud-function-sample/src/main/java/com/example/SampleApplication.java b/spring-cloud-function-samples/spring-cloud-function-sample/src/main/java/com/example/SampleApplication.java index a340236dd..6c7c3ba28 100644 --- a/spring-cloud-function-samples/spring-cloud-function-sample/src/main/java/com/example/SampleApplication.java +++ b/spring-cloud-function-samples/spring-cloud-function-sample/src/main/java/com/example/SampleApplication.java @@ -20,7 +20,10 @@ import java.util.function.Supplier; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.function.compiler.FunctionCompiler; +import org.springframework.cloud.function.support.LambdaCompilingFunction; import org.springframework.context.annotation.Bean; +import org.springframework.core.io.ByteArrayResource; import reactor.core.publisher.Flux; @@ -42,6 +45,17 @@ public class SampleApplication { return flux -> flux.map(value -> value.toLowerCase()); } + @Bean + public FunctionCompiler compiler() { + return new FunctionCompiler<>(); + } + + @Bean + public Function, Flux> compiledUppercase(FunctionCompiler, Flux> compiler) { + String lambda = "f -> f.map(o -> o.toString().toUpperCase())"; + return new LambdaCompilingFunction<>(new ByteArrayResource(lambda.getBytes()), compiler); + } + public static void main(String[] args) throws Exception { SpringApplication.run(SampleApplication.class, args); }