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;
31 import org.onap.ccsdk.sli.core.sli.ConfigurationException;
32 import org.onap.ccsdk.sli.core.sli.provider.base.SvcLogicPropertiesProvider;
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.SdncConfigEnvVarFileResolver;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * THIS CLASS IS A COPY OF {@link SvcLogicPropertiesProviderImpl} WITH REMOVED OSGi DEPENDENCIES
43 public class SvcLogicPropertiesProviderImplLighty implements SvcLogicPropertiesProvider {
45 private static final Logger log = LoggerFactory.getLogger(SvcLogicPropertiesProviderImplLighty.class);
48 * The name of the properties file for database configuration
50 private static final String SVCLOGIC_PROP_FILE_NAME = "svclogic.properties";
53 * A prioritized list of strategies for resolving dblib properties files.
55 private Vector<PropertiesFileResolver> sliPropertiesFileResolvers = new Vector<>();
58 * The configuration properties for the db connection.
60 private Properties properties;
63 * Set up the prioritized list of strategies for resolving dblib properties
66 public SvcLogicPropertiesProviderImplLighty() {
67 sliPropertiesFileResolvers
68 .add(new SdncConfigEnvVarFileResolver("Using property file (1) from environment variable"));
69 sliPropertiesFileResolvers.add(new CoreDefaultFileResolver("Using property file (2) from default directory"));
70 sliPropertiesFileResolvers.add(new KarafRootFileResolver("Using property file (4) from karaf root", this));
72 // determines properties file as according to the priority described in the
73 // class header comment
74 final File propertiesFile = determinePropertiesFile(this);
75 if (propertiesFile != null) {
76 try (FileInputStream fileInputStream = new FileInputStream(propertiesFile)) {
77 properties = new Properties();
78 properties.load(fileInputStream);
79 } catch (final IOException e) {
80 log.error("Failed to load properties for file: {}", propertiesFile.toString(),
81 new ConfigurationException("Failed to load properties for file: " + propertiesFile.toString(),
85 // Try to read properties as resource
87 InputStream propStr = getClass().getResourceAsStream("/" + SVCLOGIC_PROP_FILE_NAME);
88 if (propStr != null) {
89 properties = new Properties();
91 properties.load(propStr);
93 } catch (IOException e) {
94 log.error("IO Exception",e);
101 if (properties == null) {
102 reportFailure("Missing configuration properties resource(3)", new ConfigurationException(
103 "Missing configuration properties resource(3): " + SVCLOGIC_PROP_FILE_NAME));
108 * Extract svclogic config properties.
110 * @return the svclogic config properties
112 public Properties getProperties() {
117 * Reports the method chosen for properties resolution to the
118 * <code>Logger</code>.
121 * Some user friendly message
122 * @param fileOptional
123 * The file location of the chosen properties file
124 * @return the file location of the chosen properties file
126 private static File reportSuccess(final String message, final Optional<File> fileOptional) {
127 if (fileOptional.isPresent()) {
128 final File file = fileOptional.get();
129 log.info("{} {}", message, file.getPath());
136 * Reports fatal errors. This is the case in which no properties file could be
140 * An appropriate fatal error message
141 * @param configurationException
142 * An exception describing what went wrong during resolution
144 private static void reportFailure(final String message, final ConfigurationException configurationException) {
146 log.error("{}", message, configurationException);
150 * Determines the dblib properties file to use based on the following priority:
152 * <li>A directory identified by the system environment variable
153 * <code>SDNC_CONFIG_DIR</code></li>
154 * <li>The default directory <code>DEFAULT_DBLIB_PROP_DIR</code></li>
155 * <li>A directory identified by the JRE argument
156 * <code>dblib.properties</code></li>
157 * <li>A <code>dblib.properties</code> file located in the karaf root
161 File determinePropertiesFile(final SvcLogicPropertiesProviderImplLighty resourceProvider) {
163 for (final PropertiesFileResolver sliPropertiesFileResolver : sliPropertiesFileResolvers) {
164 final Optional<File> fileOptional = sliPropertiesFileResolver.resolveFile(SVCLOGIC_PROP_FILE_NAME);
165 if (fileOptional.isPresent()) {
166 return reportSuccess(sliPropertiesFileResolver.getSuccessfulResolutionMessage(), fileOptional);