From 897ba35f9c493b297f2a2d2a6d9dea743b7d39f8 Mon Sep 17 00:00:00 2001 From: Jorge Hernandez Date: Tue, 12 Feb 2019 07:28:18 -0600 Subject: Support of environment variables in properties Change-Id: I5028ad3b4fb63a96607915e8b424dd2e57696bf0 Issue-ID: POLICY-1514 Signed-off-by: Jorge Hernandez --- policy-utils/pom.xml | 30 +++++++++++++--------- .../org/onap/policy/drools/utils/PropertyUtil.java | 20 +++++++++++---- .../onap/policy/drools/utils/PropertyUtilTest.java | 15 ++++++++++- .../src/test/resources/interpolation.properties | 4 +++ 4 files changed, 51 insertions(+), 18 deletions(-) create mode 100644 policy-utils/src/test/resources/interpolation.properties (limited to 'policy-utils') diff --git a/policy-utils/pom.xml b/policy-utils/pom.xml index 1938ff97..d7df3b97 100644 --- a/policy-utils/pom.xml +++ b/policy-utils/pom.xml @@ -3,7 +3,7 @@ ============LICENSE_START======================================================= ONAP Policy Engine - Drools PDP ================================================================================ - Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + Copyright (C) 2017, 2019 AT&T Intellectual Property. All rights reserved. ================================================================================ Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -19,22 +19,23 @@ ============LICENSE_END========================================================= --> - - 4.0.0 + + 4.0.0 - policy-utils + policy-utils - - org.onap.policy.drools-pdp - drools-pdp - 1.4.0-SNAPSHOT - + + org.onap.policy.drools-pdp + drools-pdp + 1.4.0-SNAPSHOT + - + ch.qos.logback logback-classic - + junit junit @@ -44,7 +45,12 @@ org.onap.policy.common utils ${policy.common.version} - + + + org.apache.commons + commons-configuration2 + 2.4 + diff --git a/policy-utils/src/main/java/org/onap/policy/drools/utils/PropertyUtil.java b/policy-utils/src/main/java/org/onap/policy/drools/utils/PropertyUtil.java index c0014f4d..3891c858 100644 --- a/policy-utils/src/main/java/org/onap/policy/drools/utils/PropertyUtil.java +++ b/policy-utils/src/main/java/org/onap/policy/drools/utils/PropertyUtil.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * policy-utils * ================================================================================ - * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -31,6 +31,7 @@ import java.util.Set; import java.util.Timer; import java.util.TimerTask; +import org.apache.commons.configuration2.ConfigurationConverter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -50,8 +51,10 @@ public class PropertyUtil { private static final Logger logger = LoggerFactory.getLogger(PropertyUtil.class.getName()); /** - * Read in a properties file. - * + * Read in a properties file. Variable interpolation is performed by using + * apache commons configuration2 library. This allows for embedding system properties, + * constants, and environment variables in property files. + * * @param file the properties file * @return a Properties object, containing the associated properties * @throws IOException - subclass 'FileNotFoundException' if the file @@ -60,14 +63,21 @@ public class PropertyUtil { */ public static Properties getProperties(File file) throws IOException { // create an InputStream (may throw a FileNotFoundException) + Properties rval = new Properties(); try (FileInputStream fis = new FileInputStream(file)) { // create the properties instance - Properties rval = new Properties(); // load properties (may throw an IOException) rval.load(fis); - return rval; } + + /* + * Return properties file with environment variables interpolated. + * It is necessary to construct the object in this fashion and avoid + * builders since they use the commons-beanutils (optional) library that has been + * flagged as insecured. + */ + return ConfigurationConverter.getProperties(ConfigurationConverter.getConfiguration(rval)); } /** diff --git a/policy-utils/src/test/java/org/onap/policy/drools/utils/PropertyUtilTest.java b/policy-utils/src/test/java/org/onap/policy/drools/utils/PropertyUtilTest.java index 05440c69..2d5d356c 100644 --- a/policy-utils/src/test/java/org/onap/policy/drools/utils/PropertyUtilTest.java +++ b/policy-utils/src/test/java/org/onap/policy/drools/utils/PropertyUtilTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * policy-utils * ================================================================================ - * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -34,11 +34,18 @@ import java.util.UUID; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; +import org.onap.policy.drools.utils.logging.LoggerUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class PropertyUtilTest { + private static final Logger logger = LoggerFactory.getLogger(PropertyUtilTest.class); + private static final String INTERPOLATION_PROPERTIES = "src/test/resources/interpolation.properties"; + private static final String INTERPOLATION_NO = "interpolation.no"; + private static final String INTERPOLATION_ENV = "interpolation.env"; + private static final String INTERPOLATION_CONST = "interpolation.const"; + private static final String INTERPOLATION_SYS = "interpolation.sys"; private static File directory = null; @@ -128,6 +135,12 @@ public class PropertyUtilTest { // they should match assertEquals(prop1, prop2); + + Properties prop3 = PropertyUtil.getProperties(INTERPOLATION_PROPERTIES); + assertEquals("no", prop3.getProperty(INTERPOLATION_NO)); + assertEquals(System.getenv("HOME"), prop3.getProperty(INTERPOLATION_ENV)); + assertEquals(LoggerUtil.ROOT_LOGGER, prop3.getProperty(INTERPOLATION_CONST)); + assertEquals(System.getProperty("user.home"), prop3.getProperty(INTERPOLATION_SYS)); } /** diff --git a/policy-utils/src/test/resources/interpolation.properties b/policy-utils/src/test/resources/interpolation.properties new file mode 100644 index 00000000..66554337 --- /dev/null +++ b/policy-utils/src/test/resources/interpolation.properties @@ -0,0 +1,4 @@ +interpolation.no=no +interpolation.env=${env:HOME} +interpolation.const=${const:org.onap.policy.drools.utils.logging.LoggerUtil.ROOT_LOGGER} +interpolation.sys=${sys:user.home} -- cgit 1.2.3-korg