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.core.dblib;
23 import com.google.common.annotations.VisibleForTesting;
26 import java.io.FileInputStream;
27 import java.io.FileNotFoundException;
28 import java.io.IOException;
29 import java.util.Optional;
30 import java.util.Properties;
31 import java.util.Vector;
33 import org.onap.ccsdk.sli.core.dblib.propertiesfileresolver.DblibDefaultFileResolver;
34 import org.onap.ccsdk.sli.core.dblib.propertiesfileresolver.DblibEnvVarFileResolver;
35 import org.onap.ccsdk.sli.core.dblib.propertiesfileresolver.DblibJREFileResolver;
36 import org.onap.ccsdk.sli.core.dblib.propertiesfileresolver.DblibKarafRootFileResolver;
37 import org.onap.ccsdk.sli.core.dblib.propertiesfileresolver.DblibPropertiesFileResolver;
38 import org.osgi.framework.BundleContext;
39 import org.osgi.framework.FrameworkUtil;
40 import org.osgi.framework.ServiceReference;
41 import org.osgi.framework.ServiceRegistration;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 * Responsible for determining the properties file to use and instantiating the <code>DBResourceManager</code>
47 * Service. The priority for properties file resolution is as follows:
50 * <li>A directory identified by the system environment variable <code>SDNC_CONFIG_DIR</code></li>
51 * <li>The default directory <code>DEFAULT_DBLIB_PROP_DIR</code></li>
52 * <li>A directory identified by the JRE argument <code>dblib.properties</code></li>
53 * <li>A <code>dblib.properties</code> file located in the karaf root directory</li>
56 public class DBLIBResourceProvider {
58 private static final Logger LOG = LoggerFactory.getLogger(DBLIBResourceProvider.class);
61 * The name of the properties file for database configuration
63 private static final String DBLIB_PROP_FILE_NAME = "dblib.properties";
66 * A prioritized list of strategies for resolving dblib properties files.
68 private Vector<DblibPropertiesFileResolver> dblibPropertiesFileResolvers = 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 files.
78 public DBLIBResourceProvider() {
79 dblibPropertiesFileResolvers.add(new DblibEnvVarFileResolver(
80 "Using property file (1) from environment variable"
82 dblibPropertiesFileResolvers.add(new DblibDefaultFileResolver(
83 "Using property file (1) from default directory"
85 dblibPropertiesFileResolvers.add(new DblibJREFileResolver(
86 "Using property file (2) from JRE argument"
88 dblibPropertiesFileResolvers.add(new DblibKarafRootFileResolver(
89 "Using property file (4) from karaf root", this));
91 // determines properties file as according to the priority described in the class header comment
92 final File propertiesFile = determinePropertiesFile(this);
93 if (propertiesFile != null) {
95 final FileInputStream fileInputStream = new FileInputStream(propertiesFile);
96 properties = new Properties();
97 properties.load(fileInputStream);
98 } catch (final IOException e) {
99 LOG.error("Failed to load properties for file: {}", propertiesFile.toString(),
100 new DblibConfigurationException("Failed to load properties for file: "
101 + propertiesFile.toString(), e));
107 * Extract db config properties.
109 * @return the db config properties
111 public Properties getProperties() {
116 * Reports the method chosen for properties resolution to the <code>Logger</code>.
118 * @param message Some user friendly message
119 * @param fileOptional The file location of the chosen properties file
120 * @return the file location of the chosen properties file
122 private static File reportSuccess(final String message, final Optional<File> fileOptional) {
123 final File file = fileOptional.get();
124 LOG.info("{} {}", message, file.getPath());
129 * Reports fatal errors. This is the case in which no properties file could be found.
131 * @param message An appropriate fatal error message
132 * @param dblibConfigurationException An exception describing what went wrong during resolution
134 private static void reportFailure(final String message,
135 final DblibConfigurationException dblibConfigurationException) {
137 LOG.error("{}", message, dblibConfigurationException);
141 * Determines the dblib properties file to use based on the following priority:
143 * <li>A directory identified by the system environment variable <code>SDNC_CONFIG_DIR</code></li>
144 * <li>The default directory <code>DEFAULT_DBLIB_PROP_DIR</code></li>
145 * <li>A directory identified by the JRE argument <code>dblib.properties</code></li>
146 * <li>A <code>dblib.properties</code> file located in the karaf root directory</li>
150 File determinePropertiesFile(final DBLIBResourceProvider dblibResourceProvider) {
152 for (final DblibPropertiesFileResolver dblibPropertiesFileResolver : dblibPropertiesFileResolvers) {
153 final Optional<File> fileOptional = dblibPropertiesFileResolver.resolveFile(DBLIB_PROP_FILE_NAME);
154 if (fileOptional.isPresent()) {
155 return reportSuccess(dblibPropertiesFileResolver.getSuccessfulResolutionMessage(), fileOptional);
159 reportFailure("Missing configuration properties resource(3)",
160 new DblibConfigurationException("Missing configuration properties resource(3): "
161 + DBLIB_PROP_FILE_NAME));