001package org.unix4j.util;
002
003import java.io.IOException;
004import java.io.InputStream;
005import java.util.Properties;
006
007/**
008 * Class with static utility methods if one has to deal with {@link Properties}.
009 */
010public class PropertyUtil {
011
012        /**
013         * Returns the specified property given the {@code propertyName} either from
014         * the current user's properties file if it exists or from the given
015         * {@code defaultPropertiesFile}.
016         * <p>
017         * The method throws an exception if neither the default nor the machine
018         * specific file exists.
019         * 
020         * @param defaultPropertiesFile
021         *            the default properties file, must exist
022         * @param propertyName
023         *            the name of the desired property
024         * @param fallbackValue
025         *            the fallback value if no property of the given name exists
026         * @return the property value
027         */
028        public static String getProperty(String defaultPropertiesFile, String propertyName, String fallbackValue) {
029                final Properties props = getProperties(defaultPropertiesFile);
030                if (props == null) {
031                        throw new IllegalArgumentException("default properties file not found: " + defaultPropertiesFile);
032                }
033                return props.getProperty(propertyName, fallbackValue);
034        }
035
036        public static Properties getProperties(String defaultPropertiesFile) {
037                try {
038                        final Properties properties = new Properties();
039                        properties.load(ResourceUtil.getResource(PropertyUtil.class, defaultPropertiesFile));
040                        final String userPropertiesFile = getUserPropertiesFileNameFor(defaultPropertiesFile);
041                        InputStream userPropertiesStream = null;
042                        try {
043                                userPropertiesStream = ResourceUtil.getResource(PropertyUtil.class, userPropertiesFile);
044                        } catch (IllegalArgumentException e) {
045                                System.err.println("WARN: user properties file not found : " + userPropertiesFile);
046                                System.err.println("WARN: using default properties file  : " + defaultPropertiesFile);
047                        }
048                        if (userPropertiesStream != null) {
049                                final Properties userProperties = new Properties();
050                                userProperties.load(userPropertiesStream);
051                                properties.putAll(userProperties);//replaces default properties
052                        }
053                        return properties;
054                } catch (IOException e) {
055                        return null;
056                }
057        }
058
059        private static String getUserPropertiesFileNameFor(String defaultPropertiesFile) {
060                final String defaultPostfix = "-default";
061                final String localPostfix = "-" + getUserName();
062                final String ending = ".properties";
063                final String postfix = defaultPostfix + ending;
064                if (defaultPropertiesFile.endsWith(postfix)) {
065                        return defaultPropertiesFile.substring(0, defaultPropertiesFile.length() - postfix.length()) + localPostfix + ending;
066                }
067                throw new IllegalArgumentException("name of default properties file must end with " + postfix + ": " + defaultPropertiesFile);
068        }
069
070        private static String getUserName() {
071                final String userName = System.getProperty("user.name", null);
072                if (userName != null) {
073                        return userName;
074                }
075                throw new IllegalStateException("cannot evaulate user.name from system properties");
076        }
077
078        // no instances
079        private PropertyUtil() {
080                super();
081        }
082
083}