|
| 1 | +/* |
| 2 | + * Copyright 2012-2021 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.autoconfigure.integration; |
| 18 | + |
| 19 | +import java.io.FileNotFoundException; |
| 20 | +import java.io.IOException; |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +import org.springframework.boot.SpringApplication; |
| 25 | +import org.springframework.boot.env.EnvironmentPostProcessor; |
| 26 | +import org.springframework.boot.env.OriginTrackedMapPropertySource; |
| 27 | +import org.springframework.boot.env.PropertiesPropertySourceLoader; |
| 28 | +import org.springframework.boot.origin.Origin; |
| 29 | +import org.springframework.boot.origin.OriginLookup; |
| 30 | +import org.springframework.core.Ordered; |
| 31 | +import org.springframework.core.env.ConfigurableEnvironment; |
| 32 | +import org.springframework.core.env.PropertySource; |
| 33 | +import org.springframework.core.io.ClassPathResource; |
| 34 | +import org.springframework.core.io.Resource; |
| 35 | +import org.springframework.integration.context.IntegrationProperties; |
| 36 | + |
| 37 | +/** |
| 38 | + * The {@link EnvironmentPostProcessor} for Spring Integration. |
| 39 | + * |
| 40 | + * @author Artem Bilan |
| 41 | + * @since 2.5 |
| 42 | + */ |
| 43 | +public class IntegrationEnvironmentPostProcessor implements EnvironmentPostProcessor, Ordered { |
| 44 | + |
| 45 | + @Override |
| 46 | + public int getOrder() { |
| 47 | + return Ordered.LOWEST_PRECEDENCE; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { |
| 52 | + registerIntegrationPropertiesFileSource(environment); |
| 53 | + } |
| 54 | + |
| 55 | + private static void registerIntegrationPropertiesFileSource(ConfigurableEnvironment environment) { |
| 56 | + Resource integrationPropertiesResource = new ClassPathResource("META-INF/spring.integration.properties"); |
| 57 | + PropertiesPropertySourceLoader loader = new PropertiesPropertySourceLoader(); |
| 58 | + try { |
| 59 | + OriginTrackedMapPropertySource propertyFileSource = (OriginTrackedMapPropertySource) loader |
| 60 | + .load("integration-properties-file", integrationPropertiesResource).get(0); |
| 61 | + |
| 62 | + environment.getPropertySources().addLast(new IntegrationPropertySource(propertyFileSource)); |
| 63 | + } |
| 64 | + catch (FileNotFoundException ex) { |
| 65 | + // Ignore when no META-INF/spring.integration.properties file in classpath |
| 66 | + } |
| 67 | + catch (IOException ex) { |
| 68 | + throw new IllegalStateException( |
| 69 | + "Failed to load integration properties from " + integrationPropertiesResource, ex); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private static final class IntegrationPropertySource extends PropertySource<Map<String, Object>> |
| 74 | + implements OriginLookup<String> { |
| 75 | + |
| 76 | + private static final String PREFIX = "spring.integration."; |
| 77 | + |
| 78 | + private static final Map<String, String> KEYS_MAPPING = new HashMap<>(); |
| 79 | + |
| 80 | + static { |
| 81 | + KEYS_MAPPING.put(PREFIX + "channels.auto-create", IntegrationProperties.CHANNELS_AUTOCREATE); |
| 82 | + KEYS_MAPPING.put(PREFIX + "channels.max-unicast-subscribers", |
| 83 | + IntegrationProperties.CHANNELS_MAX_UNICAST_SUBSCRIBERS); |
| 84 | + KEYS_MAPPING.put(PREFIX + "channels.max-broadcast-subscribers", |
| 85 | + IntegrationProperties.CHANNELS_MAX_BROADCAST_SUBSCRIBERS); |
| 86 | + KEYS_MAPPING.put(PREFIX + "channels.error-require-subscribers", |
| 87 | + IntegrationProperties.ERROR_CHANNEL_REQUIRE_SUBSCRIBERS); |
| 88 | + KEYS_MAPPING.put(PREFIX + "channels.error-ignore-failures", |
| 89 | + IntegrationProperties.ERROR_CHANNEL_IGNORE_FAILURES); |
| 90 | + KEYS_MAPPING.put(PREFIX + "endpoints.throw-exception-on-late-reply", |
| 91 | + IntegrationProperties.THROW_EXCEPTION_ON_LATE_REPLY); |
| 92 | + KEYS_MAPPING.put(PREFIX + "endpoints.read-only-headers", IntegrationProperties.READ_ONLY_HEADERS); |
| 93 | + KEYS_MAPPING.put(PREFIX + "endpoints.no-auto-startup", IntegrationProperties.ENDPOINTS_NO_AUTO_STARTUP); |
| 94 | + } |
| 95 | + |
| 96 | + private final OriginTrackedMapPropertySource origin; |
| 97 | + |
| 98 | + IntegrationPropertySource(OriginTrackedMapPropertySource origin) { |
| 99 | + super("original-integration-properties", origin.getSource()); |
| 100 | + this.origin = origin; |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public Object getProperty(String name) { |
| 105 | + return this.origin.getProperty(KEYS_MAPPING.get(name)); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public Origin getOrigin(String key) { |
| 110 | + return this.origin.getOrigin(KEYS_MAPPING.get(name)); |
| 111 | + } |
| 112 | + |
| 113 | + } |
| 114 | + |
| 115 | +} |
0 commit comments