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);
118 // Always close the file
120 if (reader != null) {
123 } catch (IOException e) {
124 LOGGER.debug("Exception while closing reader for file:" + propertiesPath, e);
130 public synchronized MsoJavaProperties clone() {
131 MsoJavaProperties msoCopy = new MsoJavaProperties();
132 msoCopy.msoProperties.putAll(msoProperties);
133 msoCopy.propertiesFileName = this.propertiesFileName;
134 msoCopy.automaticRefreshInMinutes = this.automaticRefreshInMinutes;
139 public int hashCode() {
140 final int prime = 31;
142 result = prime * result + ((msoProperties == null) ? 0 : msoProperties.hashCode());
147 public boolean equals(Object obj) {
154 if (getClass() != obj.getClass()) {
157 MsoJavaProperties other = (MsoJavaProperties) obj;
158 if (!msoProperties.equals(other.msoProperties)) {
165 public String toString() {
167 StringBuffer response = new StringBuffer();
168 response.append("Config file " + propertiesFileName + "(Timer:" + automaticRefreshInMinutes + "mins):"
169 + System.getProperty("line.separator"));
170 for (Object key : this.msoProperties.keySet()) {
171 String propertyName = (String) key;
172 response.append(propertyName);
173 response.append("=");
174 response.append(this.msoProperties.getProperty(propertyName));
175 response.append(System.getProperty("line.separator"));
177 response.append(System.getProperty("line.separator"));
178 response.append(System.getProperty("line.separator"));
179 return response.toString();