Skip to content

Instantly share code, notes, and snippets.

@sauceaaron
Last active November 20, 2018 15:38
Show Gist options
  • Save sauceaaron/fce0b87fcc7b833845bf84671fa40e2f to your computer and use it in GitHub Desktop.
Save sauceaaron/fce0b87fcc7b833845bf84671fa40e2f to your computer and use it in GitHub Desktop.
Load browser configuration from properties file, environment variable, command line
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class BrowserConfig
{
public String getBrowserPath()
{
String browserPath = "/Applications/Firefox.app/Contents/MacOS/firefox"; // default
String fromPropertiesFile = loadPropertiesFile("browser.properties").getProperty("browserPath");
String fromEnvironmentVariable = System.getenv("browserPath");
String fromSystemProperty = System.getProperty("browserPath");
if (fromPropertiesFile != null) { browserPath = fromPropertiesFile; }
if (fromEnvironmentVariable != null) { browserPath = fromEnvironmentVariable; }
if (fromSystemProperty != null) { browserPath = fromSystemProperty; }
return browserPath;
}
public Properties loadPropertiesFile(String filename)
{
ClassLoader loader = Thread.currentThread().getContextClassLoader();
Properties properties = new Properties();
try
{
InputStream resourceStream = loader.getResourceAsStream(filename);
properties.load(resourceStream);
}
catch (IOException e)
{
e.printStackTrace();
}
return properties;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment