Migrate to servlet binder for web features

This commit is contained in:
Dave Syer
2017-08-08 08:27:04 +01:00
parent 540b4d378e
commit 1af0d451cf
107 changed files with 4055 additions and 2010 deletions

View File

@@ -0,0 +1,31 @@
/*
* Copyright 2016-2017 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.context.catalog;
import org.springframework.context.ApplicationEvent;
/**
* @author Dave Syer
*
*/
@SuppressWarnings("serial")
public class FunctionCatalogEvent extends ApplicationEvent {
public FunctionCatalogEvent(Object source) {
super(source);
}
}

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.cloud.function.context;
package org.springframework.cloud.function.context.catalog;
import java.lang.reflect.Type;
import java.util.Optional;

View File

@@ -0,0 +1,45 @@
/*
* Copyright 2016-2017 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.context.catalog;
import java.util.HashSet;
import java.util.Set;
/**
* @author Dave Syer
*
*/
@SuppressWarnings("serial")
public class FunctionRegistrationEvent extends FunctionCatalogEvent {
private final Class<?> type;
private final Set<String> names;
public FunctionRegistrationEvent(Object source, Class<?> type, Set<String> names) {
super(source);
this.type = type;
this.names = new HashSet<>(names);
}
public Class<?> getType() {
return type;
}
public Set<String> getNames() {
return names;
}
}

View File

