1 /*
2  * Copyright 2013-2019 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.cloud.aws.autoconfigure.context;
18
19 import org.springframework.beans.factory.support.BeanDefinitionRegistry;
20 import org.springframework.boot.context.properties.ConfigurationProperties;
21 import org.springframework.cloud.aws.autoconfigure.context.properties.AwsRegionProperties;
22 import org.springframework.context.EnvironmentAware;
23 import org.springframework.context.annotation.Bean;
24 import org.springframework.context.annotation.Configuration;
25 import org.springframework.context.annotation.Import;
26 import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
27 import org.springframework.core.env.Environment;
28 import org.springframework.core.type.AnnotationMetadata;
29 import org.springframework.util.StringUtils;
30
31 import static org.springframework.cloud.aws.context.config.support.ContextConfigurationUtils.REGION_PROVIDER_BEAN_NAME;
32 import static org.springframework.cloud.aws.context.config.support.ContextConfigurationUtils.registerRegionProvider;
33
34 /**
35  * Region auto configuration, based on <a
36  * href=https://cloud.spring.io/spring-cloud-aws/spring-cloud-aws.html#_configuring_region>cloud.aws.region</a>
37  * settings.
38  *
39  * @author Agim Emruli
40  * @author Petromir Dzhunev
41  */

42 @Configuration(proxyBeanMethods = false)
43 @Import(ContextRegionProviderAutoConfiguration.Registrar.class)
44 public class ContextRegionProviderAutoConfiguration {
45
46     /**
47      * The prefix used for AWS region related properties.
48      */

49     public static final String AWS_REGION_PROPERTIES_PREFIX = "cloud.aws.region";
50
51     /**
52      * Bind AWS region related properties to a property instance.
53      * @return An {@link AwsRegionProperties} instance
54      */

55     @Bean
56     @ConfigurationProperties(prefix = AWS_REGION_PROPERTIES_PREFIX)
57     public AwsRegionProperties awsRegionProperties() {
58         return new AwsRegionProperties();
59     }
60
61     static class Registrar implements EnvironmentAware, ImportBeanDefinitionRegistrar {
62
63         private Environment environment;
64
65         @Override
66         public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata,
67                 BeanDefinitionRegistry registry) {
68             // Do not register region provider if already existing
69             if (registry.containsBeanDefinition(REGION_PROVIDER_BEAN_NAME)) {
70                 return;
71             }
72
73             boolean useDefaultRegionChain = this.environment.getProperty(
74                     AWS_REGION_PROPERTIES_PREFIX + ".use-default-aws-region-chain",
75                     Boolean.classfalse);
76
77             String staticRegion = this.environment
78                     .getProperty(AWS_REGION_PROPERTIES_PREFIX + ".static");
79
80             boolean autoDetect = this.environment.getProperty(
81                     AWS_REGION_PROPERTIES_PREFIX + ".auto", Boolean.classtrue)
82                     && !StringUtils.hasText(staticRegion);
83
84             registerRegionProvider(registry, autoDetect, useDefaultRegionChain,
85                     staticRegion);
86         }
87
88         @Override
89         public void setEnvironment(Environment environment) {
90             this.environment = environment;
91         }
92
93     }
94
95 }
96