1 package fr.ifremer.quadrige2.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 Quadrige2Configurations {
40
41 private static final Log log = LogFactory.getLog(Quadrige2Configurations.class);
42
43
44
45
46 protected Quadrige2Configurations() {
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 fromProperty = property;
106 String endPart = fromProperty.substring(fromKeyPrefix.length());
107 String toProperty = toKeyPrefix + endPart;
108
109
110 if (applicationConfig.hasOption(toProperty)) {
111 remapOption(applicationConfig, fromProperty, toProperty);
112 }
113 }
114 }
115 }
116
117
118
119
120
121
122
123
124
125 public static void remapOptionsToPrefix(
126 ApplicationConfig applicationConfig,
127 String oldPrefix,
128 ConfigOptionDef[] newOptionDefs,
129 String newPrefix) {
130
131 for (ConfigOptionDef newOptionDef: newOptionDefs) {
132 String property = newOptionDef.getKey();
133
134
135 if (property.startsWith(newPrefix)) {
136 String newProperty = property;
137 String endPart = newProperty.substring(newPrefix.length());
138 String oldProperty = oldPrefix + endPart;
139
140 remapOption(applicationConfig, oldProperty, newProperty);
141 }
142 }
143 }
144 }