1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.anyflow.lannister;
18
19 import java.io.File;
20 import java.io.IOException;
21 import java.security.cert.CertificateException;
22 import java.util.List;
23 import java.util.Map;
24
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import com.google.common.base.Strings;
27 import com.google.common.collect.Lists;
28
29 import io.netty.handler.ssl.util.SelfSignedCertificate;
30 import net.anyflow.lannister.cluster.Mode;
31
32 public class Settings extends java.util.Properties {
33
34 private static final long serialVersionUID = -3232325711649130464L;
35
36 protected static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(Settings.class);
37
38 private final static String CONFIG_NAME = "lannister.properties";
39
40 public final static Settings INSTANCE = new Settings();
41
42 private Map<String, String> webResourceExtensionToMimes;
43 private transient SelfSignedCertificate ssc;
44 private java.util.Properties gitProperties;
45
46 @SuppressWarnings("unchecked")
47 private Settings() {
48 gitProperties = new java.util.Properties();
49
50 try {
51 load(Application.class.getClassLoader().getResourceAsStream(CONFIG_NAME));
52 gitProperties.load(Application.class.getClassLoader().getResourceAsStream("git.properties"));
53
54 ssc = new SelfSignedCertificate();
55 }
56 catch (IOException e) {
57 logger.error("Settings instantiation failed.", e);
58 }
59 catch (CertificateException e) {
60 logger.error(e.getMessage(), e);
61 }
62
63 try {
64 webResourceExtensionToMimes = new ObjectMapper().readValue(getProperty("webserver.MIME"), Map.class);
65 }
66 catch (IOException e) {
67 logger.error(e.getMessage(), e);
68 }
69
70 }
71
72 public int getInt(String key, Integer defaultValue) {
73 String valueString = this.getProperty(key.trim());
74
75 if (valueString == null) { return defaultValue; }
76
77 try {
78 return Integer.parseInt(valueString.trim());
79 }
80 catch (NumberFormatException e) {
81 return defaultValue;
82 }
83 }
84
85 public boolean getBoolean(String key, boolean defaultValue) {
86 String valueString = this.getProperty(key.trim());
87
88 if (valueString == null) { return defaultValue; }
89
90 return "true".equalsIgnoreCase(valueString.trim());
91 }
92
93 public Map<String, String> webResourceExtensionToMimes() {
94 return webResourceExtensionToMimes;
95 }
96
97 public static Integer tryParse(String text) {
98 try {
99 return Integer.parseInt(text);
100 }
101 catch (NumberFormatException e) {
102 return null;
103 }
104 }
105
106 public Integer mqttPort() {
107 return tryParse(getProperty("mqttserver.tcp.port", null));
108 }
109
110 public Integer mqttsPort() {
111 return tryParse(getProperty("mqttserver.tcp.ssl.port", null));
112 }
113
114 public Integer websocketPort() {
115 return tryParse(getProperty("mqttserver.websocket.port", null));
116 }
117
118 public Integer websocketSslPort() {
119 return tryParse(getProperty("mqttserver.websocket.ssl.port", null));
120 }
121
122 public Integer httpPort() {
123 return tryParse(getProperty("webserver.http.port", null));
124 }
125
126 public Integer httpsPort() {
127 return tryParse(getProperty("webserver.https.port", null));
128 }
129
130 public File certChainFile() {
131 String certFilePath = getProperty("lannister.ssl.certChainFilePath", null);
132
133 return "self".equalsIgnoreCase(certFilePath) ? ssc.certificate() : new File(certFilePath);
134 }
135
136 public File privateKeyFile() {
137 String privateKeyFilePath = getProperty("lannister.ssl.privateKeyFilePath", null);
138
139 return "self".equalsIgnoreCase(privateKeyFilePath) ? ssc.privateKey() : new File(privateKeyFilePath);
140 }
141
142 public List<String> bannedTopicFilters() {
143 List<String> ret = Lists.newArrayList();
144
145 String tokens = getProperty("mqttserver.subscribe.banned_topicfilters", "");
146 if (Strings.isNullOrEmpty(tokens)) { return ret; }
147
148 return Lists.newArrayList(tokens.split(","));
149 }
150
151
152
153
154 public String httpContextRoot() {
155 String ret = getProperty("webserver.contextRoot", "/");
156
157 if (ret.equalsIgnoreCase("") || ret.charAt(ret.length() - 1) != '/') {
158 ret += "/";
159 }
160
161 return ret;
162 }
163
164 public String nettyTransportMode() {
165 if (!getBoolean("netty.nativeTransportMode", true)) { return Literals.NETTY_NIO; }
166
167 return System.getProperty("os.name").startsWith("Linux") ? Literals.NETTY_EPOLL : Literals.NETTY_NIO;
168 }
169
170 public Mode clusteringMode() {
171 return Mode.from(getProperty("clustering.mode", "hazelcast"));
172 }
173
174 public String version() {
175 return this.gitProperties.getProperty("git.build.version");
176 }
177
178 public String commitId() {
179 return this.gitProperties.getProperty("git.commit.id");
180 }
181
182 public String commitIdDescribe() {
183 return this.gitProperties.getProperty("git.commit.id.describe");
184 }
185
186 public String commitMessage() {
187 return this.gitProperties.getProperty("git.commit.message.short");
188 }
189
190 public String buildTime() {
191 return this.gitProperties.getProperty("git.build.time");
192 }
193
194 @Override
195 public boolean equals(Object o) {
196 return o instanceof Settings;
197 }
198
199 @Override
200 public int hashCode() {
201 return version().hashCode();
202 }
203 }