GH-266 Consolidated Adapter infrastructure
- Moved common logic into a new AbstractSpringFunctionAdapterInitializer - Modified Azure and AWS request handlers to extend from it - Deprecated both AzureSpringFunctionInitializer and SpringFunctionInitializer(AWS) Resolves #266
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
* Copyright 2017-2019 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.
|
||||
@@ -17,9 +17,7 @@
|
||||
package org.springframework.cloud.function.adapter.azure;
|
||||
|
||||
import java.lang.reflect.UndeclaredThrowableException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
|
||||
import com.microsoft.azure.functions.ExecutionContext;
|
||||
@@ -27,12 +25,15 @@ import com.microsoft.azure.functions.OutputBinding;
|
||||
import org.reactivestreams.Publisher;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
import org.springframework.cloud.function.context.AbstractSpringFunctionAdapterInitializer;
|
||||
|
||||
/**
|
||||
* @param <I> input type
|
||||
* @param <O> result type
|
||||
* @author Soby Chacko
|
||||
* @author Oleg Zhurakousky
|
||||
*/
|
||||
public class AzureSpringBootRequestHandler<I, O> extends AzureSpringFunctionInitializer {
|
||||
public class AzureSpringBootRequestHandler<I, O> extends AbstractSpringFunctionAdapterInitializer<ExecutionContext> {
|
||||
|
||||
public AzureSpringBootRequestHandler(Class<?> configurationClass) {
|
||||
super(configurationClass);
|
||||
@@ -51,10 +52,9 @@ public class AzureSpringBootRequestHandler<I, O> extends AzureSpringFunctionInit
|
||||
}
|
||||
initialize(context);
|
||||
|
||||
Function<Publisher<?>, Publisher<?>> function = lookup(name);
|
||||
Publisher<?> events = extract(function, convertEvent(input));
|
||||
Publisher<?> output = function.apply(events);
|
||||
return result(function, input, output);
|
||||
Publisher<?> events = extract(convertEvent(input));
|
||||
Publisher<?> output = apply(events);
|
||||
return result(input, output);
|
||||
}
|
||||
catch (Throwable ex) {
|
||||
if (context != null) {
|
||||
@@ -75,6 +75,11 @@ public class AzureSpringBootRequestHandler<I, O> extends AzureSpringFunctionInit
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String doResolveName(Object targetContext) {
|
||||
return ((ExecutionContext) targetContext).getFunctionName();
|
||||
}
|
||||
|
||||
public void handleOutput(I input, OutputBinding<O> binding,
|
||||
ExecutionContext context) {
|
||||
O result = handleRequest(input, context);
|
||||
@@ -85,8 +90,8 @@ public class AzureSpringBootRequestHandler<I, O> extends AzureSpringFunctionInit
|
||||
return input;
|
||||
}
|
||||
|
||||
private Flux<?> extract(Function<?, ?> function, Object input) {
|
||||
if (!isSingleInput(function, input)) {
|
||||
protected Flux<?> extract(Object input) {
|
||||
if (!isSingleInput(this.getFunction(), input)) {
|
||||
if (input instanceof Collection) {
|
||||
return Flux.fromIterable((Iterable<?>) input);
|
||||
}
|
||||
@@ -94,29 +99,31 @@ public class AzureSpringBootRequestHandler<I, O> extends AzureSpringFunctionInit
|
||||
return Flux.just(input);
|
||||
}
|
||||
|
||||
private O result(Function<?, ?> function, Object input, Publisher<?> output) {
|
||||
List<Object> result = new ArrayList<>();
|
||||
for (Object value : Flux.from(output).toIterable()) {
|
||||
result.add(convertOutput(value));
|
||||
}
|
||||
if (isSingleInput(function, input) && result.size() == 1) {
|
||||
@SuppressWarnings("unchecked")
|
||||
O value = (O) result.get(0);
|
||||
return value;
|
||||
}
|
||||
if (isSingleOutput(function, input) && result.size() == 1) {
|
||||
@SuppressWarnings("unchecked")
|
||||
O value = (O) result.get(0);
|
||||
return value;
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
O value = (O) result;
|
||||
return value;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected O convertOutput(Object output) {
|
||||
return (O) output;
|
||||
}
|
||||
|
||||
protected boolean isSingleInput(Function<?, ?> function, Object input) {
|
||||
if (!(input instanceof Collection)) {
|
||||
return true;
|
||||
}
|
||||
if (getInspector() != null) {
|
||||
return Collection.class
|
||||
.isAssignableFrom(getInspector().getInputType(function));
|
||||
}
|
||||
return ((Collection<?>) input).size() <= 1;
|
||||
}
|
||||
|
||||
protected boolean isSingleOutput(Function<?, ?> function, Object output) {
|
||||
if (!(output instanceof Collection)) {
|
||||
return true;
|
||||
}
|
||||
if (getInspector() != null) {
|
||||
return Collection.class
|
||||
.isAssignableFrom(getInspector().getOutputType(function));
|
||||
}
|
||||
return ((Collection<?>) output).size() <= 1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -35,6 +35,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.WebApplicationType;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.function.context.AbstractSpringFunctionAdapterInitializer;
|
||||
import org.springframework.cloud.function.context.FunctionCatalog;
|
||||
import org.springframework.cloud.function.context.catalog.FunctionInspector;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
@@ -42,7 +43,10 @@ import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
* @author Soby Chacko
|
||||
* @deprecated as of 2.1 in favor of {@link AbstractSpringFunctionAdapterInitializer}.
|
||||
* It is no longer used by the framework and only exists for avoiding potential regressions.
|
||||
*/
|
||||
@Deprecated
|
||||
public class AzureSpringFunctionInitializer implements Closeable {
|
||||
|
||||
private volatile static ConfigurableApplicationContext context;
|
||||
|
||||
@@ -1,124 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2019 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.adapter.azure;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
import org.springframework.boot.SpringBootConfiguration;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.cloud.function.context.FunctionRegistration;
|
||||
import org.springframework.cloud.function.context.FunctionType;
|
||||
import org.springframework.context.ApplicationContextInitializer;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.support.GenericApplicationContext;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class AzureSpringFunctionInitializerTests {
|
||||
|
||||
private AzureSpringFunctionInitializer handler = null;
|
||||
|
||||
<I, O> AzureSpringFunctionInitializer handler(Class<?> config) {
|
||||
AzureSpringFunctionInitializer handler = new AzureSpringFunctionInitializer(
|
||||
config);
|
||||
this.handler = handler;
|
||||
return handler;
|
||||
}
|
||||
|
||||
@After
|
||||
public void close() throws IOException {
|
||||
if (this.handler != null) {
|
||||
this.handler.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bareConfig() {
|
||||
AzureSpringFunctionInitializer handler = handler(BareConfig.class);
|
||||
handler.initialize(new TestExecutionContext("uppercase"));
|
||||
Bar bar = (Bar) Flux.from(handler.getFunction().apply(Flux.just(new Foo("bar"))))
|
||||
.blockFirst();
|
||||
assertThat(bar.getValue()).isEqualTo("BAR");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void initializer() {
|
||||
AzureSpringFunctionInitializer handler = handler(InitializerConfig.class);
|
||||
handler.initialize(new TestExecutionContext("uppercase"));
|
||||
Bar bar = (Bar) Flux.from(handler.getFunction().apply(Flux.just(new Foo("bar"))))
|
||||
.blockFirst();
|
||||
assertThat(bar.getValue()).isEqualTo("BAR");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void function() {
|
||||
AzureSpringFunctionInitializer handler = handler(FunctionConfig.class);
|
||||
handler.initialize(new TestExecutionContext("uppercase"));
|
||||
Bar bar = (Bar) Flux.from(handler.getFunction().apply(Flux.just(new Foo("bar"))))
|
||||
.blockFirst();
|
||||
assertThat(bar.getValue()).isEqualTo("BAR");
|
||||
}
|
||||
|
||||
@SpringBootConfiguration
|
||||
@EnableAutoConfiguration
|
||||
protected static class BareConfig {
|
||||
|
||||
@Bean
|
||||
public Function<Foo, Bar> function() {
|
||||
return foo -> new Bar(foo.getValue().toUpperCase());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@SpringBootConfiguration
|
||||
protected static class FunctionConfig implements Function<Foo, Bar> {
|
||||
|
||||
@Override
|
||||
public Bar apply(Foo foo) {
|
||||
return new Bar(foo.getValue().toUpperCase());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@SpringBootConfiguration
|
||||
protected static class InitializerConfig
|
||||
implements ApplicationContextInitializer<GenericApplicationContext> {
|
||||
|
||||
public Function<Foo, Bar> function() {
|
||||
return foo -> new Bar(foo.getValue().toUpperCase());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initialize(GenericApplicationContext context) {
|
||||
context.registerBean(FunctionRegistration.class,
|
||||
() -> new FunctionRegistration<Function<Foo, Bar>>(function(),
|
||||
"uppercase")
|
||||
.type(FunctionType.from(Foo.class).to(Bar.class)));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user