file-based registry for serialized functions

This commit is contained in:
markfisher
2016-09-30 18:12:43 -04:00
parent 257cf8c356
commit 309993f0d4
18 changed files with 267 additions and 99 deletions

View File

@@ -0,0 +1,34 @@
/*
* 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;
import org.springframework.cloud.function.registry.FileSystemFunctionRegistry;
/**
* @author Mark Fisher
*/
public class FunctionRegistrar {
public static void main(String[] args) {
if (args.length != 2) {
System.err.println("USAGE: java FunctionRegistrar functionName functionBody");
System.exit(1);
}
FileSystemFunctionRegistry registry = new FileSystemFunctionRegistry();
registry.register(args[0], args[1]);
}
}

View File

@@ -0,0 +1,75 @@
/*
* 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 java.io.File;
import java.io.IOException;
import java.util.function.Function;
import org.springframework.cloud.function.compiler.CompiledFunctionFactory;
import org.springframework.util.Assert;
import org.springframework.util.FileCopyUtils;
/**
* @author Mark Fisher
*/
public class FileSystemFunctionRegistry extends FunctionRegistrySupport {
private final File directory;
public FileSystemFunctionRegistry() {
this(new File("/tmp/function-registry"));
}
public FileSystemFunctionRegistry(File directory) {
Assert.notNull(directory, "Directory must not be null");
if (!directory.exists()) {
directory.mkdirs();
}
else {
Assert.isTrue(directory.isDirectory(),
String.format("%s is not a directory.", directory.getAbsolutePath()));
}
this.directory = directory;
}
@Override
public <T, R> Function<T, R> lookup(String name) {
try {
byte[] bytes = FileCopyUtils.copyToByteArray(new File(this.directory, fileName(name)));
return this.deserialize(bytes);
}
catch (IOException e) {
throw new IllegalArgumentException(String.format("failed to lookup function: %s", name), e);
}
}
public void register(String name, String function) {
CompiledFunctionFactory<?, ?> factory = this.compile(function);
File file = new File(this.directory, fileName(name));
try {
FileCopyUtils.copy(factory.getGeneratedClassBytes(), file);
}
catch (IOException e) {
throw new IllegalArgumentException(String.format("failed to register function: %s", name), e);
}
}
private static String fileName(String functionName) {
return String.format("%s.%s", functionName, "fun");
}
}

View File

@@ -23,14 +23,10 @@ import java.util.function.Function;
*/
public interface FunctionRegistry {
<T, R> Function<T, R> lookup(String name);
void register(String name, Function<?, ?> function);
void register(String name, String function);
void compose(String composedFunctionName, Function<?, ?>... functions);
<T, R> Function<T, R> lookup(String name);
void compose(String composedFunctionName, String... functionNames);
<T, R> Function<T, R> compose(String... functionNames);
}

View File

@@ -18,8 +18,10 @@ package org.springframework.cloud.function.registry;
import java.util.function.Function;
import org.springframework.cloud.function.compiler.CompiledFunctionFactory;
import org.springframework.cloud.function.compiler.FunctionCompiler;
import org.springframework.util.Assert;
import org.springframework.cloud.function.compiler.FunctionFactory;
import org.springframework.cloud.function.compiler.java.SimpleClassLoader;
/**
* @author Mark Fisher
@@ -28,31 +30,31 @@ public abstract class FunctionRegistrySupport implements FunctionRegistry {
private final FunctionCompiler compiler = new FunctionCompiler();
private final SimpleClassLoader classLoader = new SimpleClassLoader(FunctionRegistrySupport.class.getClassLoader());
@Override
public void register(String name, String code) {
Function<?, ?> function = compiler.compile(code);
this.register(name, function);
}
@SuppressWarnings("unchecked")
public void compose(String name, Function<?, ?>... functions) {
Assert.isTrue(functions != null && functions.length > 1, "more than one Function is required");
@SuppressWarnings("rawtypes")
Function function = functions[0];
for (int i = 1; i < functions.length; i++) {
function = function.andThen(functions[i]);
}
this.register(name, function);
}
@SuppressWarnings("unchecked")
public void compose(String composedFunctionName, String... functionNames) {
Assert.isTrue(functionNames != null && functionNames.length > 1, "more than one Function is required");
public <T, R> Function<T, R> compose(String... functionNames) {
@SuppressWarnings("rawtypes")
Function function = this.lookup(functionNames[0]);
for (int i = 1; i < functionNames.length; i++) {
function = function.andThen(this.lookup(functionNames[i]));
}
this.register(composedFunctionName, function);
return function;
}
protected <T, R> CompiledFunctionFactory<T, R> compile(String code) {
return this.compiler.compile(code);
}
@SuppressWarnings("unchecked")
protected <T, R> Function<T, R> deserialize(byte[] bytes) {
Class<?> factoryClass = this.classLoader.defineClass(FunctionCompiler.GENERATED_FUNCTION_FACTORY_CLASS_NAME, bytes);
try {
return ((FunctionFactory<T, R>) factoryClass.newInstance()).getFunction();
}
catch (InstantiationException | IllegalAccessException e) {
throw new IllegalArgumentException("failed to deserialize function", e);
}
}
}

View File

@@ -27,12 +27,13 @@ public class InMemoryFunctionRegistry extends FunctionRegistrySupport {
private final ConcurrentHashMap<String, Function<?, ?>> map = new ConcurrentHashMap<>();
@Override
@SuppressWarnings("unchecked")
public Function<?, ?> lookup(String name) {
return this.map.get(name);
}
@Override
public void register(String name, Function<?, ?> function) {
this.map.put(name, function);
public void register(String name, String function) {
this.map.put(name, this.compile(function).getFunction());
}
}