Search in sources :

Example 1 with OrderedProperties

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");
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) ArrayList(java.util.ArrayList) OrderedProperties(com.liferay.imex.core.api.configuration.model.OrderedProperties) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 2 with OrderedProperties

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);
    }
}
Also used : FileProperty(com.liferay.imex.core.api.configuration.model.FileProperty) OrderedProperties(com.liferay.imex.core.api.configuration.model.OrderedProperties) IOException(java.io.IOException) ImexProperties(com.liferay.imex.core.api.configuration.model.ImexProperties) Properties(java.util.Properties) OrderedProperties(com.liferay.imex.core.api.configuration.model.OrderedProperties) File(java.io.File)

Aggregations

OrderedProperties (com.liferay.imex.core.api.configuration.model.OrderedProperties)2 File (java.io.File)2 IOException (java.io.IOException)2 FileProperty (com.liferay.imex.core.api.configuration.model.FileProperty)1 ImexProperties (com.liferay.imex.core.api.configuration.model.ImexProperties)1 BufferedReader (java.io.BufferedReader)1 FileInputStream (java.io.FileInputStream)1 InputStreamReader (java.io.InputStreamReader)1 ArrayList (java.util.ArrayList)1 Properties (java.util.Properties)1