|
1 | 1 | /*
|
2 |
| - * Copyright 2014-2019 the original author or authors. |
| 2 | + * Copyright 2014-2021 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.integration.config;
|
18 | 18 |
|
| 19 | +import java.util.Arrays; |
19 | 20 | import java.util.Map;
|
20 |
| -import java.util.Set; |
21 | 21 |
|
22 |
| -import org.springframework.beans.PropertyValue; |
23 |
| -import org.springframework.beans.factory.config.BeanDefinition; |
24 |
| -import org.springframework.beans.factory.support.AbstractBeanDefinition; |
25 |
| -import org.springframework.beans.factory.support.BeanDefinitionBuilder; |
| 22 | +import org.springframework.beans.factory.BeanDefinitionStoreException; |
| 23 | +import org.springframework.beans.factory.config.ConfigurableBeanFactory; |
26 | 24 | import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
27 |
| -import org.springframework.beans.factory.support.ManagedSet; |
| 25 | +import org.springframework.beans.factory.support.RootBeanDefinition; |
| 26 | +import org.springframework.context.ConfigurableApplicationContext; |
28 | 27 | import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
|
29 | 28 | import org.springframework.core.type.AnnotationMetadata;
|
30 | 29 | import org.springframework.integration.context.IntegrationContextUtils;
|
31 | 30 | import org.springframework.integration.history.MessageHistoryConfigurer;
|
| 31 | +import org.springframework.util.StringUtils; |
32 | 32 |
|
33 | 33 | /**
|
34 | 34 | * Registers the {@link MessageHistoryConfigurer} {@link org.springframework.beans.factory.config.BeanDefinition}
|
|
38 | 38 | *
|
39 | 39 | * @author Artem Bilan
|
40 | 40 | * @author Gary Russell
|
| 41 | + * |
41 | 42 | * @since 4.0
|
42 | 43 | */
|
43 | 44 | public class MessageHistoryRegistrar implements ImportBeanDefinitionRegistrar {
|
44 | 45 |
|
45 | 46 | @Override
|
46 | 47 | public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
|
47 |
| - Map<String, Object> annotationAttributes = importingClassMetadata.getAnnotationAttributes(EnableMessageHistory.class.getName()); |
48 |
| - Object componentNamePatterns = annotationAttributes.get("value"); // NOSONAR never null |
49 |
| - |
50 |
| - if (componentNamePatterns instanceof String[]) { |
51 |
| - StringBuilder componentNamePatternsString = new StringBuilder(); |
52 |
| - for (String s : (String[]) componentNamePatterns) { |
53 |
| - componentNamePatternsString.append(s).append(","); |
54 |
| - } |
55 |
| - componentNamePatterns = componentNamePatternsString.substring(0, componentNamePatternsString.length() - 1); |
| 48 | + if (registry.containsBeanDefinition(IntegrationContextUtils.INTEGRATION_MESSAGE_HISTORY_CONFIGURER_BEAN_NAME)) { |
| 49 | + throw new BeanDefinitionStoreException( |
| 50 | + "Only one @EnableMessageHistory or <message-history/> can be declared in the application context."); |
56 | 51 | }
|
57 | 52 |
|
58 |
| - if (!registry.containsBeanDefinition(IntegrationContextUtils.INTEGRATION_MESSAGE_HISTORY_CONFIGURER_BEAN_NAME)) { |
59 |
| - Set<Object> componentNamePatternsSet = new ManagedSet<Object>(); |
60 |
| - componentNamePatternsSet.add(componentNamePatterns); |
61 |
| - |
62 |
| - AbstractBeanDefinition messageHistoryConfigurer = BeanDefinitionBuilder.genericBeanDefinition(MessageHistoryConfigurer.class) |
63 |
| - .addPropertyValue("componentNamePatternsSet", componentNamePatternsSet) |
64 |
| - .getBeanDefinition(); |
| 53 | + Map<String, Object> annotationAttributes = |
| 54 | + importingClassMetadata.getAnnotationAttributes(EnableMessageHistory.class.getName()); |
| 55 | + Object componentNamePatterns = annotationAttributes.get("value"); // NOSONAR never null |
65 | 56 |
|
66 |
| - registry.registerBeanDefinition(IntegrationContextUtils.INTEGRATION_MESSAGE_HISTORY_CONFIGURER_BEAN_NAME, messageHistoryConfigurer); |
| 57 | + String patterns; |
67 | 58 |
|
| 59 | + if (componentNamePatterns instanceof String[]) { |
| 60 | + patterns = String.join(",", (String[]) componentNamePatterns); |
68 | 61 | }
|
69 | 62 | else {
|
70 |
| - BeanDefinition beanDefinition = registry.getBeanDefinition(IntegrationContextUtils.INTEGRATION_MESSAGE_HISTORY_CONFIGURER_BEAN_NAME); |
71 |
| - PropertyValue propertyValue = beanDefinition |
72 |
| - .getPropertyValues().getPropertyValue("componentNamePatternsSet"); |
73 |
| - if (propertyValue != null) { |
74 |
| - @SuppressWarnings("unchecked") |
75 |
| - Set<Object> currentComponentNamePatternsSet = (Set<Object>) propertyValue.getValue(); |
76 |
| - currentComponentNamePatternsSet.add(componentNamePatterns); // NOSONAR never null |
| 63 | + patterns = (String) componentNamePatterns; |
| 64 | + } |
| 65 | + |
| 66 | + registry.registerBeanDefinition(IntegrationContextUtils.INTEGRATION_MESSAGE_HISTORY_CONFIGURER_BEAN_NAME, |
| 67 | + new RootBeanDefinition(MessageHistoryConfigurer.class, |
| 68 | + () -> createMessageHistoryConfigurer(registry, patterns))); |
| 69 | + } |
| 70 | + |
| 71 | + private MessageHistoryConfigurer createMessageHistoryConfigurer(BeanDefinitionRegistry registry, String patterns) { |
| 72 | + MessageHistoryConfigurer messageHistoryConfigurer = new MessageHistoryConfigurer(); |
| 73 | + if (StringUtils.hasText(patterns)) { |
| 74 | + ConfigurableBeanFactory beanFactory = null; |
| 75 | + if (registry instanceof ConfigurableBeanFactory) { |
| 76 | + beanFactory = (ConfigurableBeanFactory) registry; |
77 | 77 | }
|
78 |
| - else { |
79 |
| - Set<Object> componentNamePatternsSet = new ManagedSet<Object>(); |
80 |
| - componentNamePatternsSet.add(componentNamePatterns); |
81 |
| - beanDefinition.getPropertyValues().addPropertyValue("componentNamePatternsSet", componentNamePatternsSet); |
| 78 | + else if (registry instanceof ConfigurableApplicationContext) { |
| 79 | + beanFactory = ((ConfigurableApplicationContext) registry).getBeanFactory(); |
82 | 80 | }
|
| 81 | + |
| 82 | + String[] patternsToSet = StringUtils.delimitedListToStringArray(patterns, ",", " "); |
| 83 | + if (beanFactory != null) { |
| 84 | + patternsToSet = |
| 85 | + Arrays.stream(patternsToSet) |
| 86 | + .map(beanFactory::resolveEmbeddedValue) |
| 87 | + .flatMap((pattern) -> |
| 88 | + Arrays.stream(StringUtils.delimitedListToStringArray(pattern, ",", " "))) |
| 89 | + .toArray(String[]::new); |
| 90 | + } |
| 91 | + messageHistoryConfigurer.setComponentNamePatterns(patternsToSet); |
83 | 92 | }
|
| 93 | + |
| 94 | + return messageHistoryConfigurer; |
84 | 95 | }
|
85 | 96 |
|
86 | 97 | }
|
0 commit comments