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.config.BeanDefinition;
20 import org.springframework.beans.factory.support.BeanDefinitionBuilder;
21 import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
22 import org.springframework.boot.context.properties.ConfigurationProperties;
23 import org.springframework.cloud.aws.autoconfigure.context.properties.AwsS3ResourceLoaderProperties;
24 import org.springframework.cloud.aws.context.config.annotation.ContextResourceLoaderConfiguration;
25 import org.springframework.context.EnvironmentAware;
26 import org.springframework.context.annotation.Bean;
27 import org.springframework.context.annotation.Configuration;
28 import org.springframework.context.annotation.Import;
29 import org.springframework.core.env.Environment;
30 import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
31
32 /**
33  * @author Agim Emruli
34  */

35 @Configuration(proxyBeanMethods = false)
36 @Import(ContextResourceLoaderAutoConfiguration.Registrar.class)
37 @ConditionalOnClass(name = "com.amazonaws.services.s3.AmazonS3Client")
38 public class ContextResourceLoaderAutoConfiguration {
39
40     /**
41      * The prefix used for properties related to S3 resource loading via the
42      * ResourceLoader.
43      */

44     public static final String AWS_LOADER_PROPERTY_PREFIX = "cloud.aws.loader";
45
46     /**
47      * Bind AWS resource loader related properties to a property instance.
48      * @return An {@link AwsS3ResourceLoaderProperties} instance
49      */

50     @Bean
51     @ConfigurationProperties(prefix = AWS_LOADER_PROPERTY_PREFIX)
52     public AwsS3ResourceLoaderProperties awsS3ResourceLoaderProperties() {
53         return new AwsS3ResourceLoaderProperties();
54     }
55
56     /**
57      * Sets additional properties for the task executor definition.
58      */

59     public static class Registrar extends ContextResourceLoaderConfiguration.Registrar
60             implements EnvironmentAware {
61
62         private static final String CORE_POOL_SIZE_PROPERTY_NAME = "corePoolSize";
63
64         private static final String MAX_POOL_SIZE_PROPERTY_NAME = "maxPoolSize";
65
66         private static final String QUEUE_CAPACITY_PROPERTY_NAME = "queueCapacity";
67
68         private Environment environment;
69
70         @Override
71         public void setEnvironment(Environment environment) {
72             this.environment = environment;
73         }
74
75         @Override
76         protected BeanDefinition getTaskExecutorDefinition() {
77             if (containsProperty(CORE_POOL_SIZE_PROPERTY_NAME)
78                     || containsProperty(MAX_POOL_SIZE_PROPERTY_NAME)
79                     || containsProperty(QUEUE_CAPACITY_PROPERTY_NAME)) {
80                 BeanDefinitionBuilder builder = BeanDefinitionBuilder
81                         .rootBeanDefinition(ThreadPoolTaskExecutor.class);
82
83                 setPropertyIfConfigured(builder, CORE_POOL_SIZE_PROPERTY_NAME);
84                 setPropertyIfConfigured(builder, MAX_POOL_SIZE_PROPERTY_NAME);
85                 setPropertyIfConfigured(builder, QUEUE_CAPACITY_PROPERTY_NAME);
86
87                 return builder.getBeanDefinition();
88             }
89             return super.getTaskExecutorDefinition();
90         }
91
92         private boolean containsProperty(String name) {
93             return this.environment
94                     .containsProperty(AWS_LOADER_PROPERTY_PREFIX + "." + name);
95         }
96
97         private String getProperty(String name) {
98             return this.environment.getProperty(AWS_LOADER_PROPERTY_PREFIX + "." + name);
99         }
100
101         private void setPropertyIfConfigured(BeanDefinitionBuilder builder, String name) {
102             if (containsProperty(name)) {
103                 builder.addPropertyValue(name, getProperty(name));
104             }
105         }
106
107     }
108
109 }
110