2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2016 - 2017 ONAP
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.ccsdk.sli.adaptors.resource.sql;
24 import java.io.FileInputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.util.Optional;
28 import java.util.Properties;
29 import java.util.Vector;
31 import org.onap.ccsdk.sli.core.sli.ConfigurationException;
32 import org.onap.ccsdk.sli.core.utils.JREFileResolver;
33 import org.onap.ccsdk.sli.core.utils.KarafRootFileResolver;
34 import org.onap.ccsdk.sli.core.utils.PropertiesFileResolver;
35 import org.onap.ccsdk.sli.core.utils.common.CoreDefaultFileResolver;
36 import org.onap.ccsdk.sli.core.utils.common.EnvProperties;
37 import org.onap.ccsdk.sli.core.utils.common.SdncConfigEnvVarFileResolver;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 * Responsible for determining the properties file to use and instantiating the
43 * <code>SqlResource</code> Service. The priority for properties file
44 * resolution is as follows:
47 * <li>A directory identified by the system environment variable
48 * <code>SDNC_CONFIG_DIR</code></li>
49 * <li>The default directory <code>DEFAULT_DBLIB_PROP_DIR</code></li>
50 * <li>A directory identified by the JRE argument
51 * <code>sql-resource.properties</code></li>
52 * <li>A <code>sql-resource.properties</code> file located in the karaf root
56 public class SqlResourcePropertiesProviderImpl implements SqlResourcePropertiesProvider {
58 private static final Logger LOG = LoggerFactory.getLogger(SqlResourcePropertiesProviderImpl.class);
61 * The name of the properties file for database configuration
63 private static final String SQLRESOURCE_PROP_FILE_NAME = "sql-resource.properties";
66 * A prioritized list of strategies for resolving sql-resource properties files.
68 private Vector<PropertiesFileResolver> sqlResourcePropertiesFileResolvers = new Vector<>();
71 * The configuration properties for the db connection.
73 private Properties properties;
76 * Set up the prioritized list of strategies for resolving dblib properties
79 public SqlResourcePropertiesProviderImpl() {
80 sqlResourcePropertiesFileResolvers
81 .add(new SdncConfigEnvVarFileResolver("Using property file (1) from environment variable"));
82 sqlResourcePropertiesFileResolvers.add(new CoreDefaultFileResolver("Using property file (2) from default directory"));
84 sqlResourcePropertiesFileResolvers.add(
85 new JREFileResolver("Using property file (3) from JRE argument", SqlResourcePropertiesProviderImpl.class));
86 sqlResourcePropertiesFileResolvers.add(new KarafRootFileResolver("Using property file (4) from karaf root", this));
88 // determines properties file as according to the priority described in the
89 // class header comment
90 final File propertiesFile = determinePropertiesFile(this);
91 if (propertiesFile != null) {
92 try (FileInputStream fileInputStream = new FileInputStream(propertiesFile)) {
93 properties = new EnvProperties();
94 properties.load(fileInputStream);
95 } catch (final IOException e) {
96 LOG.error("Failed to load properties for file: {}", propertiesFile.toString(),
97 new ConfigurationException("Failed to load properties for file: " + propertiesFile.toString(),
101 // Try to read properties as resource
103 InputStream propStr = getClass().getResourceAsStream("/" + SQLRESOURCE_PROP_FILE_NAME);
104 if (propStr != null) {
105 properties = new EnvProperties();
107 properties.load(propStr);
109 } catch (IOException e) {
116 if (properties == null) {
117 reportFailure("Missing configuration properties resource(3)", new ConfigurationException(
118 "Missing configuration properties resource(3): " + SQLRESOURCE_PROP_FILE_NAME));
123 * Extract svclogic config properties.
125 * @return the svclogic config properties
127 public Properties getProperties() {
132 * Reports the method chosen for properties resolution to the
133 * <code>Logger</code>.
136 * Some user friendly message
137 * @param fileOptional
138 * The file location of the chosen properties file
139 * @return the file location of the chosen properties file
141 private static File reportSuccess(final String message, final Optional<File> fileOptional) {
142 if (fileOptional.isPresent()) {
143 final File file = fileOptional.get();
144 LOG.info("{} {}", message, file.getPath());
151 * Reports fatal errors. This is the case in which no properties file could be
155 * An appropriate fatal error message
156 * @param configurationException
157 * An exception describing what went wrong during resolution
159 private static void reportFailure(final String message, final ConfigurationException configurationException) {
161 LOG.error("{}", message, configurationException);
165 * Determines the sql-resource properties file to use based on the following priority:
167 * <li>A directory identified by the system environment variable
168 * <code>SDNC_CONFIG_DIR</code></li>
169 * <li>The default directory <code>DEFAULT_DBLIB_PROP_DIR</code></li>
170 * <li>A directory identified by the JRE argument
171 * <code>sql-resource.properties</code></li>
172 * <li>A <code>sql-resource.properties</code> file located in the karaf root
176 File determinePropertiesFile(final SqlResourcePropertiesProviderImpl resourceProvider) {
178 for (final PropertiesFileResolver sliPropertiesFileResolver : sqlResourcePropertiesFileResolvers) {
179 final Optional<File> fileOptional = sliPropertiesFileResolver.resolveFile(SQLRESOURCE_PROP_FILE_NAME);
180 if (fileOptional.isPresent()) {
181 return reportSuccess(sliPropertiesFileResolver.getSuccessfulResolutionMessage(), fileOptional);