ffc48694ed0e214787f0cac97fdda51b080cb50c
[policy/common.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * site-manager
4  * ================================================================================
5  * Copyright (C) 2018 Ericsson. All rights reserved.
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.policy.common.sitemanager.utils;
22
23 import static org.onap.policy.common.sitemanager.utils.Constants.JDBC_DRIVER_PROPERTY_NAME;
24 import static org.onap.policy.common.sitemanager.utils.Constants.JDBC_PASSWORD_PROPERTY_NAME;
25 import static org.onap.policy.common.sitemanager.utils.Constants.JDBC_URL_PROPERTY_NAME;
26 import static org.onap.policy.common.sitemanager.utils.Constants.JDBC_USER_PROPERTY_NAME;
27 import static org.onap.policy.common.sitemanager.utils.ErrorMessages.SITE_MANAGER_PROPERY_FILE_MISSING_PROPERTY;
28 import static org.onap.policy.common.sitemanager.utils.ErrorMessages.SITE_MANAGER_PROPERY_FILE_NOT_DEFINED_MESSAGE;
29
30 import java.io.BufferedReader;
31 import java.io.IOException;
32 import java.nio.file.Files;
33 import java.nio.file.Path;
34 import java.nio.file.Paths;
35 import java.util.Properties;
36
37 import org.onap.policy.common.sitemanager.exception.MissingPropertyException;
38 import org.onap.policy.common.sitemanager.exception.PropertyFileProcessingException;
39
40 public class PersistenceUnitPropertiesProvider {
41
42     private PersistenceUnitPropertiesProvider() {
43         super();
44     }
45
46     /**
47      * Parser and validate properties in give property file. <br>
48      * valid and mandatory property name
49      * 
50      * <ul>
51      * <li>javax.persistence.jdbc.driver</li>
52      * <li>javax.persistence.jdbc.url</li>
53      * <li>javax.persistence.jdbc.user</li>
54      * <li>javax.persistence.jdbc.password</li>
55      * </ul>
56      * 
57      * @param propertiesFileName the properties filename
58      * @param printable {@link Printable}
59      * @return {@link Properties}
60      */
61     public static Properties getProperties(final String propertiesFileName, final Printable printable) {
62         if (propertiesFileName == null) {
63             printable.println(SITE_MANAGER_PROPERY_FILE_NOT_DEFINED_MESSAGE);
64             printable.println(SITE_MANAGER_PROPERY_FILE_MISSING_PROPERTY);
65             throw new PropertyFileProcessingException("Property file name is null :" + propertiesFileName);
66         }
67
68         final Path filePath = Paths.get(propertiesFileName);
69         final Properties properties = getProperties(filePath, printable);
70
71         // verify that we have all of the properties needed
72         if (isNotValid(properties, JDBC_DRIVER_PROPERTY_NAME, JDBC_URL_PROPERTY_NAME, JDBC_USER_PROPERTY_NAME,
73                 JDBC_PASSWORD_PROPERTY_NAME)) {
74             // one or more missing properties
75             printable.println(SITE_MANAGER_PROPERY_FILE_MISSING_PROPERTY);
76             throw new MissingPropertyException("missing mandatory attributes");
77         }
78         return properties;
79     }
80
81     private static Properties getProperties(final Path filePath, final Printable printable) {
82         if (!filePath.toFile().exists()) {
83             printable.println(SITE_MANAGER_PROPERY_FILE_NOT_DEFINED_MESSAGE);
84             printable.println(SITE_MANAGER_PROPERY_FILE_MISSING_PROPERTY);
85             throw new PropertyFileProcessingException("Property file not found");
86         }
87
88         try (final BufferedReader bufferedReader = Files.newBufferedReader(filePath);) {
89             final Properties properties = new Properties();
90             properties.load(bufferedReader);
91             return properties;
92         } catch (final IOException exception) {
93             printable.println("Exception loading properties: " + exception);
94             printable.println(ErrorMessages.SITE_MANAGER_PROPERY_FILE_NOT_DEFINED_MESSAGE);
95             printable.println(ErrorMessages.SITE_MANAGER_PROPERY_FILE_MISSING_PROPERTY);
96             throw new PropertyFileProcessingException("Exception loading properties: ", exception);
97         }
98     }
99
100     private static boolean isNotValid(final Properties properties, final String... values) {
101         if (values == null || values.length == 0) {
102             return true;
103         }
104         for (final String val : values) {
105             if (properties.get(val) == null) {
106                 return true;
107             }
108         }
109         return false;
110     }
111 }