Skip to content

Commit cc18a77

Browse files
committed
Setup custom S3PropertySource
1 parent 2aa05b4 commit cc18a77

File tree

7 files changed

+105
-149
lines changed

7 files changed

+105
-149
lines changed

src/main/java/com/spring/loader/S3PropertiesLocation.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@
77
import java.lang.annotation.Target;
88

99
import org.springframework.context.annotation.Import;
10-
import org.springframework.core.annotation.AliasFor;
1110

1211
/**
13-
* Allow the auto configuration of the {@link S3PropertyPlaceholderConfigurer} bean.
12+
* Allow the auto configuration of the {@link S3PropertySource} bean.
1413
*
1514
* @author Eric Dallo
1615
* @since 1.0.3
@@ -27,11 +26,7 @@
2726
*
2827
* @return the path of aws s3 properties, e.g. my-bucket/my-folder/app.properties
2928
*/
30-
@AliasFor("path")
31-
String[] value() default {};
32-
33-
@AliasFor("value")
34-
String[] path() default {};
29+
String[] value();
3530

3631
/**
3732
* The profiles to load the properties in aws s3.

src/main/java/com/spring/loader/S3PropertiesLocationRegistrar.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
import org.springframework.core.type.AnnotationMetadata;
1414

1515
/**
16-
* Creates the {@link S3PropertyPlaceholderConfigurer} bean.
16+
* Creates the {@link S3PropertySource} bean.
1717
* For use with the {@link S3PropertiesLocation} annotation.
1818
*
1919
* @author Eric Dallo
2020
* @since 1.0.3
2121
* @see S3PropertiesLocation
22-
* @see S3PropertyPlaceholderConfigurer
22+
* @see S3PropertySource
2323
*/
2424
class S3PropertiesLocationRegistrar implements EnvironmentAware, ImportBeanDefinitionRegistrar {
2525

@@ -36,12 +36,12 @@ public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, B
3636

3737
String[] locations = attributes.getStringArray("value");
3838

39-
BeanDefinition bd = new RootBeanDefinition(S3PropertyPlaceholderConfigurer.class);
39+
BeanDefinition bd = new RootBeanDefinition(S3PropertiesSourceConfigurer.class);
4040

4141
bd.getPropertyValues().addPropertyValue("s3ResourceLoader", new RuntimeBeanReference("s3ResourceLoader"));
4242
bd.getPropertyValues().add("s3Locations", locations);
4343

44-
String beanName = S3PropertyPlaceholderConfigurer.class.getSimpleName();
44+
String beanName = S3PropertiesSourceConfigurer.class.getSimpleName();
4545
registry.registerBeanDefinition(toLowerCase(beanName.charAt(0)) + beanName.substring(1), bd);
4646
}
4747

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.spring.loader;
2+
3+
import java.io.IOException;
4+
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
import org.springframework.beans.BeansException;
8+
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
9+
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
10+
import org.springframework.beans.factory.config.PropertiesFactoryBean;
11+
import org.springframework.context.EnvironmentAware;
12+
import org.springframework.core.Ordered;
13+
import org.springframework.core.PriorityOrdered;
14+
import org.springframework.core.env.ConfigurableEnvironment;
15+
import org.springframework.core.env.Environment;
16+
import org.springframework.core.env.MutablePropertySources;
17+
18+
/**
19+
* Add a new {@link PropertySource} to spring property sources from a S3 bucket
20+
* For use with the {@link S3PropertiesLocation} annotation.
21+
*
22+
* @author Eric Dallo
23+
* @since 2.0
24+
* @see S3PropertiesLocation
25+
* @see S3PropertySource
26+
*/
27+
class S3PropertiesSourceConfigurer implements EnvironmentAware, BeanFactoryPostProcessor, PriorityOrdered {
28+
29+
private static final Logger LOGGER = LoggerFactory.getLogger(S3PropertiesSourceConfigurer.class);
30+
31+
private Environment environment;
32+
private S3ResourceLoader s3ResourceLoader;
33+
private String[] s3Locations;
34+
35+
public void setS3ResourceLoader(S3ResourceLoader s3ResourceLoader) {
36+
this.s3ResourceLoader = s3ResourceLoader;
37+
}
38+
39+
public void setS3Locations(String[] s3Locations) {
40+
this.s3Locations = s3Locations;
41+
}
42+
43+
@Override
44+
public void setEnvironment(Environment environment) {
45+
this.environment = environment;
46+
}
47+
48+
@Override
49+
public int getOrder() {
50+
return Ordered.HIGHEST_PRECEDENCE;
51+
}
52+
53+
@Override
54+
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
55+
if (this.environment instanceof ConfigurableEnvironment) {
56+
57+
PropertiesFactoryBean propertiesFactory = new PropertiesFactoryBean();
58+
MutablePropertySources propertySources = ((ConfigurableEnvironment) this.environment).getPropertySources();
59+
60+
for (String s3Location : s3Locations) {
61+
62+
propertiesFactory.setLocation(s3ResourceLoader.getResource(s3Location));
63+
try {
64+
propertiesFactory.afterPropertiesSet();
65+
propertySources.addFirst(new S3PropertySource(propertiesFactory.getObject()));
66+
} catch (IOException e) {
67+
LOGGER.error("Could not read properties from s3Location: " + s3Location, e);
68+
}
69+
}
70+
71+
} else {
72+
LOGGER.warn("Environment is not of type '{}' property source with instance data is not available", ConfigurableEnvironment.class.getName());
73+
}
74+
}
75+
76+
}

src/main/java/com/spring/loader/S3PropertyPlaceholderConfigurer.java

Lines changed: 0 additions & 72 deletions
This file was deleted.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.spring.loader;
2+
3+
import java.util.Properties;
4+
5+
import org.springframework.core.env.PropertySource;
6+
7+
class S3PropertySource extends PropertySource<Object> {
8+
9+
private static final String S3_PROPERTY_SOURCE_NAME = "s3PropertySource";
10+
11+
private final Properties properties;
12+
13+
S3PropertySource (Properties properties) {
14+
super(S3_PROPERTY_SOURCE_NAME);
15+
this.properties = properties;
16+
}
17+
18+
@Override
19+
public Object getProperty(String name) {
20+
return properties.get(name);
21+
}
22+
}

src/main/java/com/spring/loader/S3ResourceLoader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*
1717
* @author Eric Dallo
1818
* @since 1.0.0
19-
* @see S3PropertyPlaceholderConfigurer
19+
* @see S3PropertySource
2020
*/
2121
class S3ResourceLoader implements ResourceLoader {
2222

src/test/java/com/spring/loader/S3PropertyPlaceholderConfigurerTest.java

Lines changed: 0 additions & 65 deletions
This file was deleted.

0 commit comments

Comments
 (0)