| .. | .. |
|---|
| 17 | 17 | */ |
|---|
| 18 | 18 | public class Config { |
|---|
| 19 | 19 | |
|---|
| 20 | | - private static final Logger LOG = LogManager.getLogger(Config.class); |
|---|
| 20 | + private static final Logger LOG = LogManager.getLogger(Config.class); |
|---|
| 21 | 21 | |
|---|
| 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"; |
|---|
| 26 | 27 | |
|---|
| 27 | | - private static Properties params = null; |
|---|
| 28 | + private static Properties params = null; |
|---|
| 28 | 29 | |
|---|
| 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 | + } |
|---|
| 37 | 38 | |
|---|
| 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 { |
|---|
| 46 | 48 | |
|---|
| 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); |
|---|
| 49 | 51 | |
|---|
| 50 | | - params = new Properties(); |
|---|
| 51 | | - try { |
|---|
| 52 | + params = new Properties(); |
|---|
| 53 | + try { |
|---|
| 52 | 54 | |
|---|
| 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 | + } |
|---|
| 60 | 62 | |
|---|
| 61 | | - } |
|---|
| 63 | + } |
|---|
| 62 | 64 | |
|---|
| 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 | + } |
|---|
| 66 | 68 | |
|---|
| 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 | + } |
|---|
| 70 | 72 | |
|---|
| 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 | + } |
|---|
| 74 | 76 | |
|---|
| 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 | + } |
|---|
| 78 | 80 | |
|---|
| 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 | + } |
|---|
| 82 | 84 | |
|---|
| 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 | + } |
|---|
| 86 | 88 | |
|---|
| 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>(); |
|---|
| 115 | 120 | |
|---|
| 116 | | - String tpl = prefix + ".{0}"; |
|---|
| 121 | + String tpl = prefix + ".{0}"; |
|---|
| 117 | 122 | |
|---|
| 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 | + } |
|---|
| 124 | 129 | |
|---|
| 125 | | - return list; |
|---|
| 126 | | - } |
|---|
| 130 | + return list; |
|---|
| 131 | + } |
|---|
| 127 | 132 | |
|---|
| 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) { |
|---|
| 136 | 142 | |
|---|
| 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."; |
|---|
| 138 | 144 | |
|---|
| 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 | + } |
|---|
| 144 | 150 | |
|---|
| 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 | + } |
|---|
| 157 | 163 | |
|---|
| 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 | + } |
|---|
| 169 | 176 | |
|---|
| 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 | + } |
|---|
| 182 | 190 | |
|---|
| 183 | | - public static class KEYS { |
|---|
| 191 | + public static class KEYS { |
|---|
| 184 | 192 | |
|---|
| 193 | + public static final String SERVER_HOSTNAME = "license.server.hostname"; |
|---|
| 185 | 194 | |
|---|
| 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"; |
|---|
| 188 | 197 | |
|---|
| 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"; |
|---|
| 193 | 199 | public static final String KEYSTORE_TYPE = "ssl.keystore.type"; |
|---|
| 194 | 200 | public static final String KEYSTORE_PASSWORD = "ssl.keystore.password"; |
|---|
| 195 | 201 | 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 | + } |
|---|
| 197 | 208 | |
|---|
| 198 | 209 | } |
|---|