1 package com.nilhcem.fakesmtp.core;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.util.Properties;
9 import org.apache.commons.io.IOUtils;
10 import org.slf4j.LoggerFactory;
11
12
13
14
15
16
17
18 public enum Configuration {
19 INSTANCE;
20
21 private static final String CONFIG_FILE = "/configuration.properties";
22 private static final String USER_CONFIG_FILE = ".fakeSMTP.properties";
23 private final Properties config = new Properties();
24
25
26
27
28 Configuration() {
29 InputStream in = getClass().getResourceAsStream(CONFIG_FILE);
30 try {
31
32 config.load(in);
33 in.close();
34
35 loadFromUserProfile();
36 } catch (IOException e) {
37 LoggerFactory.getLogger(Configuration.class).error("", e);
38 }
39 }
40
41
42
43
44
45
46
47 public String get(String key) {
48 if (config.containsKey(key)) {
49 return config.getProperty(key);
50 }
51 return "";
52 }
53
54
55
56
57
58
59
60 public void set(String key, String value) {
61 config.setProperty(key, value);
62 }
63
64
65
66
67
68
69
70 public void saveToFile(File file) throws IOException {
71 FileOutputStream fos = new FileOutputStream(file);
72 try {
73 config.store(fos, "Last user settings");
74 } finally {
75 IOUtils.closeQuietly(fos);
76 }
77 }
78
79
80
81
82
83
84
85 public void saveToUserProfile() throws IOException {
86 saveToFile(new File(System.getProperty("user.home"), USER_CONFIG_FILE));
87 }
88
89
90
91
92
93
94
95
96 public Configuration loadFromFile(File file) throws IOException {
97 if (file.exists() && file.canRead()) {
98 FileInputStream fis = new FileInputStream(file);
99 try {
100 config.load(fis);
101 } finally {
102 IOUtils.closeQuietly(fis);
103 }
104 }
105 return INSTANCE;
106 }
107
108
109
110
111
112
113
114
115 public Configuration loadFromUserProfile() throws IOException {
116 return loadFromFile(new File(System.getProperty("user.home"), USER_CONFIG_FILE));
117 }
118 }