rsanchez
2014-10-22 09bb2b3b9300e332f8b743481fbb412df8cd2a89
src/main/java/net/curisit/securis/utils/Params.java
....@@ -17,182 +17,197 @@
1717 */
1818 public class Params {
1919
20
- private static final Logger LOG = LogManager.getLogger(Params.class);
20
+ private static final Logger LOG = LogManager.getLogger(Params.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
- */
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
+ */
2526 public static final String DEFAUT_SERVER_URL = "https://securis.curistec.com/api";
26
- public static final String KEY_CONFIG_FILE = "/securis-client.properties";
27
+ public static final String KEY_CONFIG_FILE = "/securis-server.properties";
2728
28
- private static Properties params = null;
29
+ private static Properties params = null;
2930
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
- }
31
+ static {
32
+ try {
33
+ loadParameters(KEY_CONFIG_FILE);
34
+ } catch (IOException e) {
35
+ LOG.error("Config file {} was not found in classpath", KEY_CONFIG_FILE);
36
+ System.exit(-2);
37
+ }
38
+ }
3839
39
- /**
40
- * Loads application global parameters from a classpath resource
41
- *
42
- * @param resource
43
- * : Resource location in classpath, i.e: "/resource/cp-securis.conf"
44
- * @throws IOException
45
- */
46
- public static void loadParameters(String resource) throws IOException {
40
+ /**
41
+ * Loads application global parameters from a classpath resource
42
+ *
43
+ * @param resource
44
+ * : Resource location in classpath, i.e:
45
+ * "/resource/cp-securis.conf"
46
+ * @throws IOException
47
+ */
48
+ public static void loadParameters(String resource) throws IOException {
4749
48
- LOG.debug("Loading params from " + resource);
49
- InputStream fileis = Params.class.getResourceAsStream(resource);
50
+ LOG.debug("Loading params from " + resource);
51
+ InputStream fileis = Params.class.getResourceAsStream(resource);
5052
51
- params = new Properties();
52
- try {
53
+ params = new Properties();
54
+ try {
5355
54
- params.load(fileis);
55
- LOG.debug("Params loaded OK from {}", resource);
56
- } catch (IOException e) {
57
- LOG.error("Error loading config file: " + e);
58
- params = null;
59
- throw e;
60
- }
56
+ params.load(fileis);
57
+ LOG.debug("Params loaded OK from {}", resource);
58
+ } catch (IOException e) {
59
+ LOG.error("Error loading config file: " + e);
60
+ params = null;
61
+ throw e;
62
+ }
6163
62
- }
64
+ }
6365
64
- public static String getByDomain(String domain, String paramname) {
65
- return getByDomain(domain, paramname, null);
66
- }
66
+ public static String getByDomain(String domain, String paramname) {
67
+ return getByDomain(domain, paramname, null);
68
+ }
6769
68
- public static String getByPrefix(String prefix, String paramname) {
69
- return get(prefix + "." + paramname, get(paramname));
70
- }
70
+ public static String getByPrefix(String prefix, String paramname) {
71
+ return get(prefix + "." + paramname, get(paramname));
72
+ }
7173
72
- public static String getByPrefix(String prefix, String paramname, String defaultVal) {
73
- return get(prefix + "." + paramname, get(paramname, defaultVal));
74
- }
74
+ public static String getByPrefix(String prefix, String paramname, String defaultVal) {
75
+ return get(prefix + "." + paramname, get(paramname, defaultVal));
76
+ }
7577
76
- public static String getByDomain(String domain, String paramname, String defaultval) {
77
- return get(paramname + "." + domain, defaultval);
78
- }
78
+ public static String getByDomain(String domain, String paramname, String defaultval) {
79
+ return get(paramname + "." + domain, defaultval);
80
+ }
7981
80
- public static int getIntByDomain(String domain, String paramname) {
81
- return getInt(paramname + "." + domain, getInt(paramname));
82
- }
82
+ public static int getIntByDomain(String domain, String paramname) {
83
+ return getInt(paramname + "." + domain, getInt(paramname));
84
+ }
8385
84
- public static int getIntByDomain(String domain, String paramname, int defaultval) {
85
- return getInt(paramname + "." + domain, defaultval);
86
- }
86
+ public static int getIntByDomain(String domain, String paramname, int defaultval) {
87
+ return getInt(paramname + "." + domain, defaultval);
88
+ }
8789
88
- /**
89
- * Gets a List with all values of properties that begins with <code>prefix</code> It reads sequentially. For example:
90
- *
91
- * <pre>
92
- * securis.sort.comparator.0: net.cp.securis.comparators.ComparePttidVsPtn
93
- * securis.sort.comparator.1: net.cp.securis.comparators.CompareFrequency
94
- * securis.sort.comparator.2: net.cp.securis.comparators.CompareOutgoingVsIncomming
95
- * securis.sort.comparator.3: net.cp.securis.comparators.CompareDuration
96
- * securis.sort.comparator.4: net.cp.securis.comparators.CompareCallVsSms
97
- * </pre>
98
- *
99
- * That config (for prefix: "securis.sort.comparator" ) will return a List<String> with values:
100
- *
101
- * <pre>
102
- * "net.cp.securis.comparators.ComparePttidVsPtn",
103
- * "net.cp.securis.comparators.CompareFrequency",
104
- * "net.cp.securis.comparators.CompareOutgoingVsIncomming",
105
- * "net.cp.securis.comparators.CompareDuration",
106
- * "net.cp.securis.comparators.CompareCallVsSms"
107
- * </pre>
108
- *
109
- * Note: If there is a gap between suffixes process will stop, that is, only will be returned properties found before gap.
110
- *
111
- * @param prefix
112
- * @return
113
- */
114
- public static List<String> getListByPrefix(String prefix) {
115
- List<String> list = new ArrayList<String>();
90
+ /**
91
+ * Gets a List with all values of properties that begins with
92
+ * <code>prefix</code> It reads sequentially. For example:
93
+ *
94
+ * <pre>
95
+ * securis.sort.comparator.0: net.cp.securis.comparators.ComparePttidVsPtn
96
+ * securis.sort.comparator.1: net.cp.securis.comparators.CompareFrequency
97
+ * securis.sort.comparator.2: net.cp.securis.comparators.CompareOutgoingVsIncomming
98
+ * securis.sort.comparator.3: net.cp.securis.comparators.CompareDuration
99
+ * securis.sort.comparator.4: net.cp.securis.comparators.CompareCallVsSms
100
+ * </pre>
101
+ *
102
+ * That config (for prefix: "securis.sort.comparator" ) will return a
103
+ * List<String> with values:
104
+ *
105
+ * <pre>
106
+ * "net.cp.securis.comparators.ComparePttidVsPtn",
107
+ * "net.cp.securis.comparators.CompareFrequency",
108
+ * "net.cp.securis.comparators.CompareOutgoingVsIncomming",
109
+ * "net.cp.securis.comparators.CompareDuration",
110
+ * "net.cp.securis.comparators.CompareCallVsSms"
111
+ * </pre>
112
+ *
113
+ * Note: If there is a gap between suffixes process will stop, that is, only
114
+ * will be returned properties found before gap.
115
+ *
116
+ * @param prefix
117
+ * @return
118
+ */
119
+ public static List<String> getListByPrefix(String prefix) {
120
+ List<String> list = new ArrayList<String>();
116121
117
- String tpl = prefix + ".{0}";
122
+ String tpl = prefix + ".{0}";
118123
119
- int i = 0;
120
- String value = get(MessageFormat.format(tpl, i++));
121
- while (value != null) {
122
- list.add(value);
123
- value = get(MessageFormat.format(tpl, i++));
124
- }
124
+ int i = 0;
125
+ String value = get(MessageFormat.format(tpl, i++));
126
+ while (value != null) {
127
+ list.add(value);
128
+ value = get(MessageFormat.format(tpl, i++));
129
+ }
125130
126
- return list;
127
- }
131
+ return list;
132
+ }
128133
129
- /**
130
- * Gets param value in config file or environment variables
131
- *
132
- * @param paramname
133
- * Global parameter's name
134
- * @return Value of paramname or null if paramname is not found neither in config file nor in environment variables
135
- */
136
- public static String get(String paramname) {
134
+ /**
135
+ * Gets param value in config file or environment variables
136
+ *
137
+ * @param paramname
138
+ * Global parameter's name
139
+ * @return Value of paramname or null if paramname is not found neither in
140
+ * config file nor in environment variables
141
+ */
142
+ public static String get(String paramname) {
137143
138
- assert (params != null) : "Parameters have not been loaded. Call method loadParameters(resource) before use Params.";
144
+ assert (params != null) : "Parameters have not been loaded. Call method loadParameters(resource) before use Params.";
139145
140
- String value = params.getProperty(paramname);
141
- if (value == null)
142
- value = System.getenv(paramname);
143
- return value;
144
- }
146
+ String value = params.getProperty(paramname);
147
+ if (value == null)
148
+ value = System.getenv(paramname);
149
+ return value;
150
+ }
145151
146
- /**
147
- * Gets param value from config file or environment variables
148
- *
149
- * @param paramname
150
- * Global parameter's name
151
- * @param defaultval
152
- * @return Value of paramname or defaultval if paramname is not found
153
- */
154
- public static String get(String paramname, String defaultval) {
155
- String value = get(paramname);
156
- return (value == null ? defaultval : value);
157
- }
152
+ /**
153
+ * Gets param value from config file or environment variables
154
+ *
155
+ * @param paramname
156
+ * Global parameter's name
157
+ * @param defaultval
158
+ * @return Value of paramname or defaultval if paramname is not found
159
+ */
160
+ public static String get(String paramname, String defaultval) {
161
+ String value = get(paramname);
162
+ return (value == null ? defaultval : value);
163
+ }
158164
159
- /**
160
- * Gets param value in config file or environment variables
161
- *
162
- * @param paramname
163
- * Global parameter's name
164
- * @return Integer value of paramname or -1 if paramname is not found neither in config file nor in environment variables
165
- */
166
- public static int getInt(String paramname) {
167
- String value = get(paramname);
168
- return (value == null ? -1 : Integer.parseInt(value));
169
- }
165
+ /**
166
+ * Gets param value in config file or environment variables
167
+ *
168
+ * @param paramname
169
+ * Global parameter's name
170
+ * @return Integer value of paramname or -1 if paramname is not found
171
+ * neither in config file nor in environment variables
172
+ */
173
+ public static int getInt(String paramname) {
174
+ String value = get(paramname);
175
+ return (value == null ? -1 : Integer.parseInt(value));
176
+ }
170177
171
- /**
172
- * Gets param value from config file or environment variables
173
- *
174
- * @param paramname
175
- * Global parameter's name
176
- * @param defaultval
177
- * @return Integer value of paramname or defaultval if paramname is not found
178
- */
179
- public static int getInt(String paramname, int defaultval) {
180
- String value = get(paramname);
181
- return (value == null ? defaultval : Integer.parseInt(value));
182
- }
178
+ /**
179
+ * Gets param value from config file or environment variables
180
+ *
181
+ * @param paramname
182
+ * Global parameter's name
183
+ * @param defaultval
184
+ * @return Integer value of paramname or defaultval if paramname is not
185
+ * found
186
+ */
187
+ public static int getInt(String paramname, int defaultval) {
188
+ String value = get(paramname);
189
+ return (value == null ? defaultval : Integer.parseInt(value));
190
+ }
183191
184
- public static class KEYS {
192
+ public static class KEYS {
185193
186
- /**
187
- * Public key file, Usually in "PEM" format
188
- */
189
- public static final String PUBLIC_KEY_FILE = "public.key.file";
194
+ /**
195
+ * Public key file, Usually in "PEM" format
196
+ */
197
+ public static final String PUBLIC_KEY_FILE = "public.key.file";
190198
191
- public static final String APPLICATION_CODE = "app.code";
199
+ public static final String APPLICATION_CODE = "app.code";
192200
193
- public static final String CUSTOMER_CODE = "customer.code";
201
+ public static final String CUSTOMER_CODE = "customer.code";
194202
195
- public static final String LICENSE_SERVER_URL = "license.server.url";
196
- }
203
+ public static final String PACK_CODE = "pack.code";
204
+
205
+ public static final String LICENSE_SERVER_URL = "license.server.url";
206
+
207
+ public static final String MAILGUN_DOMAIN = "mailgun.domain";
208
+ public static final String MAILGUN_API_KEY = "mailgun.api.key";
209
+ public static final String EMAIL_FROM_ADDRESS = "email.from.address";
210
+ public static final String EMAIL_LIC_DEFAULT_SUBJECT = "email.lic.default.subject";
211
+ }
197212
198213 }