| .. | .. |
|---|
| 1 | +/* |
|---|
| 2 | + * Copyright @ 2013 CurisTEC, S.A.S. All Rights Reserved. |
|---|
| 3 | + */ |
|---|
| 1 | 4 | package net.curisit.securis.utils; |
|---|
| 2 | 5 | |
|---|
| 3 | 6 | import java.io.IOException; |
|---|
| .. | .. |
|---|
| 11 | 14 | import org.apache.logging.log4j.Logger; |
|---|
| 12 | 15 | |
|---|
| 13 | 16 | /** |
|---|
| 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. |
|---|
| 15 | 31 | * |
|---|
| 16 | | - * @author rsanchez |
|---|
| 32 | + * @author JRA |
|---|
| 33 | + * Last reviewed by JRA on Oct 5, 2025. |
|---|
| 17 | 34 | */ |
|---|
| 18 | 35 | public class Config { |
|---|
| 19 | 36 | |
|---|
| 20 | 37 | private static final Logger LOG = LogManager.getLogger(Config.class); |
|---|
| 21 | 38 | |
|---|
| 22 | 39 | /** |
|---|
| 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". |
|---|
| 25 | 42 | */ |
|---|
| 26 | 43 | public static final String KEY_CONFIG_FILE = "/securis-server.properties"; |
|---|
| 27 | 44 | |
|---|
| .. | .. |
|---|
| 37 | 54 | } |
|---|
| 38 | 55 | |
|---|
| 39 | 56 | /** |
|---|
| 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. |
|---|
| 46 | 63 | */ |
|---|
| 47 | 64 | public static void loadParameters(String resource) throws IOException { |
|---|
| 48 | 65 | |
|---|
| .. | .. |
|---|
| 51 | 68 | |
|---|
| 52 | 69 | params = new Properties(); |
|---|
| 53 | 70 | try { |
|---|
| 54 | | - |
|---|
| 55 | 71 | params.load(fileis); |
|---|
| 56 | 72 | LOG.debug("Params loaded OK from {}", resource); |
|---|
| 57 | 73 | } catch (IOException e) { |
|---|
| .. | .. |
|---|
| 59 | 75 | params = null; |
|---|
| 60 | 76 | throw e; |
|---|
| 61 | 77 | } |
|---|
| 62 | | - |
|---|
| 63 | 78 | } |
|---|
| 64 | 79 | |
|---|
| 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 | + */ |
|---|
| 65 | 89 | public static String getByDomain(String domain, String paramname) { |
|---|
| 66 | 90 | return getByDomain(domain, paramname, null); |
|---|
| 67 | 91 | } |
|---|
| 68 | 92 | |
|---|
| 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 | + */ |
|---|
| 69 | 102 | public static String getByPrefix(String prefix, String paramname) { |
|---|
| 70 | 103 | return get(prefix + "." + paramname, get(paramname)); |
|---|
| 71 | 104 | } |
|---|
| 72 | 105 | |
|---|
| 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 | + */ |
|---|
| 73 | 117 | public static String getByPrefix(String prefix, String paramname, String defaultVal) { |
|---|
| 74 | 118 | return get(prefix + "." + paramname, get(paramname, defaultVal)); |
|---|
| 75 | 119 | } |
|---|
| 76 | 120 | |
|---|
| 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 | + */ |
|---|
| 77 | 131 | public static String getByDomain(String domain, String paramname, String defaultval) { |
|---|
| 78 | 132 | return get(paramname + "." + domain, defaultval); |
|---|
| 79 | 133 | } |
|---|
| 80 | 134 | |
|---|
| 135 | + /** |
|---|
| 136 | + * getIntByDomain |
|---|
| 137 | + * <p> |
|---|
| 138 | + * Integer variant of {@link #getByDomain(String, String)} with fallback to plain param. |
|---|
| 139 | + */ |
|---|
| 81 | 140 | public static int getIntByDomain(String domain, String paramname) { |
|---|
| 82 | 141 | return getInt(paramname + "." + domain, getInt(paramname)); |
|---|
| 83 | 142 | } |
|---|
| 84 | 143 | |
|---|
| 144 | + /** |
|---|
| 145 | + * getIntByDomain |
|---|
| 146 | + * <p> |
|---|
| 147 | + * Integer variant returning provided default when missing. |
|---|
| 148 | + */ |
|---|
| 85 | 149 | public static int getIntByDomain(String domain, String paramname, int defaultval) { |
|---|
| 86 | 150 | return getInt(paramname + "." + domain, defaultval); |
|---|
| 87 | 151 | } |
|---|
| 88 | 152 | |
|---|
| 89 | 153 | /** |
|---|
| 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. |
|---|
| 117 | 164 | */ |
|---|
| 118 | 165 | public static List<String> getListByPrefix(String prefix) { |
|---|
| 119 | 166 | List<String> list = new ArrayList<String>(); |
|---|
| 120 | | - |
|---|
| 121 | 167 | String tpl = prefix + ".{0}"; |
|---|
| 122 | | - |
|---|
| 123 | 168 | int i = 0; |
|---|
| 124 | 169 | String value = get(MessageFormat.format(tpl, i++)); |
|---|
| 125 | 170 | while (value != null) { |
|---|
| 126 | 171 | list.add(value); |
|---|
| 127 | 172 | value = get(MessageFormat.format(tpl, i++)); |
|---|
| 128 | 173 | } |
|---|
| 129 | | - |
|---|
| 130 | 174 | return list; |
|---|
| 131 | 175 | } |
|---|
| 132 | 176 | |
|---|
| 133 | 177 | /** |
|---|
| 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. |
|---|
| 140 | 184 | */ |
|---|
| 141 | 185 | public static String get(String paramname) { |
|---|
| 142 | 186 | |
|---|
| .. | .. |
|---|
| 149 | 193 | } |
|---|
| 150 | 194 | |
|---|
| 151 | 195 | /** |
|---|
| 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. |
|---|
| 158 | 203 | */ |
|---|
| 159 | 204 | public static String get(String paramname, String defaultval) { |
|---|
| 160 | 205 | String value = get(paramname); |
|---|
| .. | .. |
|---|
| 162 | 207 | } |
|---|
| 163 | 208 | |
|---|
| 164 | 209 | /** |
|---|
| 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. |
|---|
| 171 | 216 | */ |
|---|
| 172 | 217 | public static int getInt(String paramname) { |
|---|
| 173 | 218 | String value = get(paramname); |
|---|
| .. | .. |
|---|
| 175 | 220 | } |
|---|
| 176 | 221 | |
|---|
| 177 | 222 | /** |
|---|
| 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. |
|---|
| 185 | 230 | */ |
|---|
| 186 | 231 | public static int getInt(String paramname, int defaultval) { |
|---|
| 187 | 232 | String value = get(paramname); |
|---|
| 188 | 233 | return (value == null ? defaultval : Integer.parseInt(value)); |
|---|
| 189 | 234 | } |
|---|
| 190 | 235 | |
|---|
| 236 | + /** |
|---|
| 237 | + * KEYS |
|---|
| 238 | + * <p> |
|---|
| 239 | + * Strongly-typed keys used across the application. |
|---|
| 240 | + */ |
|---|
| 191 | 241 | public static class KEYS { |
|---|
| 192 | 242 | |
|---|
| 193 | 243 | public static final String SERVER_HOSTNAME = "license.server.hostname"; |
|---|
| .. | .. |
|---|
| 205 | 255 | public static final String EMAIL_FROM_ADDRESS = "email.from.address"; |
|---|
| 206 | 256 | public static final String EMAIL_LIC_DEFAULT_SUBJECT = "email.lic.default.subject"; |
|---|
| 207 | 257 | } |
|---|
| 208 | | - |
|---|
| 209 | 258 | } |
|---|
| 259 | + |
|---|