This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
OptionsUtil.java
275 lines (255 loc) · 10.7 KB
/
OptionsUtil.java
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package org.zanata.client.commands;
import java.io.File;
import java.net.URISyntaxException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.HierarchicalINIConfiguration;
import org.apache.commons.configuration.SubnodeConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zanata.client.config.ConfigUtil;
import org.zanata.client.config.LocaleList;
import org.zanata.client.config.ZanataConfig;
import org.zanata.client.exceptions.ConfigException;
import org.zanata.rest.client.ZanataProxyFactory;
import org.zanata.util.VersionUtility;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
public class OptionsUtil {
private static final Logger log = LoggerFactory
.getLogger(OptionsUtil.class);
/**
* Loads the config files (controlled by the property userConfig) to supply
* any values which haven't already been set.
*
* @throws Exception
*/
public static void applyConfigFiles(ConfigurableOptions opts)
throws ConfigurationException, JAXBException {
if (opts instanceof ConfigurableProjectOptions) {
ConfigurableProjectOptions projOpts =
(ConfigurableProjectOptions) opts;
if (projOpts.getProjectConfig() != null) {
JAXBContext jc = JAXBContext.newInstance(ZanataConfig.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File projectConfigFile = projOpts.getProjectConfig();
if (projectConfigFile.exists()) {
log.info("Loading project config from {}",
projectConfigFile);
ZanataConfig projectConfig =
(ZanataConfig) unmarshaller
.unmarshal(projectConfigFile);
// local project config is supposed to override user's
// zanata.ini,
// so we apply it first
applyProjectConfig(projOpts, projectConfig);
} else {
log.warn("Project config file '{}' not found; ignoring.",
projectConfigFile);
}
}
}
if (opts.getUserConfig() != null) {
if (opts.getUserConfig().exists()) {
log.info("Loading user config from {}", opts.getUserConfig());
HierarchicalINIConfiguration dataConfig =
new HierarchicalINIConfiguration(opts.getUserConfig());
applyUserConfig(opts, dataConfig);
} else {
System.err.printf(
"User config file '%s' not found; ignoring.%n",
opts.getUserConfig());
}
}
}
/**
* Applies values from the project configuration unless they have been set
* directly via parameters.
*
* @param config
*/
private static void applyProjectConfig(ConfigurableProjectOptions opts,
ZanataConfig config) {
if (opts.getProj() == null) {
opts.setProj(config.getProject());
}
if (opts.getUrl() == null) {
opts.setUrl(config.getUrl());
}
if (opts.getProjectVersion() == null) {
opts.setProjectVersion(config.getProjectVersion());
}
if (opts.getProjectType() == null) {
opts.setProjectType(config.getProjectType());
}
applySrcDirAndTransDirFromProjectConfig(opts, config);
applyIncludesAndExcludesFromProjectConfig(opts, config);
LocaleList locales = config.getLocales();
opts.setLocaleMapList(locales);
if (opts.getCommandHooks().isEmpty() && config.getHooks() != null) {
opts.setCommandHooks(config.getHooks());
}
opts.setFileMappingRules(config.getRules());
}
/**
* Note: command line options take precedence over pom.xml which
* takes precedence over zanata.xml.
*
* @see org.zanata.client.commands.OptionMismatchChecker
* @param opts
* options
* @param config
* config from project configuration file i.e. zanata.xml
*/
@VisibleForTesting
protected static void applySrcDirAndTransDirFromProjectConfig(
ConfigurableProjectOptions opts, ZanataConfig config) {
// apply srcDir configuration
OptionMismatchChecker<File> srcDirChecker =
OptionMismatchChecker.from(opts.getSrcDir(),
config.getSrcDirAsFile(),
"Source directory");
if (srcDirChecker.hasValueInConfigOnly()) {
opts.setSrcDir(config.getSrcDirAsFile());
}
srcDirChecker.logHintIfNotDefinedInConfig(
String.format("<src-dir>%s</src-dir>", opts.getSrcDir()));
srcDirChecker.logWarningIfValuesMismatch();
// apply transDir configuration
OptionMismatchChecker<File> transDirChecker = OptionMismatchChecker
.from(opts.getTransDir(), config.getTransDirAsFile(),
"Translation directory");
if (transDirChecker.hasValueInConfigOnly()) {
opts.setTransDir(config.getTransDirAsFile());
}
transDirChecker.logHintIfNotDefinedInConfig(String.format(
"<trans-dir>%s</trans-dir>", opts.getTransDir()));
transDirChecker.logWarningIfValuesMismatch();
}
/**
* Note: command line options take precedence over pom.xml which
* takes precedence over zanata.xml.
*
* @see org.zanata.client.commands.OptionMismatchChecker
* @param opts
* options
* @param config
* config from project configuration file i.e. zanata.xml
*/
protected static void applyIncludesAndExcludesFromProjectConfig(
ConfigurableProjectOptions opts, ZanataConfig config) {
OptionMismatchChecker<ImmutableList<String>> includesChecker =
OptionMismatchChecker
.from(opts.getIncludes(), config.getIncludesAsList(),
"Includes");
if (includesChecker.hasValueInConfigOnly()) {
opts.setIncludes(config.getIncludes());
}
Joiner commaJoiner = Joiner.on(",");
includesChecker.logHintIfNotDefinedInConfig(String.format(
"<includes>%s</includes>",
commaJoiner.join(opts.getIncludes())));
includesChecker.logWarningIfValuesMismatch();
OptionMismatchChecker<ImmutableList<String>> excludesChecker =
OptionMismatchChecker
.from(opts.getExcludes(), config.getExcludesAsList(),
"Excludes");
if (excludesChecker.hasValueInConfigOnly()) {
opts.setExcludes(config.getExcludes());
}
excludesChecker
.logHintIfNotDefinedInConfig(
String.format("<excludes>%s</excludes>",
commaJoiner.join(opts.getExcludes())));
excludesChecker.logWarningIfValuesMismatch();
}
/**
* Applies values from the user's personal configuration unless they have
* been set directly (by parameters or by project configuration).
*
* @param config
*/
public static void applyUserConfig(ConfigurableOptions opts,
HierarchicalINIConfiguration config) {
if (!opts.isDebugSet()) {
Boolean debug = config.getBoolean("defaults.debug", null);
if (debug != null)
opts.setDebug(debug);
}
if (!opts.isErrorsSet()) {
Boolean errors = config.getBoolean("defaults.errors", null);
if (errors != null)
opts.setErrors(errors);
}
if (!opts.isQuietSet()) {
Boolean quiet = config.getBoolean("defaults.quiet", null);
if (quiet != null)
opts.setQuiet(quiet);
}
if ((opts.getUsername() == null || opts.getKey() == null)
&& opts.getUrl() != null) {
SubnodeConfiguration servers = config.getSection("servers");
String prefix = ConfigUtil.findPrefix(servers, opts.getUrl());
if (prefix != null) {
if (opts.getUsername() == null) {
opts.setUsername(servers.getString(prefix + ".username",
null));
}
if (opts.getKey() == null) {
opts.setKey(servers.getString(prefix + ".key", null));
}
}
}
}
/**
* Creates proxy factory that will perform an eager REST version check.
*/
public static ZanataProxyFactory createRequestFactory(
ConfigurableOptions opts) {
try {
checkMandatoryOptsForRequestFactory(opts);
return new ZanataProxyFactory(opts.getUrl().toURI(),
opts.getUsername(), opts.getKey(),
VersionUtility.getAPIVersionInfo(), opts.getLogHttp(),
opts.isDisableSSLCert());
} catch (URISyntaxException e) {
throw new ConfigException(e);
}
}
private static void checkMandatoryOptsForRequestFactory(
ConfigurableOptions opts) {
if (opts.getUrl() == null) {
throw new ConfigException("Server URL must be specified");
}
if (opts.getUsername() == null) {
throw new ConfigException("Username must be specified");
}
if (opts.getKey() == null) {
throw new ConfigException("API key must be specified");
}
if (opts.isDisableSSLCert()) {
log.warn("SSL certificate verification will be disabled. You should consider adding the certificate instead of disabling it.");
}
}
/**
* Creates proxy factory that will NOT perform an eager REST version check.
* You can call
* org.zanata.rest.client.ZanataProxyFactory#performVersionCheck()
* afterwards.
*/
public static ZanataProxyFactory createRequestFactoryWithoutVersionCheck(
ConfigurableProjectOptions opts) {
try {
checkMandatoryOptsForRequestFactory(opts);
return new ZanataProxyFactory(opts.getUrl().toURI(),
opts.getUsername(), opts.getKey(),
VersionUtility.getAPIVersionInfo(), opts.getLogHttp(),
opts.isDisableSSLCert(), false);
} catch (URISyntaxException e) {
throw new ConfigException(e);
}
}
}