1 package com.leonarduk.utils;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.net.URL;
6 import java.util.Properties;
7
8 import org.apache.log4j.Logger;
9
10 /***
11 *
12 *
13 *
14 * @author Stephen Leonard
15 * @since 31 Jan 2014
16 *
17 * @version $Author:: $: Author of last commit
18 * @version $Rev:: $: Revision of last commit
19 * @version $Date:: $: Date of last commit
20 *
21 */
22 public class Config {
23
24 private static final Logger _logger = Logger.getLogger(Config.class);
25 private String configName = "config.properties";
26 private Properties properties;
27
28 public Config() {
29 loadConfig();
30 }
31
32 /***
33 * This will read the config from src/main/resources/config.properties and
34 * store it for use in later calls by
35 * {@link Config#getMandatoryPropertyValue(String)}
36 *
37 */
38 private void loadConfig() {
39 URL propertiesFileURL = getConfigLocation();
40 _logger.info("Loading properties from '" + propertiesFileURL + "'");
41 InputStream is;
42 try {
43 is = propertiesFileURL.openStream();
44
45 this.properties = new Properties();
46 this.properties.load(is);
47 is.close();
48 } catch (NullPointerException | IOException e) {
49 String errorMsg = "Failed to load properties file " + configName
50 + " into Properties object";
51 _logger.fatal(errorMsg, e);
52 throw new IllegalArgumentException(errorMsg, e);
53 }
54 }
55
56 /***
57 * Looks for value of the named parameter, using command line parameters in
58 * preference to those in the config if they exist. Fails if the parameter
59 * is not set.
60 *
61 * @param key
62 * @return
63 */
64 public String getMandatoryPropertyValue(String key) {
65 String value = getOptionalPropertyValue(key);
66 if (value != null) {
67 _logger.info("Using " + value + " for " + key);
68 return value;
69 } else {
70 String errorMsg = "Mandatory property [" + key
71 + "] is not defined in the environment";
72 _logger.fatal(errorMsg);
73 throw new IllegalArgumentException(errorMsg);
74 }
75 }
76
77 /***
78 * Parameters passed on the command line over-ride values from file
79 *
80 * @param key
81 * @return
82 */
83 public String getOptionalPropertyValue(String key) {
84 String cliOverrideStr = System.getProperty(key);
85 if (cliOverrideStr != null) {
86 _logger.debug("Using command line override for [" + key + "]");
87 return cliOverrideStr;
88 }
89 _logger.debug("Getting value of [" + key + "] from config file");
90 return properties.getProperty(key);
91 }
92
93 /***
94 * Helper method to show where the config file is stored.
95 *
96 * @return
97 */
98 public URL getConfigLocation() {
99 URL propertiesFileURL = getClass().getClassLoader().getResource(
100 this.configName);
101 return propertiesFileURL;
102 }
103
104 public Integer getPropertyIntegerValue(String key) {
105 return Integer.valueOf(this.getMandatoryPropertyValue(key));
106 }
107
108 public Double getPropertyDoubleValue(String key) {
109 return Double.valueOf(this.getMandatoryPropertyValue(key));
110 }
111
112 public long getPropertyLongValue(String key) {
113 return Long.valueOf(this.getMandatoryPropertyValue(key));
114 }
115 }