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

org.deephacks.tools4j.config.model.SystemProperties Maven / Gradle / Ivy

package org.deephacks.tools4j.config.model;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Strings;
import com.google.common.io.Closeables;

/**
 * SystemProperties provides a way for reading simple properties.
 * 
 * Properties will be read from multiple sources using a fall-back mechanism. The first
 * source to return a valid value will be returned.
 * 
 * Properties are read in the following order:
 * 
 * 1) System.getProperty
 * 2) User home level file
 * 3) System level file    
 * 4) Class path file
 * 
 */
public class SystemProperties {
    private static final Logger log = LoggerFactory.getLogger(SystemProperties.class);
    public static final String PROPERTY_FILE_PROPERTY = "tools4j.conf";
    private static final String DEFAULT_GLOBAL_PROPERTY_FILENAME = "tools4j.conf";
    private static final String DEFAULT_PROPERTY_FILE_DIR = ".tools4j";

    private static final String GLOBAL_PROPERTY_FILE_DIR_UNIX = "/etc";
    private static final String GLOBAL_PROPERTY_FILE_DIR_WIN = "c:\\";

    private String dir;
    private String filename;

    private static final Properties properties = new Properties();

    private SystemProperties(String dir, String filename) {
        this.dir = dir;
        this.filename = filename;
    }

    public static SystemProperties createDefault() {
        String value = System.getProperty(PROPERTY_FILE_PROPERTY);
        if (!Strings.isNullOrEmpty(value)) {
            File file = new File(value);
            if (file.exists()) {
                return new SystemProperties(file.getParent(), file.getName());
            }
        }
        return new SystemProperties(DEFAULT_PROPERTY_FILE_DIR, DEFAULT_GLOBAL_PROPERTY_FILENAME);
    }

    public static void add(Properties props) {
        properties.putAll(props);
    }

    public static SystemProperties create(String dir, String filename) {
        return new SystemProperties(dir, filename);
    }

    public String get(String name) {
        String v = getSystemProperty(name);
        if (v != null) {
            log.trace("Read SystemProperty {}={}", name, v);
            return v;
        }
        Object value = (String) properties.get(name);
        if (value != null) {
            return value.toString();
        }
        v = getUserHomeProperty(name);
        if (v != null) {
            log.trace("Read user home property {}={}", name, v);
            return v;
        }
        v = getGlobalProperty(name);
        if (v != null) {
            log.trace("Read global property {}={}", name, v);
            return v;
        }
        URL url = SystemProperties.class.getClassLoader().getResource(filename);
        if (url != null) {
            v = getFileProperty(name, url);
            if (v != null) {
                log.trace("Read classloader resource property {}={}", name, v);
                return v;
            }
        }
        log.trace("Property not found {}", name);
        return null;
    }

    private String getGlobalProperty(String name) {
        File propFile = new File(dir, filename);
        if (!propFile.exists()) {
            return null;
        }
        String v = getFileProperty(name, propFile);
        return v;
    }

    public String getSystemProperty(String name) {
        return System.getProperty(name);
    }

    public String getUserHomeProperty(String name) {
        File home = getUserHome();
        if (home == null) {
            return null;
        }
        File propFileDir = new File(home, dir);
        File propFile = new File(propFileDir, filename);
        if (!propFile.exists()) {
            return null;
        }
        return getFileProperty(name, propFile);
    }

    private String getFileProperty(String name, File file) {
        try {
            return getFileProperty(name, new FileInputStream(file));
        } catch (FileNotFoundException e) {
            return null;
        }
    }

    private String getFileProperty(String name, URL url) {
        try {
            return getFileProperty(name, url.openStream());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private String getFileProperty(String name, InputStream in) {
        try {
            Properties p = new Properties();
            p.load(in);
            Object v = p.get(name);
            if (v == null) {
                return null;
            }
            return v.toString();
        } catch (IOException e) {
            // user might want to know about this
            throw new RuntimeException(e);
        } finally {
            Closeables.closeQuietly(in);
        }
    }

    private File getUserHome() {
        String home = System.getProperty("user.home");
        if (home == null || "".equals(home)) {
            return null;
        }
        File homeDir = new File(home);
        if (!homeDir.exists()) {
            return null;
        }
        return homeDir;
    }

    public static boolean isWindows() {
        String os = System.getProperty("os.name").toLowerCase();
        return (os.indexOf("win") >= 0);
    }

    public static boolean isMac() {
        String os = System.getProperty("os.name").toLowerCase();
        return (os.indexOf("mac") >= 0);

    }

    public static boolean isUnix() {
        String os = System.getProperty("os.name").toLowerCase();
        return (os.indexOf("nix") >= 0 || os.indexOf("nux") >= 0);

    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy