GH-247 Added spring.cloud.function.definition property

Added spring.cloud.function.definition property which is used by FunctionRegistry as a supplement instruction to resolve nameless lookups.
It is used by web module to map single or multiple (composed) functions to the root path (/)

Resolves #247
This commit is contained in:
Oleg Zhurakousky
2019-01-28 18:47:14 +01:00
parent 3196a2ee8b
commit 589e451d14
6 changed files with 147 additions and 9 deletions

View File

@@ -0,0 +1,44 @@
/*
* Copyright 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.context;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.util.StringUtils;
/**
*
* @author Oleg Zhurakousky
*
* @since 2.0.1
*
*/
public abstract class AbstractFunctionRegistry implements FunctionRegistry {
@Autowired
private Environment environment = new StandardEnvironment();
public <T> T lookup(Class<?> type, String name) {
String functionDefinitionName = !StringUtils.hasText(name) && environment.containsProperty("spring.cloud.function.definition")
? environment.getProperty("spring.cloud.function.definition") : name;
return this.doLookup(type, functionDefinitionName);
}
protected abstract <T> T doLookup(Class<?> type, String name);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2018 the original author or authors.
* Copyright 2016-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.
@@ -28,8 +28,8 @@ import java.util.stream.Collectors;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.springframework.cloud.function.context.AbstractFunctionRegistry;
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;
@@ -39,8 +39,8 @@ import org.springframework.util.Assert;
* @author Mark Fisher
* @author Oleg Zhurakousky
*/
public class InMemoryFunctionCatalog
implements FunctionRegistry, FunctionInspector, ApplicationEventPublisherAware {
public class InMemoryFunctionCatalog extends AbstractFunctionRegistry
implements FunctionInspector, ApplicationEventPublisherAware {
private final Map<Class<?>, Map<String, Object>> functions;
@@ -122,7 +122,7 @@ public class InMemoryFunctionCatalog
@Override
@SuppressWarnings("unchecked")
public <T> T lookup(Class<?> type, String name) {
public <T> T doLookup(Class<?> type, String name) {
T function = null;
if (type == null) {
function = (T) functions.values().stream()

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2018 the original author or authors.
* Copyright 2016-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.
@@ -41,6 +41,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.function.context.AbstractFunctionRegistry;
import org.springframework.cloud.function.context.FunctionCatalog;
import org.springframework.cloud.function.context.FunctionRegistration;
import org.springframework.cloud.function.context.FunctionRegistry;
@@ -103,6 +104,8 @@ public class ContextFunctionCatalogAutoConfiguration {
@Autowired(required = false)
private Map<String, FunctionRegistration<?>> registrations = Collections.emptyMap();
@Bean
public FunctionRegistry functionCatalog(ContextFunctionRegistry processor) {
processor.merge(registrations, consumers, suppliers, functions);
@@ -114,7 +117,7 @@ public class ContextFunctionCatalogAutoConfiguration {
return new BeanFactoryFunctionInspector(processor);
}
protected static class BeanFactoryFunctionCatalog implements FunctionRegistry {
protected static class BeanFactoryFunctionCatalog extends AbstractFunctionRegistry {
private final ContextFunctionRegistry processor;
@@ -127,7 +130,7 @@ public class ContextFunctionCatalogAutoConfiguration {
@Override
@SuppressWarnings("unchecked")
public <T> T lookup(Class<?> type, String name) {
protected <T> T doLookup(Class<?> type, String name) {
T function = null;
if (type == null) {
function = (T) processor.lookupFunction(name);

View File

@@ -7,6 +7,12 @@
"type": "java.lang.String",
"description": "Triggers scanning within the specified base packages for any class that is assignable to java.util.function.Function. For each detected Function class, a bean instance will be added to the context.",
"defaultValue": "functions"
},
{
"name": "spring.cloud.function.definition",
"type": "java.lang.String",
"description": "Name (e.g., 'foo') or composition instruction (e.g., 'foo|bar') used to resolve default function especially for cases where there is more then once function available in catalog.",
"defaultValue": ""
}
]
}