generate name per function to allow composition

This commit is contained in:
markfisher
2016-10-01 16:32:28 -04:00
parent 309993f0d4
commit b78024e7ea
7 changed files with 106 additions and 24 deletions

View File

@@ -51,7 +51,7 @@ public class FileSystemFunctionRegistry extends FunctionRegistrySupport {
public <T, R> Function<T, R> lookup(String name) {
try {
byte[] bytes = FileCopyUtils.copyToByteArray(new File(this.directory, fileName(name)));
return this.deserialize(bytes);
return this.deserialize(name, bytes);
}
catch (IOException e) {
throw new IllegalArgumentException(String.format("failed to lookup function: %s", name), e);
@@ -59,7 +59,7 @@ public class FileSystemFunctionRegistry extends FunctionRegistrySupport {
}
public void register(String name, String function) {
CompiledFunctionFactory<?, ?> factory = this.compile(function);
CompiledFunctionFactory<?, ?> factory = this.compile(name, function);
File file = new File(this.directory, fileName(name));
try {
FileCopyUtils.copy(factory.getGeneratedClassBytes(), file);

View File

@@ -22,6 +22,7 @@ import org.springframework.cloud.function.compiler.CompiledFunctionFactory;
import org.springframework.cloud.function.compiler.FunctionCompiler;
import org.springframework.cloud.function.compiler.FunctionFactory;
import org.springframework.cloud.function.compiler.java.SimpleClassLoader;
import org.springframework.util.Assert;
/**
* @author Mark Fisher
@@ -43,13 +44,17 @@ public abstract class FunctionRegistrySupport implements FunctionRegistry {
return function;
}
protected <T, R> CompiledFunctionFactory<T, R> compile(String code) {
return this.compiler.compile(code);
protected <T, R> CompiledFunctionFactory<T, R> compile(String name, String code) {
return this.compiler.compile(name, code);
}
@SuppressWarnings("unchecked")
protected <T, R> Function<T, R> deserialize(byte[] bytes) {
Class<?> factoryClass = this.classLoader.defineClass(FunctionCompiler.GENERATED_FUNCTION_FACTORY_CLASS_NAME, bytes);
protected <T, R> Function<T, R> deserialize(String name, byte[] bytes) {
Assert.hasLength(name, "name must not be empty");
String firstLetter = name.substring(0, 1).toUpperCase();
name = (name.length() > 1) ? firstLetter + name.substring(1) : firstLetter;
String className = String.format("%s.%sFunctionFactory", FunctionCompiler.class.getPackage().getName(), name);
Class<?> factoryClass = this.classLoader.defineClass(className, bytes);
try {
return ((FunctionFactory<T, R>) factoryClass.newInstance()).getFunction();
}

View File

@@ -34,6 +34,6 @@ public class InMemoryFunctionRegistry extends FunctionRegistrySupport {
@Override
public void register(String name, String function) {
this.map.put(name, this.compile(function).getFunction());
this.map.put(name, this.compile(name, function).getFunction());
}
}

View File

@@ -0,0 +1,67 @@
/*
* Copyright 2016 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.registry;
import static org.junit.Assert.assertEquals;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.function.Function;
import org.junit.Before;
import org.junit.Test;
import reactor.core.publisher.Flux;
/**
* @author Mark Fisher
*/
public class FileSystemFunctionRegistryTests {
private File directory;
@Before
public void init() throws IOException {
this.directory = new File("/tmp/file-system-function-registry-tests");
this.directory.mkdirs();
this.directory.deleteOnExit();
}
@Test
public void registerAndLookup() throws IOException {
FileSystemFunctionRegistry registry = new FileSystemFunctionRegistry(this.directory);
registry.register("uppercase", "f->f.map(s->s.toString().toUpperCase())");
Function<Flux<String>, Flux<String>> function = registry.lookup("uppercase");
Flux<String> output = function.apply(Flux.just("foo", "bar"));
List<String> results = output.collectList().block();
assertEquals("FOO", results.get(0));
assertEquals("BAR", results.get(1));
}
@Test
public void compose() throws IOException {
FileSystemFunctionRegistry registry = new FileSystemFunctionRegistry(this.directory);
registry.register("uppercase", "f->f.map(s->s.toString().toUpperCase())");
registry.register("exclaim", "f->f.map(s->s+\"!!!\")");
Function<Flux<String>, Flux<String>> function = registry.compose("uppercase", "exclaim");
Flux<String> output = function.apply(Flux.just("foo", "bar"));
List<String> results = output.collectList().block();
assertEquals("FOO!!!", results.get(0));
assertEquals("BAR!!!", results.get(1));
}
}