diff --git a/spring-cloud-function-compiler/src/main/java/org/springframework/cloud/function/compiler/AbstractFunctionCompiler.java b/spring-cloud-function-compiler/src/main/java/org/springframework/cloud/function/compiler/AbstractFunctionCompiler.java index 7471306e0..ad563a5fe 100644 --- a/spring-cloud-function-compiler/src/main/java/org/springframework/cloud/function/compiler/AbstractFunctionCompiler.java +++ b/spring-cloud-function-compiler/src/main/java/org/springframework/cloud/function/compiler/AbstractFunctionCompiler.java @@ -31,7 +31,7 @@ import org.springframework.cloud.function.compiler.java.RuntimeJavaCompiler; * @author Andy Clement * @author Mark Fisher */ -abstract class AbstractFunctionCompiler { +public abstract class AbstractFunctionCompiler { private static Logger logger = LoggerFactory.getLogger(AbstractFunctionCompiler.class); diff --git a/spring-cloud-function-compiler/src/main/java/org/springframework/cloud/function/compiler/proxy/AbstractLambdaCompilingProxy.java b/spring-cloud-function-compiler/src/main/java/org/springframework/cloud/function/compiler/proxy/AbstractLambdaCompilingProxy.java new file mode 100644 index 000000000..b77df9b52 --- /dev/null +++ b/spring-cloud-function-compiler/src/main/java/org/springframework/cloud/function/compiler/proxy/AbstractLambdaCompilingProxy.java @@ -0,0 +1,64 @@ +/* + * 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.proxy; + +import java.io.InputStreamReader; + +import org.springframework.beans.factory.BeanNameAware; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.cloud.function.compiler.AbstractFunctionCompiler; +import org.springframework.cloud.function.compiler.CompiledFunctionFactory; +import org.springframework.core.io.Resource; +import org.springframework.util.Assert; +import org.springframework.util.FileCopyUtils; + +/** + * @author Mark Fisher + */ +public class AbstractLambdaCompilingProxy implements InitializingBean, BeanNameAware { + + private final Resource resource; + + private final AbstractFunctionCompiler compiler; + + private String beanName; + + private T target; + + public AbstractLambdaCompilingProxy(Resource resource, AbstractFunctionCompiler compiler) { + Assert.notNull(resource, "Resource must not be null"); + Assert.notNull(compiler, "Compiler 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.target = factory.getResult(); + } + + protected final T getTarget() { + return this.target; + } +} diff --git a/spring-cloud-function-compiler/src/main/java/org/springframework/cloud/function/compiler/proxy/LambdaCompilingConsumer.java b/spring-cloud-function-compiler/src/main/java/org/springframework/cloud/function/compiler/proxy/LambdaCompilingConsumer.java new file mode 100644 index 000000000..dcd26ffab --- /dev/null +++ b/spring-cloud-function-compiler/src/main/java/org/springframework/cloud/function/compiler/proxy/LambdaCompilingConsumer.java @@ -0,0 +1,37 @@ +/* + * 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.proxy; + +import java.util.function.Consumer; + +import org.springframework.cloud.function.compiler.ConsumerCompiler; +import org.springframework.core.io.Resource; + +/** + * @author Mark Fisher + */ +public class LambdaCompilingConsumer extends AbstractLambdaCompilingProxy> implements Consumer { + + public LambdaCompilingConsumer(Resource resource, ConsumerCompiler compiler) { + super(resource, compiler); + } + + @Override + public void accept(T input) { + this.getTarget().accept(input); + } +} diff --git a/spring-cloud-function-compiler/src/main/java/org/springframework/cloud/function/compiler/proxy/LambdaCompilingFunction.java b/spring-cloud-function-compiler/src/main/java/org/springframework/cloud/function/compiler/proxy/LambdaCompilingFunction.java index f5c930c3a..855231919 100644 --- a/spring-cloud-function-compiler/src/main/java/org/springframework/cloud/function/compiler/proxy/LambdaCompilingFunction.java +++ b/spring-cloud-function-compiler/src/main/java/org/springframework/cloud/function/compiler/proxy/LambdaCompilingFunction.java @@ -16,51 +16,22 @@ package org.springframework.cloud.function.compiler.proxy; -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 class LambdaCompilingFunction extends AbstractLambdaCompilingProxy> implements Function { 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(); + super(resource, compiler); } @Override public R apply(T input) { - return this.compiledFunction.apply(input); + return this.getTarget().apply(input); } } diff --git a/spring-cloud-function-compiler/src/main/java/org/springframework/cloud/function/compiler/proxy/LambdaCompilingSupplier.java b/spring-cloud-function-compiler/src/main/java/org/springframework/cloud/function/compiler/proxy/LambdaCompilingSupplier.java new file mode 100644 index 000000000..a4996f70c --- /dev/null +++ b/spring-cloud-function-compiler/src/main/java/org/springframework/cloud/function/compiler/proxy/LambdaCompilingSupplier.java @@ -0,0 +1,37 @@ +/* + * 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.proxy; + +import java.util.function.Supplier; + +import org.springframework.cloud.function.compiler.SupplierCompiler; +import org.springframework.core.io.Resource; + +/** + * @author Mark Fisher + */ +public class LambdaCompilingSupplier extends AbstractLambdaCompilingProxy> implements Supplier { + + public LambdaCompilingSupplier(Resource resource, SupplierCompiler compiler) { + super(resource, compiler); + } + + @Override + public T get() { + return this.getTarget().get(); + } +}