2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2016 - 2017 ONAP
6 * Modifications Copyright (C) 2018 IBM.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.ccsdk.sli.core.sli.provider;
25 import java.io.FileInputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.util.Optional;
29 import java.util.Properties;
30 import java.util.Vector;
32 import org.onap.ccsdk.sli.core.sli.ConfigurationException;
33 import org.onap.ccsdk.sli.core.sli.provider.base.SvcLogicPropertiesProvider;
34 import org.onap.ccsdk.sli.core.utils.JREFileResolver;
35 import org.onap.ccsdk.sli.core.utils.KarafRootFileResolver;
36 import org.onap.ccsdk.sli.core.utils.PropertiesFileResolver;
37 import org.onap.ccsdk.sli.core.utils.common.CoreDefaultFileResolver;
38 import org.onap.ccsdk.sli.core.utils.common.SdncConfigEnvVarFileResolver;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * Responsible for determining the properties file to use and instantiating the
44 * <code>DBResourceManager</code> Service. The priority for properties file
45 * resolution is as follows:
48 * <li>A directory identified by the system environment variable
49 * <code>SDNC_CONFIG_DIR</code></li>
50 * <li>The default directory <code>DEFAULT_DBLIB_PROP_DIR</code></li>
51 * <li>A directory identified by the JRE argument
52 * <code>dblib.properties</code></li>
53 * <li>A <code>dblib.properties</code> file located in the karaf root
57 public class SvcLogicPropertiesProviderImpl implements SvcLogicPropertiesProvider {
59 private static final Logger log = LoggerFactory.getLogger(SvcLogicPropertiesProviderImpl.class);
62 * The name of the properties file for database configuration
64 private static final String SVCLOGIC_PROP_FILE_NAME = "svclogic.properties";
67 * A prioritized list of strategies for resolving dblib properties files.
69 private Vector<PropertiesFileResolver> sliPropertiesFileResolvers = new Vector<>();
72 * The configuration properties for the db connection.
74 private Properties properties;
77 * Set up the prioritized list of strategies for resolving dblib properties
80 public SvcLogicPropertiesProviderImpl() {
81 sliPropertiesFileResolvers
82 .add(new SdncConfigEnvVarFileResolver("Using property file (1) from environment variable"));
83 sliPropertiesFileResolvers.add(new CoreDefaultFileResolver("Using property file (2) from default directory"));
85 sliPropertiesFileResolvers.add(
86 new JREFileResolver("Using property file (3) from JRE argument", SvcLogicPropertiesProviderImpl.class));
87 sliPropertiesFileResolvers.add(new KarafRootFileResolver("Using property file (4) from karaf root", this));
89 // determines properties file as according to the priority described in the
90 // class header comment
91 final File propertiesFile = determinePropertiesFile(this);
92 if (propertiesFile != null) {
93 try (FileInputStream fileInputStream = new FileInputStream(propertiesFile)) {
94 properties = new Properties();
95 properties.load(fileInputStream);
96 } catch (final IOException e) {
97 log.error("Failed to load properties for file: {}", propertiesFile.toString(),
98 new ConfigurationException("Failed to load properties for file: " + propertiesFile.toString(),
102 // Try to read properties as resource
104 InputStream propStr = getClass().getResourceAsStream("/" + SVCLOGIC_PROP_FILE_NAME);
105 if (propStr != null) {
106 properties = new Properties();
108 properties.load(propStr);
110 } catch (IOException e) {
111 log.error("IO Exception",e);
118 if (properties == null) {
119 reportFailure("Missing configuration properties resource(3)", new ConfigurationException(
120 "Missing configuration properties resource(3): " + SVCLOGIC_PROP_FILE_NAME));
125 * Extract svclogic config properties.
127 * @return the svclogic config properties
129 public Properties getProperties() {
134 * Reports the method chosen for properties resolution to the
135 * <code>Logger</code>.
138 * Some user friendly message
139 * @param fileOptional
140 * The file location of the chosen properties file
141 * @return the file location of the chosen properties file
143 private static File reportSuccess(final String message, final Optional<File> fileOptional) {
144 if (fileOptional.isPresent()) {
145 final File file = fileOptional.get();
146 log.info("{} {}", message, file.getPath());
153 * Reports fatal errors. This is the case in which no properties file could be
157 * An appropriate fatal error message
158 * @param configurationException
159 * An exception describing what went wrong during resolution
161 private static void reportFailure(final String message, final ConfigurationException configurationException) {
163 log.error("{}", message, configurationException);
167 * Determines the dblib properties file to use based on the following priority:
169 * <li>A directory identified by the system environment variable
170 * <code>SDNC_CONFIG_DIR</code></li>
171 * <li>The default directory <code>DEFAULT_DBLIB_PROP_DIR</code></li>
172 * <li>A directory identified by the JRE argument
173 * <code>dblib.properties</code></li>
174 * <li>A <code>dblib.properties</code> file located in the karaf root
178 File determinePropertiesFile(final SvcLogicPropertiesProviderImpl resourceProvider) {
180 for (final PropertiesFileResolver sliPropertiesFileResolver : sliPropertiesFileResolvers) {
181 final Optional<File> fileOptional = sliPropertiesFileResolver.resolveFile(SVCLOGIC_PROP_FILE_NAME);
182 if (fileOptional.isPresent()) {
183 return reportSuccess(sliPropertiesFileResolver.getSuccessfulResolutionMessage(), fileOptional);