rsanchez
2014-09-29 3293b85ad46d2622ae6ead2caf107e3c41c15561
#2021 fix - Added Config class to read parameters
1 files added
1 files modified
changed files
securis/src/main/java/net/curisit/securis/SeCurisServer.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/utils/Config.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/SeCurisServer.java
....@@ -12,6 +12,7 @@
1212
1313 import net.curisit.securis.ioc.RequestsModule;
1414 import net.curisit.securis.ioc.SecurisModule;
15
+import net.curisit.securis.utils.Config;
1516
1617 import org.apache.commons.io.FileUtils;
1718 import org.apache.logging.log4j.LogManager;
....@@ -149,7 +150,7 @@
149150 QueuedThreadPool threadPool = new QueuedThreadPool();
150151 threadPool.setMaxThreads(50);
151152
152
- server = new Server(9997);
153
+ server = new Server(Config.getInt(Config.KEYS.SERVER_PORT, 9080));
153154 ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
154155 context.setContextPath("/");
155156 context.addEventListener(injector.getInstance(GuiceResteasyBootstrapServletContextListener.class));
....@@ -184,7 +185,7 @@
184185
185186 HttpConfiguration http_config = new HttpConfiguration();
186187 http_config.setSecureScheme("https");
187
- http_config.setSecurePort(8443);
188
+ http_config.setSecurePort(Config.getInt(Config.KEYS.SERVER_SSL_PORT, 9443));
188189 http_config.setOutputBufferSize(32768);
189190 http_config.setSendServerVersion(true);
190191 http_config.setSendDateHeader(false);
....@@ -194,9 +195,9 @@
194195 https_config.addCustomizer(new SecureRequestCustomizer());
195196
196197 SslContextFactory sslContextFactory = new SslContextFactory();
197
- sslContextFactory.setKeyStorePath("/Users/rob/.ssh/keys/securis.pkcs12");
198
- sslContextFactory.setKeyStoreType("PKCS12");
199
- sslContextFactory.setKeyStorePassword("curist3c");
198
+ sslContextFactory.setKeyStorePath(Config.get(Config.KEYS.KEYSTORE_PATH));
199
+ sslContextFactory.setKeyStoreType(Config.get(Config.KEYS.KEYSTORE_TYPE, "JKS"));
200
+ sslContextFactory.setKeyStorePassword(Config.get(Config.KEYS.KEYSTORE_PASSWORD, ""));
200201 //sslContextFactory.setCertAlias("1");
201202 // sslContextFactory.setKeyManagerPassword("curist3c");
202203 // sslContextFactory.setTrustStorePath("/Users/rob/.ssh/keys/keystore");
....@@ -206,8 +207,8 @@
206207 LOG.info("Protocol: {}", sslContextFactory.getProtocol());
207208
208209 ServerConnector sslConnector = new ServerConnector(server, new SslConnectionFactory(sslContextFactory,HttpVersion.HTTP_1_1.asString()), new HttpConnectionFactory(https_config));
209
- sslConnector.setPort(8443);
210
- sslConnector.setHost("securis.curistec.com");
210
+ sslConnector.setPort(Config.getInt(Config.KEYS.SERVER_SSL_PORT, 9443));
211
+ sslConnector.setHost(Config.get(Config.KEYS.SERVER_HOSTNAME, "0.0.0.0"));
211212 server.addConnector( sslConnector );
212213
213214 server.setHandler(context);
securis/src/main/java/net/curisit/securis/utils/Config.java
....@@ -0,0 +1,198 @@
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
+}