Fix Sonar issues
[ccsdk/sli/core.git] / dblib / provider / src / main / java / org / onap / ccsdk / sli / core / dblib / DBLIBResourceProvider.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * onap
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.ccsdk.sli.core.dblib;
22
23 import com.google.common.annotations.VisibleForTesting;
24
25 import java.io.File;
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;
32
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;
44
45 /**
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:
48  *
49  * <ol>
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>
54  * </ol>
55  */
56 public class DBLIBResourceProvider {
57
58     private static final Logger LOG = LoggerFactory.getLogger(DBLIBResourceProvider.class);
59
60     /**
61      * The name of the properties file for database configuration
62      */
63     private static final String DBLIB_PROP_FILE_NAME = "dblib.properties";
64
65     /**
66      * A prioritized list of strategies for resolving dblib properties files.
67      */
68     private Vector<DblibPropertiesFileResolver> dblibPropertiesFileResolvers = new Vector();
69
70     /**
71      * The configuration properties for the db connection.
72      */
73     private Properties properties;
74
75     /**
76      * Set up the prioritized list of strategies for resolving dblib properties files.
77      */
78     public DBLIBResourceProvider() {
79         dblibPropertiesFileResolvers.add(new DblibEnvVarFileResolver(
80                 "Using property file (1) from environment variable"
81         ));
82         dblibPropertiesFileResolvers.add(new DblibDefaultFileResolver(
83                 "Using property file (1) from default directory"
84         ));
85         dblibPropertiesFileResolvers.add(new DblibJREFileResolver(
86                 "Using property file (2) from JRE argument"
87         ));
88         dblibPropertiesFileResolvers.add(new DblibKarafRootFileResolver(
89                 "Using property file (4) from karaf root", this));
90
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) {
94             try {
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));
102             }
103         }
104     }
105
106     /**
107      * Extract db config properties.
108      *
109      * @return the db config properties
110      */
111     public Properties getProperties() {
112         return properties;
113     }
114
115     /**
116      * Reports the method chosen for properties resolution to the <code>Logger</code>.
117      *
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
121      */
122     private static File reportSuccess(final String message, final Optional<File> fileOptional) {
123         final File file = fileOptional.get();
124         LOG.info("{} {}", message, file.getPath());
125         return file;
126     }
127
128     /**
129      * Reports fatal errors.  This is the case in which no properties file could be found.
130      *
131      * @param message An appropriate fatal error message
132      * @param dblibConfigurationException An exception describing what went wrong during resolution
133      */
134     private static void reportFailure(final String message,
135                                       final DblibConfigurationException dblibConfigurationException) {
136
137         LOG.error("{}", message, dblibConfigurationException);
138     }
139
140     /**
141      * Determines the dblib properties file to use based on the following priority:
142      * <ol>
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>
147      * </ol>
148      */
149     @VisibleForTesting
150     File determinePropertiesFile(final DBLIBResourceProvider dblibResourceProvider) {
151
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);
156             }
157         }
158
159         reportFailure("Missing configuration properties resource(3)",
160                 new DblibConfigurationException("Missing configuration properties resource(3): "
161                         + DBLIB_PROP_FILE_NAME));
162         return null;
163     }
164 }