| .. | .. |
|---|
| 1 | +package net.curisit.securis.utils; |
|---|
| 2 | + |
|---|
| 3 | +import java.io.IOException; |
|---|
| 4 | +import java.io.InputStream; |
|---|
| 5 | +import java.text.MessageFormat; |
|---|
| 6 | +import java.util.ArrayList; |
|---|
| 7 | +import java.util.List; |
|---|
| 8 | +import java.util.Properties; |
|---|
| 9 | + |
|---|
| 10 | +import org.apache.logging.log4j.LogManager; |
|---|
| 11 | +import org.apache.logging.log4j.Logger; |
|---|
| 12 | + |
|---|
| 13 | +/** |
|---|
| 14 | + * Class that loads and serves global config parameters. |
|---|
| 15 | + * |
|---|
| 16 | + * @author rsanchez |
|---|
| 17 | + */ |
|---|
| 18 | +public class Config { |
|---|
| 19 | + |
|---|
| 20 | + private static final Logger LOG = LogManager.getLogger(Config.class); |
|---|
| 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"; |
|---|
| 26 | + |
|---|
| 27 | + private static Properties params = null; |
|---|
| 28 | + |
|---|
| 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 | + } |
|---|
| 37 | + |
|---|
| 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 { |
|---|
| 46 | + |
|---|
| 47 | + LOG.debug("Loading params from " + resource); |
|---|
| 48 | + InputStream fileis = Params.class.getResourceAsStream(resource); |
|---|
| 49 | + |
|---|
| 50 | + params = new Properties(); |
|---|
| 51 | + try { |
|---|
| 52 | + |
|---|
| 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 | + } |
|---|
| 60 | + |
|---|
| 61 | + } |
|---|
| 62 | + |
|---|
| 63 | + public static String getByDomain(String domain, String paramname) { |
|---|
| 64 | + return getByDomain(domain, paramname, null); |
|---|
| 65 | + } |
|---|
| 66 | + |
|---|
| 67 | + public static String getByPrefix(String prefix, String paramname) { |
|---|
| 68 | + return get(prefix + "." + paramname, get(paramname)); |
|---|
| 69 | + } |
|---|
| 70 | + |
|---|
| 71 | + public static String getByPrefix(String prefix, String paramname, String defaultVal) { |
|---|
| 72 | + return get(prefix + "." + paramname, get(paramname, defaultVal)); |
|---|
| 73 | + } |
|---|
| 74 | + |
|---|
| 75 | + public static String getByDomain(String domain, String paramname, String defaultval) { |
|---|
| 76 | + return get(paramname + "." + domain, defaultval); |
|---|
| 77 | + } |
|---|
| 78 | + |
|---|
| 79 | + public static int getIntByDomain(String domain, String paramname) { |
|---|
| 80 | + return getInt(paramname + "." + domain, getInt(paramname)); |
|---|
| 81 | + } |
|---|
| 82 | + |
|---|
| 83 | + public static int getIntByDomain(String domain, String paramname, int defaultval) { |
|---|
| 84 | + return getInt(paramname + "." + domain, defaultval); |
|---|
| 85 | + } |
|---|
| 86 | + |
|---|
| 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>(); |
|---|
| 115 | + |
|---|
| 116 | + String tpl = prefix + ".{0}"; |
|---|
| 117 | + |
|---|
| 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 | + } |
|---|
| 124 | + |
|---|
| 125 | + return list; |
|---|
| 126 | + } |
|---|
| 127 | + |
|---|
| 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) { |
|---|
| 136 | + |
|---|
| 137 | + assert (params != null) : "Parameters have not been loaded. Call method loadParameters(resource) before use Params."; |
|---|
| 138 | + |
|---|
| 139 | + String value = params.getProperty(paramname); |
|---|
| 140 | + if (value == null) |
|---|
| 141 | + value = System.getenv(paramname); |
|---|
| 142 | + return value; |
|---|
| 143 | + } |
|---|
| 144 | + |
|---|
| 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 | + } |
|---|
| 157 | + |
|---|
| 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 | + } |
|---|
| 169 | + |
|---|
| 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 | + } |
|---|
| 182 | + |
|---|
| 183 | + public static class KEYS { |
|---|
| 184 | + |
|---|
| 185 | + |
|---|
| 186 | + |
|---|
| 187 | + public static final String SERVER_HOSTNAME = "license.server.hostname"; |
|---|
| 188 | + |
|---|
| 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"; |
|---|
| 193 | + public static final String KEYSTORE_TYPE = "ssl.keystore.type"; |
|---|
| 194 | + public static final String KEYSTORE_PASSWORD = "ssl.keystore.password"; |
|---|
| 195 | + public static final String KEYSTORE_ALIAS = "ssl.keystore.alias"; |
|---|
| 196 | + } |
|---|
| 197 | + |
|---|
| 198 | +} |
|---|