Change the header to SO
[so.git] / common / src / main / java / org / openecomp / mso / properties / MsoJavaProperties.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
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.openecomp.mso.properties;
22
23
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;
29
30 public class MsoJavaProperties extends AbstractMsoProperties {
31
32         
33         private Properties msoProperties = new Properties();
34
35
36         public MsoJavaProperties() {
37                 
38         }
39
40         public synchronized void setProperty(String key,String value) {
41                 msoProperties.setProperty(key, value);
42         }
43         
44         public synchronized String getProperty(String key, String defaultValue) {
45                 if (msoProperties.containsKey(key)) {
46                         return msoProperties.getProperty(key);
47                 } else {
48                         return defaultValue;
49                 }
50         }
51
52         public synchronized int getIntProperty(String key, int defaultValue) {
53
54                 int value = defaultValue;
55                 if (msoProperties.containsKey(key)) {
56                         try {
57                                 value = Integer.parseInt(msoProperties.getProperty(key));
58                         } catch (NumberFormatException e) {
59                                 LOGGER.debug("Exception while parsing integer: " + msoProperties.getProperty(key), e);
60                         }
61                 }
62                 return value;
63
64         }
65
66         public synchronized boolean getBooleanProperty(String key, boolean defaultValue) {
67
68                 if (msoProperties.containsKey(key)) {
69                         return Boolean.parseBoolean(msoProperties.getProperty(key));
70                 } else {
71                         return defaultValue;
72                 }
73
74         }
75
76         public synchronized String getEncryptedProperty(String key, String defaultValue, String encryptionKey) {
77
78                 if (msoProperties.containsKey(key)) {
79                         try {
80                                 return CryptoUtils.decrypt(msoProperties.getProperty(key), encryptionKey);
81                         } catch (GeneralSecurityException e) {
82                                 LOGGER.debug("Exception while decrypting property: " + msoProperties.getProperty(key), e);
83                         }
84                 }
85                 return defaultValue;
86
87         }
88
89         public synchronized int size() {
90                 return this.msoProperties.size();
91         }
92         
93
94         @Override
95         protected synchronized void reloadPropertiesFile() throws IOException {
96                 this.loadPropertiesFile(this.propertiesFileName);
97         }
98
99         /**
100          * This method load a properties file from a source path.
101          *
102          * @param propertiesPath The path to the file
103          * @throws IOException In case of issues during the opening
104          */
105         @Override
106         protected synchronized void loadPropertiesFile(String propertiesPath) throws IOException {
107
108                 FileReader reader = null;
109         
110                 propertiesFileName = propertiesPath;
111                 try {
112                         msoProperties.clear();
113                         reader = new FileReader(propertiesPath);
114                         msoProperties.load(reader);
115
116                 } finally {
117                         this.automaticRefreshInMinutes = this.getIntProperty(RELOAD_TIME_PROPERTY, DEFAULT_RELOAD_TIME_MIN);
118                         // Always close the file
119                         try {
120                                 if (reader != null) {
121                                         reader.close();
122                                 }
123                         } catch (IOException e) {
124                                 LOGGER.debug("Exception while closing reader for file:" + propertiesPath, e);
125                         }
126                 }
127         }
128
129         @Override
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;
135                 return msoCopy;
136         }
137
138         @Override
139         public int hashCode() {
140                 final int prime = 31;
141                 int result = 1;
142                 result = prime * result + ((msoProperties == null) ? 0 : msoProperties.hashCode());
143                 return result;
144         }
145
146         @Override
147         public boolean equals(Object obj) {
148                 if (this == obj) {
149                         return true;
150                 }
151                 if (obj == null) {
152                         return false;
153                 }
154                 if (getClass() != obj.getClass()) {
155                         return false;
156                 }
157                 MsoJavaProperties other = (MsoJavaProperties) obj;
158                 if (!msoProperties.equals(other.msoProperties)) {
159                         return false;
160                 }
161                 return true;
162         }
163
164         @Override
165         public String toString() {
166
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"));
176                 }
177                 response.append(System.getProperty("line.separator"));
178                 response.append(System.getProperty("line.separator"));
179                 return response.toString();
180         }
181 }