use of com.liferay.imex.core.api.configuration.model.OrderedProperties in project liferay-imex by jpdacunha.
the class PropertyMergerServiceImpl method merge.
public void merge(FileProperty sourceFileProperty, FileProperty modifiedFileProperty, File outpuFile) {
if (outpuFile == null || !outpuFile.exists()) {
log.error("Invalid outputfile");
return;
}
reportService.getStartMessage(log, "properties merge");
File sourceFile = sourceFileProperty.getOriginalFile();
File modifiedFile = modifiedFileProperty.getOriginalFile();
reportService.getMessage(log, "Merging ORIGINAL : [" + sourceFile.getAbsolutePath() + "] with MODIFIED : [" + modifiedFile.getAbsolutePath() + "]");
OrderedProperties modifiedProperties = modifiedFileProperty.getProperties();
OrderedProperties sourceFileProperties = sourceFileProperty.getProperties();
try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(sourceFile)))) {
List<String> toWriteList = new ArrayList<>();
String line = null;
while ((line = br.readLine()) != null) {
if (log.isDebugEnabled()) {
log.debug(" # Processing line [" + line + "] ...");
}
String trimedLine = line.trim();
boolean isComment = trimedLine.startsWith("#");
boolean isEmpty = Validator.isNull(trimedLine);
if (!isEmpty && !isComment) {
// search key of the line
String[] keyNVal = line.split(EQUAL);
if (keyNVal != null && StringUtils.countMatches(line, EQUAL) == 1) {
// Saving original key to preserve indentation in final property file
String originalKey = keyNVal[0];
String key = originalKey.trim();
String modifiedValue = modifiedProperties.getProperty(key);
String sourceValue = sourceFileProperties.getProperty(key);
if (log.isDebugEnabled()) {
log.debug("Processing key [" + key + "] with sourceValue:[" + sourceValue + "] versus modifiedValue:[" + modifiedValue + "] ...");
}
String relevantValue = StringPool.BLANK;
if (Validator.isNotNull(modifiedValue)) {
relevantValue = modifiedValue;
} else if (Validator.isNotNull(sourceValue)) {
relevantValue = sourceValue;
} else {
relevantValue = StringPool.BLANK;
}
toWriteList.add(originalKey + EQUAL + relevantValue);
reportService.getMessage(log, "Registering : [" + key + "] = [" + relevantValue + "] as merged line.");
} else {
toWriteList.add(line);
reportService.getError(log, "Property parsing error", "Unable to merge line [" + line + "] because it is not a valid property line. Writing unmerged line.");
}
} else {
// Writing original line to preserve final file indentation
toWriteList.add(line);
}
}
// Finally writing lines
if (toWriteList.size() > 0) {
log.debug(" : Writing [" + toWriteList.size() + "] lines in [" + outpuFile.getAbsolutePath() + "]");
FileUtils.writeLines(outpuFile, StandardCharsets.UTF_8.name(), toWriteList);
log.debug(" : Done.");
}
} catch (IOException e) {
log.error(e, e);
}
reportService.getEndMessage(log, "properties merge");
}
use of com.liferay.imex.core.api.configuration.model.OrderedProperties in project liferay-imex by jpdacunha.
the class ImexCoreServiceImpl method mergeConfiguration.
private void mergeConfiguration(Map<String, Properties> props, String entryKey, Bundle bundle) {
// Loading relevant configuration to apply (this configuration contain all properties to apply)
String bundleSymbolicName = bundle.getSymbolicName();
try {
// Configuration file does not exists
File overridedPropsFile = configurationService.getConfigurationOverrideFileName(bundle);
if (overridedPropsFile == null || (overridedPropsFile != null && !overridedPropsFile.exists())) {
// Writing file with default datas
configurationService.loadDefaultConfigurationInFile(bundle, overridedPropsFile);
reportService.getOK(_log, entryKey, overridedPropsFile.getName(), overridedPropsFile, ImexOperationEnum.CREATE);
} else {
// Configuration file exists executing merge
String tempFilesPrefix = ImExCorePropsKeys.IMEX_PREFIX + StringPool.MINUS + bundleSymbolicName + StringPool.MINUS;
// Loading default configuration and saving it in temp file
String defaultPropsFileName = tempFilesPrefix + "default" + StringPool.MINUS;
File defaultTempPropsFile = File.createTempFile(defaultPropsFileName, FileUtil.PROPERTIES_EXTENSION, getImexTempDir());
configurationService.loadDefaultConfigurationInFile(bundle, defaultTempPropsFile);
if (defaultTempPropsFile != null && defaultTempPropsFile.exists()) {
// To file Property convertion
OrderedProperties defaultOrderedProperty = new OrderedProperties();
defaultOrderedProperty.load(defaultTempPropsFile);
FileProperty defaultFileProperties = new FileProperty(defaultTempPropsFile, defaultOrderedProperty);
// Loading relevant properties merged (embbeded in jar + default)
Properties toApplyProperties = props.get(bundleSymbolicName);
// Creating temp File with content to merge
String tempFileName = tempFilesPrefix + "overrided" + StringPool.MINUS;
File toApplyPropertiesTempFile = File.createTempFile(tempFileName, FileUtil.PROPERTIES_EXTENSION, getImexTempDir());
FileUtil.loadPropertiesInFile(toApplyPropertiesTempFile, toApplyProperties);
OrderedProperties toApplyOrderedProperty = new OrderedProperties();
toApplyOrderedProperty.load(toApplyPropertiesTempFile);
FileProperty toApplyFileProperties = new FileProperty(toApplyPropertiesTempFile, toApplyOrderedProperty);
// Call merge method Property merger
propertyMergerService.merge(defaultFileProperties, toApplyFileProperties, overridedPropsFile);
// Cleaning
if (toApplyPropertiesTempFile.exists()) {
toApplyPropertiesTempFile.delete();
}
if (defaultTempPropsFile.exists()) {
defaultTempPropsFile.delete();
}
} else {
reportService.getDNE(_log, defaultTempPropsFile);
}
reportService.getOK(_log, entryKey, overridedPropsFile.getName(), overridedPropsFile, ImexOperationEnum.UPDATE);
}
} catch (IOException e) {
_log.error(e, e);
}
}
Aggregations