dfc5855fd654ac33e32eab161f39ef202f98e60b
[ccsdk/sli.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * onap
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.ccsdk.sli.core.sli.provider;
23
24 import java.io.File;
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;
39
40 /**
41  * THIS CLASS IS A COPY OF {@link SvcLogicPropertiesProviderImpl} WITH REMOVED OSGi DEPENDENCIES
42  */
43 public class SvcLogicPropertiesProviderImplLighty implements SvcLogicPropertiesProvider {
44
45         private static final Logger log = LoggerFactory.getLogger(SvcLogicPropertiesProviderImplLighty.class);
46
47         /**
48          * The name of the properties file for database configuration
49          */
50         private static final String SVCLOGIC_PROP_FILE_NAME = "svclogic.properties";
51
52         /**
53          * A prioritized list of strategies for resolving dblib properties files.
54          */
55         private Vector<PropertiesFileResolver> sliPropertiesFileResolvers = new Vector<>();
56
57         /**
58          * The configuration properties for the db connection.
59          */
60         private Properties properties;
61
62         /**
63          * Set up the prioritized list of strategies for resolving dblib properties
64          * files.
65          */
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));
71
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(),
82                                                                 e));
83                         }
84                 } else {
85                         // Try to read properties as resource
86
87                         InputStream propStr = getClass().getResourceAsStream("/" + SVCLOGIC_PROP_FILE_NAME);
88                         if (propStr != null) {
89                                 properties = new Properties();
90                                 try {
91                                         properties.load(propStr);
92                                         propStr.close();
93                                 } catch (IOException e) {
94                                         log.error("IO Exception",e);
95                                         properties = null;
96                                 }
97                         }
98
99                 }
100
101                 if (properties == null) {
102                         reportFailure("Missing configuration properties resource(3)", new ConfigurationException(
103                                         "Missing configuration properties resource(3): " + SVCLOGIC_PROP_FILE_NAME));
104                 }
105         }
106
107         /**
108          * Extract svclogic config properties.
109          *
110          * @return the svclogic config properties
111          */
112         public Properties getProperties() {
113                 return properties;
114         }
115
116         /**
117          * Reports the method chosen for properties resolution to the
118          * <code>Logger</code>.
119          *
120          * @param message
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
125          */
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());
130                         return file;
131                 }
132                 return null;
133         }
134
135         /**
136          * Reports fatal errors. This is the case in which no properties file could be
137          * found.
138          *
139          * @param message
140          *            An appropriate fatal error message
141          * @param configurationException
142          *            An exception describing what went wrong during resolution
143          */
144         private static void reportFailure(final String message, final ConfigurationException configurationException) {
145
146                 log.error("{}", message, configurationException);
147         }
148
149         /**
150          * Determines the dblib properties file to use based on the following priority:
151          * <ol>
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
158          * directory</li>
159          * </ol>
160          */
161         File determinePropertiesFile(final SvcLogicPropertiesProviderImplLighty resourceProvider) {
162
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);
167                         }
168                 }
169
170                 return null;
171         }
172 }