Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.cantaa.util;
  
  import java.io.File;
  import java.util.Arrays;
 import java.util.List;
 import java.util.Set;
 
Load properties for different environments.

Author(s):
Hans Lesmeister
 
 public class PropertiesLoader {
     private static final Logger log = LoggerFactory.getLogger(PropertiesLoader.class);
 
     public static Properties loadProperties(String[] propResourceNames) {
         return loadProperties(Arrays.asList(propResourceNames));
     }
 
     public static Properties loadProperties(Iterable<StringpropResourceNames) {
 
         Properties properties = new Properties();
 
         for (String propFile : propResourceNames) {
             loadPropertiesFromResources(propFileproperties);
         }
 
         return properties;
     }
 
     private static void loadPropertiesFromResources(String propFileProperties targetProperties) {
         if (propFile.startsWith("classpath:")) {
             propFile = propFile.replace("classpath:""");
         }
 
         InputStream in = PropertiesLoader.class.getResourceAsStream("/" + propFile);
         if (in == null) {
             // Properties-File does not exist. Ignore
             .warn("Properties File {} does not exist."propFile);
             return;
         }
 
         .info("Loading properties from file: {}"propFile);
         loadPropertiesFromStream(intargetProperties);
     }

    
Loads properties from the stream and adds them to the applied target properties. The input stream is closed after reading the properties so the caller does not need to take care about that.

Parameters:
in Stream to read from. May not be null, the caller must take care that the stream is not null
targetProperties
 
     public static void loadPropertiesFromStream(InputStream inProperties targetProperties) {
         try {
             Properties properties = new Properties();
             properties.load(in);
             targetProperties.putAll(properties);
         } catch (IOException e) {
             throw new RuntimeException(e);
         } finally {
             IOUtils.closeQuietly(in);
         }
     }

    
Write the contents of a properties instance to the log

Parameters:
props Properties to log
 
     public static void logProperties(Properties props) {
         .info("*** Configured Properties ***");
         List<Stringlist = getPropertiesAsList(props);
         for (String s : list) {
             .info(s);
         }
     }
 
     public static List<StringgetPropertiesAsList(Properties properties) {
         List<Stringlist = new ArrayList<String>(properties.size());
         Enumeration en = properties.propertyNames();
         while (en.hasMoreElements()) {
             String property = (Stringen.nextElement();
             String value = property.toLowerCase().contains("password") ? "*****" : properties.getProperty(property);
             list.add(property + " = " + value);
         }
         Collections.sort(list);
 
        return list;
    }
    public static Properties stripProperties(String prefixProperties properties) {
        return stripProperties(prefixpropertiestruenull);
    }
    public static Properties stripProperties(String prefixProperties propertiesboolean rewrite) {
        return stripProperties(prefixpropertiesrewritenull);
    }

    
Strips a prefix from properties

Parameters:
prefix Prefix to strip
sourceProps Properties to strip
rewrite if true (default) then properties with stripped names overwrite back to the source properties
separator is added to the prefix (Default is a dot.)
Returns:
new Properties-instance with the stripped properties only

     Properties props = new Properties();
     props.setProperty("check.uno", "Some Value");  // Separated by a dot
     stripProperties("check", props);               // Without the dot
     log.debug(prop.getProperty("uno"));            // ==> "Some Value"
     log.debug(prop.getProperty("check.uno"));      // ==> "Some Value" => Original prop with prefix is NOT removed
 
    public static Properties stripProperties(String prefixProperties sourcePropsboolean rewriteString separator) {
        if (StringUtil.isEmpty(prefix) || (sourceProps == null)) {
            return null;
        }
        String localPrefix = prefix + (separator == null ? "."separator);
        Properties strippedProps = new Properties();
        Set<Stringnames = sourceProps.stringPropertyNames();
        for (String name : names) {
            if (name.startsWith(localPrefix)) {
                String value = sourceProps.getProperty(name);
                String newName = name.substring(localPrefix.length());
                strippedProps.setProperty(newNamevalue);
                if (rewrite) {
                    sourceProps.remove(name);
                    sourceProps.setProperty(newNamevalue);
                }
            }
        }
        return strippedProps;
    }

    
Read additional properties from an external file and use the default property name

Parameters:
properties input properties
See also:
extendPropertiesFromExternalFile(java.util.Properties,java.lang.String)
    public static void extendPropertiesFromExternalFile(Properties properties) {
        extendPropertiesFromExternalFile(propertiesnull);
    }

    
Get a filename from the applied properties. If such a filename is provided and the file exists and is readable, then read properties from that file and merge them into the applied properties

Parameters:
properties input properties
key name of the property to read the filename from. If omitted (null or empty) then the key will be "system.extendedproperties"
    public static void extendPropertiesFromExternalFile(Properties propertiesString key) {
        if (properties == null) {
            .debug("'properties' is null");
            return;
        }
        if (StringUtil.isEmpty(key)) {
            key = "system.extendedproperties";
        }
        String fileName = properties.getProperty(key);
        if (StringUtil.isEmpty(fileName)) {
            .debug("No property '{}'"key);
            return;
        }
        File file = new File(fileName);
        if (!file.exists()) {
            .debug("File '{}' does not exist"file);
            return;
        }
        if (!file.canRead()) {
            .debug("File '{}' cannot be read"file);
            return;
        }
        InputStream in = null;
        try {
            .debug("Properties merged from file '{}'"file);
            in = new FileInputStream(file);
            Properties extendedProperties = new Properties();
            extendedProperties.load(in);
            properties.putAll(extendedProperties);
        }
        catch (IOException e) {
            throw new RuntimeException(e);
        }
        finally {
            IOUtils.closeQuietly(in);
        }
    }
New to GrepCode? Check out our FAQ X