Skip to content

Commit

Permalink
Add support for user-specific runtime property files
Browse files Browse the repository at this point in the history
  • Loading branch information
Andre Azzolini committed May 14, 2013
1 parent 5049002 commit 35194ca
Showing 1 changed file with 45 additions and 23 deletions.
Expand Up @@ -17,13 +17,15 @@
package org.broadleafcommerce.common.config;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.util.PropertyPlaceholderHelper;
import org.springframework.util.StringValueResolver;
Expand Down Expand Up @@ -74,18 +76,19 @@ public class RuntimeEnvironmentPropertiesConfigurer extends PropertyPlaceholderC

private static final Log LOG = LogFactory.getLog(RuntimeEnvironmentPropertiesConfigurer.class);

protected static final String SHARED_PROPERTY_OVERRIDE = "property-shared-override";
protected static final String PROPERTY_OVERRIDE = "property-override";

protected static Set<String> defaultEnvironments = new LinkedHashSet<String>();
protected static Set<Resource> blcPropertyLocations = new LinkedHashSet<Resource>();
protected static Set<Resource> defaultPropertyLocations = new LinkedHashSet<Resource>();


static {
defaultEnvironments.add("production");
defaultEnvironments.add("staging");
defaultEnvironments.add("integrationqa");
defaultEnvironments.add("integrationdev");
defaultEnvironments.add("development");
defaultEnvironments.add("local");

blcPropertyLocations.add(new ClassPathResource("config/bc/admin/"));
blcPropertyLocations.add(new ClassPathResource("config/bc/"));
Expand Down Expand Up @@ -134,71 +137,80 @@ public void afterPropertiesSet() throws IOException {
}

String environment = determineEnvironment();

Resource[] blcPropertiesLocation = createBroadleafResource();

Resource[] sharedPropertiesLocation = createSharedPropertiesResource(environment);
Resource[] sharedCommonLocation = createSharedCommonResource();

Resource[] propertiesLocation = createPropertiesResource(environment);
Resource[] commonLocation = createCommonResource();

ArrayList<Resource> allLocations = new ArrayList<Resource>();

/* Process configuration in the following order (later files override earlier files
* common-shared.properties
* [environment]-shared.properties
* common.properties
* [environment].properties */
* [environment].properties
* -Dproperty-override-shared specified value, if any
* -Dproperty-override specified value, if any */

for (Resource resource : blcPropertiesLocation) {
for (Resource resource : createBroadleafResource()) {
if (resource.exists()) {
allLocations.add(resource);
}
}

for (Resource resource : sharedCommonLocation) {
for (Resource resource : createSharedCommonResource()) {
if (resource.exists()) {
allLocations.add(resource);
}
}

for (Resource resource : sharedPropertiesLocation) {
for (Resource resource : createSharedPropertiesResource(environment)) {
if (resource.exists()) {
allLocations.add(resource);
}
}

for (Resource resource : commonLocation) {
for (Resource resource : createCommonResource()) {
if (resource.exists()) {
allLocations.add(resource);
}
}

for (Resource resource : propertiesLocation) {
for (Resource resource : createPropertiesResource(environment)) {
if (resource.exists()) {
allLocations.add(resource);
}
}

Resource sharedPropertyOverride = createSharedOverrideResource();
if (sharedPropertyOverride != null) {
allLocations.add(sharedPropertyOverride);
}

Resource propertyOverride = createOverrideResource();
if (propertyOverride != null) {
allLocations.add(propertyOverride);
}

if (LOG.isDebugEnabled()) {
Properties props = new Properties();
for (Resource resource : allLocations) {
if (resource.exists()) {
props = new Properties(props);
props.load(resource.getInputStream());
for (Entry<Object, Object> entry : props.entrySet()) {
LOG.debug("Read " + entry.getKey() + " as " + entry.getValue());
// We will log source-control managed properties with trace and overrides with info
if (((resource.equals(sharedPropertyOverride) || resource.equals(propertyOverride)) && LOG.isDebugEnabled())
|| LOG.isTraceEnabled()) {
props = new Properties(props);
props.load(resource.getInputStream());
for (Entry<Object, Object> entry : props.entrySet()) {
if (resource.equals(sharedPropertyOverride) || resource.equals(propertyOverride)) {
LOG.debug("Read " + entry.getKey() + " from " + resource.getFilename());
} else {
LOG.trace("Read " + entry.getKey() + " from " + resource.getFilename());
}
}
}
} else {
LOG.debug("Unable to locate resource: " + resource.getFilename());
}
}
}


setLocations(allLocations.toArray(new Resource[] {}));

}

protected Resource[] createSharedPropertiesResource(String environment) throws IOException {
Expand Down Expand Up @@ -252,6 +264,16 @@ protected Resource[] createCommonResource() throws IOException {
}
return resources;
}

protected Resource createSharedOverrideResource() throws IOException {
String path = System.getProperty(SHARED_PROPERTY_OVERRIDE);
return StringUtils.isBlank(path) ? null : new FileSystemResource(path);
}

protected Resource createOverrideResource() throws IOException {
String path = System.getProperty(PROPERTY_OVERRIDE);
return StringUtils.isBlank(path) ? null : new FileSystemResource(path);
}

public String determineEnvironment() {
String environment = keyResolver.resolveRuntimeEnvironmentKey();
Expand Down

0 comments on commit 35194ca

Please sign in to comment.