All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.rapiddweller.common.file.PropertiesFileMerger Maven / Gradle / Ivy

Go to download

'rapiddweller Common' is an open source Java library forked from Databene Commons by Volker Bergmann. It provides extensions to the Java core library by utility classes, abstract concepts and concrete implementations.

There is a newer version: 2.0.1-jdk-11
Show newest version
/*
 * Copyright (C) 2004-2015 Volker Bergmann ([email protected]).
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.rapiddweller.common.file;

import com.rapiddweller.common.Encodings;
import com.rapiddweller.common.IOUtil;
import com.rapiddweller.common.StringUtil;
import com.rapiddweller.common.collection.TreeBuilder;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Map;

/**
 * Merges properties files with priority and allows for override by VM parameters.
 * Created: 01.08.2013 10:37:30
 *
 * @author Volker Bergmann
 * @since 0.5.24
 */
public class PropertiesFileMerger {

  private static final Logger LOGGER = LogManager.getLogger(PropertiesFileMerger.class);

  /**
   * Merge.
   *
   * @param targetPath  the target path
   * @param sourceFiles the source files
   * @throws IOException the io exception
   */
  public static void merge(String targetPath, String... sourceFiles) throws IOException {
    merge(targetPath, true, sourceFiles);
  }

  /**
   * Merge.
   *
   * @param targetPath  the target path
   * @param vmOverride  the vm override
   * @param sourceFiles the source files
   * @throws IOException the io exception
   */
  public static void merge(String targetPath, boolean vmOverride, String... sourceFiles) throws IOException {
    LOGGER.debug("Merging the files {} into target file: {}", Arrays.toString(sourceFiles), targetPath);
    TreeBuilder tree = null;
    for (String sourceFile : sourceFiles) {
      tree = loadClasspathResourceIfPresent(sourceFile, tree); // find resource on class path
      tree = loadFileIfPresent(sourceFile, tree); // find resource on file system
    }
    if (tree != null) {
      if (vmOverride) {
        overwritePropertiesWithVMParams(tree);
      }
      // write properties in UTF-8
      if (targetPath.toLowerCase().endsWith(".xml")) {
        tree.saveAsXML(new FileOutputStream(targetPath), Encodings.UTF_8);
      } else {
        tree.saveAsProperties(new FileOutputStream(targetPath));
      }
    }
  }

  /**
   * Load classpath resource if present tree builder.
   *
   * @param resourceName the resource name
   * @param base         the base
   * @return the tree builder
   * @throws IOException the io exception
   */
  static TreeBuilder loadClasspathResourceIfPresent(String resourceName, TreeBuilder base) throws IOException {
    InputStream in = IOUtil.getResourceAsStream(resourceName, false);
    if (in != null) {
      LOGGER.debug("Loading and merging structure of classpath resource '{}' ", resourceName);
      try {
        TreeBuilder newTree = TreeBuilder.loadFromStream(in, resourceName);
        return overwriteProperties(base, newTree);
      } finally {
        IOUtil.close(in);
      }
    } else {
      return base;
    }
  }

  /**
   * Load file if present tree builder.
   *
   * @param sourceFile the source file
   * @param base       the base
   * @return the tree builder
   * @throws IOException the io exception
   */
  static TreeBuilder loadFileIfPresent(String sourceFile, TreeBuilder base) throws IOException {
    File file = new File(sourceFile);
    if (file.exists()) {
      LOGGER.debug("Loading and merging properties of file '{}' ", sourceFile);
      FileInputStream in = new FileInputStream(file);
      try {
        TreeBuilder newTree = TreeBuilder.loadFromStream(in, sourceFile);
        return overwriteProperties(base, newTree);
      } finally {
        IOUtil.close(in);
      }
    } else {
      return base;
    }
  }

  /**
   * Overwrite properties tree builder.
   *
   * @param base       the base
   * @param overwrites the overwrites
   * @return the tree builder
   */
  static TreeBuilder overwriteProperties(TreeBuilder base, TreeBuilder overwrites) {
    if (base == null) {
      return overwrites;
    }
    if (overwrites == null) {
      return base;
    }
    overwrite(base.getRootNode(), overwrites.getRootNode());
    return base;
  }

  @SuppressWarnings("unchecked")
  private static void overwrite(Map baseNode, Map overwriteNode) {
    for (Map.Entry overEntry : overwriteNode.entrySet()) {
      String key = overEntry.getKey();
      Object oldValue = baseNode.get(key);
      if (oldValue == null) {
        baseNode.put(key, overEntry.getValue());
      } else if (oldValue instanceof Map) {
        overwrite((Map) oldValue, (Map) overEntry.getValue());
      } else {
        baseNode.put(key, overEntry.getValue());
      }
    }
  }

  /**
   * Overwrite properties with vm params.
   *
   * @param tree the tree
   */
  static void overwritePropertiesWithVMParams(TreeBuilder tree) {
    LOGGER.debug("Checking properties against VM settings override");
    overwritePropertiesWithVMParams(tree.getRootNode(), tree.getRootName());
  }

  @SuppressWarnings("unchecked")
  private static void overwritePropertiesWithVMParams(Map node, String path) {
    for (Map.Entry entry : node.entrySet()) {
      String subPath = subPath(path, entry.getKey(), '.');
      Object child = entry.getValue();
      if (child instanceof Map) {
        overwritePropertiesWithVMParams((Map) child, subPath);
      } else if (child instanceof String) {
        String vmSetting = System.getProperty(subPath);
        if (vmSetting != null && vmSetting.length() > 0) {
          LOGGER.debug("Overwriting '{}' property with '{}'", subPath, vmSetting);
          entry.setValue(vmSetting);
        }
      }
    }
  }

  private static String subPath(String parentPath, String childName, char separator) {
    return (StringUtil.isEmpty(parentPath) ? "" : parentPath + separator) + childName;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy