├── .github
└── workflows
│ └── build-and-test.yml
├── .gitignore
├── .travis.yml
├── LICENSE
├── README.md
├── _config.yml
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
└── src
├── integrationTest
├── java
│ └── com
│ │ └── spring
│ │ └── loader
│ │ ├── PojoBindingTest.java
│ │ └── configuration
│ │ ├── BasicTestConfigPojo.java
│ │ ├── SpringBootTestApplication.java
│ │ └── TestNestedPropertiesYaml.java
└── resources
│ ├── basic-props.properties
│ └── test-nested-properties.yml
├── main
└── java
│ └── com
│ └── spring
│ └── loader
│ ├── S3PropertiesLocation.java
│ ├── cloud
│ ├── S3PropertiesContext.java
│ ├── S3PropertySource.java
│ ├── S3Service.java
│ └── S3StreamLoader.java
│ ├── configuration
│ ├── S3PropertiesLoaderConfiguration.java
│ ├── S3PropertiesLocationRegistrar.java
│ └── S3PropertiesSourceConfigurer.java
│ ├── exception
│ ├── EnviromentPropertyNotFoundException.java
│ ├── InvalidS3LocationException.java
│ ├── S3ContextRefreshException.java
│ └── S3ResourceException.java
│ └── util
│ ├── SystemPropertyResolver.java
│ └── WordUtils.java
└── test
├── java
└── com
│ └── spring
│ └── loader
│ ├── cloud
│ └── S3ServiceTest.java
│ ├── configuration
│ ├── S3PropertiesLocationRegistrarTest.java
│ └── S3PropertiesSourceConfigurerTest.java
│ └── util
│ └── SystemPropertyResolverTest.java
└── resources
├── external-config.properties
└── external-config.yaml
/.github/workflows/build-and-test.yml:
--------------------------------------------------------------------------------
1 | name: Run Tests
2 |
3 | on: [push]
4 |
5 | jobs:
6 | tests:
7 | runs-on: ubuntu-latest
8 |
9 | steps:
10 | - uses: actions/checkout@v2
11 | - name: Set up JDK 11
12 | uses: actions/setup-java@v1
13 | with:
14 | java-version: 11
15 | - name: Unit and Integration Tests
16 | run: ./gradlew clean test integrationTest
17 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .classpath
2 | .settings
3 | .project
4 | .class
5 | .DS_Store
6 | .bin
7 | .lock
8 | .gradle
9 | target/
10 | build/
11 | bin/
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: java
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2019 Eric Dallo
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | [](https://travis-ci.org/ericdallo/spring-s3-properties-loader)
2 | # Spring S3 Property Loader
3 |
4 |
5 | _S3 Property Loader_ has the aim of allowing loading of Spring property files from S3 bucket, in order to guarantee stateless machine configuration.
6 |
7 | Spring PropertyConfigurer uses `PropertiesFactoryBean` to load property files from *AWS S3* bucket.
8 |
9 | ## Install
10 | _Gradle_:
11 | ```groovy
12 | repositories {
13 | jcenter()
14 | }
15 | ```
16 | ```groovy
17 | compile "com.spring.loader:s3-loader:3.0.0"
18 | ```
19 | _Maven_:
20 | ```xml
21 |
22 | com.spring.loader
23 | s3-loader
24 | 3.0.0
25 | pom
26 |
27 | ```
28 |
29 | ## How to use
30 |
31 | - Adding this annotation to any spring managed bean
32 | ```java
33 | @S3PropertiesLocation("my-bucket/my-folder/my-properties.yaml")
34 | ```
35 | - Using a specific profile to only load properties if the app is running with that profile
36 | ```java
37 | @S3PropertiesLocation(value = "my-bucket/my-folder/my-properties.properties", profiles = "production")
38 | ```
39 | - Load from a System env variable
40 | ```java
41 | @S3PropertiesLocation(value = "${AWS_S3_LOCATION}", profiles = "developer")
42 | // or
43 | @S3PropertiesLocation(value = "${AWS_S3_BUCKET}/application/my.properties", profiles = "developer")
44 | ```
45 |
46 | ### Binding properties to a POJO
47 | You can bind the externally loaded properties to a POJO as well.
48 |
49 | For e.g., if you have a YAML file as
50 | ```yaml
51 | zuul:
52 | routes:
53 | query1:
54 | path: /api/apps/test1/query/**
55 | stripPrefix: false
56 | url: "https://test.url.com/query1"
57 | query2:
58 | path: /api/apps/test2/query/**
59 | stripPrefix: false
60 | url: "https://test.url.com/query2"
61 | index1:
62 | path: /api/apps/*/index/**
63 | stripPrefix: false
64 | url: "https://test.url.com/index"
65 | ```
66 | Then you can bind the properties to a POJO using ConfigurationProperties:
67 | ```java
68 | @Component
69 | @ConfigurationProperties("zuul")
70 | public class RouteConfig {
71 | private Map> routes = new HashMap<>();
72 |
73 | public void setRoutes(Map> routes) {
74 | this.routes = routes;
75 | }
76 |
77 | public Map> getRoutes() {
78 | return routes;
79 | }
80 | }
81 |
82 | // or
83 |
84 | @Component
85 | @ConfigurationProperties("zuul")
86 | public class RouteConfig {
87 | private Map routes;
88 |
89 | public void setRoutes(Map routes) {
90 | this.routes = routes;
91 | }
92 |
93 | public Map getRoutes() {
94 | return routes;
95 | }
96 |
97 | public static class Route {
98 | private String path;
99 | private boolean stripPrefix;
100 | String url;
101 |
102 | public String getPath() {
103 | return path;
104 | }
105 |
106 | public void setPath(String path) {
107 | this.path = path;
108 | }
109 |
110 | public boolean isStripPrefix() {
111 | return stripPrefix;
112 | }
113 |
114 | public void setStripPrefix(boolean stripPrefix) {
115 | this.stripPrefix = stripPrefix;
116 | }
117 |
118 | public String getUrl() {
119 | return url;
120 | }
121 |
122 | public void setUrl(String url) {
123 | this.url = url;
124 | }
125 |
126 | @Override
127 | public String toString() {
128 | try {
129 | return new ObjectMapper().writeValueAsString(this);
130 | } catch (JsonProcessingException e) {
131 | e.printStackTrace();
132 | }
133 | return this.toString();
134 | }
135 | }
136 |
137 | @Override
138 | public String toString() {
139 | try {
140 | return new ObjectMapper().writeValueAsString(this);
141 | } catch (JsonProcessingException e) {
142 | e.printStackTrace();
143 | }
144 | return this.toString();
145 | }
146 | }
147 | ```
148 |
149 | ### Refreshing properties in runtime
150 |
151 | You can force your application to load properties from S3 again without restart. _S3 Properties Loader_ uses a [Spring Cloud](http://projects.spring.io/spring-cloud/) feature that allows the spring beans annotated with `@RefreshScope` to reload properties.
152 | To work, *it is only necessary* to inject the `S3PropertiesContext` bean and call `refresh()` method. After this, _S3 Properties Loader_ will get properties again from s3 bucket defined previously and refresh your beans annotated with `@RefreshScope`.
153 |
154 | _tip_: You can create a endpoint that calls this class and refresh your application via endpoint or create a `@Scheduled` class which updates from time to time.
155 |
156 | Example:
157 | ```java
158 | @RestController
159 | public SomeController {
160 |
161 | @Autowired
162 | private S3PropertiesContext s3PropertiesContext;
163 |
164 | @PostMapping("/refresh-properties")
165 | public void refresh() {
166 | s3PropertiesContext.refresh();
167 | }
168 | }
169 | ```
170 | ## Requisites
171 |
172 | Official [spring aws sdk lib](https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-aws).
173 |
174 | ## Problems and Issues
175 |
176 | Found some bug? Have some enhancement ? Open a Issue [here](https://github.com/ericdallo/spring-s3-properties-loader/issues)
177 |
--------------------------------------------------------------------------------
/_config.yml:
--------------------------------------------------------------------------------
1 | theme: jekyll-theme-cayman
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | import org.springframework.boot.gradle.plugin.SpringBootPlugin
2 |
3 | plugins {
4 | id 'java'
5 | id 'eclipse'
6 | id 'application'
7 | id 'net.researchgate.release' version '2.6.0'
8 | id 'maven-publish'
9 | id 'maven'
10 | id 'signing'
11 | id 'com.jfrog.bintray' version '1.8.5'
12 |
13 | id 'org.springframework.boot' version '2.3.5.RELEASE' apply false
14 |
15 | // for separating out unit and integration tests
16 | id 'org.unbroken-dome.test-sets' version '3.0.1'
17 | }
18 |
19 | compileJava.options.encoding = 'UTF-8'
20 |
21 | group = 'com.spring.loader'
22 | archivesBaseName = 's3-loader'
23 |
24 | eclipse {
25 | classpath {
26 | downloadJavadoc = true
27 | downloadSources = true
28 | }
29 | }
30 |
31 | release {
32 | failOnCommitNeeded = false
33 | failOnPublishNeeded = true
34 | failOnSnapshotDependencies = true
35 | failOnUnversionedFiles = true
36 | failOnUpdateNeeded = true
37 | revertOnFail = true
38 | }
39 |
40 | afterReleaseBuild.dependsOn publish
41 |
42 | bintray {
43 | user = System.getenv('BINTRAY_USER')
44 | key = System.getenv('BINTRAY_KEY')
45 | configurations = ['archives']
46 | pkg {
47 | repo = 'spring-properties-loader'
48 | name = 'spring-s3-properties-loader'
49 | licenses = ['Apache-2.0']
50 | vcsUrl = 'https://github.com/ericdallo/spring-s3-properties-loader.git'
51 | }
52 | }
53 |
54 | repositories {
55 | mavenCentral()
56 | mavenLocal()
57 | }
58 |
59 | dependencies {
60 | // annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")
61 |
62 | implementation "org.springframework:spring-context:5.3.0"
63 | implementation "org.springframework.cloud:spring-cloud-aws-core:2.2.4.RELEASE"
64 | implementation "org.springframework.cloud:spring-cloud-context:2.2.5.RELEASE"
65 |
66 | testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
67 | testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
68 |
69 | testImplementation "org.assertj:assertj-core:3.18.0"
70 | testImplementation "org.mockito:mockito-inline:3.6.0"
71 | testImplementation "org.mockito:mockito-junit-jupiter:3.6.0"
72 | testRuntimeOnly "org.yaml:snakeyaml:1.27"
73 |
74 | testImplementation enforcedPlatform(SpringBootPlugin.BOM_COORDINATES)
75 | testImplementation 'cloud.localstack:localstack-utils:0.2.5'
76 | testImplementation 'com.amazonaws:aws-java-sdk-s3:1.11.896'
77 | testImplementation 'org.springframework.boot:spring-boot-starter-web'
78 | testImplementation('org.springframework.boot:spring-boot-starter-test') {
79 | exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
80 | }
81 | }
82 |
83 | task sourcesJar(type: Jar) {
84 | from sourceSets.main.allSource
85 | archiveClassifier = 'sources'
86 | }
87 |
88 | artifacts {
89 | archives jar
90 | archives sourcesJar
91 | }
92 |
93 | testSets {
94 | integrationTest
95 | }
96 |
97 | check.dependsOn integrationTest
98 |
99 | // Make all tests use JUnit 5
100 | tasks.withType(Test) {
101 | useJUnitPlatform()
102 | }
103 |
104 | mainClassName = ''
105 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | version=3.0.0
2 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ericdallo/spring-s3-properties-loader/0e6e708cfe5cda5869b5675921d8a006253b437b/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | distributionBase=GRADLE_USER_HOME
2 | distributionPath=wrapper/dists
3 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-bin.zip
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | #
4 | # Copyright 2015 the original author or authors.
5 | #
6 | # Licensed under the Apache License, Version 2.0 (the "License");
7 | # you may not use this file except in compliance with the License.
8 | # You may obtain a copy of the License at
9 | #
10 | # https://www.apache.org/licenses/LICENSE-2.0
11 | #
12 | # Unless required by applicable law or agreed to in writing, software
13 | # distributed under the License is distributed on an "AS IS" BASIS,
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | # See the License for the specific language governing permissions and
16 | # limitations under the License.
17 | #
18 |
19 | ##############################################################################
20 | ##
21 | ## Gradle start up script for UN*X
22 | ##
23 | ##############################################################################
24 |
25 | # Attempt to set APP_HOME
26 | # Resolve links: $0 may be a link
27 | PRG="$0"
28 | # Need this for relative symlinks.
29 | while [ -h "$PRG" ] ; do
30 | ls=`ls -ld "$PRG"`
31 | link=`expr "$ls" : '.*-> \(.*\)$'`
32 | if expr "$link" : '/.*' > /dev/null; then
33 | PRG="$link"
34 | else
35 | PRG=`dirname "$PRG"`"/$link"
36 | fi
37 | done
38 | SAVED="`pwd`"
39 | cd "`dirname \"$PRG\"`/" >/dev/null
40 | APP_HOME="`pwd -P`"
41 | cd "$SAVED" >/dev/null
42 |
43 | APP_NAME="Gradle"
44 | APP_BASE_NAME=`basename "$0"`
45 |
46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
48 |
49 | # Use the maximum available, or set MAX_FD != -1 to use that value.
50 | MAX_FD="maximum"
51 |
52 | warn () {
53 | echo "$*"
54 | }
55 |
56 | die () {
57 | echo
58 | echo "$*"
59 | echo
60 | exit 1
61 | }
62 |
63 | # OS specific support (must be 'true' or 'false').
64 | cygwin=false
65 | msys=false
66 | darwin=false
67 | nonstop=false
68 | case "`uname`" in
69 | CYGWIN* )
70 | cygwin=true
71 | ;;
72 | Darwin* )
73 | darwin=true
74 | ;;
75 | MINGW* )
76 | msys=true
77 | ;;
78 | NONSTOP* )
79 | nonstop=true
80 | ;;
81 | esac
82 |
83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
84 |
85 |
86 | # Determine the Java command to use to start the JVM.
87 | if [ -n "$JAVA_HOME" ] ; then
88 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
89 | # IBM's JDK on AIX uses strange locations for the executables
90 | JAVACMD="$JAVA_HOME/jre/sh/java"
91 | else
92 | JAVACMD="$JAVA_HOME/bin/java"
93 | fi
94 | if [ ! -x "$JAVACMD" ] ; then
95 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
96 |
97 | Please set the JAVA_HOME variable in your environment to match the
98 | location of your Java installation."
99 | fi
100 | else
101 | JAVACMD="java"
102 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
103 |
104 | Please set the JAVA_HOME variable in your environment to match the
105 | location of your Java installation."
106 | fi
107 |
108 | # Increase the maximum file descriptors if we can.
109 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
110 | MAX_FD_LIMIT=`ulimit -H -n`
111 | if [ $? -eq 0 ] ; then
112 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
113 | MAX_FD="$MAX_FD_LIMIT"
114 | fi
115 | ulimit -n $MAX_FD
116 | if [ $? -ne 0 ] ; then
117 | warn "Could not set maximum file descriptor limit: $MAX_FD"
118 | fi
119 | else
120 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
121 | fi
122 | fi
123 |
124 | # For Darwin, add options to specify how the application appears in the dock
125 | if $darwin; then
126 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
127 | fi
128 |
129 | # For Cygwin or MSYS, switch paths to Windows format before running java
130 | if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
131 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
132 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
133 |
134 | JAVACMD=`cygpath --unix "$JAVACMD"`
135 |
136 | # We build the pattern for arguments to be converted via cygpath
137 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
138 | SEP=""
139 | for dir in $ROOTDIRSRAW ; do
140 | ROOTDIRS="$ROOTDIRS$SEP$dir"
141 | SEP="|"
142 | done
143 | OURCYGPATTERN="(^($ROOTDIRS))"
144 | # Add a user-defined pattern to the cygpath arguments
145 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
146 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
147 | fi
148 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
149 | i=0
150 | for arg in "$@" ; do
151 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
152 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
153 |
154 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
155 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
156 | else
157 | eval `echo args$i`="\"$arg\""
158 | fi
159 | i=`expr $i + 1`
160 | done
161 | case $i in
162 | 0) set -- ;;
163 | 1) set -- "$args0" ;;
164 | 2) set -- "$args0" "$args1" ;;
165 | 3) set -- "$args0" "$args1" "$args2" ;;
166 | 4) set -- "$args0" "$args1" "$args2" "$args3" ;;
167 | 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
168 | 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
169 | 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
170 | 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
171 | 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
172 | esac
173 | fi
174 |
175 | # Escape application args
176 | save () {
177 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
178 | echo " "
179 | }
180 | APP_ARGS=`save "$@"`
181 |
182 | # Collect all arguments for the java command, following the shell quoting and substitution rules
183 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
184 |
185 | exec "$JAVACMD" "$@"
186 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @rem
2 | @rem Copyright 2015 the original author or authors.
3 | @rem
4 | @rem Licensed under the Apache License, Version 2.0 (the "License");
5 | @rem you may not use this file except in compliance with the License.
6 | @rem You may obtain a copy of the License at
7 | @rem
8 | @rem https://www.apache.org/licenses/LICENSE-2.0
9 | @rem
10 | @rem Unless required by applicable law or agreed to in writing, software
11 | @rem distributed under the License is distributed on an "AS IS" BASIS,
12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | @rem See the License for the specific language governing permissions and
14 | @rem limitations under the License.
15 | @rem
16 |
17 | @if "%DEBUG%" == "" @echo off
18 | @rem ##########################################################################
19 | @rem
20 | @rem Gradle startup script for Windows
21 | @rem
22 | @rem ##########################################################################
23 |
24 | @rem Set local scope for the variables with windows NT shell
25 | if "%OS%"=="Windows_NT" setlocal
26 |
27 | set DIRNAME=%~dp0
28 | if "%DIRNAME%" == "" set DIRNAME=.
29 | set APP_BASE_NAME=%~n0
30 | set APP_HOME=%DIRNAME%
31 |
32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter.
33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
34 |
35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
37 |
38 | @rem Find java.exe
39 | if defined JAVA_HOME goto findJavaFromJavaHome
40 |
41 | set JAVA_EXE=java.exe
42 | %JAVA_EXE% -version >NUL 2>&1
43 | if "%ERRORLEVEL%" == "0" goto execute
44 |
45 | echo.
46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
47 | echo.
48 | echo Please set the JAVA_HOME variable in your environment to match the
49 | echo location of your Java installation.
50 |
51 | goto fail
52 |
53 | :findJavaFromJavaHome
54 | set JAVA_HOME=%JAVA_HOME:"=%
55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
56 |
57 | if exist "%JAVA_EXE%" goto execute
58 |
59 | echo.
60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
61 | echo.
62 | echo Please set the JAVA_HOME variable in your environment to match the
63 | echo location of your Java installation.
64 |
65 | goto fail
66 |
67 | :execute
68 | @rem Setup the command line
69 |
70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
71 |
72 |
73 | @rem Execute Gradle
74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
75 |
76 | :end
77 | @rem End local scope for the variables with windows NT shell
78 | if "%ERRORLEVEL%"=="0" goto mainEnd
79 |
80 | :fail
81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
82 | rem the _cmd.exe /c_ return code!
83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
84 | exit /b 1
85 |
86 | :mainEnd
87 | if "%OS%"=="Windows_NT" endlocal
88 |
89 | :omega
90 |
--------------------------------------------------------------------------------
/src/integrationTest/java/com/spring/loader/PojoBindingTest.java:
--------------------------------------------------------------------------------
1 | package com.spring.loader;
2 |
3 | import cloud.localstack.awssdkv1.TestUtils;
4 | import cloud.localstack.docker.LocalstackDockerExtension;
5 | import cloud.localstack.docker.annotation.LocalstackDockerProperties;
6 | import com.amazonaws.services.s3.AmazonS3;
7 | import com.spring.loader.configuration.BasicTestConfigPojo;
8 | import com.spring.loader.configuration.SpringBootTestApplication;
9 | import com.spring.loader.configuration.TestNestedPropertiesYaml;
10 | import org.junit.jupiter.api.BeforeAll;
11 | import org.junit.jupiter.api.Test;
12 | import org.junit.jupiter.api.extension.ExtendWith;
13 | import org.springframework.beans.factory.annotation.Autowired;
14 | import org.springframework.beans.factory.annotation.Value;
15 | import org.springframework.boot.test.context.SpringBootTest;
16 | import org.springframework.boot.test.context.TestConfiguration;
17 | import org.springframework.context.annotation.Bean;
18 | import org.springframework.context.annotation.Import;
19 | import org.springframework.context.annotation.Primary;
20 |
21 | import java.nio.file.Paths;
22 |
23 | import static org.junit.jupiter.api.Assertions.*;
24 |
25 | @ExtendWith(LocalstackDockerExtension.class)
26 | @LocalstackDockerProperties(services = { "s3" })
27 | @Import( { S3Config.class, BasicTestConfigPojo.class, TestNestedPropertiesYaml.class })
28 | @S3PropertiesLocation( { "integration-test/basic-props.properties", "integration-test/test-nested-properties.yml" } )
29 | @SpringBootTest (classes = { SpringBootTestApplication.class }, webEnvironment = SpringBootTest.WebEnvironment.NONE)
30 | public class PojoBindingTest {
31 |
32 | @Autowired
33 | AmazonS3 amazonS3Client;
34 |
35 | @Autowired
36 | TestNestedPropertiesYaml testNestedPropertiesYaml;
37 |
38 | @Autowired
39 | BasicTestConfigPojo basicTestConfigPojo;
40 |
41 | @Value("${test.property1}")
42 | String testProp1;
43 |
44 | @BeforeAll
45 | public static void init() {
46 | AmazonS3 s3 = TestUtils.getClientS3();
47 |
48 | s3.createBucket("integration-test");
49 | s3.putObject("integration-test", "test-nested-properties.yml",
50 | Paths.get("./src/integrationTest/resources/test-nested-properties.yml").toFile());
51 | s3.putObject("integration-test", "basic-props.properties",
52 | Paths.get("./src/integrationTest/resources/basic-props.properties").toFile());
53 | }
54 |
55 | @Test
56 | public void testS3PropertiesAreBoundedToPojo() {
57 | assertEquals("value1", testProp1);
58 | assertEquals("value1", basicTestConfigPojo.getProperty1());
59 | assertEquals("value2", basicTestConfigPojo.getProperty2());
60 |
61 | assertEquals(testNestedPropertiesYaml.getRoutes().size(), 3);
62 | assertNotNull(testNestedPropertiesYaml.getRoutes().get("index"));
63 | assertNotNull(testNestedPropertiesYaml.getRoutes().get("query1"));
64 | assertNotNull(testNestedPropertiesYaml.getRoutes().get("query2"));
65 | assertTrue(testNestedPropertiesYaml.getRoutes().get("index").isStripPrefix());
66 | assertEquals("https://test.url.com/query1", testNestedPropertiesYaml.getRoutes().get("query1").getUrl());
67 | assertEquals("/api/apps/test2/query/**", testNestedPropertiesYaml.getRoutes().get("query2").getPath());
68 | }
69 | }
70 |
71 | @TestConfiguration
72 | class S3Config {
73 |
74 | @Primary
75 | @Bean
76 | public AmazonS3 amazonS3Client() {
77 | return TestUtils.getClientS3();
78 | }
79 | }
80 |
--------------------------------------------------------------------------------
/src/integrationTest/java/com/spring/loader/configuration/BasicTestConfigPojo.java:
--------------------------------------------------------------------------------
1 | package com.spring.loader.configuration;
2 |
3 | import org.springframework.boot.context.properties.ConfigurationProperties;
4 | import org.springframework.boot.test.context.TestConfiguration;
5 |
6 | @TestConfiguration
7 | @ConfigurationProperties("test")
8 | public class BasicTestConfigPojo {
9 | String property1;
10 | String property2;
11 |
12 | public String getProperty1() {
13 | return property1;
14 | }
15 |
16 | public void setProperty1(String property1) {
17 | this.property1 = property1;
18 | }
19 |
20 | public String getProperty2() {
21 | return property2;
22 | }
23 |
24 | public void setProperty2(String property2) {
25 | this.property2 = property2;
26 | }
27 |
28 | @Override
29 | public String toString() {
30 | return new StringBuilder().append("{ ")
31 | .append("property1=").append(property1)
32 | .append(", ")
33 | .append("property2=").append(property2)
34 | .append(" }").toString();
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/src/integrationTest/java/com/spring/loader/configuration/SpringBootTestApplication.java:
--------------------------------------------------------------------------------
1 | package com.spring.loader.configuration;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 | import org.springframework.boot.context.properties.EnableConfigurationProperties;
6 |
7 | @SpringBootApplication
8 | @EnableConfigurationProperties
9 | public class SpringBootTestApplication {
10 |
11 | public static void main(String[] args) {
12 | SpringApplication.run(SpringBootTestApplication.class, args);
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/src/integrationTest/java/com/spring/loader/configuration/TestNestedPropertiesYaml.java:
--------------------------------------------------------------------------------
1 | package com.spring.loader.configuration;
2 |
3 | import org.springframework.boot.context.properties.ConfigurationProperties;
4 | import org.springframework.boot.test.context.TestConfiguration;
5 |
6 | import java.util.Collections;
7 | import java.util.Map;
8 |
9 | @TestConfiguration
10 | @ConfigurationProperties("zuul")
11 | public class TestNestedPropertiesYaml {
12 | private Map routes = Collections.emptyMap();
13 |
14 | public void setRoutes(Map routes) {
15 | this.routes = routes;
16 | }
17 |
18 | public Map getRoutes() {
19 | return routes;
20 | }
21 |
22 | public static class Route {
23 | private String path;
24 | private boolean stripPrefix;
25 | private String url;
26 |
27 | public String getPath() {
28 | return path;
29 | }
30 |
31 | public void setPath(String path) {
32 | this.path = path;
33 | }
34 |
35 | public boolean isStripPrefix() {
36 | return stripPrefix;
37 | }
38 |
39 | public void setStripPrefix(boolean stripPrefix) {
40 | this.stripPrefix = stripPrefix;
41 | }
42 |
43 | public String getUrl() {
44 | return url;
45 | }
46 |
47 | public void setUrl(String url) {
48 | this.url = url;
49 | }
50 |
51 | @Override
52 | public String toString() {
53 | return new StringBuilder().append("{ ")
54 | .append("path=").append(path)
55 | .append(", ")
56 | .append("stripPrefix=").append(stripPrefix)
57 | .append(", ")
58 | .append("url=").append(url)
59 | .append(" }").toString();
60 | }
61 | }
62 |
63 | @Override
64 | public String toString() {
65 | return "{ routes = " + routes + " }";
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/src/integrationTest/resources/basic-props.properties:
--------------------------------------------------------------------------------
1 | test.property1=value1
2 | test.property2=value2
3 |
--------------------------------------------------------------------------------
/src/integrationTest/resources/test-nested-properties.yml:
--------------------------------------------------------------------------------
1 | zuul:
2 | routes:
3 | query1:
4 | path: /api/apps/test1/query/**
5 | stripPrefix: false
6 | url: "https://test.url.com/query1"
7 | query2:
8 | path: /api/apps/test2/query/**
9 | stripPrefix: false
10 | url: "https://test.url.com/query2"
11 | index:
12 | path: /api/apps/*/index/**
13 | stripPrefix: true
14 | url: "https://test.url.com/index"
15 |
--------------------------------------------------------------------------------
/src/main/java/com/spring/loader/S3PropertiesLocation.java:
--------------------------------------------------------------------------------
1 | package com.spring.loader;
2 |
3 | import java.lang.annotation.Documented;
4 | import java.lang.annotation.ElementType;
5 | import java.lang.annotation.Retention;
6 | import java.lang.annotation.RetentionPolicy;
7 | import java.lang.annotation.Target;
8 |
9 | import org.springframework.context.annotation.Import;
10 |
11 | import com.spring.loader.configuration.S3PropertiesLocationRegistrar;
12 | import com.spring.loader.cloud.S3PropertySource;
13 | import com.spring.loader.configuration.S3PropertiesLoaderConfiguration;
14 |
15 | /**
16 | * Allow the auto configuration of the {@link S3PropertySource} bean.
17 | *
18 | * @author Eric Dallo
19 | * @since 1.0.3
20 | * @see S3PropertiesLocationRegistrar
21 | */
22 | @Target(ElementType.TYPE)
23 | @Retention(RetentionPolicy.RUNTIME)
24 | @Import({ S3PropertiesLoaderConfiguration.class, S3PropertiesLocationRegistrar.class })
25 | @Documented
26 | public @interface S3PropertiesLocation {
27 |
28 | /**
29 | * The location of the properties in aws s3.
30 | *
31 | * @return the path of aws s3 properties e.g. "my-bucket/my-folder/app.properties"
32 | * or a enviroment system to the s3 path e.g. "${MY_BUCKET_IN_AWS_S3}"
33 | */
34 | String[] value();
35 |
36 | /**
37 | * The profiles to load the properties in aws s3.
38 | *
39 | * @return the profile name e.g. "prod"
40 | */
41 | String[] profiles() default {};
42 | }
43 |
--------------------------------------------------------------------------------
/src/main/java/com/spring/loader/cloud/S3PropertiesContext.java:
--------------------------------------------------------------------------------
1 | package com.spring.loader.cloud;
2 |
3 | import static org.springframework.util.ClassUtils.getUserClass;
4 |
5 | import java.util.Map.Entry;
6 | import java.util.Properties;
7 |
8 | import org.slf4j.Logger;
9 | import org.slf4j.LoggerFactory;
10 | import org.springframework.cloud.context.config.annotation.RefreshScope;
11 | import org.springframework.cloud.context.environment.EnvironmentManager;
12 | import org.springframework.cloud.context.refresh.ContextRefresher;
13 | import org.springframework.context.ApplicationContext;
14 |
15 | import com.amazonaws.services.s3.model.S3Object;
16 | import com.spring.loader.S3PropertiesLocation;
17 | import com.spring.loader.exception.S3ContextRefreshException;
18 |
19 | /**
20 | * Manage the context of properties from S3
21 | *
22 | * @author Eric Dallo
23 | * @since 2.1
24 | * @see S3PropertiesLocation
25 | */
26 | public class S3PropertiesContext {
27 |
28 | private static final Logger LOGGER = LoggerFactory.getLogger(S3PropertiesContext.class);
29 |
30 | private final ApplicationContext applicationContext;
31 | private final EnvironmentManager environment;
32 | private final ContextRefresher contextRefresher;
33 | private final S3Service s3Service;
34 |
35 | public S3PropertiesContext(ApplicationContext applicationContext, EnvironmentManager environment, ContextRefresher contextRefresher, S3Service s3Service) {
36 | this.applicationContext = applicationContext;
37 | this.environment = environment;
38 | this.contextRefresher = contextRefresher;
39 | this.s3Service = s3Service;
40 | }
41 |
42 | /**
43 | * Allows the feature of refresh beans annotated with {@link RefreshScope} of spring cloud.
44 | * The annotated beans will be updated with the new properties of the location setted previously
45 | * from {@link S3PropertiesLocation#value() }
46 | *
47 | * @throws S3ContextRefreshException for any error on refresh properties
48 | * @see RefreshScope
49 | */
50 | public void refresh() {
51 | try {
52 | Object annotatedBean = applicationContext.getBeansWithAnnotation(S3PropertiesLocation.class).values().iterator().next();
53 |
54 | String[] locations = getUserClass(annotatedBean).getAnnotation(S3PropertiesLocation.class).value();
55 |
56 | for (String location : locations) {
57 |
58 | Properties properties = new Properties();
59 | S3Object s3Object = s3Service.retriveFrom(location);
60 |
61 | properties.load(s3Object.getObjectContent());
62 |
63 | for (Entry