Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions 3 .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ cache:
addons:
sonarcloud:
organization: "bordertech-github"
token:
secure: $SONAR_TOKEN
token: $SONAR_TOKEN

before_install:
- echo "MAVEN_OPTS='-Xmx512m -XX:MaxPermSize=128m'" > ~/.mavenrc
Expand Down
51 changes: 51 additions & 0 deletions 51 CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Change log

## Release in-progress

### API Changes
* The runtime property `bordertech.config.parameters.useSystemOverWriteOnly` now defaults to false which allows all system properties to be
merged when system properties enabled. Use the optional runtime property `bordertech.config.parameters.useSystemPrefixes` to limit the
properties merged.

### Enhancements
* Latest qa-parent
* Ability to override the default config file name via a system or environment property `BT_CONFIG_FILE=my-config.properties`
* Option to append extra resources to the defined resources via config property `bordertech.config.resources.append`
* Option to load environment variables #31
* Enable via runtime property `bordertech.config.parameters.useEnvProperties=true`
* Option to limit variables merged via property `bordertech.config.parameters.useEnvPrefixes`. Defaults to allow all.

## 1.0.5

### Enhancements
* Latest qa-parent #27
* Improve README #24

## 1.0.4

### Enhancements
* Optional dump parameters to a file. The file name can be set via runtime property `bordertech.config.parameters.dump.file`
* Optional merge system properties into config.
* Enable system properties via runtime property `bordertech.config.parameters.useSystemProperties=true`
* Option to limit system properties merged via runtime property `bordertech.config.parameters.useSystemPrefixes`. Defaults to allow all.
* Defaults to only merge properties that already exist. Disable via runtime property `bordertech.config.parameters.useSystemOverWriteOnly=false`

## 1.0.3

### Enhancements
* Latest qa-parent

## 1.0.2
* Latest qa-parent
* Switch to travis
* The reload of the configuration can be triggered via a touchfile. The touchfile can be set via the runtime property`bordertech.config.touchfile`.
To avoid excessive IO on the touchfile an interval (in milli seconds) between checks can be set via the runtime property
`bordertech.config.touchfile.interval` which defaults to 10000ms.

## 1.0.1
* Ability to define properties to only take effect in a certain environment. When the runtime property `bordertech.config.environment` is set,
it is used as the suffix for each property lookup. If no property exists with the current environment suffix then the default property (ie no
suffix) value is used.

