Load Music properties without Spring Boot
[music.git] / 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("${music.ip}")
43     public String musicIp;        
44 */
45     @Value("${debug}")
46     public String debug;
47     
48     @Value("${version}")
49     public String version;
50
51     @Value("${build}")
52     public String build;
53     
54     @Value("${music.properties}")
55     public String musicProperties;
56     
57     @Value("${lock.lease.period}")
58     public String lockLeasePeriod;
59     
60     @Value("${cassandra.user}")
61     public String cassandraUser;
62     
63     @Value("${cassandra.password}")
64     public String cassandraPassword;
65     
66     @Value("${cassandra.port}")
67     public String cassandraPort;
68     
69     @Value("${cadi}")
70     public String isCadi;
71     
72     @Value("${keyspace.active}")
73     public String isKeyspaceActive;
74
75     @Value("${retry.count}")
76     public String rertryCount;
77     
78     @Value("${transId.header.prefix}")
79     private String transIdPrefix;
80
81     @Value("${conversation.header.prefix}")
82     private String conversationIdPrefix;
83
84     @Value("${clientId.header.prefix}")
85     private String clientIdPrefix;
86
87     @Value("${messageId.header.prefix}")
88     private String messageIdPrefix;    
89     
90     @Value("${transId.header.required}")
91     private Boolean transIdRequired;
92
93     @Value("${conversation.header.required}")
94     private Boolean conversationIdRequired;
95
96     @Value("${clientId.header.required}")
97     private Boolean clientIdRequired;
98
99     @Value("${messageId.header.required}")
100     private Boolean messageIdRequired;
101
102     @Value("${music.aaf.ns}")
103     private String musicAafNs;
104
105     @Value("${cipher.enc.key}")
106     private String cipherEncKey;
107     
108     private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(PropertiesLoader.class);
109     
110     @Bean
111     public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
112         //return new PropertySourcesPlaceholderConfigurer();
113         PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();
114         pspc.setIgnoreResourceNotFound(true);
115         pspc.setIgnoreUnresolvablePlaceholders(true);
116         return pspc;
117     }
118     
119     /**
120      * .
121      */
122     public void loadProperties() {
123         if(cipherEncKey != null) {
124             MusicUtil.setCipherEncKey(cipherEncKey);
125         }
126         if (musicAafNs != null) {
127             MusicUtil.setMusicAafNs(musicAafNs);
128         }
129         if (cassandraPort != null && !cassandraPort.equals("${cassandra.port}")) {
130             MusicUtil.setCassandraPort(Integer.parseInt(cassandraPort));
131         }
132         if (cassandraUser != null && !cassandraUser.equals("${cassandra.user}")) {
133             MusicUtil.setCassName(cassandraUser);
134         }
135         if (cassandraPassword != null && !cassandraPassword.equals("${cassandra.password}")) {
136             MusicUtil.setCassPwd(cassandraPassword);
137         }
138         if (debug != null && !debug.equals("${debug}")) {
139             MusicUtil.setDebug(Boolean.parseBoolean(debug));
140         }
141         if (lockLeasePeriod != null && !lockLeasePeriod.equals("${lock.lease.period}")) {
142             MusicUtil.setDefaultLockLeasePeriod(Long.parseLong(lockLeasePeriod));
143         }
144         if (musicProperties != null && !musicProperties.equals("${music.properties}")) {
145             MusicUtil.setMusicPropertiesFilePath(musicProperties);
146         }
147         if (cassandraHost != null && !cassandraHost.equals("${cassandra.host}")) {
148             MusicUtil.setMyCassaHost(cassandraHost);
149         }
150         if (version != null && !version.equals("${version}")) {
151             MusicUtil.setVersion(version);
152         }
153         if (build != null && !version.equals("${build}")) {
154             MusicUtil.setBuild(build);
155         }
156         if (isCadi != null && !isCadi.equals("${cadi}")) {
157             MusicUtil.setIsCadi(Boolean.parseBoolean(isCadi));
158         }
159         if (rertryCount != null && !rertryCount.equals("${retry.count}")) {
160             MusicUtil.setRetryCount(Integer.parseInt(rertryCount));
161         }
162         if (isKeyspaceActive != null && !isKeyspaceActive.equals("${keyspace.active}")) {
163             MusicUtil.setKeyspaceActive(Boolean.parseBoolean(isKeyspaceActive));
164         }
165         if(transIdPrefix!=null) {
166             MusicUtil.setTransIdPrefix(transIdPrefix);
167         }
168
169         if(conversationIdPrefix!=null) {
170             MusicUtil.setConversationIdPrefix(conversationIdPrefix);
171         }
172
173         if(clientIdPrefix!=null) {
174             MusicUtil.setClientIdPrefix(clientIdPrefix);
175         }
176
177         if(messageIdPrefix!=null) {
178             MusicUtil.setMessageIdPrefix(messageIdPrefix);
179         }
180
181         if(transIdRequired!=null) {
182             MusicUtil.setTransIdRequired(transIdRequired);
183         }
184
185         if(conversationIdRequired!=null) {
186             MusicUtil.setConversationIdRequired(conversationIdRequired);
187         }
188
189         if(clientIdRequired!=null) {
190             MusicUtil.setClientIdRequired(clientIdRequired);
191         }
192
193         if(messageIdRequired!=null) {
194             MusicUtil.setMessageIdRequired(messageIdRequired);
195         }
196     }
197
198     public static void loadProperties(Properties properties) {
199         if (properties.getProperty("cassandra.host")!=null) {
200             MusicUtil.setMyCassaHost(properties.getProperty("cassandra.host"));
201         }
202
203         if (properties.getProperty("cassandra.port")!=null) {
204             MusicUtil.setCassandraPort(Integer.parseInt(properties.getProperty("cassandra.port")));
205         }
206
207         if (properties.getProperty("cassandra.user")!=null) {
208             MusicUtil.setCassName(properties.getProperty("cassandra.user"));
209         }
210
211         if (properties.getProperty("cassandra.password")!=null) {
212             MusicUtil.setCassPwd(properties.getProperty("cassandra.password"));
213         }
214         
215         if (properties.getProperty("music.properties")!=null) {
216             MusicUtil.setMusicPropertiesFilePath(properties.getProperty("music.properties"));
217         }
218
219         if (properties.getProperty("debug")!=null) {
220             MusicUtil.setDebug(Boolean.parseBoolean(properties.getProperty("debug")));
221         }
222
223         if (properties.getProperty("version")!=null) {
224             MusicUtil.setVersion(properties.getProperty("version"));
225         }
226
227         if (properties.getProperty("build")!=null) {
228             MusicUtil.setBuild(properties.getProperty("build"));
229         }
230
231         if (properties.getProperty("lock.lease.period")!=null) {
232             MusicUtil.setDefaultLockLeasePeriod(Long.parseLong(properties.getProperty("lock.lease.period")));
233         }
234
235         if (properties.getProperty("cadi")!=null) {
236             MusicUtil.setIsCadi(Boolean.parseBoolean(properties.getProperty("cadi")));
237         }
238
239         if (properties.getProperty("keyspace.active")!=null) {
240             MusicUtil.setKeyspaceActive(Boolean.parseBoolean(properties.getProperty("keyspace.active")));
241         }
242
243         if (properties.getProperty("retry.count")!=null) {
244             MusicUtil.setRetryCount(Integer.parseInt(properties.getProperty("retry.count")));
245         }
246
247         if (properties.getProperty("transId.header.prefix")!=null) {
248             MusicUtil.setTransIdPrefix(properties.getProperty("transId.header.prefix"));
249         }
250
251         if (properties.getProperty("conversation.header.prefix")!=null) {
252             MusicUtil.setConversationIdPrefix(properties.getProperty("conversation.header.prefix"));
253         }
254
255         if (properties.getProperty("clientId.header.prefix")!=null) {
256             MusicUtil.setClientIdPrefix(properties.getProperty("clientId.header.prefix"));
257         }
258
259         if (properties.getProperty("messageId.header.prefix")!=null) {
260             MusicUtil.setMessageIdPrefix(properties.getProperty("messageId.header.prefix"));
261         }
262
263         if (properties.getProperty("transId.header.required")!=null) {
264             MusicUtil.setTransIdRequired(Boolean.parseBoolean(properties.getProperty("transId.header.required")));
265         }
266
267         if (properties.getProperty("conversation.header.required")!=null) {
268             MusicUtil.setConversationIdRequired(Boolean.parseBoolean(properties.getProperty("conversation.header.required")));
269         }
270
271         if (properties.getProperty("clientId.header.required")!=null) {
272             MusicUtil.setClientIdRequired(Boolean.parseBoolean(properties.getProperty("clientId.header.required")));
273         }
274
275         if (properties.getProperty("messageId.header.required")!=null) {
276             MusicUtil.setMessageIdRequired(Boolean.parseBoolean(properties.getProperty("messageId.header.required")));
277         }
278
279         if (properties.getProperty("music.aaf.ns")!=null) {
280             MusicUtil.setMusicAafNs(properties.getProperty("music.aaf.ns"));
281         }
282
283         if (properties.getProperty("cipher.enc.key")!=null) {
284             MusicUtil.setCipherEncKey(properties.getProperty("cipher.enc.key"));
285         }
286
287     }
288     
289     @Override
290     public void afterPropertiesSet() throws Exception {
291         // TODO Auto-generated method stub
292         
293     }
294
295 }