upgraded svjatoslav-commons
[instantlauncher.git] / src / main / java / eu / svjatoslav / instantlauncher / Configuration.java
1 package eu.svjatoslav.instantlauncher;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7 import java.util.Properties;
8
9 public class Configuration {
10
11     private static final String KEY_ROOT_DIRECTORY = "KEY_ROOT_DIRECTORY";
12     private static final String CONFIG_FILE_NAME = ".instantlauncher";
13     private final Properties properties = new Properties();
14
15     private boolean propertiesChanged = false;
16
17     public Configuration() throws IOException {
18         initialize();
19     }
20
21     private File getPropertiesFile() {
22         return new File(System.getProperty("user.home") + "/" + CONFIG_FILE_NAME);
23     }
24
25     public File getRootDirectory() {
26
27         if (properties.containsKey(KEY_ROOT_DIRECTORY)) {
28             return new File(properties.getProperty(KEY_ROOT_DIRECTORY));
29         } else {
30             properties.put(KEY_ROOT_DIRECTORY, System.getProperty("user.home") + "/");
31             propertiesChanged = true;
32             return getRootDirectory();
33         }
34
35     }
36
37     private void initialize() throws IOException {
38
39         loadIfFileExists();
40
41         validatePropertiesFile();
42
43         if (propertiesChanged) {
44             saveFile();
45         }
46     }
47
48     private void loadIfFileExists() throws IOException {
49         final File propertiesFile = getPropertiesFile();
50
51         if (propertiesFile.exists()) {
52             final FileInputStream inStream = new FileInputStream(propertiesFile);
53             properties.load(inStream);
54             inStream.close();
55         }
56     }
57
58     private void saveFile() throws IOException {
59         properties.store(new FileOutputStream(getPropertiesFile()), "Instantlauncher configuration file.");
60     }
61
62     private void validatePropertiesFile() {
63         getRootDirectory();
64     }
65
66 }