diff options
Diffstat (limited to 'policy-utils')
3 files changed, 111 insertions, 67 deletions
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 3891c858..8594995d 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 @@ -32,6 +32,7 @@ import java.util.Timer; import java.util.TimerTask; import org.apache.commons.configuration2.ConfigurationConverter; +import org.apache.commons.configuration2.SystemConfiguration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -77,12 +78,12 @@ public class PropertyUtil { * builders since they use the commons-beanutils (optional) library that has been * flagged as insecured. */ - return ConfigurationConverter.getProperties(ConfigurationConverter.getConfiguration(rval)); + return getInterpolatedProperties(rval); } /** * Read in a properties file. - * + * * @param fileName the properties file * @return a Properties object, containing the associated properties * @throws IOException - subclass 'FileNotFoundException' if the file @@ -93,6 +94,88 @@ public class PropertyUtil { return getProperties(new File(fileName)); } + /** + * Read in a properties file, and register for update notifications. + * NOTE: it is possible that the first callback will occur while this + * method is still in progress. To avoid this problem, use 'synchronized' + * blocks around this invocation and in the callback -- that will ensure + * that the processing of the initial properties complete before any + * updates are processed. + * + * @param file the properties file + * @param listener notify if not null, this is a callback interface that is used for + * notifications of changes + * @return a Properties object, containing the associated properties + * @throws IOException - subclass 'FileNotFoundException' if the file + * does not exist or can't be opened, and 'IOException' if there is + * a problem loading the properties file. + */ + public static Properties getProperties(File file, Listener listener) + throws IOException { + File propFile = file; + if (listener == null) { + // no listener specified -- just fetch the properties + return getProperties(propFile); + } + + // Convert the file to a canonical form in order to avoid the situation + // where different names refer to the same file. + propFile = propFile.getCanonicalFile(); + + // See if there is an existing registration. The 'synchronized' block + // is needed to handle the case where a new listener is added at about + // the same time that another one is being removed. + synchronized (registrations) { + ListenerRegistration reg = registrations.get(propFile); + if (reg == null) { + // a new registration is needed + reg = new ListenerRegistration(propFile); + } + return reg.addListener(listener); + } + } + + /** + * Read in a properties file, and register for update notifications. + * NOTE: it is possible that the first callback will occur while this + * method is still in progress. To avoid this problem, use 'synchronized' + * blocks around this invocation and in the callback -- that will ensure + * that the processing of the initial properties complete before any + * updates are processed. + * + * @param fileName the properties file + * @param listener notify if not null, this is a callback interface that is used for + * notifications of changes + * @return a Properties object, containing the associated properties + * @throws IOException - subclass 'FileNotFoundException' if the file + * does not exist or can't be opened, and 'IOException' if there is + * a problem loading the properties file. + */ + public static Properties getProperties(String fileName, Listener listener) + throws IOException { + return getProperties(new File(fileName), listener); + } + + /** + * gets interpolated properties from a properties object. + * + * @param properties object + * @return properties + */ + public static Properties getInterpolatedProperties(Properties properties) { + return ConfigurationConverter.getProperties(ConfigurationConverter.getConfiguration(properties)); + } + + /** + * sets system properties from a properties file. + * + * @param properties properties file + */ + public static void setSystemProperties(Properties properties) { + Properties interpolatedProps = getInterpolatedProperties(properties); + SystemConfiguration.setSystemProperties(ConfigurationConverter.getConfiguration(interpolatedProps)); + } + /* ============================================================ */ /** @@ -277,68 +360,6 @@ public class PropertyUtil { } /** - * Read in a properties file, and register for update notifications. - * NOTE: it is possible that the first callback will occur while this - * method is still in progress. To avoid this problem, use 'synchronized' - * blocks around this invocation and in the callback -- that will ensure - * that the processing of the initial properties complete before any - * updates are processed. - * - * @param file the properties file - * @param listener notify if not null, this is a callback interface that is used for - * notifications of changes - * @return a Properties object, containing the associated properties - * @throws IOException - subclass 'FileNotFoundException' if the file - * does not exist or can't be opened, and 'IOException' if there is - * a problem loading the properties file. - */ - public static Properties getProperties(File file, Listener listener) - throws IOException { - File propFile = file; - if (listener == null) { - // no listener specified -- just fetch the properties - return getProperties(propFile); - } - - // Convert the file to a canonical form in order to avoid the situation - // where different names refer to the same file. - propFile = propFile.getCanonicalFile(); - - // See if there is an existing registration. The 'synchronized' block - // is needed to handle the case where a new listener is added at about - // the same time that another one is being removed. - synchronized (registrations) { - ListenerRegistration reg = registrations.get(propFile); - if (reg == null) { - // a new registration is needed - reg = new ListenerRegistration(propFile); - } - return reg.addListener(listener); - } - } - - /** - * Read in a properties file, and register for update notifications. - * NOTE: it is possible that the first callback will occur while this - * method is still in progress. To avoid this problem, use 'synchronized' - * blocks around this invocation and in the callback -- that will ensure - * that the processing of the initial properties complete before any - * updates are processed. - * - * @param fileName the properties file - * @param listener notify if not null, this is a callback interface that is used for - * notifications of changes - * @return a Properties object, containing the associated properties - * @throws IOException - subclass 'FileNotFoundException' if the file - * does not exist or can't be opened, and 'IOException' if there is - * a problem loading the properties file. - */ - public static Properties getProperties(String fileName, Listener listener) - throws IOException { - return getProperties(new File(fileName), listener); - } - - /** * Stop listenening for updates. * * @param file the properties file diff --git a/policy-utils/src/main/java/org/onap/policy/drools/utils/logging/LoggerUtil.java b/policy-utils/src/main/java/org/onap/policy/drools/utils/logging/LoggerUtil.java index 0fe848c9..1697f697 100644 --- a/policy-utils/src/main/java/org/onap/policy/drools/utils/logging/LoggerUtil.java +++ b/policy-utils/src/main/java/org/onap/policy/drools/utils/logging/LoggerUtil.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * 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,16 @@ import org.slf4j.MarkerFactory; public class LoggerUtil { /** + * Logback configuration file system property. + */ + public static final String LOGBACK_CONFIGURATION_FILE_SYSTEM_PROPERTY = "logback.configurationFile"; + + /** + * Logback default configuration file location. + */ + public static final String LOGBACK_CONFIGURATION_FILE_DEFAULT = "config/logback.xml"; + + /** * Root logger. */ public static final String ROOT_LOGGER = "ROOT"; @@ -55,7 +65,6 @@ public class LoggerUtil { */ public static final Marker TRANSACTION_LOG_MARKER = MarkerFactory.getMarker(TRANSACTION_LOG_MARKER_NAME); - private LoggerUtil() { // Empty constructor } 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 2d5d356c..f05974c6 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 @@ -79,7 +79,7 @@ public class PropertyUtilTest { * Utility method to write a properties file. * * @param name the file name, relative to the temporary directory - * @param the properties to store in the file + * @param properties to store in the file * @return a File instance associated with the newly-created file * @throws IOException if the file can't be created for some reason */ @@ -137,10 +137,24 @@ public class PropertyUtilTest { 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)); + + Properties prop4 = new Properties(); + prop4.put(INTERPOLATION_NO, "no"); + prop4.put(INTERPOLATION_ENV, "${env:HOME}"); + prop4.put(INTERPOLATION_CONST, "${const:org.onap.policy.drools.utils.logging.LoggerUtil.ROOT_LOGGER}"); + prop4.put(INTERPOLATION_SYS, "${sys:user.home}"); + + PropertyUtil.setSystemProperties(prop4); + + assertEquals("no", System.getProperty(INTERPOLATION_NO)); + assertEquals(System.getenv("HOME"), System.getProperty(INTERPOLATION_ENV)); + assertEquals(LoggerUtil.ROOT_LOGGER, System.getProperty(INTERPOLATION_CONST)); + assertEquals(System.getProperty("user.home"), System.getProperty(INTERPOLATION_SYS)); } /** |