View Javadoc
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  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  	 * Opens the "{@code configuration.properties}" file and maps data.
27  	 */
28  	Configuration() {
29  		InputStream in = getClass().getResourceAsStream(CONFIG_FILE);
30  		try {
31  			// Load defaults settings
32  			config.load(in);
33  			in.close();
34  			// and override them from user settings
35  			loadFromUserProfile();
36  		} catch (IOException e) {
37  			LoggerFactory.getLogger(Configuration.class).error("", e);
38  		}
39  	}
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  		if (config.containsKey(key)) {
49  			return config.getProperty(key);
50  		}
51  		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  		config.setProperty(key, value);
62  	}
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  		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  	 * 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  		saveToFile(new File(System.getProperty("user.home"), USER_CONFIG_FILE));
87  	}
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  		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 	 * 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 		return loadFromFile(new File(System.getProperty("user.home"), USER_CONFIG_FILE));
117 	}
118 }