re base code
[sdc.git] / common-app-api / src / main / java / org / openecomp / sdc / fe / config / ConfigurationManager.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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.sdc.fe.config;
22
23 import org.openecomp.sdc.common.api.BasicConfiguration;
24 import org.openecomp.sdc.common.api.ConfigurationListener;
25 import org.openecomp.sdc.common.api.ConfigurationSource;
26 import org.openecomp.sdc.common.api.FileChangeCallback;
27 import org.openecomp.sdc.common.config.EcompErrorConfiguration;
28 import org.openecomp.sdc.common.config.IEcompConfigurationManager;
29 import org.openecomp.sdc.common.rest.api.RestConfigurationInfo;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 import java.util.HashMap;
34 import java.util.Map;
35
36 public class ConfigurationManager implements FileChangeCallback, IEcompConfigurationManager {
37
38         ConfigurationSource configurationSource = null;
39         private static ConfigurationManager instance;
40         private static Logger log = LoggerFactory.getLogger(ConfigurationManager.class.getName());
41
42         public ConfigurationManager(ConfigurationSource configurationSource) {
43                 super();
44                 this.configurationSource = configurationSource;
45                 loadConfigurationFiles();
46                 instance = this;
47         }
48
49         Map<String, Object> configurations = new HashMap<>();
50
51         private void loadConfigurationFiles() {
52
53                 loadConfigurationClass(Configuration.class);
54                 loadConfigurationClass(RestConfigurationInfo.class);
55                 loadConfigurationClass(EcompErrorConfiguration.class);
56                 loadConfigurationClass(PluginsConfiguration.class);
57
58         }
59
60         private <T extends BasicConfiguration> void loadConfigurationClass(Class<T> clazz) {
61                 ConfigurationListener configurationListener = new ConfigurationListener(clazz, this);
62                 
63                 log.info("created listener for class {}: {}", clazz.getName(), configurationListener);
64
65                 T object = configurationSource.getAndWatchConfiguration(clazz, configurationListener);
66
67                 configurations.put(getKey(clazz), object);
68         }
69
70         private <T> String getKey(Class<T> class1) {
71
72                 return class1.getSimpleName();
73
74         }
75
76         public Configuration getConfiguration() {
77
78                 return (Configuration) configurations.get(getKey(Configuration.class));
79
80         }
81
82         public RestConfigurationInfo getRestClientConfiguration() {
83
84                 return (RestConfigurationInfo) configurations.get(getKey(RestConfigurationInfo.class));
85
86         }
87
88         @Override
89         public EcompErrorConfiguration getEcompErrorConfiguration() {
90
91                 return (EcompErrorConfiguration) configurations.get(getKey(EcompErrorConfiguration.class));
92
93         }
94         
95         public PluginsConfiguration getPluginsConfiguration() {
96                 
97                 log.info("requested plugins configuration and got this:{}", configurations.get(getKey(PluginsConfiguration.class)));
98                 
99                 return (PluginsConfiguration) configurations.get(getKey(PluginsConfiguration.class));
100         }
101
102         public Configuration getConfigurationAndWatch(ConfigurationListener configurationListener) {
103
104                 if (configurationListener != null) {
105
106                         configurationSource.addWatchConfiguration(Configuration.class, configurationListener);
107
108                 }
109                 return (Configuration) configurations.get(getKey(Configuration.class));
110
111         }
112
113         public void reconfigure(BasicConfiguration obj) {
114                 //
115                 // if (obj != null) {
116                 //
117                 // if (obj instanceof Configuration) {
118                 // configurations.put(getKey(Configuration.class), obj);
119                 // }
120                 //
121                 // if (obj instanceof EcompErrorConfiguration) {
122                 // configurations.put(getKey(EcompErrorConfiguration.class), obj);
123                 // }
124                 // }
125         }
126
127         public static ConfigurationManager getConfigurationManager() {
128                 return instance;
129         }
130         
131         public static void setTestInstance(ConfigurationManager configurationManagerInstance) {
132                 instance = configurationManagerInstance;
133         }
134 }