2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. 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.openecomp.mso.properties;
24 import java.io.FileReader;
25 import java.io.IOException;
26 import java.security.GeneralSecurityException;
27 import java.util.Properties;
28 import org.openecomp.mso.utils.CryptoUtils;
30 public class MsoJavaProperties extends AbstractMsoProperties {
33 private Properties msoProperties = new Properties();
36 public MsoJavaProperties() {
40 public synchronized void setProperty(String key,String value) {
41 msoProperties.setProperty(key, value);
44 public synchronized String getProperty(String key, String defaultValue) {
45 if (msoProperties.containsKey(key)) {
46 return msoProperties.getProperty(key);
52 public synchronized int getIntProperty(String key, int defaultValue) {
54 int value = defaultValue;
55 if (msoProperties.containsKey(key)) {
57 value = Integer.parseInt(msoProperties.getProperty(key));
58 } catch (NumberFormatException e) {
59 LOGGER.debug("Exception while parsing integer: " + msoProperties.getProperty(key), e);
66 public synchronized boolean getBooleanProperty(String key, boolean defaultValue) {
68 if (msoProperties.containsKey(key)) {
69 return Boolean.parseBoolean(msoProperties.getProperty(key));
76 public synchronized String getEncryptedProperty(String key, String defaultValue, String encryptionKey) {
78 if (msoProperties.containsKey(key)) {
80 return CryptoUtils.decrypt(msoProperties.getProperty(key), encryptionKey);
81 } catch (GeneralSecurityException e) {
82 LOGGER.debug("Exception while decrypting property: " + msoProperties.getProperty(key), e);
89 public synchronized int size() {
90 return this.msoProperties.size();
95 protected synchronized void reloadPropertiesFile() throws IOException {
96 this.loadPropertiesFile(this.propertiesFileName);
100 * This method load a properties file from a source path.
102 * @param propertiesPath The path to the file
103 * @throws IOException In case of issues during the opening
106 protected synchronized void loadPropertiesFile(String propertiesPath) throws IOException {
108 FileReader reader = null;
110 propertiesFileName = propertiesPath;
112 msoProperties.clear();
113 reader = new FileReader(propertiesPath);
114 msoProperties.load(reader);
117 this.automaticRefreshInMinutes = this.getIntProperty(RELOAD_TIME_PROPERTY, DEFAULT_RELOAD_TIME_MIN);
119 if (reader != null) {
122 } catch (IOException e) {
123 LOGGER.debug("Exception while closing reader for file:" + propertiesPath, e);
129 public synchronized MsoJavaProperties clone() {
130 MsoJavaProperties msoCopy = new MsoJavaProperties();
131 msoCopy.msoProperties.putAll(msoProperties);
132 msoCopy.propertiesFileName = this.propertiesFileName;
133 msoCopy.automaticRefreshInMinutes = this.automaticRefreshInMinutes;
138 public int hashCode() {
139 final int prime = 31;
141 result = prime * result + ((msoProperties == null) ? 0 : msoProperties.hashCode());
146 public boolean equals(Object obj) {
153 if (getClass() != obj.getClass()) {
156 MsoJavaProperties other = (MsoJavaProperties) obj;
158 return msoProperties.equals(other.msoProperties);
162 public String toString() {
164 StringBuilder response = new StringBuilder();
165 response.append("Config file ")
166 .append(propertiesFileName)
168 .append(automaticRefreshInMinutes)
170 .append(System.lineSeparator());
172 for (Object key : this.msoProperties.keySet()) {
173 String propertyName = (String) key;
174 response.append(propertyName);
175 response.append("=");
176 response.append(this.msoProperties.getProperty(propertyName));
177 response.append(System.lineSeparator());
179 response.append(System.lineSeparator());
180 response.append(System.lineSeparator());
182 return response.toString();