Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package Configuration; import java.io.ByteArrayOutputStream; import java.io.File; import org.apache.commons.io.IOUtils; /** * * @author estevo */ /*================================================================= | Interface: ConfigHelper | Purpose: Search and return the database configuration file ===================================================================*/ public class ConfigHelper implements StaticConfiguration { public static File getConfigFile(String fileName) throws NoConfigFileFoundException { for (int i = 0; i < CONFIG_LOCATIONS.length; i++) { String location = CONFIG_LOCATIONS[i]; if (location.startsWith("%HOME%")) { if (isUnix()) { String home = getUserHome(); if (home == null || home.length() <= 0) { continue; } location = location.replace("%HOME%", home); } else { continue; } } File dir = new File(location); if (!dir.isDirectory() || !dir.exists()) { continue; } File configFile = new File(dir, fileName); if (!configFile.exists() || !configFile.canRead()) { continue; } return configFile; } throw new NoConfigFileFoundException(); } public static String getUserHome() { String home = System.getProperty("user.home"); if (home == null || home.length() <= 0) { try { Process exec = Runtime.getRuntime().exec(new String[] { "bash", "-c", "echo $HOME" }); ByteArrayOutputStream baos = new ByteArrayOutputStream(); IOUtils.copy(exec.getInputStream(), baos); exec.getInputStream().close(); home = baos.toString(); } catch (Exception e) { return null; } } return home; } public static boolean isUnix() { String OS = System.getProperty("os.name").toLowerCase(); return (OS.indexOf("nix") >= 0 || OS.indexOf("nux") >= 0 || OS.indexOf("") >= 0 || OS.indexOf("aix") >= 0); } }