blob: e83e6aa8747e96a37bfbf529d8c11a87d8628d7c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
package org.openecomp.sdc.tosca.parser.config;
import com.google.common.base.Charsets;
import com.google.common.io.Resources;
import org.openecomp.sdc.tosca.parser.utils.YamlToObjectConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
public class ConfigurationManager {
private static Logger log = LoggerFactory.getLogger(ConfigurationManager.class.getName());
private static final String CONFIGURATION_DIR = "config/";
private static volatile ConfigurationManager instance;
// private Configuration configuration;
// private ErrorConfiguration errorConfiguration;
Map<String, Object> configurations = new HashMap<String, Object>();
private ConfigurationManager() {
initialConfigObjectsFromFiles();
}
private void initialConfigObjectsFromFiles() {
loadConfigurationClass(ErrorConfiguration.class);
loadConfigurationClass(Configuration.class);
}
private <T> void loadConfigurationClass(Class<T> clazz) {
T object = getObjectFromYaml(clazz);
configurations.put(clazz.getSimpleName(), object);
}
public <T> T getObjectFromYaml(Class<T> className) {
String configFileName = calculateFileName(className);
URL url = Resources.getResource(CONFIGURATION_DIR + configFileName);
String configFileContents = null;
try {
configFileContents = Resources.toString(url, Charsets.UTF_8);
} catch (IOException e) {
log.error("ConfigurationManager - Failed to load configuration file");
}
YamlToObjectConverter yamlToObjectConverter = new YamlToObjectConverter();
T object = yamlToObjectConverter.convertFromString(configFileContents, className);
return object;
}
public static ConfigurationManager getInstance() {
if (instance == null) {
synchronized (ConfigurationManager.class) {
if (instance == null) {
instance = new ConfigurationManager();
}
}
}
return instance;
}
private static <T> String calculateFileName(Class<T> className) {
String[] words = className.getSimpleName().split("(?=\\p{Upper})");
StringBuilder builder = new StringBuilder();
// There cannot be a null value returned from "split" - words != null is
// redundant
// if (words != null) {
boolean isFirst = true;
for (int i = 0; i < words.length; i++) {
String word = words[i];
if (word != null && !word.isEmpty()) {
if (!isFirst) {
builder.append("-");
} else {
isFirst = false;
}
builder.append(words[i].toLowerCase());
}
}
return builder.toString() + ".yaml";
/*
* } else { return className.getSimpleName().toLowerCase() + Constants.YAML_SUFFIX; }
*/
}
public ErrorConfiguration getErrorConfiguration() {
return (ErrorConfiguration) configurations.get((ErrorConfiguration.class.getSimpleName()));
}
public Configuration getConfiguration() {
return (Configuration) configurations.get((Configuration.class.getSimpleName()));
}
}
|