1 package fr.ifremer.quadrige3.core.config;
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 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.nuiton.config.ApplicationConfig;
31 import org.nuiton.config.ConfigOptionDef;
32
33
34
35
36
37
38
39 public class QuadrigeConfigurations {
40
41 private static final Log log = LogFactory.getLog(QuadrigeConfigurations.class);
42
43
44
45
46 protected QuadrigeConfigurations() {
47
48 }
49
50
51
52
53
54
55
56
57 public static void remapOption(ApplicationConfig applicationConfig,
58 ConfigOptionDef fromKey,
59 ConfigOptionDef toKey
60 ) {
61 remapOption(applicationConfig, fromKey.getKey(), toKey.getKey());
62 }
63
64
65
66
67
68
69
70
71 public static void remapOption(ApplicationConfig applicationConfig,
72 String fromKey,
73 String toKey
74 ) {
75
76 if (log.isDebugEnabled()) {
77 log.debug(String.format("Change default value of config option: %s=${%s}", fromKey, toKey));
78 }
79 applicationConfig.setDefaultOption(fromKey,
80 String.format("${%s}",
81 toKey));
82 }
83
84
85
86
87
88
89
90
91
92 public static void remapOptionsFromPrefix(
93 ApplicationConfig applicationConfig,
94 ConfigOptionDef[] optionDefs,
95 String fromKeyPrefix,
96 String toKeyPrefix
97 ) {
98
99
100 for (ConfigOptionDef optionDef: optionDefs) {
101 String property = optionDef.getKey();
102
103
104 if (property.startsWith(fromKeyPrefix)) {
105 String endPart = property.substring(fromKeyPrefix.length());
106 String toProperty = toKeyPrefix + endPart;
107
108
109 if (applicationConfig.hasOption(toProperty)) {
110 remapOption(applicationConfig, property, toProperty);
111 }
112 }
113 }
114 }
115
116
117
118
119
120
121
122
123
124 public static void remapOptionsToPrefix(
125 ApplicationConfig applicationConfig,
126 String oldPrefix,
127 ConfigOptionDef[] newOptionDefs,
128 String newPrefix) {
129
130 for (ConfigOptionDef newOptionDef: newOptionDefs) {
131 String property = newOptionDef.getKey();
132
133
134 if (property.startsWith(newPrefix)) {
135 String endPart = property.substring(newPrefix.length());
136 String oldProperty = oldPrefix + endPart;
137
138 remapOption(applicationConfig, oldProperty, property);
139 }
140 }
141 }
142 }