Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package com.cantaa.util;
 
 import java.util.List;
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) {
        Properties properties = new Properties();
        for (String propFile : propResourceNames) {
            loadPropertiesFromResources(propFileproperties);
        }
        return properties;
    }
    private static void loadPropertiesFromResources(String propFileProperties targetProperties) {
        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();
            list.add(property + " = " + properties.getProperty(property));
        }
        Collections.sort(list);
        return list;
    }
New to GrepCode? Check out our FAQ X