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;
29 import java.io.BufferedReader;
30 import java.io.IOException;
31 import java.nio.file.Files;
32 import java.nio.file.Path;
33 import java.nio.file.Paths;
34 import java.util.Properties;
35 import org.onap.policy.common.sitemanager.exception.MissingPropertyException;
36 import org.onap.policy.common.sitemanager.exception.PropertyFileProcessingException;
38 public class PersistenceUnitPropertiesProvider {
40 private PersistenceUnitPropertiesProvider() {
44 public static Properties getProperties(final String propertiesFileName, final Printable printable) {
45 if (propertiesFileName == null) {
46 printable.println(SITE_MANAGER_PROPERY_FILE_NOT_DEFINED_MESSAGE);
47 printable.println(SITE_MANAGER_PROPERY_FILE_MISSING_PROPERTY);
48 throw new PropertyFileProcessingException("Property file name is null :" + propertiesFileName);
51 final Path filePath = Paths.get(propertiesFileName);
52 final Properties properties = getProperties(filePath, printable);
54 // verify that we have all of the properties needed
55 if (isNotValid(properties, JDBC_DRIVER_PROPERTY_NAME, JDBC_URL_PROPERTY_NAME, JDBC_USER_PROPERTY_NAME,
56 JDBC_PASSWORD_PROPERTY_NAME)) {
57 // one or more missing properties
58 printable.println(SITE_MANAGER_PROPERY_FILE_MISSING_PROPERTY);
59 throw new MissingPropertyException("missing mandatory attributes");
64 private static boolean isNotValid(final Properties properties, final String... values) {
65 if (values == null || values.length == 0) {
68 for (final String val : values) {
69 if (properties.get(val) == null) {
76 private static Properties getProperties(final Path filePath, final Printable printable) {
77 if (!filePath.toFile().exists()) {
78 printable.println(SITE_MANAGER_PROPERY_FILE_NOT_DEFINED_MESSAGE);
79 printable.println(SITE_MANAGER_PROPERY_FILE_MISSING_PROPERTY);
80 throw new PropertyFileProcessingException("Property file not found");
83 try (final BufferedReader bufferedReader = Files.newBufferedReader(filePath);) {
84 final Properties properties = new Properties();
85 properties.load(bufferedReader);
87 } catch (final IOException exception) {
88 printable.println("Exception loading properties: " + exception);
89 printable.println(ErrorMessages.SITE_MANAGER_PROPERY_FILE_NOT_DEFINED_MESSAGE);
90 printable.println(ErrorMessages.SITE_MANAGER_PROPERY_FILE_MISSING_PROPERTY);
91 throw new PropertyFileProcessingException("Exception loading properties: ", exception);