Skip to content

Commit

Permalink
Fix #401 - Support config_ordinal in Env and Sys Config Sources. (#403)
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez authored Sep 25, 2020
1 parent 02c6ed9 commit 7b23f21
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 12 deletions.
1 change: 1 addition & 0 deletions implementation/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
<SMALLRYE_MP_CONFIG_PROP>1234</SMALLRYE_MP_CONFIG_PROP>
<smallrye_mp_config_prop_lower>1234</smallrye_mp_config_prop_lower>
<SMALLRYE_MP_CONFIG_EMPTY />
<CONFIG_ORDINAL>301</CONFIG_ORDINAL>
</environmentVariables>
<redirectTestOutputToFile>true</redirectTestOutputToFile>
<enableAssertions>true</enableAssertions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@

package io.smallrye.config;

import java.security.AccessController;
import static io.smallrye.config.common.utils.ConfigSourceUtil.CONFIG_ORDINAL_KEY;
import static java.security.AccessController.doPrivileged;
import static java.util.Collections.unmodifiableMap;

import java.security.PrivilegedAction;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

Expand All @@ -29,17 +31,17 @@
*/
public class EnvConfigSource extends AbstractConfigSource {
private static final long serialVersionUID = -4525015934376795496L;
private static final int DEFAULT_ORDINAL = 300;

private final Map<String, String> cache = new ConcurrentHashMap<>(); //the regex match is expensive

protected EnvConfigSource() {
super("EnvConfigSource", 300);
super("EnvConfigSource", getEnvOrdinal());
}

@Override
public Map<String, String> getProperties() {
return Collections
.unmodifiableMap(AccessController.doPrivileged((PrivilegedAction<Map<String, String>>) System::getenv));
return getEnvProperties();
}

@Override
Expand Down Expand Up @@ -96,4 +98,39 @@ private static String replaceNonAlphanumericByUnderscores(String name) {
}
return sb.toString();
}

private static Map<String, String> getEnvProperties() {
return unmodifiableMap(doPrivileged((PrivilegedAction<Map<String, String>>) System::getenv));
}

/**
* TODO
* Ideally, this should use {@link EnvConfigSource#getValue(String)} directly, so we don't duplicate the property
* names logic, but we need this method to be static.
*
* We do require a bigger change to rewrite {@link EnvConfigSource#getValue(String)} as static and still cache
* values in each separate instance.
*
* @return the {@link EnvConfigSource} ordinal.
*/
private static int getEnvOrdinal() {
Map<String, String> envProperties = getEnvProperties();
String ordStr = envProperties.get(CONFIG_ORDINAL_KEY);
if (ordStr != null) {
return Converters.INTEGER_CONVERTER.convert(ordStr);
}

String sanitazedOrdinalKey = replaceNonAlphanumericByUnderscores(CONFIG_ORDINAL_KEY);
ordStr = envProperties.get(sanitazedOrdinalKey);
if (ordStr != null) {
return Converters.INTEGER_CONVERTER.convert(ordStr);
}

ordStr = envProperties.get(sanitazedOrdinalKey.toUpperCase());
if (ordStr != null) {
return Converters.INTEGER_CONVERTER.convert(ordStr);
}

return DEFAULT_ORDINAL;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,41 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.smallrye.config;

import static io.smallrye.config.common.utils.ConfigSourceUtil.propertiesToMap;
import static java.security.AccessController.doPrivileged;
import static java.util.Collections.unmodifiableMap;

import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Collections;
import java.util.Map;
import java.util.Properties;

import io.smallrye.config.common.AbstractConfigSource;
import io.smallrye.config.common.utils.ConfigSourceUtil;

/**
* @author <a href="http://jmesnil.net/">Jeff Mesnil</a> (c) 2017 Red Hat inc.
*/
class SysPropConfigSource extends AbstractConfigSource {
private static final long serialVersionUID = 9167738611308785403L;
private static final int DEFAULT_ORDINAL = 400;

SysPropConfigSource() {
super("SysPropConfigSource", 400);
super("SysPropConfigSource", ConfigSourceUtil.getOrdinalFromMap(getSystemProperties(), DEFAULT_ORDINAL));
}

@Override
public Map<String, String> getProperties() {
return Collections.unmodifiableMap(
propertiesToMap(AccessController.doPrivileged((PrivilegedAction<Properties>) System::getProperties)));
return getSystemProperties();
}

@Override
public String getValue(String s) {
return AccessController.doPrivileged((PrivilegedAction<String>) () -> System.getProperty(s));
return doPrivileged((PrivilegedAction<String>) () -> System.getProperty(s));
}

private static Map<String, String> getSystemProperties() {
return unmodifiableMap(propertiesToMap(doPrivileged((PrivilegedAction<Properties>) System::getProperties)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,13 @@ void empty() {

assertEquals("", envConfigSource.getValue("SMALLRYE_MP_CONFIG_EMPTY"));
}

@Test
void ordinal() {
SmallRyeConfig config = new SmallRyeConfigBuilder().withSources(new EnvConfigSource()).build();
ConfigSource configSource = config.getConfigSources().iterator().next();

assertTrue(configSource instanceof EnvConfigSource);
assertEquals(configSource.getOrdinal(), 301);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.smallrye.config;

import static org.junit.jupiter.api.Assertions.*;

import org.eclipse.microprofile.config.spi.ConfigSource;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class SysPropConfigSourceTest {
@BeforeEach
void setUp() {
System.setProperty("config_ordinal", "1000");
}

@AfterEach
void tearDown() {
System.clearProperty("config_ordinal");
}

@Test
void ordinal() {
SmallRyeConfig config = new SmallRyeConfigBuilder().withSources(new SysPropConfigSource()).build();
ConfigSource configSource = config.getConfigSources().iterator().next();

assertTrue(configSource instanceof SysPropConfigSource);
assertEquals(configSource.getOrdinal(), 1000);
}
}

0 comments on commit 7b23f21

Please sign in to comment.