2 * ============LICENSE_START=======================================================
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
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.policy.common.sitemanager.utils;
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;
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;
37 import org.onap.policy.common.sitemanager.exception.MissingPropertyException;
38 import org.onap.policy.common.sitemanager.exception.PropertyFileProcessingException;
40 public class PersistenceUnitPropertiesProvider {
42 private PersistenceUnitPropertiesProvider() {
47 * Parser and validate properties in give property file. <br>
48 * valid and mandatory property name
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>
57 * @param propertiesFileName the properties filename
58 * @param printable {@link Printable}
59 * @return {@link Properties}
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);
68 final Path filePath = Paths.get(propertiesFileName);
69 final Properties properties = getProperties(filePath, printable);
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");
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");
88 try (final BufferedReader bufferedReader = Files.newBufferedReader(filePath);) {
89 final Properties properties = new Properties();
90 properties.load(bufferedReader);
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);
100 private static boolean isNotValid(final Properties properties, final String... values) {
101 if (values == null || values.length == 0) {
104 for (final String val : values) {
105 if (properties.get(val) == null) {