Java Properties Load from File loadRecursively(String propertiesFilename)

Here you can find the source of loadRecursively(String propertiesFilename)

Description

Load the specified properties file and recursively merge all included properties files.

License

Open Source License

Parameter

Parameter Description
propertiesFilename The name of the properies file

Exception

Parameter Description
IOException When an exception occurs while attempting to read a properties file

Return

A Properties instance containing all of the properties

Declaration


public static Properties loadRecursively(String propertiesFilename) throws IOException 

Method Source Code


//package com.java2s;
/*/*w  ww  . ja v  a 2s . co m*/
 * @(#)PropertyUtility.java
 *
 * Copyright 2003 by EkoLiving Pty Ltd.  All Rights Reserved.
 *
 * This software is the proprietary information of EkoLiving Pty Ltd.
 * Use is subject to license terms.
 */

import java.io.*;
import java.util.*;

public class Main {
    public static final String PROPERTY_INCLUDE_ABSOLUTE = "org.openmaji.includeFileAbsolute";
    public static final String PROPERTY_INCLUDE_RELATIVE = "org.openmaji.includeFileRelative";

    /**
     * Load the specified properties file and recursively merge all included properties files.
     * If the properties file contains the property PROPERTY_INCLUDE_ABSOLUTE this method
     * will attempt to load the properties from that second properties file and merge them with
     * the properties from the first property file. If the properties file contains the
     * property PROPERTY_INCLUDE_RELATIVE this method will attempt to load the properties
     * from a file whose path is relative to the first file.
     * 
     * <p>
     * Note that properties contained in included properties file
     * will NOT override identically named properties in the first properties file.
     * </p>
     *
     * @param propertiesFilename The name of the properies file
     * @return A Properties instance containing all of the properties
     * @throws IOException When an exception occurs while attempting to read a properties file
     */

    public static Properties loadRecursively(String propertiesFilename) throws IOException {

        Properties properties = new Properties();
        File propertiesFile = new File(propertiesFilename);
        FileInputStream fis = new FileInputStream(propertiesFile);
        properties.load(fis);
        fis.close();

        String secondPropertiesFilename = properties.getProperty(PROPERTY_INCLUDE_ABSOLUTE);
        if (secondPropertiesFilename != null) {
            properties.remove(PROPERTY_INCLUDE_ABSOLUTE);
            Properties include = loadRecursively(secondPropertiesFilename);
            mergeProperties(properties, include);
        }

        secondPropertiesFilename = properties.getProperty(PROPERTY_INCLUDE_RELATIVE);
        if (secondPropertiesFilename != null) {
            String directoryName = propertiesFile.getParent();
            File secondPropertiesFile = new File(directoryName, secondPropertiesFilename);
            properties.remove(PROPERTY_INCLUDE_RELATIVE);
            Properties include = loadRecursively(secondPropertiesFile.getAbsolutePath());
            mergeProperties(properties, include);
        }

        return properties;
    }

    /**
     * <p>
     * Merge a new set of properties, which are located in the specified
     * properties file, with an existing set of properties.  The merged properties end up
     * in the existing set of properties. This method invokes loadRecursively() to load
     * the properties file.
     * </p>
     * 
     * <p>
     * Note that properties contained in 'sourcePropertiesFilename' will NOT override identically
     * named properties in 'destinationProperties'.
     * </p>
     *
     * @param destinationProperties An existing set of properties
     * @param sourcePropertiesFilename Properties to merge into the existing ones
     * @exception IOException Problem reading the sourcePropertiesFilename
     */

    public static void mergeProperties(Properties destinationProperties, String sourcePropertiesFilename)
            throws IOException {

        Properties properties = loadRecursively(sourcePropertiesFilename);
        mergeProperties(destinationProperties, properties);
    }

    /**
     * Merge a new set of properties with an existing set of properties.
     * The merger ends up in the existing set of properties. Note that properties
     * contained in 'sourceProperties' will NOT override identically named properties in
     * 'destinationProperties'.
     *
     * @param destinationProperties An existing set of properties
     * @param sourceProperties Properties to merge into the existing ones
     */

    public static void mergeProperties(Properties destinationProperties, Properties sourceProperties)
            throws IOException {

        if (sourceProperties == null)
            return;

        Enumeration enumeration = sourceProperties.propertyNames();
        while (enumeration.hasMoreElements()) {
            String name = (String) enumeration.nextElement();
            if (destinationProperties.containsKey(name) == false) {
                String value = sourceProperties.getProperty(name);
                destinationProperties.setProperty(name, value);
            }
        }
    }
}

Related

  1. loadPropertiesFromPath(String propsFilePath)
  2. loadPropertiesInClassPath(String propertiesFileName)
  3. loadProvidersConfiguration(Class base, String confPropertiesName)
  4. loadPythonScript(String pythonScriptName)
  5. loadReader(Reader stream)
  6. loadRQGProperties()
  7. loadSetting(InputStream in, String key)
  8. loadSettings(String propertiesFileName)
  9. loadStrictly(File file)