-
Notifications
You must be signed in to change notification settings - Fork 13
/
PropertiesLoader.java
272 lines (220 loc) · 6.73 KB
/
PropertiesLoader.java
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/**
* "First, solve the problem. Then, write the code. -John Johnson"
* "Or use Vangav M"
* www.vangav.com
* */
/**
* MIT License
*
* Copyright (c) 2016 Vangav
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
* */
/**
* Community
* Facebook Group: Vangav Open Source - Backend
* fb.com/groups/575834775932682/
* Facebook Page: Vangav
* fb.com/vangav.f
*
* Third party communities for Vangav Backend
* - play framework
* - cassandra
* - datastax
*
* Tag your question online (e.g.: stack overflow, etc ...) with
* #vangav_backend
* to easier find questions/answers online
* */
package com.vangav.backend.properties;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import com.vangav.backend.exceptions.CodeException;
import com.vangav.backend.exceptions.VangavException.ExceptionClass;
import com.vangav.backend.files.FileLoaderInl;
/**
* @author mustapha
* fb.com/mustapha.abdallah
*/
/**
* PropertiesLoader Loads all properties files and stores
* key:value pairs in memory
* It's also reload/get safe so a real-time-reload is safe with other threads
* getting properties' values
* */
public class PropertiesLoader {
private static PropertiesLoader instance = null;
/**
* propertiesFiles holds all properties files (with .prop extension)
* under ./conf/prop/
*/
private Map<String, Properties> propertiesFiles;
/**
* loadLatch is used activated on loading/reloading properties to make new
* get requests wait till loading/reloading is finished
*/
private CountDownLatch loadLatch;
/**
* getLatch is used to make loading/reloading requests wait till current
* get requests are finished
*/
private AtomicInteger getLatch;
/**
* Constructor - PropertiesLoader
* @return new PropertiesLoader Object
* @throws Exception
*/
protected PropertiesLoader () throws Exception {
this.propertiesFiles = new HashMap<String, Properties>();
this.loadLatch = new CountDownLatch(0);
this.getLatch = new AtomicInteger(0);
this.loadProperties();
}
/**
* load
* OPTIONAL method to load properties files ahead of using them to save
* time on the first time use
* @throws Exception
*/
public static void load () throws Exception {
if (instance == null) {
instance = new PropertiesLoader();
}
}
/**
* i
* singleton method
* @return static instance of PropertiesLoader
* @throws Exception
*/
public static PropertiesLoader i () throws Exception {
if (instance == null) {
instance = new PropertiesLoader();
}
return instance;
}
private static final String kPropPath = "conf/prop";
private static final String kPropExt = ".prop";
/**
* loadProperties
* loads all properties files (with extension .prop) from ./conf/prop
* @throws Exception
*/
private synchronized void loadProperties() throws Exception {
// lock for loading/reloading properties
this.loadLatch = new CountDownLatch(1);
// wait for in-processing get requests to finish
while (this.getLatch.get() > 0) {
try {
this.getLatch.wait();
} catch (InterruptedException e) {
}
}
try {
Map<String, Properties> tempMap = new HashMap<String, Properties>();
File [] propFiles =
FileLoaderInl.loadFiles(
kPropPath,
kPropExt);
InputStream input;
Properties prop;
for (File propFile : propFiles) {
input = new FileInputStream(propFile.getAbsolutePath() );
prop = new Properties();
prop.load(input);
tempMap.put(
propFile.getName().substring(
0,
propFile.getName().length() - kPropExt.length() ),
prop);
}
this.propertiesFiles.clear();
this.propertiesFiles.putAll(tempMap);
} catch (Exception e) {
throw e;
} finally {
// release loading/reloading lock
this.loadLatch.countDown();
}
}
/**
* getProperty
* @param fileName: properties file's name
* @param propName: property's name
* @param defaultValue: default value to return if not found here
* @return string value of the property
* @throws Exception
*/
public String getProperty (
String fileName,
String propName,
String defaultValue) throws Exception {
if (defaultValue == null) {
throw new CodeException(
152,
1,
"Invalid property, properties file ["
+ fileName
+ "] doesn't have property ["
+ propName +
"]",
ExceptionClass.MISSING_ITEM);
}
// wait for loading/reloading to finish
try {
this.loadLatch.await();
} catch (InterruptedException e) {
}
// increment get latch
this.getLatch.incrementAndGet();
try {
Properties prop =
propertiesFiles.get(fileName);
if (prop == null) {
return defaultValue;
}
return
prop.getProperty(
propName,
defaultValue);
} catch (Exception e) {
throw e;
} finally {
// decrement get latch and notify it
this.getLatch.decrementAndGet();
synchronized (this.getLatch) {
this.getLatch.notify();
}
}
}
/**
* reload
* used to reload properties file
* @throws Exception
*/
public void reload () throws Exception {
this.loadProperties();
}
}