-
Notifications
You must be signed in to change notification settings - Fork 24
Added SystemEnvironmentAwareProperties #338
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ykoer
wants to merge
1
commit into
lightblue-platform:master
Choose a base branch
from
ykoer:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
core/src/main/java/com/redhat/lightblue/client/SystemEnvironmentAwareProperties.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.redhat.lightblue.client; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.Reader; | ||
import java.util.Enumeration; | ||
import java.util.Properties; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* Extended Properties class for working with Strings that have placeholders for system environment variables in them. | ||
* A placeholder takes the form ${env.NAME}. | ||
* | ||
* <p> | ||
* Example: dataServiceURI=${env.LIGHTBLUE_DATA_SERVICE_URL} | ||
* </p> | ||
*/ | ||
public class SystemEnvironmentAwareProperties extends Properties { | ||
|
||
private static final Pattern PROPERTY_PLACEHOLDER_PATTERN = Pattern.compile("\\$\\{env\\.(.*?)\\}"); | ||
|
||
@Override | ||
public synchronized void load(Reader reader) throws IOException { | ||
super.load(reader); | ||
replacePlaceholders(); | ||
} | ||
|
||
@Override | ||
public synchronized void load(InputStream inStream) throws IOException { | ||
super.load(inStream); | ||
replacePlaceholders(); | ||
} | ||
|
||
/** | ||
* Loop through all properties and replace the placeholders of format ${env.NAME} | ||
* with the corresponding property value from the system environment. | ||
*/ | ||
private synchronized void replacePlaceholders() { | ||
for (Enumeration e = super.propertyNames() ; e.hasMoreElements() ;) { | ||
String name = (String)e.nextElement() ; | ||
super.setProperty(name, findAndReplacePlaceholders(super.getProperty(name))); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Replace placeholders in the provided value by the appropriate system environment variable, otherwise return the original value. | ||
* Placeholder referencing non-existing system environment variables are replaced by an empty string. | ||
* | ||
* @param value The property value with or without property placeholders. | ||
* @return the supplied value with placeholders replaced inline | ||
*/ | ||
private String findAndReplacePlaceholders(String value) { | ||
if (value==null) { | ||
return value; | ||
} | ||
|
||
Matcher matchPattern = PROPERTY_PLACEHOLDER_PATTERN.matcher(value); | ||
StringBuffer sb = new StringBuffer(); | ||
while(matchPattern.find()) { | ||
String environmentVariable = System.getenv(matchPattern.group(1)); | ||
matchPattern.appendReplacement(sb, environmentVariable!=null?Matcher.quoteReplacement(environmentVariable):""); | ||
} | ||
matchPattern.appendTail(sb); | ||
return sb.toString(); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
core/src/test/java/com/redhat/lightblue/client/PropertiesLightblueClientSystemEnvTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.redhat.lightblue.client; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.powermock.api.mockito.PowerMockito; | ||
import org.powermock.core.classloader.annotations.PrepareForTest; | ||
import org.powermock.modules.junit4.PowerMockRunner; | ||
|
||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.mockito.Mockito.when; | ||
|
||
/** | ||
* Created by ykoer on 7/31/17. | ||
*/ | ||
@RunWith(PowerMockRunner.class) | ||
@PrepareForTest({System.class, SystemEnvironmentAwareProperties.class}) | ||
public class PropertiesLightblueClientSystemEnvTest { | ||
|
||
@Test | ||
public void shouldReplaceSystemEnvironmentPropertyPlaceholders() { | ||
|
||
PowerMockito.mockStatic(System.class); | ||
when(System.getenv("LB_DATA_SERVICE_URL")).thenReturn("http://this.is.a/test/data"); | ||
when(System.getenv("LB_METADATA_SERVICE_URL")).thenReturn("http://this.is.a/test/metadata"); | ||
|
||
Path resourceWithSystemEnvPlaceholders = Paths.get("lightblue-client-systemenv.properties"); | ||
LightblueClientConfiguration config = PropertiesLightblueClientConfiguration.fromPath(resourceWithSystemEnvPlaceholders); | ||
|
||
assertEquals("http://this.is.a/test/data", config.getDataServiceURI()); | ||
assertEquals("http://this.is.a/test/metadata", config.getMetadataServiceURI()); | ||
|
||
// properties without placeholders should still work | ||
assertEquals("certFile", config.getCertAlias()); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
core/src/test/resources/lightblue-client-systemenv.properties
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
metadataServiceURI=${env.LB_METADATA_SERVICE_URL} | ||
dataServiceURI=${env.LB_DATA_SERVICE_URL} | ||
useCertAuth=true | ||
caFilePath=${env.LB_CACERT_FILE} | ||
certFilePath=${env.LB_CERT_FILE} | ||
certPassword=${env.LB_CERT_PASSWORD} | ||
certAlias=certFile |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
scope=test please, for both