## 1.0.0
* Initial version
26 changes: 20 additions & 6 deletions 26 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=bordertech-java-config&metric=alert_status)](https://sonarcloud.io/dashboard?id=bordertech-java-config)
[![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=bordertech-java-config&metric=reliability_rating)](https://sonarcloud.io/dashboard?id=bordertech-java-config)
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=bordertech-java-config&metric=coverage)](https://sonarcloud.io/dashboard?id=bordertech-java-config)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/738a3851c483470da86ffe1d047f344c)](https://www.codacy.com/app/BorderTech/java-config?utm_source=github.com&utm_medium=referral&utm_content=BorderTech/java-config&utm_campaign=Badge_Grade)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/ff9d14e9be2c4071b5e94bed4c7545cb)](https://www.codacy.com/gh/BorderTech/java-config?utm_source=github.com&utm_medium=referral&utm_content=BorderTech/java-config&utm_campaign=Badge_Grade)
[![Javadocs](https://www.javadoc.io/badge/com.github.bordertech.config/config.svg)](https://www.javadoc.io/doc/com.github.bordertech.config/config)
[![Maven Central](https://img.shields.io/maven-central/v/com.github.bordertech.config/config.svg?label=Maven%20Central)](https://search.maven.org/search?q=g:%22com.github.bordertech.config%22%20AND%20a:%22config%22)

Expand Down Expand Up @@ -87,7 +87,7 @@ include=another.properties

It is possible to define properties to only take effect in a certain environment.

When the environment property is set, it is used as the suffix for each property lookup:
When the runtime property `bordertech.config.environment` is set, it is used as the suffix for each property lookup:

``` java properties
## MOCK Environment
Expand Down Expand Up @@ -121,8 +121,21 @@ Sometimes you may need to include System Properties in the Configuration:
|Property key|Description|Default value|
|-------------|-----------|-------------|
|bordertech.config.parameters.useSystemProperties|This flag allows system properties to be merged into the Configuration at the end of the loading process.|false|
|bordertech.config.parameters.useSystemOverWriteOnly|This flag controls if a system property will only overwrite an existing property|true|
|bordertech.config.parameters.useSystemPrefixes|Define a list of system attribute prefixes that are allowed to be merged. Default is allow all.|n/a|
|bordertech.config.parameters.useSystemOverWriteOnly|This flag controls if a system property will only overwrite an existing property|false|
|bordertech.config.parameters.useSystemPrefixes|Define a list of system property prefixes that are allowed to be merged. Default is allow all.|n/a|

System properties will override properties in resource files.

### Merge Environment Properties into Configuration

Sometimes you may need to include Environment Properties in the Configuration:

|Property key|Description|Default value|
|-------------|-----------|-------------|
|bordertech.config.parameters.useEnvProperties|This flag allows environment properties to be merged into the Configuration at the end of the loading process.|false|
|bordertech.config.parameters.useEnvPrefixes|Define a list of environment property prefixes that are allowed to be merged. Default is allow all.|n/a|

Environemnt properties will override system properties and properties in resource files.

### Merge Configuration into System Properties

Expand Down Expand Up @@ -161,7 +174,7 @@ The following methods in the `Config` class are useful for unit testing:

## Configuration

The initial configuration of `Config` can be overridden by setting properties in a file `bordertech-config.properties`.
The initial configuration of `Config` can be overridden by setting properties in a file `bordertech-config.properties`. The file name can be overriden via a System or Environment property `BT_CONFIG_FILE`.

The following options can be set:-

Expand All @@ -171,6 +184,7 @@ The following options can be set:-
|bordertech.config.spi.enabled|The flag to enable SPI lookup|true|
|bordertech.config.spi.append.default|The flag to append the default configuration|true|
|bordertech.config.resource.order|The list of property resources to load into the configuration. Priority of properties is in reverse order of the list.|bordertech-defaults.properties, bordertech-app.properties, bordertech-local.properties|
|bordertech.config.resource.append|An optional list of extra property resources to append to the resources. Useful to add extra resources to the default resources.|n/a|

### Default Implementation

Expand All @@ -185,7 +199,7 @@ bordertech.config.default.impl=my.example.SpecialConfiguration
Example of loading the default resources and a project specific resource:

``` java properties
bordertech.config.resource.order+=my-project.properties
bordertech.config.resource.append=my-project.properties
```

### SPI
Expand Down
2 changes: 1 addition & 1 deletion 2 pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<parent>
<groupId>com.github.bordertech.common</groupId>
<artifactId>qa-parent</artifactId>
<version>1.0.15</version>
<version>1.0.16</version>
</parent>

<packaging>jar</packaging>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,11 @@ public class DefaultConfiguration implements Configuration {

/**
* If merging System Properties this parameter controls if a system property will only overwrite an existing
* property. The default is true.
* property. The default is false.
*
* @deprecated Use {@link #USE_SYSTEM_PREFIXES} to control which properties can be merged
*/
@Deprecated
public static final String USE_SYSTEM_OVERWRITEONLY = "bordertech.config.parameters.useSystemOverWriteOnly";

/**
Expand All @@ -88,6 +91,18 @@ public class DefaultConfiguration implements Configuration {
*/
public static final String USE_SYSTEM_PREFIXES = "bordertech.config.parameters.useSystemPrefixes";

/**
* If this parameter is defined and resolves to true as a boolean, then the OS Environment properties will be merged
* at the end of the loading process.
*/
public static final String USE_OSENV_PROPERTIES = "bordertech.config.parameters.useEnvProperties";

/**
* If merging OS Environment Properties, this parameter can be used to define a list of attribute prefixes that are
* allowed to be merged. The default is allow all Environment Properties to be merged.
*/
public static final String USE_OSENV_PREFIXES = "bordertech.config.parameters.useEnvPrefixes";

/**
* If this parameter is set to true, then after loading the parameters, they will be dumped to the console.
*/
Expand Down Expand Up @@ -295,6 +310,11 @@ private void load() {
loadSystemProperties();
}

if (isUseOsEnvProperties()) {
recordMessage("Loading from environment properties");
loadEnvironmentProperties();
}

// Now perform variable substitution.
do {
// Do nothing while loop
Expand Down Expand Up @@ -328,6 +348,13 @@ private boolean isUseSystemProperties() {
return getBoolean(USE_SYSTEM_PROPERTIES) || getBoolean(LEGACY_USE_SYSTEM_PROPERTIES);
}

/**
* @return true if load OS Environment properties into config
*/
private boolean isUseOsEnvProperties() {
return getBoolean(USE_OSENV_PROPERTIES);
}

/**
* @return true if dump properties to the console
*/
Expand Down Expand Up @@ -705,33 +732,51 @@ private ClassLoader getParamsClassLoader() {
* Load the System Properties into Config.
*/
private void loadSystemProperties() {

boolean overWriteOnly = getBoolean(USE_SYSTEM_OVERWRITEONLY, true);
boolean overWriteOnly = getBoolean(USE_SYSTEM_OVERWRITEONLY, false);
List<String> allowedPrefixes = getList(USE_SYSTEM_PREFIXES);
System.getProperties().entrySet().forEach(entry
-> mergeExternalProperty("System Properties", (String) entry.getKey(), (String) entry.getValue(), overWriteOnly, allowedPrefixes)
);
}

for (Map.Entry<Object, Object> entry : System.getProperties().entrySet()) {

String key = (String) entry.getKey();
String value = (String) entry.getValue();
/**
* Load the OS Environment Properties into Config.
*/
private void loadEnvironmentProperties() {
List<String> allowedPrefixes = getList(USE_OSENV_PREFIXES);
System.getenv().entrySet().forEach(entry
-> mergeExternalProperty("Environment Properties", entry.getKey(), entry.getValue(), false, allowedPrefixes)
);
}

// Check for "include" keys (should not come from System Properties)
if (INCLUDE.equals(key) || INCLUDE_AFTER.equals(key)) {
continue;
}
/**
* Merge the external property.
*
* @param location the location of the properties
* @param key the property key
* @param value the property value
* @param overWriteOnly true if only overwrite existing properties
* @param allowedPrefixes the list of allowed property prefixes
*/
private void mergeExternalProperty(final String location, final String key, final String value, final boolean overWriteOnly, final List<String> allowedPrefixes) {

// Check allowed prefixes
if (!isAllowedKeyPrefix(allowedPrefixes, key)) {
continue;
}
// Check for "include" keys (should not come from System or Environment Properties)
if (INCLUDE.equals(key) || INCLUDE_AFTER.equals(key)) {
return;
}

// Check overwrite only
if (overWriteOnly && get(key) == null) {
continue;
}
// Check allowed prefixes
if (!isAllowedKeyPrefix(allowedPrefixes, key)) {
return;
}

// Load property
load(key, value, "System Properties");
// Check overwrite only
if (overWriteOnly && get(key) == null) {
return;
}

// Load property
load(key, value, location);
}

/**
Expand Down
69 changes: 62 additions & 7 deletions 69 src/main/java/com/github/bordertech/config/InitHelper.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
package com.github.bordertech.config;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.ConfigurationUtils;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.lang3.StringUtils;

/**
* Helper class for {@link Config} initialisation.
* <p>
* The helper checks for configuration overrides by searching for a property file named <code>BT_CONFIG_FILE</code> in
* the user home directory, the current classpath and the system classpath. The file name can be overridden by setting
* an environment or system property with the key <code>bordertech.config.file</code>.
* </p>
* <p>
* The following properties can be set:-
* <ul>
* <li>bordertech.config.default.impl - Default implementation class name</li>
* <li>bordertech.config.spi.enabled - enable SPI lookup (default: true)</li>
* <li>bordertech.config.spi.append.default - append the default configuration (default: true)</li>
* <li>bordertech.config.resource.order - order of resources to load into the configuration</li>
* <li>bordertech.config.resource.append - append additional resources. This is helpful when adding extra resources to
* the default resources</li>
* </ul>
* <p>
* The default resources Config looks for are:-
Expand All @@ -26,6 +36,10 @@
* <li><code>bordertech-local.properties</code> - local developer properties</li>
* </ul>
*
* <p>
* Resources are ordered lowest to highest priority in determining which property value is used if property keys are
* duplicated.
* </p>
*
* @author Jonathan Austin
* @since 1.0.0
Expand All @@ -34,11 +48,13 @@
*/
public final class InitHelper {

private static final String DEFAULTS_FILE_PARAM_KEY = "BT_CONFIG_FILE";
private static final String DEFAULTS_FILE_NAME = "bordertech-config.properties";
private static final String PARAM_KEY_DEFAULT_CONFIG_IMPL = "bordertech.config.default.impl";
private static final String PARAM_KEY_SPI_ENABLED = "bordertech.config.spi.enabled";
private static final String PARAM_KEY_SPI_APPEND_DEFAULT = "bordertech.config.spi.append.default";
private static final String PARAM_KEY_RESOURCE_ORDER = "bordertech.config.resource.order";
private static final String PARAM_KEY_RESOURCE_APPEND = "bordertech.config.resource.append";
private static final List<String> DEFAULT_BORDERTECH_LOAD_ORDER = Arrays.asList(
// The name of the first resource we look for is for internal default properties
"bordertech-defaults.properties",
Expand All @@ -63,16 +79,15 @@ public final class InitHelper {

static {
// Load the config defaults (if exists)
Configuration configDefaults = loadPropertyFile(DEFAULTS_FILE_NAME);
String configFile = getDefaultConfigFileName();
Configuration configDefaults = loadPropertyFile(configFile);
// Default config impl
DEFAULT_CONFIG_IMPL = configDefaults.getString(PARAM_KEY_DEFAULT_CONFIG_IMPL, DefaultConfiguration.class.getName());
// Check if SPI enabled
SPI_APPEND_DEFAULT_CONFIG = configDefaults.getBoolean(PARAM_KEY_SPI_APPEND_DEFAULT, true);
SPI_ENABLED = configDefaults.getBoolean(PARAM_KEY_SPI_ENABLED, true);
String[] override = configDefaults.getStringArray(PARAM_KEY_RESOURCE_ORDER);
if (override == null || override.length == 0) {
DEFAULT_RESOURCE_LOAD_ORDER = DEFAULT_BORDERTECH_LOAD_ORDER;
} else {
DEFAULT_RESOURCE_LOAD_ORDER = Arrays.asList(override);
}
// Load resource order
DEFAULT_RESOURCE_LOAD_ORDER = getResourceOrder(configDefaults);
}

/**
Expand Down Expand Up @@ -117,4 +132,44 @@ public static Configuration loadPropertyFile(final String fileName) {
return configDefaults;
}

/**
* Check if the default config file name has been overridden via environment or system properties.
*
* @return the default config file name
*/
private static String getDefaultConfigFileName() {
// Check environment variable
String name = System.getenv(DEFAULTS_FILE_PARAM_KEY);
if (!StringUtils.isBlank(name)) {
return name;
}
// Check system property
name = System.getProperty(DEFAULTS_FILE_PARAM_KEY);
// If no system property, return the default file name
return StringUtils.isBlank(name) ? DEFAULTS_FILE_NAME : name;
}

/**
* Retrieve the resource order.
*
* @param configDefaults the config defaults
* @return the list of resources in load order
*/
private static List<String> getResourceOrder(final Configuration configDefaults) {
List<String> resources = new ArrayList<>();
// Check for default resource overrides
String[] override = configDefaults.getStringArray(PARAM_KEY_RESOURCE_ORDER);
if (override == null || override.length == 0) {
resources.addAll(DEFAULT_BORDERTECH_LOAD_ORDER);
} else {
resources.addAll(Arrays.asList(override));
}
// Check for append resources
String[] append = configDefaults.getStringArray(PARAM_KEY_RESOURCE_APPEND);
if (append != null && append.length > 0) {
resources.addAll(Arrays.asList(append));
}
return Collections.unmodifiableList(resources);
}

}
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.