avoid duplicate linkage errors
This commit is contained in:
@@ -16,6 +16,8 @@
|
|||||||
|
|
||||||
package org.springframework.cloud.function.registry;
|
package org.springframework.cloud.function.registry;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
import org.springframework.cloud.function.compiler.CompiledFunctionFactory;
|
import org.springframework.cloud.function.compiler.CompiledFunctionFactory;
|
||||||
@@ -27,11 +29,13 @@ import org.springframework.util.Assert;
|
|||||||
/**
|
/**
|
||||||
* @author Mark Fisher
|
* @author Mark Fisher
|
||||||
*/
|
*/
|
||||||
public abstract class FunctionRegistrySupport implements FunctionRegistry {
|
public abstract class AbstractFunctionRegistry implements FunctionRegistry {
|
||||||
|
|
||||||
|
private static final Map<String, FunctionFactory<?, ?>> FACTORY_CACHE = new HashMap<>();
|
||||||
|
|
||||||
private final FunctionCompiler compiler = new FunctionCompiler();
|
private final FunctionCompiler compiler = new FunctionCompiler();
|
||||||
|
|
||||||
private final SimpleClassLoader classLoader = new SimpleClassLoader(FunctionRegistrySupport.class.getClassLoader());
|
private final SimpleClassLoader classLoader = new SimpleClassLoader(AbstractFunctionRegistry.class.getClassLoader());
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@@ -44,19 +48,33 @@ public abstract class FunctionRegistrySupport implements FunctionRegistry {
|
|||||||
return function;
|
return function;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public final <T, R> Function<T, R> lookup(String name) {
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
FunctionFactory<T, R> factory = (FunctionFactory<T, R>) FACTORY_CACHE.get(name);
|
||||||
|
if (factory != null) {
|
||||||
|
return factory.getFunction();
|
||||||
|
}
|
||||||
|
return this.doLookup(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract <T, R> Function<T, R> doLookup(String name);
|
||||||
|
|
||||||
protected <T, R> CompiledFunctionFactory<T, R> compile(String name, String code) {
|
protected <T, R> CompiledFunctionFactory<T, R> compile(String name, String code) {
|
||||||
return this.compiler.compile(name, code);
|
return this.compiler.compile(name, code);
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
protected <T, R> Function<T, R> deserialize(String name, byte[] bytes) {
|
protected <T, R> Function<T, R> deserialize(final String name, byte[] bytes) {
|
||||||
Assert.hasLength(name, "name must not be empty");
|
Assert.hasLength(name, "name must not be empty");
|
||||||
String firstLetter = name.substring(0, 1).toUpperCase();
|
String firstLetter = name.substring(0, 1).toUpperCase();
|
||||||
name = (name.length() > 1) ? firstLetter + name.substring(1) : firstLetter;
|
String upperCasedName = (name.length() > 1) ? firstLetter + name.substring(1) : firstLetter;
|
||||||
String className = String.format("%s.%sFunctionFactory", FunctionCompiler.class.getPackage().getName(), name);
|
String className = String.format("%s.%sFunctionFactory", FunctionCompiler.class.getPackage().getName(), upperCasedName);
|
||||||
Class<?> factoryClass = this.classLoader.defineClass(className, bytes);
|
Class<?> factoryClass = this.classLoader.defineClass(className, bytes);
|
||||||
try {
|
try {
|
||||||
return ((FunctionFactory<T, R>) factoryClass.newInstance()).getFunction();
|
FunctionFactory<T, R> factory = ((FunctionFactory<T, R>) factoryClass.newInstance());
|
||||||
|
FACTORY_CACHE.put(name, factory);
|
||||||
|
return factory.getFunction();
|
||||||
}
|
}
|
||||||
catch (InstantiationException | IllegalAccessException e) {
|
catch (InstantiationException | IllegalAccessException e) {
|
||||||
throw new IllegalArgumentException("failed to deserialize function", e);
|
throw new IllegalArgumentException("failed to deserialize function", e);
|
||||||
@@ -27,7 +27,7 @@ import org.springframework.util.FileCopyUtils;
|
|||||||
/**
|
/**
|
||||||
* @author Mark Fisher
|
* @author Mark Fisher
|
||||||
*/
|
*/
|
||||||
public class FileSystemFunctionRegistry extends FunctionRegistrySupport {
|
public class FileSystemFunctionRegistry extends AbstractFunctionRegistry {
|
||||||
|
|
||||||
private final File directory;
|
private final File directory;
|
||||||
|
|
||||||
@@ -48,7 +48,7 @@ public class FileSystemFunctionRegistry extends FunctionRegistrySupport {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T, R> Function<T, R> lookup(String name) {
|
public <T, R> Function<T, R> doLookup(String name) {
|
||||||
try {
|
try {
|
||||||
byte[] bytes = FileCopyUtils.copyToByteArray(new File(this.directory, fileName(name)));
|
byte[] bytes = FileCopyUtils.copyToByteArray(new File(this.directory, fileName(name)));
|
||||||
return this.deserialize(name, bytes);
|
return this.deserialize(name, bytes);
|
||||||
|
|||||||
@@ -22,13 +22,13 @@ import java.util.function.Function;
|
|||||||
/**
|
/**
|
||||||
* @author Mark Fisher
|
* @author Mark Fisher
|
||||||
*/
|
*/
|
||||||
public class InMemoryFunctionRegistry extends FunctionRegistrySupport {
|
public class InMemoryFunctionRegistry extends AbstractFunctionRegistry {
|
||||||
|
|
||||||
private final ConcurrentHashMap<String, Function<?, ?>> map = new ConcurrentHashMap<>();
|
private final ConcurrentHashMap<String, Function<?, ?>> map = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public Function<?, ?> lookup(String name) {
|
public Function<?, ?> doLookup(String name) {
|
||||||
return this.map.get(name);
|
return this.map.get(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,61 @@
|
|||||||
|
/*
|
||||||
|
* 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.gateway;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
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.scheduling.concurrent.ThreadPoolTaskScheduler;
|
||||||
|
|
||||||
|
import reactor.core.publisher.Flux;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Mark Fisher
|
||||||
|
*/
|
||||||
|
public class LocalFunctionGatewayTests {
|
||||||
|
|
||||||
|
private final FunctionRegistry registry = new FileSystemFunctionRegistry();
|
||||||
|
|
||||||
|
private final ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void init() {
|
||||||
|
this.scheduler.initialize();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test() {
|
||||||
|
LocalFunctionGateway gateway = new LocalFunctionGateway(registry, scheduler);
|
||||||
|
Flux<String> output = gateway.invoke("uppercase", Flux.just("foo", "bar"));
|
||||||
|
List<String> results = output.collectList().block();
|
||||||
|
assertEquals("FOO", results.get(0));
|
||||||
|
assertEquals("BAR", results.get(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testMultiple() {
|
||||||
|
for (int i = 0; i < 100; i++) {
|
||||||
|
test();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user