Joaquín Reñé
2025-10-07 146a0fb8b0e90f9196e569152f649baf60d6cc8f
securis/src/main/java/net/curisit/securis/utils/Config.java
....@@ -1,3 +1,6 @@
1
+/*
2
+ * Copyright @ 2013 CurisTEC, S.A.S. All Rights Reserved.
3
+ */
14 package net.curisit.securis.utils;
25
36 import java.io.IOException;
....@@ -11,17 +14,31 @@
1114 import org.apache.logging.log4j.Logger;
1215
1316 /**
14
- * Class that loads and serves global config parameters.
17
+ * Config
18
+ * <p>
19
+ * Class that loads and serves global config parameters from a classpath properties file
20
+ * and, as a fallback, from environment variables.
21
+ *
22
+ * Initialization:
23
+ * - Static initializer loads {@link #KEY_CONFIG_FILE} from classpath (fails hard if missing).
24
+ *
25
+ * Accessors:
26
+ * - {@link #get(String)} / {@link #get(String, String)}
27
+ * - Integer variants: {@link #getInt(String)} / {@link #getInt(String, int)}
28
+ * - Namespaced helpers: by prefix/domain, and sequential lists via {@link #getListByPrefix(String)}.
29
+ *
30
+ * Thread-safety: static-only utility; internal state is read-only after init.
1531 *
16
- * @author rsanchez
32
+ * @author JRA
33
+ * Last reviewed by JRA on Oct 5, 2025.
1734 */
1835 public class Config {
1936
2037 private static final Logger LOG = LogManager.getLogger(Config.class);
2138
2239 /**
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
40
+ * Resource path of the application properties file (in classpath).
41
+ * E.g. "/securis-server.properties".
2542 */
2643 public static final String KEY_CONFIG_FILE = "/securis-server.properties";
2744
....@@ -37,12 +54,12 @@
3754 }
3855
3956 /**
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
57
+ * loadParameters
58
+ * <p>
59
+ * Loads application global parameters from a classpath resource.
60
+ *
61
+ * @param resource Classpath location (e.g. "/resource/cp-securis.conf").
62
+ * @throws IOException If the resource cannot be found or read.
4663 */
4764 public static void loadParameters(String resource) throws IOException {
4865
....@@ -51,7 +68,6 @@
5168
5269 params = new Properties();
5370 try {
54
-
5571 params.load(fileis);
5672 LOG.debug("Params loaded OK from {}", resource);
5773 } catch (IOException e) {
....@@ -59,84 +75,112 @@
5975 params = null;
6076 throw e;
6177 }
62
-
6378 }
6479
80
+ /**
81
+ * getByDomain
82
+ * <p>
83
+ * Convenience accessor for domain-suffixed parameters (param.domain).
84
+ *
85
+ * @param domain Domain suffix.
86
+ * @param paramname Base parameter name.
87
+ * @return String value or null if not present.
88
+ */
6589 public static String getByDomain(String domain, String paramname) {
6690 return getByDomain(domain, paramname, null);
6791 }
6892
93
+ /**
94
+ * getByPrefix
95
+ * <p>
96
+ * Returns parameter value from "{prefix}.{param}" or falls back to the plain "{param}".
97
+ *
98
+ * @param prefix Namespace prefix.
99
+ * @param paramname Parameter name.
100
+ * @return Resolved value or null.
101
+ */
69102 public static String getByPrefix(String prefix, String paramname) {
70103 return get(prefix + "." + paramname, get(paramname));
71104 }
72105
106
+ /**
107
+ * getByPrefix
108
+ * <p>
109
+ * Returns parameter value from "{prefix}.{param}" or provided default (which itself
110
+ * falls back to "{param}" or its default).
111
+ *
112
+ * @param prefix Namespace prefix.
113
+ * @param paramname Parameter name.
114
+ * @param defaultVal Default value if none resolved.
115
+ * @return Resolved value.
116
+ */
73117 public static String getByPrefix(String prefix, String paramname, String defaultVal) {
74118 return get(prefix + "." + paramname, get(paramname, defaultVal));
75119 }
76120
121
+ /**
122
+ * getByDomain
123
+ * <p>
124
+ * Returns value from "{param}.{domain}" or provided default.
125
+ *
126
+ * @param domain domain suffix.
127
+ * @param paramname base name.
128
+ * @param defaultval fallback if not found.
129
+ * @return resolved string.
130
+ */
77131 public static String getByDomain(String domain, String paramname, String defaultval) {
78132 return get(paramname + "." + domain, defaultval);
79133 }
80134
135
+ /**
136
+ * getIntByDomain
137
+ * <p>
138
+ * Integer variant of {@link #getByDomain(String, String)} with fallback to plain param.
139
+ */
81140 public static int getIntByDomain(String domain, String paramname) {
82141 return getInt(paramname + "." + domain, getInt(paramname));
83142 }
84143
144
+ /**
145
+ * getIntByDomain
146
+ * <p>
147
+ * Integer variant returning provided default when missing.
148
+ */
85149 public static int getIntByDomain(String domain, String paramname, int defaultval) {
86150 return getInt(paramname + "." + domain, defaultval);
87151 }
88152
89153 /**
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
154
+ * getListByPrefix
155
+ * <p>
156
+ * Reads sequential properties using numeric suffixes starting from 0 and stops on first gap.
157
+ * Example:
158
+ * securis.sort.comparator.0=...
159
+ * securis.sort.comparator.1=...
160
+ * ...
161
+ *
162
+ * @param prefix Base prefix (e.g. "securis.sort.comparator").
163
+ * @return Ordered list of values until first missing index.
117164 */
118165 public static List<String> getListByPrefix(String prefix) {
119166 List<String> list = new ArrayList<String>();
120
-
121167 String tpl = prefix + ".{0}";
122
-
123168 int i = 0;
124169 String value = get(MessageFormat.format(tpl, i++));
125170 while (value != null) {
126171 list.add(value);
127172 value = get(MessageFormat.format(tpl, i++));
128173 }
129
-
130174 return list;
131175 }
132176
133177 /**
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
178
+ * get
179
+ * <p>
180
+ * Get a parameter value from the loaded properties or environment variables.
181
+ *
182
+ * @param paramname Parameter key.
183
+ * @return Value or null if not found anywhere.
140184 */
141185 public static String get(String paramname) {
142186
....@@ -149,12 +193,13 @@
149193 }
150194
151195 /**
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
196
+ * get
197
+ * <p>
198
+ * Returns parameter value or default if missing.
199
+ *
200
+ * @param paramname Key.
201
+ * @param defaultval Default fallback.
202
+ * @return value or default.
158203 */
159204 public static String get(String paramname, String defaultval) {
160205 String value = get(paramname);
....@@ -162,12 +207,12 @@
162207 }
163208
164209 /**
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
210
+ * getInt
211
+ * <p>
212
+ * Integer accessor, returns -1 if missing.
213
+ *
214
+ * @param paramname Key.
215
+ * @return Parsed integer or -1.
171216 */
172217 public static int getInt(String paramname) {
173218 String value = get(paramname);
....@@ -175,19 +220,24 @@
175220 }
176221
177222 /**
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
223
+ * getInt
224
+ * <p>
225
+ * Integer accessor, returns provided default when missing.
226
+ *
227
+ * @param paramname Key.
228
+ * @param defaultval Default fallback.
229
+ * @return Parsed integer or default.
185230 */
186231 public static int getInt(String paramname, int defaultval) {
187232 String value = get(paramname);
188233 return (value == null ? defaultval : Integer.parseInt(value));
189234 }
190235
236
+ /**
237
+ * KEYS
238
+ * <p>
239
+ * Strongly-typed keys used across the application.
240
+ */
191241 public static class KEYS {
192242
193243 public static final String SERVER_HOSTNAME = "license.server.hostname";
....@@ -205,5 +255,5 @@
205255 public static final String EMAIL_FROM_ADDRESS = "email.from.address";
206256 public static final String EMAIL_LIC_DEFAULT_SUBJECT = "email.lic.default.subject";
207257 }
208
-
209258 }
259
+