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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
package config;
import java.io.File;
import java.io.IOException;
import org.apache.log4j.Logger;
import org.ini4j.Wini;
/**
* Represents the configuration of the client
*
* @author Jonathan Bauer
*/
public class Config {
/**
* Logger for this class
*/
private final static Logger LOGGER = Logger.getLogger(Config.class);
/**
* The main configuration object is of type Wini
* It contains the content of the config.ini as
* determined in the init() function.
*/
private static Wini ini = null;
/**
* Initializes the class by determining the path
* to the config.ini on the system and setting the
* private creating the ini member from that file.
*
* This function will make a distinction between
* Linux and Windows OS's, as the standard paths
* for configuration files obviously differ.
*
* @throws IOException
*/
public static void init() throws IOException {
// Variables only needed locally
String configPath = null;
File configFile = null;
// Determine OS
String OSName = System.getProperty("os.name").toLowerCase();
LOGGER.info("Machine's OS: " + OSName);
if (OSName.contains("windows")) {
// Windows machine. Use the environment variable 'APPDATA' which
// should point to a path similar to:
// C:\Users\<user>\AppData\Roaming
String appDataPath = System.getenv("APPDATA");
if (!appDataPath.isEmpty()) {
configPath = appDataPath + "\\bwSuite\\config.ini";
} else {
// APPDATA was empty, let's build it ourselves...
LOGGER.warn("APPDATA ist leer.");
configPath = System.getProperty("user.home") + "\\AppData\\Roaming\\bwSuite\\config.ini";
}
} else if (OSName.contains("linux")) {
configPath=System.getProperty("user.home") + "/.config/bwSuite/config.ini";
} else {
// Not Windows nor Linux, TODO MacOS Support?
configPath = null;
}
// Check if we got a path
if (!(configPath.isEmpty()||configPath == null)) {
configFile = new File(configPath);
} else {
throw new IOException("Konnte keinen Pfad für die Konfigurationsdatei ermitteln.");
}
// Check if the directory exists.
if (!configFile.getParentFile().exists()) {
LOGGER.info("Ordner " + configFile.getParentFile() + " exisitiert nicht - lege ihn jetzt an.");
// Does not, create it
if (!configFile.getParentFile().mkdirs()) {
throw new IOException("Konnte '" + configFile.getParentFile() + "' nicht erstellen.");
}
}
// Check if the file already exists
if (!configFile.exists()) {
// Does not, create it
configFile.createNewFile();
// Check if file is writeable
if(configFile.canWrite()) {
ini = new Wini(configFile);
LOGGER.info("Erzeuge '" + configFile + "'...");
// write default configuration options and values
ini.put("main", "BillOfRights", false);
ini.put("main", "vmware", false);
ini.put("main", "Benutzername speichern", false);
ini.put("main", "Benutzername", "");
ini.put("main", "Letzter Downloadpfad", "");
ini.put("main", "Letzter Uploadpfad", "");
ini.put("main", "IdP", "");
ini.store();
} else {
throw new IOException("Konnte nicht in '" + configFile + "' schreiben. Haben Sie Rechte dazu?");
}
} else {
LOGGER.info("'" + configFile + "' existiert bereits - keine weitere Aktion.");
ini = new Wini(configFile);
}
} // end constructor.
/**
* Query the value of 'BillOfRights' from the configuration file.
* @return true if the user already accepted bill of rights, false otherwise.
*/
public static boolean getBillOfRights() {
return getBoolean("main", "BillOfRights", false);
}
/**
* Query the value of 'vmware' from the configuration file.
* @return true if the user already accepted vmware license, false otherwise.
*/
public static boolean getVmwareLicense() {
return getBoolean("main", "vmware", false);
}
/**
* Query the value of 'Benutzername speichern' from the configuration file.
* @return true if the username should be saved, false otherwise.
*/
public static boolean getSaveUsername() {
return getBoolean("main", "Benutzername speichern", false);
}
/**
* Query the value of 'Benutzername' from the configuration file.
* @return username if saved, an empty string otherwise.
*/
public static String getUsername() {
return getString("main", "Benutzername", "");
}
/**
* Query the value of 'Letzter Downloadpfad' from the configuration file.
* @return last download path if saved, the path to the user's home otherwise.
*/
public static String getLastDownloadPath() {
return getString("main", "Letzter Downloadpfad", System.getProperty("user.home"));
}
/**
* Query the value of 'Letzter Uploadpfad' from the configuration file.
* @return last upload path if saved, the path to the user's home otherwise.
*/
public static String getLastUploadPath() {
return getString("main", "Letzter Uploadpfad", System.getProperty("user.home"));
}
/**
* Query the path of the configuration file
* @return path to the configuration file
*/
public static String getPath() {
if (ini.getFile().getParentFile().isDirectory())
return ini.getFile().getParentFile().toString();
else
return null;
}
/**
* Query the IdP of the configuration file
* @return stored IdP
*/
public static int getIdP() {
return Integer.parseInt(getString("main", "IdP", ""));
}
/**
* Query the authentication method of the configuration file
* @return stored IdP
*/
public static String getAuthenticationMethod() {
return getString("main", "authMethod", "");
}
/**
* Sets the value of 'BillOfRights' in the configuration file to 'value'
* @return true if it succeeded, false otherwise
*/
public static boolean setBillOfRights(boolean value) {
return setBoolean("main", "BillOfRights", value);
}
/**
* Sets the value of 'vmware' in the configuration file to 'value'
* @return true if it succeeded, false otherwise
*/
public static boolean setVmwareLicense(boolean value) {
return setBoolean("main", "vmware", value);
}
/**
* Sets the value of 'Benutzername speichern' in the configuration file to 'value'
* @return true if it succeeded, false otherwise
*/
public static boolean setSaveUsername(boolean value) {
return setBoolean("main", "Benutzername speichern", value);
}
/**
* Sets the value of 'Benutzername' in the configuration file to 'value'
* @return true if it succeeded, false otherwise
*/
public static boolean setUsername(String value) {
return setString("main", "Benutzername", value);
}
/**
* Sets the value of 'Letzter Downloadpfad' in the configuration file to 'value'
* @return true if it succeeded, false otherwise
*/
public static boolean setLastDownloadPath(String value) {
return setString("main", "Letzter Downloadpfad", value);
}
/**
* Sets the value of "Letzter Uploadpfad" in the configuration file to 'value'
* @return true if it succeeded, false otherwise
*/
public static boolean setLastUploadPath(String value) {
return setString("main", "Letzter Uploadpfad", value);
}
/**
* Sets the value of "IdP" in the configuration file to 'value'
* @return true if it succeeded, false otherwise
*/
public static boolean setIdP(String value) {
return setString("main", "IdP", value);
}
/**
* Sets the value of the selected authentication method in the configuration file to 'value'
* @return true if it succeeded, false otherwise
*/
public static boolean setAuthenticationMethod(String value) {
return setString("main","authMethod", value);
}
/**
* Save the changes to the ini file to actual file on the disk.
*
* @return true if succeeded, false otherwise
*/
public static boolean store() {
try {
ini.store();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
/**
* Gets the boolean from the given 'key' in the given 'section'.
* If nothing is found, return the given 'defaultValue'
*
* @param section section to search the key in
* @param key key to query in that section
* @param defaultValue default value to be returned, if none is found.
* @return
*/
private static boolean getBoolean(String section, String key, Boolean defaultValue) {
if (ini.containsKey(section) && ini.get(section).containsKey(key)) {
return ini.get(section, key, Boolean.class);
} else {
return defaultValue;
}
}
/**
* Gets the string from the given 'key' in the given 'section'
* If nothing is found, return the given 'defaultValue'
*
* @param section section of the configuration file to search for
* @param key key to lookup in the section
* @param defaultValue default value to return if none is found in the file
* @return value of 'key' in 'section' if it exists, 'defaultValue' otherwise
*/
private static String getString(String section, String key, String defaultValue) {
if (ini.containsKey(section) && ini.get(section).containsKey(key)) {
return ini.get(section, key);
} else {
return defaultValue;
}
}
/**
* Sets the given 'key' in the given 'section' to 'value'.
* Restricted to boolean.
*
* @param section section of the configuration file
* @param key key to set
* @param value value to assign to key
* @return true if it succeeded, false otherwise
*/
private static boolean setBoolean(String section, String key, boolean value) {
return ini.put(section, key, value) != null;
}
/**
* Sets the given 'key' in the given 'section' to 'value'.
* Restricted to string.
*
* @param section section of the configuration file
* @param key key to set
* @param value value to assign to key
* @return true if it succeeded, false otherwise
*/
private static boolean setString(String section, String key, String value) {
return ini.put(section, key, value) != null;
}
}
|