removing FileSystemFunctionRegistry

This commit is contained in:
markfisher
2017-01-11 20:59:18 -05:00
parent 69fc017565
commit cfd416590d
11 changed files with 9 additions and 312 deletions

View File

@@ -42,22 +42,4 @@
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>registrar</classifier>
</configuration>
<dependencies>
<dependency>
<groupId>org.springframework.boot.experimental</groupId>
<artifactId>spring-boot-thin-launcher</artifactId>
<version>${wrapper.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>

View File

@@ -1,48 +0,0 @@
/*
* 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) {
String usage = "USAGE: java FunctionRegistrar [supplier|function|consumer] name lambda";
if (args.length != 3) {
System.err.println(usage);
System.exit(1);
}
FileSystemFunctionRegistry registry = new FileSystemFunctionRegistry();
String type = args[0];
if ("supplier".equalsIgnoreCase(type)) {
registry.registerSupplier(args[1], args[2]);
}
else if ("function".equalsIgnoreCase(type)) {
registry.registerFunction(args[1], args[2]);
}
else if ("consumer".equalsIgnoreCase(type)) {
registry.registerConsumer(args[1], args[2]);
}
else {
System.err.println(usage);
System.exit(1);
}
}
}

View File

@@ -1,33 +0,0 @@
/*
* 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 org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConditionalOnClass(FileSystemFunctionRegistry.class)
@ConditionalOnMissingBean(FunctionCatalog.class)
public class DefaultFunctionRegistryAutoConfiguration {
@Bean
public FunctionRegistry functionRegistry() {
return new FileSystemFunctionRegistry();
}
}

View File

@@ -1,139 +0,0 @@
/*
* 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.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import org.springframework.cloud.function.compiler.CompiledFunctionFactory;
import org.springframework.util.Assert;
import org.springframework.util.FileCopyUtils;
/**
* @author Mark Fisher
*/
public class FileSystemFunctionRegistry extends AbstractFunctionRegistry {
private static final String CONSUMER_DIRECTORY = "consumers";
private static final String FUNCTION_DIRECTORY = "functions";
private static final String SUPPLIER_DIRECTORY = "suppliers";
private final File consumerDirectory;
private final File functionDirectory;
private final File supplierDirectory;
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.consumerDirectory = new File(directory, CONSUMER_DIRECTORY);
this.functionDirectory = new File(directory, FUNCTION_DIRECTORY);
this.supplierDirectory = new File(directory, SUPPLIER_DIRECTORY);
this.consumerDirectory.mkdir();
this.functionDirectory.mkdir();
this.supplierDirectory.mkdir();
}
@Override
public <T> Consumer<T> doLookupConsumer(String name) {
try {
byte[] bytes = FileCopyUtils.copyToByteArray(new File(this.consumerDirectory, fileName(name)));
return this.deserializeConsumer(name, bytes);
}
catch (IOException e) {
throw new IllegalArgumentException(String.format("failed to lookup Consumer: %s", name), e);
}
}
@Override
public <T, R> Function<T, R> doLookupFunction(String name) {
try {
byte[] bytes = FileCopyUtils.copyToByteArray(new File(this.functionDirectory, fileName(name)));
return this.deserializeFunction(name, bytes);
}
catch (IOException e) {
throw new IllegalArgumentException(String.format("failed to lookup Function: %s", name), e);
}
}
@Override
public <T> Supplier<T> doLookupSupplier(String name) {
try {
byte[] bytes = FileCopyUtils.copyToByteArray(new File(this.supplierDirectory, fileName(name)));
return this.deserializeSupplier(name, bytes);
}
catch (IOException e) {
throw new IllegalArgumentException(String.format("failed to lookup Supplier: %s", name), e);
}
}
@Override
public void registerConsumer(String name, String consumer) {
CompiledFunctionFactory<?> factory = this.compileConsumer(name, consumer);
File file = new File(this.consumerDirectory, fileName(name));
try {
FileCopyUtils.copy(factory.getGeneratedClassBytes(), file);
}
catch (IOException e) {
throw new IllegalArgumentException(String.format("failed to register Consumer: %s", name), e);
}
}
@Override
public void registerFunction(String name, String function) {
CompiledFunctionFactory<?> factory = this.compileFunction(name, function);
File file = new File(this.functionDirectory, fileName(name));
try {
FileCopyUtils.copy(factory.getGeneratedClassBytes(), file);
}
catch (IOException e) {
throw new IllegalArgumentException(String.format("failed to register Function: %s", name), e);
}
}
@Override
public void registerSupplier(String name, String supplier) {
CompiledFunctionFactory<?> factory = this.compileSupplier(name, supplier);
File file = new File(this.supplierDirectory, fileName(name));
try {
FileCopyUtils.copy(factory.getGeneratedClassBytes(), file);
}
catch (IOException e) {
throw new IllegalArgumentException(String.format("failed to register Supplier: %s", name), e);
}
}
private static String fileName(String functionName) {
return String.format("%s.%s", functionName, "fun");
}
}

View File

@@ -1,2 +0,0 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.cloud.function.registry.DefaultFunctionRegistryAutoConfiguration

View File

@@ -23,8 +23,8 @@ import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.springframework.cloud.function.registry.FileSystemFunctionRegistry;
import org.springframework.cloud.function.registry.FunctionRegistry;
import org.springframework.cloud.function.registry.InMemoryFunctionRegistry;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import reactor.core.publisher.Flux;
@@ -34,7 +34,7 @@ import reactor.core.publisher.Flux;
*/
public class LocalFunctionGatewayTests {
private final FunctionRegistry registry = new FileSystemFunctionRegistry();
private final FunctionRegistry registry = new InMemoryFunctionRegistry();
private final ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();

View File

@@ -1,56 +0,0 @@
/*
* 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.List;
import java.util.function.Function;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
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 registerAndLookupFunction() throws IOException {
FileSystemFunctionRegistry registry = new FileSystemFunctionRegistry(this.directory);
registry.registerFunction("uppercase", "f->f.map(s->s.toString().toUpperCase())");
Function<Flux<String>, Flux<String>> function = registry.lookupFunction("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));
}
}