Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
Configuration |
|
| 1.7142857142857142;1.714 |
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 | * Contains and returns some project-specific configuration variables. | |
14 | * | |
15 | * @author Nilhcem | |
16 | * @since 1.0 | |
17 | */ | |
18 | 1 | public enum Configuration { |
19 | 1 | INSTANCE; |
20 | ||
21 | private static final String CONFIG_FILE = "/configuration.properties"; | |
22 | private static final String USER_CONFIG_FILE = ".fakeSMTP.properties"; | |
23 | 1 | private final Properties config = new Properties(); |
24 | ||
25 | /** | |
26 | * Opens the "{@code configuration.properties}" file and maps data. | |
27 | */ | |
28 | 1 | Configuration() { |
29 | 1 | InputStream in = getClass().getResourceAsStream(CONFIG_FILE); |
30 | try { | |
31 | // Load defaults settings | |
32 | 1 | config.load(in); |
33 | 1 | in.close(); |
34 | // and override them from user settings | |
35 | 1 | loadFromUserProfile(); |
36 | 0 | } catch (IOException e) { |
37 | 0 | LoggerFactory.getLogger(Configuration.class).error("", e); |
38 | 1 | } |
39 | 1 | } |
40 | ||
41 | /** | |
42 | * Returns the value of a specific entry from the "{@code configuration.properties}" file. | |
43 | * | |
44 | * @param key a string representing the key from a key/value couple. | |
45 | * @return the value of the key, or an empty string if the key was not found. | |
46 | */ | |
47 | public String get(String key) { | |
48 | 3 | if (config.containsKey(key)) { |
49 | 2 | return config.getProperty(key); |
50 | } | |
51 | 1 | return ""; |
52 | } | |
53 | ||
54 | /** | |
55 | * Sets the value of a specific entry. | |
56 | * | |
57 | * @param key a string representing the key from a key/value couple. | |
58 | * @param value the value of the key. | |
59 | */ | |
60 | public void set(String key, String value) { | |
61 | 0 | config.setProperty(key, value); |
62 | 0 | } |
63 | ||
64 | /** | |
65 | * Saves configuration to file. | |
66 | * | |
67 | * @param file file to save configuration. | |
68 | * @throws IOException | |
69 | */ | |
70 | public void saveToFile(File file) throws IOException { | |
71 | 0 | FileOutputStream fos = new FileOutputStream(file); |
72 | try { | |
73 | 0 | config.store(fos, "Last user settings"); |
74 | } finally { | |
75 | 0 | IOUtils.closeQuietly(fos); |
76 | 0 | } |
77 | 0 | } |
78 | ||
79 | /** | |
80 | * Saves configuration to the {@code .fakesmtp.properties} file in user profile directory. | |
81 | * Calls {@link Configuration#saveToFile(java.io.File)}. | |
82 | * | |
83 | * @throws IOException | |
84 | */ | |
85 | public void saveToUserProfile() throws IOException { | |
86 | 0 | saveToFile(new File(System.getProperty("user.home"), USER_CONFIG_FILE)); |
87 | 0 | } |
88 | ||
89 | /** | |
90 | * Loads configuration from file. | |
91 | * | |
92 | * @param file file to load configuration. | |
93 | * @return INSTANCE. | |
94 | * @throws IOException | |
95 | */ | |
96 | public Configuration loadFromFile(File file) throws IOException { | |
97 | 1 | if (file.exists() && file.canRead()) { |
98 | 1 | FileInputStream fis = new FileInputStream(file); |
99 | try { | |
100 | 1 | config.load(fis); |
101 | } finally { | |
102 | 1 | IOUtils.closeQuietly(fis); |
103 | 1 | } |
104 | } | |
105 | 1 | return INSTANCE; |
106 | } | |
107 | ||
108 | /** | |
109 | * Loads configuration from the .fakesmtp.properties file in user profile directory. | |
110 | * Calls {@link Configuration#loadFromFile(java.io.File)}. | |
111 | * | |
112 | * @return INSTANCE. | |
113 | * @throws IOException | |
114 | */ | |
115 | public Configuration loadFromUserProfile() throws IOException { | |
116 | 1 | return loadFromFile(new File(System.getProperty("user.home"), USER_CONFIG_FILE)); |
117 | } | |
118 | } |