rsanchez
2014-11-18 fdbc8ca146b8e3aff0425e2faf94c0b4a6e3dd28
securis/src/main/java/net/curisit/securis/utils/Config.java
....@@ -17,182 +17,193 @@
1717 */
1818 public class Config {
1919
20
- private static final Logger LOG = LogManager.getLogger(Config.class);
20
+ private static final Logger LOG = LogManager.getLogger(Config.class);
2121
22
- /**
23
- * Key used to store config file resource location. In a web application, can be set as initial parameter in a servlet loaded on startup
24
- */
25
- public static final String KEY_CONFIG_FILE = "/securis-server.properties";
22
+ /**
23
+ * Key used to store config file resource location. In a web application,
24
+ * can be set as initial parameter in a servlet loaded on startup
25
+ */
26
+ public static final String KEY_CONFIG_FILE = "/securis-server.properties";
2627
27
- private static Properties params = null;
28
+ private static Properties params = null;
2829
29
- static {
30
- try {
31
- loadParameters(KEY_CONFIG_FILE);
32
- } catch (IOException e) {
33
- LOG.error("Config file {} was not found in classpath", KEY_CONFIG_FILE);
34
- System.exit(-2);
35
- }
36
- }
30
+ static {
31
+ try {
32
+ loadParameters(KEY_CONFIG_FILE);
33
+ } catch (IOException e) {
34
+ LOG.error("Config file {} was not found in classpath", KEY_CONFIG_FILE);
35
+ System.exit(-2);
36
+ }
37
+ }
3738
38
- /**
39
- * Loads application global parameters from a classpath resource
40
- *
41
- * @param resource
42
- * : Resource location in classpath, i.e: "/resource/cp-securis.conf"
43
- * @throws IOException
44
- */
45
- public static void loadParameters(String resource) throws IOException {
39
+ /**
40
+ * Loads application global parameters from a classpath resource
41
+ *
42
+ * @param resource
43
+ * : Resource location in classpath, i.e:
44
+ * "/resource/cp-securis.conf"
45
+ * @throws IOException
46
+ */
47
+ public static void loadParameters(String resource) throws IOException {
4648
47
- LOG.debug("Loading params from " + resource);
48
- InputStream fileis = Params.class.getResourceAsStream(resource);
49
+ LOG.debug("Loading params from " + resource);
50
+ InputStream fileis = Params.class.getResourceAsStream(resource);
4951
50
- params = new Properties();
51
- try {
52
+ params = new Properties();
53
+ try {
5254
53
- params.load(fileis);
54
- LOG.debug("Params loaded OK from {}", resource);
55
- } catch (IOException e) {
56
- LOG.error("Error loading config file: " + e);
57
- params = null;
58
- throw e;
59
- }
55
+ params.load(fileis);
56
+ LOG.debug("Params loaded OK from {}", resource);
57
+ } catch (IOException e) {
58
+ LOG.error("Error loading config file: " + e);
59
+ params = null;
60
+ throw e;
61
+ }
6062
61
- }
63
+ }
6264
63
- public static String getByDomain(String domain, String paramname) {
64
- return getByDomain(domain, paramname, null);
65
- }
65
+ public static String getByDomain(String domain, String paramname) {
66
+ return getByDomain(domain, paramname, null);
67
+ }
6668
67
- public static String getByPrefix(String prefix, String paramname) {
68
- return get(prefix + "." + paramname, get(paramname));
69
- }
69
+ public static String getByPrefix(String prefix, String paramname) {
70
+ return get(prefix + "." + paramname, get(paramname));
71
+ }
7072
71
- public static String getByPrefix(String prefix, String paramname, String defaultVal) {
72
- return get(prefix + "." + paramname, get(paramname, defaultVal));
73
- }
73
+ public static String getByPrefix(String prefix, String paramname, String defaultVal) {
74
+ return get(prefix + "." + paramname, get(paramname, defaultVal));
75
+ }
7476
75
- public static String getByDomain(String domain, String paramname, String defaultval) {
76
- return get(paramname + "." + domain, defaultval);
77
- }
77
+ public static String getByDomain(String domain, String paramname, String defaultval) {
78
+ return get(paramname + "." + domain, defaultval);
79
+ }
7880
79
- public static int getIntByDomain(String domain, String paramname) {
80
- return getInt(paramname + "." + domain, getInt(paramname));
81
- }
81
+ public static int getIntByDomain(String domain, String paramname) {
82
+ return getInt(paramname + "." + domain, getInt(paramname));
83
+ }
8284
83
- public static int getIntByDomain(String domain, String paramname, int defaultval) {
84
- return getInt(paramname + "." + domain, defaultval);
85
- }
85
+ public static int getIntByDomain(String domain, String paramname, int defaultval) {
86
+ return getInt(paramname + "." + domain, defaultval);
87
+ }
8688
87
- /**
88
- * Gets a List with all values of properties that begins with <code>prefix</code> It reads sequentially. For example:
89
- *
90
- * <pre>
91
- * securis.sort.comparator.0: net.cp.securis.comparators.ComparePttidVsPtn
92
- * securis.sort.comparator.1: net.cp.securis.comparators.CompareFrequency
93
- * securis.sort.comparator.2: net.cp.securis.comparators.CompareOutgoingVsIncomming
94
- * securis.sort.comparator.3: net.cp.securis.comparators.CompareDuration
95
- * securis.sort.comparator.4: net.cp.securis.comparators.CompareCallVsSms
96
- * </pre>
97
- *
98
- * That config (for prefix: "securis.sort.comparator" ) will return a List<String> with values:
99
- *
100
- * <pre>
101
- * "net.cp.securis.comparators.ComparePttidVsPtn",
102
- * "net.cp.securis.comparators.CompareFrequency",
103
- * "net.cp.securis.comparators.CompareOutgoingVsIncomming",
104
- * "net.cp.securis.comparators.CompareDuration",
105
- * "net.cp.securis.comparators.CompareCallVsSms"
106
- * </pre>
107
- *
108
- * Note: If there is a gap between suffixes process will stop, that is, only will be returned properties found before gap.
109
- *
110
- * @param prefix
111
- * @return
112
- */
113
- public static List<String> getListByPrefix(String prefix) {
114
- List<String> list = new ArrayList<String>();
89
+ /**
90
+ * Gets a List with all values of properties that begins with
91
+ * <code>prefix</code> It reads sequentially. For example:
92
+ *
93
+ * <pre>
94
+ * securis.sort.comparator.0: net.cp.securis.comparators.ComparePttidVsPtn
95
+ * securis.sort.comparator.1: net.cp.securis.comparators.CompareFrequency
96
+ * securis.sort.comparator.2: net.cp.securis.comparators.CompareOutgoingVsIncomming
97
+ * securis.sort.comparator.3: net.cp.securis.comparators.CompareDuration
98
+ * securis.sort.comparator.4: net.cp.securis.comparators.CompareCallVsSms
99
+ * </pre>
100
+ *
101
+ * That config (for prefix: "securis.sort.comparator" ) will return a
102
+ * List<String> with values:
103
+ *
104
+ * <pre>
105
+ * "net.cp.securis.comparators.ComparePttidVsPtn",
106
+ * "net.cp.securis.comparators.CompareFrequency",
107
+ * "net.cp.securis.comparators.CompareOutgoingVsIncomming",
108
+ * "net.cp.securis.comparators.CompareDuration",
109
+ * "net.cp.securis.comparators.CompareCallVsSms"
110
+ * </pre>
111
+ *
112
+ * Note: If there is a gap between suffixes process will stop, that is, only
113
+ * will be returned properties found before gap.
114
+ *
115
+ * @param prefix
116
+ * @return
117
+ */
118
+ public static List<String> getListByPrefix(String prefix) {
119
+ List<String> list = new ArrayList<String>();
115120
116
- String tpl = prefix + ".{0}";
121
+ String tpl = prefix + ".{0}";
117122
118
- int i = 0;
119
- String value = get(MessageFormat.format(tpl, i++));
120
- while (value != null) {
121
- list.add(value);
122
- value = get(MessageFormat.format(tpl, i++));
123
- }
123
+ int i = 0;
124
+ String value = get(MessageFormat.format(tpl, i++));
125
+ while (value != null) {
126
+ list.add(value);
127
+ value = get(MessageFormat.format(tpl, i++));
128
+ }
124129
125
- return list;
126
- }
130
+ return list;
131
+ }
127132
128
- /**
129
- * Gets param value in config file or environment variables
130
- *
131
- * @param paramname
132
- * Global parameter's name
133
- * @return Value of paramname or null if paramname is not found neither in config file nor in environment variables
134
- */
135
- public static String get(String paramname) {
133
+ /**
134
+ * Gets param value in config file or environment variables
135
+ *
136
+ * @param paramname
137
+ * Global parameter's name
138
+ * @return Value of paramname or null if paramname is not found neither in
139
+ * config file nor in environment variables
140
+ */
141
+ public static String get(String paramname) {
136142
137
- assert (params != null) : "Parameters have not been loaded. Call method loadParameters(resource) before use Params.";
143
+ assert (params != null) : "Parameters have not been loaded. Call method loadParameters(resource) before use Params.";
138144
139
- String value = params.getProperty(paramname);
140
- if (value == null)
141
- value = System.getenv(paramname);
142
- return value;
143
- }
145
+ String value = params.getProperty(paramname);
146
+ if (value == null)
147
+ value = System.getenv(paramname);
148
+ return value;
149
+ }
144150
145
- /**
146
- * Gets param value from config file or environment variables
147
- *
148
- * @param paramname
149
- * Global parameter's name
150
- * @param defaultval
151
- * @return Value of paramname or defaultval if paramname is not found
152
- */
153
- public static String get(String paramname, String defaultval) {
154
- String value = get(paramname);
155
- return (value == null ? defaultval : value);
156
- }
151
+ /**
152
+ * Gets param value from config file or environment variables
153
+ *
154
+ * @param paramname
155
+ * Global parameter's name
156
+ * @param defaultval
157
+ * @return Value of paramname or defaultval if paramname is not found
158
+ */
159
+ public static String get(String paramname, String defaultval) {
160
+ String value = get(paramname);
161
+ return (value == null ? defaultval : value);
162
+ }
157163
158
- /**
159
- * Gets param value in config file or environment variables
160
- *
161
- * @param paramname
162
- * Global parameter's name
163
- * @return Integer value of paramname or -1 if paramname is not found neither in config file nor in environment variables
164
- */
165
- public static int getInt(String paramname) {
166
- String value = get(paramname);
167
- return (value == null ? -1 : Integer.parseInt(value));
168
- }
164
+ /**
165
+ * Gets param value in config file or environment variables
166
+ *
167
+ * @param paramname
168
+ * Global parameter's name
169
+ * @return Integer value of paramname or -1 if paramname is not found
170
+ * neither in config file nor in environment variables
171
+ */
172
+ public static int getInt(String paramname) {
173
+ String value = get(paramname);
174
+ return (value == null ? -1 : Integer.parseInt(value));
175
+ }
169176
170
- /**
171
- * Gets param value from config file or environment variables
172
- *
173
- * @param paramname
174
- * Global parameter's name
175
- * @param defaultval
176
- * @return Integer value of paramname or defaultval if paramname is not found
177
- */
178
- public static int getInt(String paramname, int defaultval) {
179
- String value = get(paramname);
180
- return (value == null ? defaultval : Integer.parseInt(value));
181
- }
177
+ /**
178
+ * Gets param value from config file or environment variables
179
+ *
180
+ * @param paramname
181
+ * Global parameter's name
182
+ * @param defaultval
183
+ * @return Integer value of paramname or defaultval if paramname is not
184
+ * found
185
+ */
186
+ public static int getInt(String paramname, int defaultval) {
187
+ String value = get(paramname);
188
+ return (value == null ? defaultval : Integer.parseInt(value));
189
+ }
182190
183
- public static class KEYS {
191
+ public static class KEYS {
184192
193
+ public static final String SERVER_HOSTNAME = "license.server.hostname";
185194
186
-
187
- public static final String SERVER_HOSTNAME = "license.server.hostname";
195
+ public static final String SERVER_PORT = "license.server.port";
196
+ public static final String SERVER_SSL_PORT = "license.server.ssl.port";
188197
189
- public static final String SERVER_PORT = "license.server.port";
190
- public static final String SERVER_SSL_PORT = "license.server.ssl.port";
191
-
192
- public static final String KEYSTORE_PATH = "ssl.keystore.path";
198
+ public static final String KEYSTORE_PATH = "ssl.keystore.path";
193199 public static final String KEYSTORE_TYPE = "ssl.keystore.type";
194200 public static final String KEYSTORE_PASSWORD = "ssl.keystore.password";
195201 public static final String KEYSTORE_ALIAS = "ssl.keystore.alias";
196
- }
202
+
203
+ public static final String MAILGUN_DOMAIN = "mailgun.domain";
204
+ public static final String MAILGUN_API_KEY = "mailgun.api.key";
205
+ public static final String EMAIL_FROM_ADDRESS = "email.from.address";
206
+ public static final String EMAIL_LIC_DEFAULT_SUBJECT = "email.lic.default.subject";
207
+ }
197208
198209 }