Added properties for cassandra connect and read time outs with Junit cases.
[music.git] / music-rest / src / main / java / org / onap / music / main / PropertiesLoader.java
1 /*
2  * ============LICENSE_START==========================================
3  * org.onap.music
4  * ===================================================================
5  *  Copyright (c) 2017 AT&T Intellectual Property
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  * 
19  * ============LICENSE_END=============================================
20  * ====================================================================
21  */
22
23 package org.onap.music.main;
24
25 import java.util.Properties;
26
27 import org.onap.music.eelf.logging.EELFLoggerDelegate;
28 import org.springframework.beans.factory.InitializingBean;
29 import org.springframework.beans.factory.annotation.Value;
30 import org.springframework.context.annotation.Bean;
31 import org.springframework.context.annotation.PropertySource;
32 import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
33 import org.springframework.stereotype.Component;
34
35 @PropertySource(value = {"file:/opt/app/music/etc/music.properties", "classpath:/project.properties","file:/opt/app/music/etc/key.properties"})
36 @Component
37 public class PropertiesLoader implements InitializingBean {
38
39     @Value("${cassandra.host}")
40     public String cassandraHost;
41
42     @Value("${debug}")
43     public String debug;
44
45     @Value("${version}")
46     public String version;
47
48     @Value("${build}")
49     public String build;
50
51     @Value("${music.properties}")
52     public String musicProperties;
53
54     @Value("${lock.lease.period}")
55     public String lockLeasePeriod;
56
57     @Value("${cassandra.user}")
58     public String cassandraUser;
59
60     @Value("${cassandra.password}")
61     public String cassandraPassword;
62
63     @Value("${cassandra.port}")
64     public String cassandraPort;
65
66     @Value("${cassandra.connecttimeoutms}")
67     public String cassandraConnectTimeOutMS;
68
69     @Value("${cassandra.readtimeoutms}")
70     public String cassandraReadTimeOutMS;
71
72     @Value("${cadi}")
73     public String isCadi;
74
75     @Value("${keyspace.active}")
76     public String isKeyspaceActive;
77
78     @Value("${retry.count}")
79     public String rertryCount;
80
81     @Value("${transId.header.prefix}")
82     private String transIdPrefix;
83
84     @Value("${conversation.header.prefix}")
85     private String conversationIdPrefix;
86
87     @Value("${clientId.header.prefix}")
88     private String clientIdPrefix;
89
90     @Value("${messageId.header.prefix}")
91     private String messageIdPrefix;    
92
93     @Value("${transId.header.required}")
94     private Boolean transIdRequired;
95
96     @Value("${conversation.header.required}")
97     private Boolean conversationIdRequired;
98
99     @Value("${clientId.header.required}")
100     private Boolean clientIdRequired;
101
102     @Value("${messageId.header.required}")
103     private Boolean messageIdRequired;
104
105     @Value("${music.aaf.ns}")
106     private String musicAafNs;
107
108     @Value("${cipher.enc.key}")
109     private String cipherEncKey;
110
111     @SuppressWarnings("unused")
112     private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(PropertiesLoader.class);
113
114     @Bean
115     public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
116
117         PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();
118         pspc.setIgnoreResourceNotFound(true);
119         pspc.setIgnoreUnresolvablePlaceholders(true);
120         return pspc;
121     }
122
123     /**
124      * .
125      */
126     public void loadProperties() {
127         if(cipherEncKey != null) {
128             MusicUtil.setCipherEncKey(cipherEncKey);
129         }
130         if (musicAafNs != null) {
131             MusicUtil.setMusicAafNs(musicAafNs);
132         }
133         if (cassandraPort != null && !cassandraPort.equals("${cassandra.port}")) {
134             MusicUtil.setCassandraPort(Integer.parseInt(cassandraPort));
135         }
136         if (cassandraUser != null && !cassandraUser.equals("${cassandra.user}")) {
137             MusicUtil.setCassName(cassandraUser);
138         }
139         if (cassandraPassword != null && !cassandraPassword.equals("${cassandra.password}")) {
140             MusicUtil.setCassPwd(cassandraPassword);
141         }
142         if (cassandraConnectTimeOutMS != null && !cassandraConnectTimeOutMS.equals("${cassandra.connecttimeoutms}")) {
143             MusicUtil.setCassandraConnectTimeOutMS(Integer.parseInt(cassandraConnectTimeOutMS));
144         }
145         if (cassandraReadTimeOutMS != null && !cassandraReadTimeOutMS.equals("${cassandra.readtimeoutms}")) {
146             MusicUtil.setCassandraReadTimeOutMS(Integer.parseInt(cassandraReadTimeOutMS));
147         }
148         if (debug != null && !debug.equals("${debug}")) {
149             MusicUtil.setDebug(Boolean.parseBoolean(debug));
150         }
151         if (lockLeasePeriod != null && !lockLeasePeriod.equals("${lock.lease.period}")) {
152             MusicUtil.setDefaultLockLeasePeriod(Long.parseLong(lockLeasePeriod));
153         }
154         if (musicProperties != null && !musicProperties.equals("${music.properties}")) {
155             MusicUtil.setMusicPropertiesFilePath(musicProperties);
156         }
157         if (cassandraHost != null && !cassandraHost.equals("${cassandra.host}")) {
158             MusicUtil.setMyCassaHost(cassandraHost);
159         }
160         if (version != null && !version.equals("${version}")) {
161             MusicUtil.setVersion(version);
162         }
163         if (build != null && !version.equals("${build}")) {
164             MusicUtil.setBuild(build);
165         }
166         if (isCadi != null && !isCadi.equals("${cadi}")) {
167             MusicUtil.setIsCadi(Boolean.parseBoolean(isCadi));
168         }
169         if (rertryCount != null && !rertryCount.equals("${retry.count}")) {
170             MusicUtil.setRetryCount(Integer.parseInt(rertryCount));
171         }
172         if (isKeyspaceActive != null && !isKeyspaceActive.equals("${keyspace.active}")) {
173             MusicUtil.setKeyspaceActive(Boolean.parseBoolean(isKeyspaceActive));
174         }
175         if(transIdPrefix!=null) {
176             MusicUtil.setTransIdPrefix(transIdPrefix);
177         }
178
179         if(conversationIdPrefix!=null) {
180             MusicUtil.setConversationIdPrefix(conversationIdPrefix);
181         }
182
183         if(clientIdPrefix!=null) {
184             MusicUtil.setClientIdPrefix(clientIdPrefix);
185         }
186
187         if(messageIdPrefix!=null) {
188             MusicUtil.setMessageIdPrefix(messageIdPrefix);
189         }
190
191         if(transIdRequired!=null) {
192             MusicUtil.setTransIdRequired(transIdRequired);
193         }
194
195         if(conversationIdRequired!=null) {
196             MusicUtil.setConversationIdRequired(conversationIdRequired);
197         }
198
199         if(clientIdRequired!=null) {
200             MusicUtil.setClientIdRequired(clientIdRequired);
201         }
202
203         if(messageIdRequired!=null) {
204             MusicUtil.setMessageIdRequired(messageIdRequired);
205         }
206     }
207
208     public static void loadProperties(Properties properties) {
209         if (properties.getProperty("cassandra.host")!=null) {
210             MusicUtil.setMyCassaHost(properties.getProperty("cassandra.host"));
211         }
212
213         if (properties.getProperty("cassandra.port")!=null) {
214             MusicUtil.setCassandraPort(Integer.parseInt(properties.getProperty("cassandra.port")));
215         }
216
217         if (properties.getProperty("cassandra.user")!=null) {
218             MusicUtil.setCassName(properties.getProperty("cassandra.user"));
219         }
220
221         if (properties.getProperty("cassandra.password")!=null) {
222             MusicUtil.setCassPwd(properties.getProperty("cassandra.password"));
223         }
224
225         if(properties.getProperty("cassandra.connectTimeOutMS")!=null) {
226             MusicUtil.setCassandraConnectTimeOutMS(Integer.parseInt(properties.getProperty("cassandra.connecttimeoutms")));
227         }
228
229         if(properties.getProperty("cassandra.readTimeOutMS")!=null) {
230             MusicUtil.setCassandraReadTimeOutMS(Integer.parseInt(properties.getProperty("cassandra.readtimeoutms")));
231         }
232
233         if (properties.getProperty("music.properties")!=null) {
234             MusicUtil.setMusicPropertiesFilePath(properties.getProperty("music.properties"));
235         }
236
237         if (properties.getProperty("debug")!=null) {
238             MusicUtil.setDebug(Boolean.parseBoolean(properties.getProperty("debug")));
239         }
240
241         if (properties.getProperty("version")!=null) {
242             MusicUtil.setVersion(properties.getProperty("version"));
243         }
244
245         if (properties.getProperty("build")!=null) {
246             MusicUtil.setBuild(properties.getProperty("build"));
247         }
248
249         if (properties.getProperty("lock.lease.period")!=null) {
250             MusicUtil.setDefaultLockLeasePeriod(Long.parseLong(properties.getProperty("lock.lease.period")));
251         }
252
253         if (properties.getProperty("cadi")!=null) {
254             MusicUtil.setIsCadi(Boolean.parseBoolean(properties.getProperty("cadi")));
255         }
256
257         if (properties.getProperty("keyspace.active")!=null) {
258             MusicUtil.setKeyspaceActive(Boolean.parseBoolean(properties.getProperty("keyspace.active")));
259         }
260
261         if (properties.getProperty("retry.count")!=null) {
262             MusicUtil.setRetryCount(Integer.parseInt(properties.getProperty("retry.count")));
263         }
264
265         if (properties.getProperty("transId.header.prefix")!=null) {
266             MusicUtil.setTransIdPrefix(properties.getProperty("transId.header.prefix"));
267         }
268
269         if (properties.getProperty("conversation.header.prefix")!=null) {
270             MusicUtil.setConversationIdPrefix(properties.getProperty("conversation.header.prefix"));
271         }
272
273         if (properties.getProperty("clientId.header.prefix")!=null) {
274             MusicUtil.setClientIdPrefix(properties.getProperty("clientId.header.prefix"));
275         }
276
277         if (properties.getProperty("messageId.header.prefix")!=null) {
278             MusicUtil.setMessageIdPrefix(properties.getProperty("messageId.header.prefix"));
279         }
280
281         if (properties.getProperty("transId.header.required")!=null) {
282             MusicUtil.setTransIdRequired(Boolean.parseBoolean(properties.getProperty("transId.header.required")));
283         }
284
285         if (properties.getProperty("conversation.header.required")!=null) {
286             MusicUtil.setConversationIdRequired(Boolean.parseBoolean(properties.getProperty("conversation.header.required")));
287         }
288
289         if (properties.getProperty("clientId.header.required")!=null) {
290             MusicUtil.setClientIdRequired(Boolean.parseBoolean(properties.getProperty("clientId.header.required")));
291         }
292
293         if (properties.getProperty("messageId.header.required")!=null) {
294             MusicUtil.setMessageIdRequired(Boolean.parseBoolean(properties.getProperty("messageId.header.required")));
295         }
296
297         if (properties.getProperty("music.aaf.ns")!=null) {
298             MusicUtil.setMusicAafNs(properties.getProperty("music.aaf.ns"));
299         }
300
301         if (properties.getProperty("cipher.enc.key")!=null) {
302             MusicUtil.setCipherEncKey(properties.getProperty("cipher.enc.key"));
303         }
304
305     }
306
307     @Override
308     public void afterPropertiesSet() throws Exception {
309         // TODO Auto-generated method stub
310
311     }
312
313 }