1
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
35 @Configuration(proxyBeanMethods = false)
36 @Import(ContextResourceLoaderAutoConfiguration.Registrar.class)
37 @ConditionalOnClass(name = "com.amazonaws.services.s3.AmazonS3Client")
38 public class ContextResourceLoaderAutoConfiguration {
39
40
44 public static final String AWS_LOADER_PROPERTY_PREFIX = "cloud.aws.loader";
45
46
50 @Bean
51 @ConfigurationProperties(prefix = AWS_LOADER_PROPERTY_PREFIX)
52 public AwsS3ResourceLoaderProperties awsS3ResourceLoaderProperties() {
53 return new AwsS3ResourceLoaderProperties();
54 }
55
56
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