use of com.typesafe.config.ConfigRenderOptions in project incubator-gobblin by apache.
the class PullFileToConfigConverter method convert.
public void convert() throws IOException {
Config baseConfig = ConfigFactory.parseString(DO_NOT_OVERRIDE_KEY + ": []");
FileSystem pullFileFs = pullFileRootPath.getFileSystem(new Configuration());
FileSystem outputFs = this.outputPath.getFileSystem(new Configuration());
Config sysConfig = ConfigFactory.parseFile(this.sysConfigPath);
PullFileLoader pullFileLoader = new PullFileLoader(this.pullFileRootPath, pullFileFs, PullFileLoader.DEFAULT_JAVA_PROPS_PULL_FILE_EXTENSIONS, PullFileLoader.DEFAULT_HOCON_PULL_FILE_EXTENSIONS);
PackagedTemplatesJobCatalogDecorator catalog = new PackagedTemplatesJobCatalogDecorator();
ConfigResolveOptions configResolveOptions = ConfigResolveOptions.defaults();
configResolveOptions = configResolveOptions.setAllowUnresolved(true);
ResourceBasedJobTemplate template;
Config templateConfig;
try {
template = (ResourceBasedJobTemplate) catalog.getTemplate(templateURI.toUri());
templateConfig = sysConfig.withFallback(template.getRawTemplateConfig()).withFallback(baseConfig).resolve(configResolveOptions);
} catch (SpecNotFoundException | JobTemplate.TemplateException exc) {
throw new IOException(exc);
}
Set<String> doNotOverride = templateConfig.hasPath(DO_NOT_OVERRIDE_KEY) ? Sets.newHashSet(templateConfig.getStringList(DO_NOT_OVERRIDE_KEY)) : Sets.<String>newHashSet();
ConfigRenderOptions configRenderOptions = ConfigRenderOptions.defaults();
configRenderOptions = configRenderOptions.setComments(false);
configRenderOptions = configRenderOptions.setOriginComments(false);
configRenderOptions = configRenderOptions.setFormatted(true);
configRenderOptions = configRenderOptions.setJson(false);
for (FileStatus pullFile : pullFileFs.globStatus(this.fileGlobToConvert)) {
Config pullFileConfig = pullFileLoader.loadPullFile(pullFile.getPath(), ConfigFactory.empty(), true).resolve();
Map<String, String> outputConfigMap = Maps.newHashMap();
outputConfigMap.put(ConfigurationKeys.JOB_TEMPLATE_PATH, this.templateURI.toString());
boolean somethingChanged;
do {
somethingChanged = false;
Config currentOutputConfig = ConfigFactory.parseMap(outputConfigMap);
Config currentResolvedConfig = currentOutputConfig.withFallback(templateConfig).resolve(configResolveOptions);
for (Map.Entry<Object, Object> entry : ConfigUtils.configToProperties(pullFileConfig).entrySet()) {
String key = (String) entry.getKey();
String value = (String) entry.getValue();
try {
if ((!currentResolvedConfig.hasPath(key)) || (!currentResolvedConfig.getString(key).equals(value) && !doNotOverride.contains(key))) {
if (!FILTER_KEYS.contains(key)) {
somethingChanged = true;
outputConfigMap.put(key, value);
}
}
} catch (ConfigException.NotResolved nre) {
// path is unresolved in config, will try again next iteration
}
}
} while (somethingChanged);
try {
Config outputConfig = ConfigFactory.parseMap(outputConfigMap);
Config currentResolvedConfig = outputConfig.withFallback(templateConfig).resolve();
String rendered = outputConfig.root().render(configRenderOptions);
Path newPath = PathUtils.removeExtension(pullFile.getPath(), PullFileLoader.DEFAULT_JAVA_PROPS_PULL_FILE_EXTENSIONS.toArray(new String[] {}));
newPath = PathUtils.addExtension(newPath, "conf");
newPath = new Path(this.outputPath, newPath.getName());
FSDataOutputStream os = outputFs.create(newPath);
os.write(rendered.getBytes(Charsets.UTF_8));
os.close();
} catch (ConfigException.NotResolved nre) {
throw new IOException("Not all configuration keys were resolved in pull file " + pullFile.getPath(), nre);
}
}
}
Aggregations