Roberto Sánchez
2014-02-24 ef282bd185482f2cab9648a8293aa74db85d787f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package net.curisit.securis.utils;
import java.io.IOException;
import java.io.InputStream;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
 * Class that loads and serves global config parameters.
 * 
 * @author rsanchez
 */
public class Params {
   private static Logger log = LogManager.getLogger(Params.class);
   /**
    * Key used to store config file resource location. In a web application, can be set as initial parameter in a servlet loaded on startup
    */
   public static final String KEY_CONFIG_FILE = "/securis-client.properties";
   private static Properties params = null;
   static {
       try {
           loadParameters(KEY_CONFIG_FILE);
       } catch (IOException e) {
           log.error("Config file {} was not found in classpath", KEY_CONFIG_FILE);
           System.exit(-2);
       }
   }
   /**
    * Loads application global parameters from a classpath resource
    * 
    * @param resource
    *            : Resource location in classpath, i.e: "/resource/cp-securis.conf"
    * @throws IOException
    */
   public static void loadParameters(String resource) throws IOException {
       log.debug("Loading params from " + resource);
       InputStream fileis = Params.class.getResourceAsStream(resource);
       params = new Properties();
       try {
           params.load(fileis);
           log.debug("Params loaded OK from {}", resource);
       } catch (IOException e) {
           log.error("Error loading config file: " + e);
           params = null;
           throw e;
       }
   }
   public static String getByDomain(String domain, String paramname) {
       return getByDomain(domain, paramname, null);
   }
   public static String getByPrefix(String prefix, String paramname) {
       return get(prefix + "." + paramname, get(paramname));
   }
   public static String getByPrefix(String prefix, String paramname, String defaultVal) {
       return get(prefix + "." + paramname, get(paramname, defaultVal));
   }
   public static String getByDomain(String domain, String paramname, String defaultval) {
       return get(paramname + "." + domain, defaultval);
   }
   public static int getIntByDomain(String domain, String paramname) {
       return getInt(paramname + "." + domain, getInt(paramname));
   }
   public static int getIntByDomain(String domain, String paramname, int defaultval) {
       return getInt(paramname + "." + domain, defaultval);
   }
   /**
    * Gets a List with all values of properties that begins with <code>prefix</code> It reads sequentially. For example:
    * 
    * <pre>
    *     securis.sort.comparator.0: net.cp.securis.comparators.ComparePttidVsPtn
    *     securis.sort.comparator.1: net.cp.securis.comparators.CompareFrequency
    *     securis.sort.comparator.2: net.cp.securis.comparators.CompareOutgoingVsIncomming
    *     securis.sort.comparator.3: net.cp.securis.comparators.CompareDuration 
    *     securis.sort.comparator.4: net.cp.securis.comparators.CompareCallVsSms
    * </pre>
    * 
    * That config (for prefix: "securis.sort.comparator" ) will return a List<String> with values:
    * 
    * <pre>
    *     "net.cp.securis.comparators.ComparePttidVsPtn", 
    *     "net.cp.securis.comparators.CompareFrequency", 
    *     "net.cp.securis.comparators.CompareOutgoingVsIncomming", 
    *     "net.cp.securis.comparators.CompareDuration", 
    *     "net.cp.securis.comparators.CompareCallVsSms"
    * </pre>
    * 
    * Note: If there is a gap between suffixes process will stop, that is, only will be returned properties found before gap.
    * 
    * @param prefix
    * @return
    */
   public static List<String> getListByPrefix(String prefix) {
       List<String> list = new ArrayList<String>();
       String tpl = prefix + ".{0}";
       int i = 0;
       String value = get(MessageFormat.format(tpl, i++));
       while (value != null) {
           list.add(value);
           value = get(MessageFormat.format(tpl, i++));
       }
       return list;
   }
   /**
    * Gets param value in config file or environment variables
    * 
    * @param paramname
    *            Global parameter's name
    * @return Value of paramname or null if paramname is not found neither in config file nor in environment variables
    */
   public static String get(String paramname) {
       assert (params != null) : "Parameters have not been loaded. Call method loadParameters(resource) before use Params.";
       String value = params.getProperty(paramname);
       if (value == null)
           value = System.getenv(paramname);
       return value;
   }
   /**
    * Gets param value from config file or environment variables
    * 
    * @param paramname
    *            Global parameter's name
    * @param defaultval
    * @return Value of paramname or defaultval if paramname is not found
    */
   public static String get(String paramname, String defaultval) {
       String value = get(paramname);
       return (value == null ? defaultval : value);
   }
   /**
    * Gets param value in config file or environment variables
    * 
    * @param paramname
    *            Global parameter's name
    * @return Integer value of paramname or -1 if paramname is not found neither in config file nor in environment variables
    */
   public static int getInt(String paramname) {
       String value = get(paramname);
       return (value == null ? -1 : Integer.parseInt(value));
   }
   /**
    * Gets param value from config file or environment variables
    * 
    * @param paramname
    *            Global parameter's name
    * @param defaultval
    * @return Integer value of paramname or defaultval if paramname is not found
    */
   public static int getInt(String paramname, int defaultval) {
       String value = get(paramname);
       return (value == null ? defaultval : Integer.parseInt(value));
   }
   public static class KEYS {
       /**
        * Public key file, Usually in "PEM" format
        */
       public static final String PUBLIC_KEY_FILE = "public.key.file";
       public static final String APPLICATION_CODE = "app.code";
       public static final String CUSTOMER_CODE = "customer.code";
   }
}