Skip to content

Commit

Permalink
Change the environment variable naming for namespaces. wso2#66
Browse files Browse the repository at this point in the history
  • Loading branch information
tharindu1st committed Oct 24, 2018
1 parent 882687a commit b6e096a
Show file tree
Hide file tree
Showing 5 changed files with 240 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
public class ConfigProviderImpl implements ConfigProvider {
private static final Logger logger = LoggerFactory.getLogger(ConfigProviderImpl.class.getName());
private static final String CONFIG_LEVEL_SEPARATOR = "_";
private static final String CONFIG_NAMESPACE_WORD_SEPERATOR = ".";

private static final int CONFIG_MIN_ELEMENT_COUNT = 2;
private static final String[] UNIQUE_ATTRIBUTE_NAMES = {"ID", "NAME"};
private static final String UNIQUE_ATTRIBUTE_SPECIFIER = "UNIQUE";
Expand Down Expand Up @@ -186,16 +188,24 @@ private Map<String, String> filterVariables(String namespace, Map<String, String
// Filter 2: Check if the key format is correct
// Filter 3: Check if the configuration is not empty
// Filter 4: Ignore unique identification specifiers
return variables.entrySet().stream()
.filter(entry -> entry.getKey().toUpperCase()
.startsWith(namespace.toUpperCase() + CONFIG_LEVEL_SEPARATOR))
.filter(entry -> (entry.getKey().split(CONFIG_LEVEL_SEPARATOR, CONFIG_MIN_ELEMENT_COUNT)
.length == CONFIG_MIN_ELEMENT_COUNT))
.filter(entry -> (!entry.getKey().split(CONFIG_LEVEL_SEPARATOR, CONFIG_MIN_ELEMENT_COUNT)[1]
.trim().isEmpty()))
.filter(entry -> (!entry.getKey().split(CONFIG_LEVEL_SEPARATOR, CONFIG_MIN_ELEMENT_COUNT)[1]
.toUpperCase().endsWith(UNIQUE_ATTRIBUTE_SPECIFIER.toUpperCase())))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a, b) -> a, HashMap::new));
HashMap<String, String> map = new HashMap<>();
variables.entrySet().forEach((Map.Entry<String, String> entry) -> {
String environmentVariableNameSpacePart = namespace.toUpperCase().replace
(CONFIG_NAMESPACE_WORD_SEPERATOR, CONFIG_LEVEL_SEPARATOR) + CONFIG_LEVEL_SEPARATOR;
if (entry.getKey().toUpperCase().startsWith(environmentVariableNameSpacePart)) {
String[] splicedEnvironmentVariableParts = entry.getKey().split(environmentVariableNameSpacePart,
CONFIG_MIN_ELEMENT_COUNT);
if (splicedEnvironmentVariableParts.length == CONFIG_MIN_ELEMENT_COUNT) {
if ((!splicedEnvironmentVariableParts[1].trim().isEmpty())) {
if ((!splicedEnvironmentVariableParts[1].toUpperCase().endsWith(UNIQUE_ATTRIBUTE_SPECIFIER
.toUpperCase()))) {
map.putIfAbsent(entry.getKey(), entry.getValue());
}
}
}
}
});
return map;
}

/**
Expand Down Expand Up @@ -239,7 +249,9 @@ private <T> T overrideConfigWithSystemVars(String namespace, T configClass) thro

for (Map.Entry<String, String> entry : systemVariables.entrySet()) {
String systemVarKey = entry.getKey();
String configKey = systemVarKey.split(CONFIG_LEVEL_SEPARATOR, CONFIG_MIN_ELEMENT_COUNT)[1];
String environmentVariableNameSpacePart = namespace.toUpperCase().replace
(CONFIG_NAMESPACE_WORD_SEPERATOR, CONFIG_LEVEL_SEPARATOR) + CONFIG_LEVEL_SEPARATOR;
String configKey = systemVarKey.split(environmentVariableNameSpacePart)[1];
String value = entry.getValue();

List<String> configKeyElements = new ArrayList<>(Arrays.asList(configKey.split(CONFIG_LEVEL_SEPARATOR)));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
package org.wso2.carbon.config.configprovider;

import org.wso2.carbon.config.annotation.Configuration;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

/**
* Sample configuration class for testing purposes.
*
* @since 1.0.0
*/
@XmlRootElement(name = "testconfiguration")
@Configuration(namespace = "wso2.test.config", description = "Test Configurations Bean")
public class ComplexNameSpaceConfiguration {

private String tenant = "default";
private Transports transports = new Transports();

@XmlElement
public void setTenant(String tenant) {
this.tenant = tenant;
}

public String getTenant() {
return tenant;
}

@XmlElement
public void setTransports(Transports transports) {
this.transports = transports;
}

public Transports getTransports() {
return transports;
}
}
Loading

0 comments on commit b6e096a

Please sign in to comment.