@@ -0,0 +1,45 @@
/*
* Copyright 2016-2017 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.context.catalog;
import java.util.HashSet;
import java.util.Set;
/**
* @author Dave Syer
*
*/
@SuppressWarnings("serial")
public class FunctionUnregistrationEvent extends FunctionCatalogEvent {
private final Class<?> type;
private final Set<String> names;
public FunctionUnregistrationEvent(Object source, Class<?> type, Set<String> names) {
super(source);
this.type = type;
this.names = new HashSet<>(names);
}
public Class<?> getType() {
return type;
}
public Set<String> getNames() {
return names;
}
}

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.cloud.function.context;
package org.springframework.cloud.function.context.catalog;
import java.util.Collections;
import java.util.HashMap;
@@ -24,7 +24,14 @@ import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.function.context.FunctionRegistration;
import org.springframework.cloud.function.context.FunctionRegistry;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.util.Assert;
/**
@@ -32,7 +39,8 @@ import org.springframework.util.Assert;
* @author Mark Fisher
* @author Oleg Zhurakousky
*/
public class InMemoryFunctionCatalog implements FunctionRegistry {
public class InMemoryFunctionCatalog
implements FunctionRegistry, ApplicationEventPublisherAware {
private final Map<String, Function<?, ?>> functions;
@@ -40,11 +48,13 @@ public class InMemoryFunctionCatalog implements FunctionRegistry {
private final Map<String, Supplier<?>> suppliers;
@Autowired(required = false)
private ApplicationEventPublisher publisher;
public InMemoryFunctionCatalog() {
this(Collections.emptySet());
}
@Autowired(required=false)
public InMemoryFunctionCatalog(Set<FunctionRegistration<?>> registrations) {
Assert.notNull(registrations, "'registrations' must not be null");
this.suppliers = new HashMap<>();
@@ -66,20 +76,68 @@ public class InMemoryFunctionCatalog implements FunctionRegistry {
@Override
public <T> void register(FunctionRegistration<T> registration) {
Map<String, ?> values = null;
FunctionRegistrationEvent event;
if (registration.getTarget() instanceof Function) {
values = this.functions;
event = new FunctionRegistrationEvent(this, Function.class,
registration.getNames());
}
else if (registration.getTarget() instanceof Supplier) {
values = this.suppliers;
event = new FunctionRegistrationEvent(this, Supplier.class,
registration.getNames());
}
else {
values = this.consumers;
event = new FunctionRegistrationEvent(this, Consumer.class,
registration.getNames());
}
@SuppressWarnings("unchecked")
Map<String, Object> map = (Map<String, Object>) values;
for (String name : registration.getNames()) {
map.put(name, registration.getTarget());
}
publisher.publishEvent(event);
}
public void setApplicationEventPublisher(ApplicationEventPublisher publisher) {
this.publisher = publisher;
}
@PostConstruct
public void init() {
if (publisher != null) {
if (!functions.isEmpty()) {
publisher.publishEvent(new FunctionRegistrationEvent(this, Function.class,
functions.keySet()));
}
if (!consumers.isEmpty()) {
publisher.publishEvent(new FunctionRegistrationEvent(this, Consumer.class,
consumers.keySet()));
}
if (!suppliers.isEmpty()) {
publisher.publishEvent(new FunctionRegistrationEvent(this, Supplier.class,
suppliers.keySet()));
}
}
}
@PreDestroy
public void close() {
if (publisher != null) {
if (!functions.isEmpty()) {
publisher.publishEvent(new FunctionUnregistrationEvent(this,
Function.class, functions.keySet()));
}
if (!consumers.isEmpty()) {
publisher.publishEvent(new FunctionUnregistrationEvent(this,
Consumer.class, consumers.keySet()));
}
if (!suppliers.isEmpty()) {
publisher.publishEvent(new FunctionUnregistrationEvent(this,
Supplier.class, suppliers.keySet()));
}
}
}
@Override

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.cloud.function.context;
package org.springframework.cloud.function.context.config;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
@@ -34,6 +34,8 @@ import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import javax.annotation.PreDestroy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.BeanDefinition;
@@ -42,6 +44,13 @@ import org.springframework.beans.factory.config.ConstructorArgumentValues.ValueH
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cloud.function.context.FunctionRegistration;
import org.springframework.cloud.function.context.FunctionRegistry;
import org.springframework.cloud.function.context.FunctionScan;
import org.springframework.cloud.function.context.catalog.FunctionInspector;
import org.springframework.cloud.function.context.catalog.FunctionRegistrationEvent;
import org.springframework.cloud.function.context.catalog.FunctionUnregistrationEvent;
import org.springframework.cloud.function.context.catalog.InMemoryFunctionCatalog;
import org.springframework.cloud.function.core.FluxConsumer;
import org.springframework.cloud.function.core.FluxFunction;
import org.springframework.cloud.function.core.FluxSupplier;
@@ -51,6 +60,7 @@ import org.springframework.cloud.function.core.FunctionFactoryUtils;
import org.springframework.cloud.function.core.IsolatedConsumer;
import org.springframework.cloud.function.core.IsolatedFunction;
import org.springframework.cloud.function.core.IsolatedSupplier;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.ResolvableType;
@@ -112,32 +122,38 @@ public class ContextFunctionCatalogAutoConfiguration {
processor.register(registration);
}
@Override
@SuppressWarnings("unchecked")
public <T> Supplier<T> lookupSupplier(String name) {
Supplier<T> result = (Supplier<T>) processor.lookupSupplier(name);
return result;
}
@Override
@SuppressWarnings("unchecked")
public <T, R> Function<T, R> lookupFunction(String name) {
Function<T, R> result = (Function<T, R>) processor.lookupFunction(name);
return result;
}
@Override
@SuppressWarnings("unchecked")
public <T> Consumer<T> lookupConsumer(String name) {
Consumer<T> result = (Consumer<T>) processor.lookupConsumer(name);
return result;
}
@Override
public Set<String> getSupplierNames() {
return this.processor.getSuppliers();
}
@Override
public Set<String> getFunctionNames() {
return this.processor.getFunctions();
}
@Override
public Set<String> getConsumerNames() {
return this.processor.getConsumers();
}
@@ -202,6 +218,8 @@ public class ContextFunctionCatalogAutoConfiguration {
private Map<String, Object> consumers = new HashMap<>();
@Autowired(required = false)
private ApplicationEventPublisher publisher;
@Autowired
private ConfigurableListableBeanFactory registry;
@@ -339,6 +357,24 @@ public class ContextFunctionCatalogAutoConfiguration {
function.getNames().iterator().next());
}
@PreDestroy
public void close() {
if (publisher != null) {
if (!functions.isEmpty()) {
publisher.publishEvent(new FunctionUnregistrationEvent(this,
Function.class, functions.keySet()));
}
if (!consumers.isEmpty()) {
publisher.publishEvent(new FunctionUnregistrationEvent(this,
Consumer.class, consumers.keySet()));
}
if (!suppliers.isEmpty()) {
publisher.publishEvent(new FunctionUnregistrationEvent(this,
Supplier.class, suppliers.keySet()));
}
}
}
public Set<FunctionRegistration<?>> merge(
Map<String, FunctionRegistration<?>> initial,
Map<String, Consumer<?>> consumers, Map<String, Supplier<?>> suppliers,
@@ -417,7 +453,9 @@ public class ContextFunctionCatalogAutoConfiguration {
private void wrap(FunctionRegistration<Object> registration, String key) {
Object target = registration.getTarget();
this.registrations.put(target, key);
Class<?> type;
if (target instanceof Supplier) {
type = Supplier.class;
findType(target, ParamType.OUTPUT);
registration.target(target((Supplier<?>) target, key));
for (String name : registration.getNames()) {
@@ -425,6 +463,7 @@ public class ContextFunctionCatalogAutoConfiguration {
}
}
else if (target instanceof Consumer) {
type = Consumer.class;
findType(target, ParamType.INPUT);
registration.target(target((Consumer<?>) target, key));
for (String name : registration.getNames()) {
@@ -432,6 +471,7 @@ public class ContextFunctionCatalogAutoConfiguration {
}
}
else if (target instanceof Function) {
type = Function.class;
findType(target, ParamType.INPUT);
findType(target, ParamType.OUTPUT);
registration.target(target((Function<?, ?>) target, key));
@@ -439,8 +479,15 @@ public class ContextFunctionCatalogAutoConfiguration {
this.functions.put(name, (Function<?, ?>) registration.getTarget());
}
}
else {
return;
}
registrations.remove(target);
this.registrations.put(registration.getTarget(), key);
if (publisher != null) {
publisher.publishEvent(new FunctionRegistrationEvent(
registration.getTarget(), type, registration.getNames()));
}
}
private String getQualifier(String key) {

View File

@@ -1,2 +1,2 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.cloud.function.context.ContextFunctionCatalogAutoConfiguration
org.springframework.cloud.function.context.config.ContextFunctionCatalogAutoConfiguration

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.cloud.function.context;
package org.springframework.cloud.function.context.config;
import java.util.ArrayList;
import java.util.List;
@@ -24,8 +24,9 @@ import java.util.function.Supplier;
import org.junit.Test;
import org.springframework.cloud.function.context.ContextFunctionCatalogAutoConfiguration.BeanFactoryFunctionCatalog;
import org.springframework.cloud.function.context.ContextFunctionCatalogAutoConfiguration.ContextFunctionRegistry;
import org.springframework.cloud.function.context.FunctionRegistration;
import org.springframework.cloud.function.context.config.ContextFunctionCatalogAutoConfiguration.BeanFactoryFunctionCatalog;
import org.springframework.cloud.function.context.config.ContextFunctionCatalogAutoConfiguration.ContextFunctionRegistry;
import static org.assertj.core.api.Assertions.assertThat;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.cloud.function.context;
package org.springframework.cloud.function.context.config;
import static org.assertj.core.api.Assertions.assertThat;
@@ -43,6 +43,9 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.function.compiler.CompiledFunctionFactory;
import org.springframework.cloud.function.compiler.FunctionCompiler;
import org.springframework.cloud.function.context.FunctionRegistration;
import org.springframework.cloud.function.context.FunctionScan;
import org.springframework.cloud.function.context.catalog.FunctionInspector;
import org.springframework.cloud.function.core.FunctionCatalog;
import org.springframework.cloud.function.scan.ScannedFunction;
import org.springframework.cloud.function.test.GenericFunction;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.cloud.function.context;
package org.springframework.cloud.function.context.config;
import java.net.URLClassLoader;
import java.util.ArrayList;
@@ -28,10 +28,10 @@ import org.junit.After;
import org.junit.Test;
import org.springframework.beans.BeanUtils;
import org.springframework.cloud.function.context.ContextFunctionCatalogAutoConfiguration.ContextFunctionRegistry;
import org.springframework.cloud.function.context.ContextFunctionCatalogAutoConfigurationTests.Bar;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.util.ClassUtils;
import org.springframework.cloud.function.context.FunctionRegistration;
import org.springframework.cloud.function.context.config.ContextFunctionCatalogAutoConfiguration.ContextFunctionRegistry;
import static org.assertj.core.api.Assertions.assertThat;
@@ -189,4 +189,43 @@ public class ContextFunctionPostProcessorTests {
}
public static class Foo {
private String value;
public Foo(String value) {
this.value = value;
}
Foo() {
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
public static class Bar {
private String message;
public Bar(String value) {
this.message = value;
}
Bar() {
}
public String getMessage() {
return this.message;
}
public void setMessage(String message) {
this.message = message;
}
}
}