Skip to content

Commit fbafbac

Browse files
authored
Merge pull request #6 from larsgberggren/master
Allowing a more fine granular system env loading
2 parents 1ddc372 + 989d317 commit fbafbac

File tree

3 files changed

+62
-16
lines changed

3 files changed

+62
-16
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,13 @@ Maven:
3434
```
3535
- Using a specific profile to only load properties if the app is running with that profile
3636
```java
37-
@S3PropertiesLocation(path = "my-bucket/my-folder/my-properties.properties", profiles = "production")
37+
@S3PropertiesLocation(value = "my-bucket/my-folder/my-properties.properties", profiles = "production")
3838
```
3939
- Load from a System env variable
4040
```java
41-
@S3PropertiesLocation(path = "${AWS_S3_LOCATION}", profiles = "developer")
41+
@S3PropertiesLocation(value = "${AWS_S3_LOCATION}", profiles = "developer")
42+
or
43+
@S3PropertiesLocation(value = "${AWS_S3_BUCKET}/application/my.properties", profiles = "developer")
4244
```
4345

4446
## Requisites

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

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import com.spring.loader.exception.EnviromentPropertyNotFoundException;
77
import com.spring.loader.exception.InvalidS3LocationException;
88

9+
import java.util.regex.Matcher;
10+
import java.util.regex.Pattern;
11+
912
/**
1013
* Resolver for properties that will be retrieved from system environment.
1114
*
@@ -15,28 +18,39 @@
1518
public class SystemPropertyResolver {
1619

1720
private static final String SYSTEM_NOTATION_PREFIX = "${";
18-
private static final String SYSTEM_NOTATION_SUFIX = "}";
1921

2022
public String getFormattedValue(String value) {
2123
if (isEmpty(value)) {
2224
throw new InvalidS3LocationException("The location cannot be empty or null");
2325
}
24-
if (value.startsWith(SYSTEM_NOTATION_PREFIX)) {
25-
26-
if (value.endsWith(SYSTEM_NOTATION_SUFIX)) {
27-
String rawProperty = value.substring(SYSTEM_NOTATION_PREFIX.length(), value.length() - SYSTEM_NOTATION_SUFIX.length());
28-
String valueFromEnv = System.getenv(rawProperty);
29-
30-
if (isEmpty(valueFromEnv)) {
31-
throw new EnviromentPropertyNotFoundException(format("Enviroment variable %s not found in system", rawProperty));
32-
}
33-
34-
return valueFromEnv;
26+
27+
if (value.contains(SYSTEM_NOTATION_PREFIX)) {
28+
String bucket = value;
29+
String pattern = "\\$\\{([A-Za-z0-9_]+)\\}";
30+
Matcher matcher = Pattern.compile(pattern).matcher(bucket);
31+
while (matcher.find()) {
32+
String envValue = getFromEnv(matcher.group(1));
33+
Pattern subExpression = Pattern.compile(Pattern.quote(matcher.group(0)));
34+
bucket = subExpression.matcher(bucket).replaceAll(envValue);
3535
}
36-
throw new InvalidS3LocationException("Syntax error for system property: " + value);
36+
37+
if (bucket.contains(SYSTEM_NOTATION_PREFIX)) {
38+
throw new InvalidS3LocationException("Syntax error for system property: " + value);
39+
}
40+
41+
return bucket;
3742
}
3843

3944
return value;
4045
}
4146

47+
private String getFromEnv(String key) {
48+
String valueFromEnv = System.getenv(key);
49+
50+
if (isEmpty(valueFromEnv)) {
51+
throw new EnviromentPropertyNotFoundException(format("Environment variable %s not found in system", key));
52+
}
53+
54+
return valueFromEnv;
55+
}
4256
}

src/test/java/com/spring/loader/resolver/SystemPropertyResolverTest.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,37 @@ public void shouldGetFormattedValueWhenValueIsValid() {
3838

3939
assertEquals(expected, formattedValue);
4040
}
41-
41+
42+
@Test
43+
public void shouldGetCombinedFormattedValueWhenValueIsValid() {
44+
String region = "someRegion";
45+
String environment = "someEnvironment";
46+
47+
PowerMockito.when(System.getenv(Mockito.eq("S3_BUCKET_REGION"))).thenReturn(region);
48+
PowerMockito.when(System.getenv(Mockito.eq("S3_BUCKET_ENVIRONMENT"))).thenReturn(environment);
49+
50+
String configValue = "${S3_BUCKET_REGION}/${S3_BUCKET_ENVIRONMENT}/myApplication/application.properties";
51+
String expected = String.format("%s/%s/myApplication/application.properties", region, environment);
52+
53+
String formattedValue = subject.getFormattedValue(configValue);
54+
55+
assertEquals(expected, formattedValue);
56+
}
57+
58+
@Test
59+
public void shouldReplaceMultiple() {
60+
String environment = "dev";
61+
62+
PowerMockito.when(System.getenv(Mockito.eq("EC2_ENVIRONMENT"))).thenReturn(environment);
63+
64+
String configValue = "region-${EC2_ENVIRONMENT}/deploy-${EC2_ENVIRONMENT}/application.properties";
65+
String expected = String.format("region-%s/deploy-%s/application.properties", environment, environment);
66+
67+
String formattedValue = subject.getFormattedValue(configValue);
68+
69+
assertEquals(expected, formattedValue);
70+
}
71+
4272
@Test
4373
public void shouldGetFormattedValueWhenValueIsValidAndNotASystemEnv() {
4474
String expected = "someValue";

0 commit comments

Comments
 (0)