diff options
author | Peter Wu <peter@lekensteyn.nl> | 2014-04-24 01:58:02 +0200 |
---|---|---|
committer | Peter Wu <peter@lekensteyn.nl> | 2014-04-24 01:58:02 +0200 |
commit | c79e6a27e9e0617171e9d4a78364ed71f9153c80 (patch) | |
tree | 40633e853188bc016d0320ae9b211204345cd4f8 /src/utils/Configuration.java | |
parent | 4d22f09283ad6ac66c0e76cc41747e0b8e492436 (diff) | |
download | TwitterDataAnalytics-c79e6a27e9e0617171e9d4a78364ed71f9153c80.tar.gz |
Extend configuration with preferences store
Diffstat (limited to 'src/utils/Configuration.java')
-rw-r--r-- | src/utils/Configuration.java | 84 |
1 files changed, 76 insertions, 8 deletions
diff --git a/src/utils/Configuration.java b/src/utils/Configuration.java index 0595a86..abb4df0 100644 --- a/src/utils/Configuration.java +++ b/src/utils/Configuration.java @@ -1,14 +1,82 @@ package utils; -// This class contains important values for the open authentication. -public class Configuration -{ +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.Properties; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Global configuration including user-specific settings. + */ +public class Configuration { + // The constumer secret value obtained from twitter.com - public static final String CONSUMER_SECRET = "0aQLFiAwAgPHOYt6tziWoDftayQR7n3cUzMbGlhiNqsNNylLu4"; + public static final String CONSUMER_SECRET = "0aQLFiAwAgPHOYt6tziWoDftayQR7n3cUzMbGlhiNqsNNylLu4"; // The constumer public value obtained from twitter.com - public static final String CONSUMER_KEY = "1Qc8Y5kmWIZy13m0hVxgGJOPt"; - + public static final String CONSUMER_KEY = "1Qc8Y5kmWIZy13m0hVxgGJOPt"; + public static final String REQUEST_TOKEN_URL = "https://twitter.com/oauth/request_token"; - public static final String AUTHORIZE_URL = "https://twitter.com/oauth/authorize"; - public static final String ACCESS_TOKEN_URL = "https://twitter.com/oauth/access_token"; + public static final String AUTHORIZE_URL = "https://twitter.com/oauth/authorize"; + public static final String ACCESS_TOKEN_URL = "https://twitter.com/oauth/access_token"; + + private final Properties properties; + private final File storeFile; + + private Configuration(File storeFile) { + this.storeFile = storeFile; + properties = new Properties(getDefaults()); + try (FileInputStream fis = new FileInputStream(storeFile)) { + properties.load(fis); + } catch (FileNotFoundException ex) { + /* ignore non-existing configuration files, will be written later */ + } catch (IOException ex) { + System.err.println("Could not load configuration: " + ex); + } + } + + public static Configuration getConfig() { + return new Configuration(new File("config.txt")); + } + + private Properties getDefaults() { + Properties defs = new Properties(); + // set default preferences as needed + return defs; + } + + /** + * Writes the configuration to file. + */ + public void save() { + try (FileOutputStream fos = new FileOutputStream(storeFile)) { + properties.store(fos, ""); + } catch (IOException ex) { + Logger.getLogger(getClass().getName()).log(Level.SEVERE, + "Failed to save prefs", ex); + } + } + + /** + * Changes a setting with the given key. + * + * @param key The key of the setting. + * @param val The new value for the setting. + */ + public void setProperty(String key, String val) { + properties.setProperty(key, val); + } + + /** + * Gets a setting with the given key. + * + * @param key Name of the configuration setting. + * @return The value for the key or null if there is none. + */ + public String getProperty(String key) { + return properties.getProperty(key); + } } |