GH-3502: Make the framework compatible with Native (#3531)

Fixes https://github.com/spring-projects/spring-integration/issues/3502
This commit is contained in:
Artem Bilan
2021-03-30 15:26:06 -04:00
committed by GitHub
parent 5a42effb48
commit 0549dbd102
12 changed files with 265 additions and 276 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2019 the original author or authors.
* Copyright 2016-2021 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.
@@ -40,7 +40,7 @@ import org.springframework.core.annotation.AliasFor;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Import(IntegrationGraphControllerRegistrarImportSelector.class)
@Import(IntegrationGraphControllerRegistrar.class)
public @interface EnableIntegrationGraphController {
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2019 the original author or authors.
* Copyright 2016-2021 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,6 +28,7 @@ import org.springframework.integration.config.annotation.AnnotationMetadataAdapt
/**
* The {@link BeanDefinitionParser} for the {@code <int-http:graph-controller>} component.
*
* @author Artem Bilan
*
* @since 4.3
@@ -39,22 +40,15 @@ public class IntegrationGraphControllerParser implements BeanDefinitionParser {
@Override
public BeanDefinition parse(final Element element, ParserContext parserContext) {
if (HttpContextUtils.WEB_MVC_PRESENT) {
this.graphControllerRegistrar.registerBeanDefinitions(
new AnnotationMetadataAdapter() {
this.graphControllerRegistrar.registerBeanDefinitions(
new AnnotationMetadataAdapter() {
@Override
public Map<String, Object> getAnnotationAttributes(String annotationType) {
return Collections.singletonMap("value", element.getAttribute("path"));
}
@Override
public Map<String, Object> getAnnotationAttributes(String annotationType) {
return Collections.singletonMap("value", element.getAttribute("path"));
}
}, parserContext.getRegistry());
}
else {
parserContext.getReaderContext().warning("The 'IntegrationGraphController' isn't registered " +
"with the application context because" +
" there is no 'org.springframework.web.servlet.DispatcherServlet' in the classpath.", element);
}
}, parserContext.getRegistry());
return null;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2019 the original author or authors.
* Copyright 2016-2021 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.
@@ -20,7 +20,11 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
@@ -52,8 +56,17 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
*/
class IntegrationGraphControllerRegistrar implements ImportBeanDefinitionRegistrar {
private static final Log LOGGER = LogFactory.getLog(IntegrationGraphControllerRegistrar.class);
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
if (!HttpContextUtils.WEB_MVC_PRESENT && !HttpContextUtils.WEB_FLUX_PRESENT) {
LOGGER.warn("The 'IntegrationGraphController' isn't registered with the application context because" +
" there is no 'org.springframework.web.servlet.DispatcherServlet' or" +
" 'org.springframework.web.reactive.DispatcherHandler' in the classpath.");
return;
}
Map<String, Object> annotationAttributes =
importingClassMetadata.getAnnotationAttributes(EnableIntegrationGraphController.class.getName());
if (annotationAttributes == null) {
@@ -62,40 +75,41 @@ class IntegrationGraphControllerRegistrar implements ImportBeanDefinitionRegistr
if (!registry.containsBeanDefinition(IntegrationContextUtils.INTEGRATION_GRAPH_SERVER_BEAN_NAME)) {
registry.registerBeanDefinition(IntegrationContextUtils.INTEGRATION_GRAPH_SERVER_BEAN_NAME,
new RootBeanDefinition(IntegrationGraphServer.class));
new RootBeanDefinition(IntegrationGraphServer.class, IntegrationGraphServer::new));
}
String path = (String) annotationAttributes.get("value");
String[] allowedOrigins = (String[]) annotationAttributes.get("allowedOrigins");
if (allowedOrigins != null && allowedOrigins.length > 0) {
AbstractBeanDefinition controllerCorsConfigurer =
BeanDefinitionBuilder.genericBeanDefinition(IntegrationGraphCorsConfigurer.class)
.addConstructorArgValue(annotationAttributes.get("value"))
.addConstructorArgValue(allowedOrigins)
.getBeanDefinition();
new RootBeanDefinition(IntegrationGraphCorsConfigurer.class,
() -> new IntegrationGraphCorsConfigurer(path, allowedOrigins));
BeanDefinitionReaderUtils.registerWithGeneratedName(controllerCorsConfigurer, registry);
}
if (!registry.containsBeanDefinition(HttpContextUtils.GRAPH_CONTROLLER_BEAN_NAME)) {
Map<String, Object> properties = annotationAttributes;
AbstractBeanDefinition controllerPropertiesPopulator =
BeanDefinitionBuilder.genericBeanDefinition(GraphControllerPropertiesPopulator.class)
.addConstructorArgValue(annotationAttributes)
BeanDefinitionBuilder.genericBeanDefinition(GraphControllerPropertiesPopulator.class,
() -> new GraphControllerPropertiesPopulator(properties))
.setRole(BeanDefinition.ROLE_INFRASTRUCTURE)
.getBeanDefinition();
BeanDefinitionReaderUtils.registerWithGeneratedName(controllerPropertiesPopulator, registry);
BeanDefinition graphController =
BeanDefinitionBuilder.genericBeanDefinition(IntegrationGraphController.class)
.addConstructorArgReference(IntegrationContextUtils.INTEGRATION_GRAPH_SERVER_BEAN_NAME)
.getBeanDefinition();
new RootBeanDefinition(IntegrationGraphController.class, () ->
new IntegrationGraphController(
((BeanFactory) registry)
.getBean(IntegrationContextUtils.INTEGRATION_GRAPH_SERVER_BEAN_NAME,
IntegrationGraphServer.class)));
registry.registerBeanDefinition(HttpContextUtils.GRAPH_CONTROLLER_BEAN_NAME, graphController);
}
}
private static final class GraphControllerPropertiesPopulator
implements BeanFactoryPostProcessor, EnvironmentAware {
private static final class GraphControllerPropertiesPopulator implements BeanFactoryPostProcessor, EnvironmentAware {
private final Map<String, Object> properties = new HashMap<String, Object>();
private final Map<String, Object> properties = new HashMap<>();
private GraphControllerPropertiesPopulator(Map<String, Object> annotationAttributes) {
Object graphControllerPath = annotationAttributes.get(AnnotationUtils.VALUE);

View File

@@ -1,47 +0,0 @@
/*
* Copyright 2016-2020 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
*
* https://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.integration.http.config;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;
/**
* @author Artem Bilan
*
* @since 4.3
*/
class IntegrationGraphControllerRegistrarImportSelector implements ImportSelector {
private static final Log LOGGER = LogFactory.getLog(IntegrationGraphControllerRegistrarImportSelector.class);
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
if (HttpContextUtils.WEB_MVC_PRESENT || HttpContextUtils.WEB_FLUX_PRESENT) {
return new String[] { IntegrationGraphControllerRegistrar.class.getName() };
}
else {
LOGGER.warn("The 'IntegrationGraphController' isn't registered with the application context because" +
" there is no 'org.springframework.web.servlet.DispatcherServlet' or" +
" 'org.springframework.web.reactive.DispatcherHandler' in the classpath.");
return new String[0];
}
}
}