1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
package utils;
import io.DataWriter;
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";
// The constumer public value obtained from twitter.com
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 DEFAULT_TWEETS_FILENAME = "tweets.txt";
public static final String DEFAULT_PROFILE_FILENAME = "profiles.txt";
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
String profilesFilename = defs.getProperty(DataWriter.CFG_PROFILE_FILENAME, "profiles.txt");
String tweetsFilename = defs.getProperty(DataWriter.CFG_TWEETS_FILENAME, "tweets.txt");
defs.setProperty(DataWriter.CFG_PROFILE_FILENAME, profilesFilename);
defs.setProperty(DataWriter.CFG_TWEETS_FILENAME, tweetsFilename);
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 getProperty(key, null);
}
/**
* Gets a setting with the given key.
*
* @param key Name of the configuration setting.
* @param defaultVal The default value for the key.
* @return The value for the key or null if there is none.
*/
public String getProperty(String key, String defaultVal) {
return properties.getProperty(key, defaultVal);
}
}
|