-
Notifications
You must be signed in to change notification settings - Fork 1
/
ActionsManager.java
726 lines (579 loc) · 20.1 KB
/
ActionsManager.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
/**
* "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.vos_vangav_analytics_reader.actions;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import com.vangav.backend.content.checking.CharVerifierInl;
import com.vangav.backend.content.checking.CharVerifierInl.CharType;
import com.vangav.backend.exceptions.CodeException;
import com.vangav.backend.exceptions.VangavException.ExceptionClass;
import com.vangav.backend.files.FileLoaderInl;
import com.vangav.vos_vangav_analytics_reader.actions.json.ActionClassJson;
import com.vangav.vos_vangav_analytics_reader.actions.json.ActionIdJson;
import com.vangav.vos_vangav_analytics_reader.actions.json.ActionsJson;
import com.vangav.vos_vangav_analytics_reader.actions.json.CategoryJson;
/**
* @author mustapha
* fb.com/mustapha.abdallah
*/
/**
* ActionsManager loads/reloads and stores in memory actions
* it's also used to validate and retrieve actions
*/
public class ActionsManager {
private static ActionsManager instance = null;
// category name short > class prefix > action id
private Map<String, Map<String, Set<String> > > categoriesIndex;
// class prefix > action id
private Map<String, Set<String> > actionClassesIndex;
/**
* loadLatch is activated on loading/reloading actions 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 - ActionsManager
* @return new ActionsManager Object
* @throws Exception
*/
private ActionsManager () throws Exception {
this.categoriesIndex = new HashMap<String, Map<String, Set<String> > >();
this.actionClassesIndex = new HashMap<String, Set<String> >();
this.loadLatch = new CountDownLatch(0);
this.getLatch = new AtomicInteger(0);
}
/**
* i
* @return singleton instance of ActionsManager
* @throws Exception
*/
public static ActionsManager i () throws Exception {
if (instance == null) {
instance = new ActionsManager();
}
return instance;
}
/**
* lockLoad
* locks for loading/reloading then waits for in-processing get requests
* @throws Exception
*/
private void lockLoad () throws Exception {
// lock for loading/reloading actions
this.loadLatch = new CountDownLatch(1);
// wait for in-processing get requests to finish
while (this.getLatch.get() > 0) {
try {
synchronized (this.getLatch) {
this.getLatch.wait();
}
} catch (InterruptedException e) {
}
}
}
/**
* releaseLoad
* releases the loading/reloading lock to allow for new get requests
* @throws Exception
*/
private void releaseLoad () throws Exception {
// release loading/reloading lock
this.loadLatch.countDown();
}
/**
* lockGet
* waits for in-processing loading/reloading requests then adds a get lock
* @throws Exception
*/
private void lockGet () throws Exception {
// wait for loading/reloading to finish
try {
this.loadLatch.await();
} catch (InterruptedException e) {
}
// increment get latch
this.getLatch.incrementAndGet();
}
/**
* releaseGet
* releases a get lock
* @throws Exception
*/
private void releaseGet () throws Exception {
// decrement get latch and notify it
this.getLatch.decrementAndGet();
synchronized (this.getLatch) {
this.getLatch.notify();
}
}
/**
* load
* loads actions.json and organizes it in in-memory data structures
* @throws Exception
*/
public synchronized void load () throws Exception {
// locks for loading/reloading
this.lockLoad();
// load actions.json
ActionsJson actionsJson =
(ActionsJson)FileLoaderInl.loadJsonFile(new ActionsJson() );
// verify actions.json
this.verify(actionsJson);
// store in memory
this.storeInMemory(actionsJson);
// releases loading/reloading lock
this.releaseLoad();
}
/**
* verify
* throws an exception if the loaded json file isn't correct
* @param actionsJson
* @throws Exception
*/
private void verify (ActionsJson actionsJson) throws Exception {
Set<String> categoryNamesShort = new HashSet<String>();
// verify categories?
if (actionsJson.categories != null) {
// verify categories
for (CategoryJson categoryJson : actionsJson.categories) {
// verify category
if (categoryJson == null ||
categoryJson.category_name_short == null ||
categoryJson.category_name_short.length() == 0 ||
CharVerifierInl.consistsOfCharTypes(
categoryJson.category_name_short,
CharType.LOWER_CASE,
CharType.UPPER_CASE,
CharType.DIGIT,
CharType.DASH,
CharType.UNDER_SCORE) == false ||
categoryNamesShort.contains(categoryJson.category_name_short) ==
true) {
throw new CodeException(
400,
1,
"an actions' catoegory must be:"
+ " 1) not null"
+ " 2) its short name isn't null"
+ " 3) its short name isn't empty"
+ " 4) its short name consists of"
+ " letters/numbers/dashes/underscores"
+ " 5) its short name isn't a duplicate",
ExceptionClass.JSON);
}
categoryNamesShort.add(categoryJson.category_name_short);
}
}
// verify action classes
if (actionsJson.action_classes == null ||
actionsJson.action_classes.length == 0) {
throw new CodeException(
400,
2,
"an actions.json file must have at least one actions class",
ExceptionClass.JSON);
}
Set<String> classesPrefixes = new HashSet<String>();
Set<String> actionIds;
// for each actions class
for (ActionClassJson actionClassJson : actionsJson.action_classes) {
// verify actions class's prefix
if (actionClassJson == null ||
actionClassJson.class_prefix == null ||
actionClassJson.class_prefix.length() == 0 ||
CharVerifierInl.consistsOfCharTypes(
actionClassJson.class_prefix,
CharType.LOWER_CASE,
CharType.UPPER_CASE,
CharType.DIGIT,
CharType.DASH,
CharType.UNDER_SCORE) == false ||
classesPrefixes.contains(actionClassJson.class_prefix) == true ||
actionClassJson.action_ids == null ||
actionClassJson.action_ids.length == 0) {
throw new CodeException(
400,
3,
"an actions class must be:"
+ " 1) not null"
+ " 2) its prefix isn't null"
+ " 3) its prefix isn't empty"
+ " 4) its prefix consists of"
+ " letters/numbers/dashes/underscores"
+ " 5) its prefix isn't a duplicate"
+ " 6) its array of action ids has at least one element",
ExceptionClass.JSON);
}
classesPrefixes.add(actionClassJson.class_prefix);
// verify actions class's wide categories?
if (actionClassJson.class_wide_categories != null) {
// for each action class wide category
for (String classWideCategory :
actionClassJson.class_wide_categories) {
// action class wide category doesn't exist?
if (categoryNamesShort.contains(classWideCategory) == false) {
throw new CodeException(
400,
4,
"class wide category prefix ["
+ classWideCategory
+ "] for action class ["
+ actionClassJson.class_name
+ ", "
+ actionClassJson.class_prefix
+ "] doesn't exist",
ExceptionClass.JSON);
}
}
}
// an action class must have at least one action id
if (actionClassJson.action_ids == null ||
actionClassJson.action_ids.length == 0) {
throw new CodeException(
400,
5,
"action class ["
+ actionClassJson.class_name
+ ", "
+ actionClassJson.class_prefix
+ "] must have at least one action id",
ExceptionClass.JSON);
}
// verify action class's action ids
actionIds = new HashSet<String>();
// for each action id
for (ActionIdJson actionIdJson : actionClassJson.action_ids) {
// verify action and id
if (actionIdJson == null ||
actionIdJson.action_id == null ||
actionIdJson.action_id.length() == 0 ||
CharVerifierInl.consistsOfCharTypes(
actionIdJson.action_id,
CharType.LOWER_CASE,
CharType.UPPER_CASE,
CharType.DIGIT,
CharType.DASH,
CharType.UNDER_SCORE) == false ||
actionIds.contains(actionIdJson.action_id) == true) {
throw new CodeException(
400,
6,
"action class ["
+ actionClassJson.class_name
+ ", "
+ actionClassJson.class_prefix
+ "]"
+ " an action id must be:"
+ " 1) not null"
+ " 2) its id isn't null"
+ " 3) its id isn't empty"
+ " 4) its id consists of"
+ " letters/numbers/dashes/underscores"
+ " 5) its id isn't a duplicate",
ExceptionClass.JSON);
}
actionIds.add(actionIdJson.action_id);
// verify action's categories?
if (actionIdJson.action_categories != null) {
// for each of this action's category
for (String actionCategory : actionIdJson.action_categories) {
// action's category doesn't exist?
if (categoryNamesShort.contains(actionCategory) == false) {
throw new CodeException(
400,
7,
"action category prefix ["
+ actionCategory
+ "] for action class ["
+ actionClassJson.class_name
+ ", "
+ actionClassJson.class_prefix
+ "] for action id ["
+ actionIdJson.action_id
+ "] doesn't exist",
ExceptionClass.JSON);
}
}
}
}
}
}
/**
* storeInMemory
* stores actions in categories index and action classes index
* @param actionsJson
* @throws Exception
*/
private void storeInMemory (ActionsJson actionsJson) throws Exception {
// reset memory
this.categoriesIndex.clear();
this.actionClassesIndex.clear();
// got categories
if (actionsJson.categories != null) {
// index categories' keys (short names)
for (CategoryJson categoryJson : actionsJson.categories) {
this.categoriesIndex.put(
categoryJson.category_name_short,
new HashMap<String, Set<String> >() );
}
}
// fill categories index
for (ActionClassJson actionClassJson : actionsJson.action_classes) {
// got class wide categories?
if (actionClassJson.class_wide_categories != null) {
// index action class's wide categories
for (String classWideCategory :
actionClassJson.class_wide_categories) {
// first entry?
if (this.categoriesIndex.get(classWideCategory).containsKey(
actionClassJson.class_prefix) == false) {
this.categoriesIndex.get(classWideCategory).put(
actionClassJson.class_prefix,
new HashSet<String>() );
}
// index action id
for (ActionIdJson actionIdJson : actionClassJson.action_ids) {
this.categoriesIndex.get(classWideCategory).get(
actionClassJson.class_prefix).add(actionIdJson.action_id);
}
}
}
// index per-action-id categories
for (ActionIdJson actionIdJson : actionClassJson.action_ids) {
if (actionIdJson.action_categories != null) {
for (String actionCategory : actionIdJson.action_categories) {
// first entry?
if (this.categoriesIndex.get(actionCategory).containsKey(
actionClassJson.class_prefix) == false) {
this.categoriesIndex.get(actionCategory).put(
actionClassJson.class_prefix,
new HashSet<String>() );
}
// index action id
this.categoriesIndex.get(actionCategory).get(
actionClassJson.class_prefix).add(actionIdJson.action_id);
}
}
}
}
// fill action classes index
for (ActionClassJson actionClassJson : actionsJson.action_classes) {
this.actionClassesIndex.put(
actionClassJson.class_prefix,
new HashSet<String>() );
// for each action class's action id
for (ActionIdJson actionIdJson : actionClassJson.action_ids) {
this.actionClassesIndex.get(actionClassJson.class_prefix).add(
actionIdJson.action_id);
}
}
}
/**
* validateCategory
* @param categoryNameShort
* @return true if param categoryNameShort exists and false otherwise
* @throws Exception
*/
public boolean validateCategory (String categoryNameShort) throws Exception {
this.lockGet();
boolean result = this.categoriesIndex.containsKey(categoryNameShort);
this.releaseGet();
return result;
}
/**
* validateActionClass
* @param actionClassPrefix
* @return true if param actionClassPrefix exists and false otherwise
* @throws Exception
*/
public boolean validateActionClass (
String actionClassPrefix) throws Exception {
this.lockGet();
boolean result = this.actionClassesIndex.containsKey(actionClassPrefix);
this.releaseGet();
return result;
}
/**
* validateActionId
* @param actionClassPrefix
* @param actionId
* @return true if param actionId exists under param actionClassPrefix and
* false otherwise
* @throws Exception
*/
public boolean validateActionId (
String actionClassPrefix,
String actionId) throws Exception {
this.lockGet();
boolean result;
if (this.actionClassesIndex.containsKey(actionClassPrefix) == false) {
result = false;
} else {
result = this.actionClassesIndex.get(actionClassPrefix).contains(actionId);
}
this.releaseGet();
return result;
}
/**
* getCategoryActions
* @param categoryNameShort
* @return a map of action classes pointing to their action ids belonging to
* the category identified by param categoryNameShort and null
* otherwise
* @throws Exception
*/
public Map<String, Set<String> > getCategoryActions (
String categoryNameShort) throws Exception {
this.lockGet();
Map<String, Set<String> > result =
this.categoriesIndex.get(categoryNameShort);
this.releaseGet();
return result;
}
/**
* getClassActions
* @param classPrefix
* @return a set of the action ids belonging to the action class identified
* by param classPrefix and null otherwise
* @throws Exception
*/
public Set<String> getClassActions (String classPrefix) throws Exception {
this.lockGet();
Set<String> result = this.actionClassesIndex.get(classPrefix);
this.releaseGet();
return result;
}
private static final String kSeparator = "_";
/**
* format
* @param actionClassPrefix
* @param actionId
* @return formatted version of the action id for purposes like using it
* as a single string (varchar) id in cassandra by concatenating
* the action class's prefix with the action id
* @throws Exception
*/
public static String format (
String actionClassPrefix,
String actionId) throws Exception {
if (actionClassPrefix == null) {
return actionId;
}
if (actionId == null) {
return actionClassPrefix;
}
return
actionClassPrefix
+ kSeparator
+ actionId;
}
/**
* format
* @param actions
* @return formatted version of the action id for purposes like using it
* as a single string (varchar) id in cassandra by concatenating
* the action class's prefix with the action id
* @throws Exception
*/
public static ArrayList<String> format (
Map<String, Set<String> > actions) throws Exception {
ArrayList<String> result = new ArrayList<String>();
if (actions == null) {
return result;
}
for (String key : actions.keySet() ) {
if (key == null || actions.get(key) == null) {
continue;
}
for (String value : actions.get(key) ) {
if (value == null) {
continue;
}
result.add(
key
+ kSeparator
+ value);
}
}
return result;
}
/**
* format
* @param actionClassPrefix
* @param actionIds
* @return formatted version of the action id for purposes like using it
* as a single string (varchar) id in cassandra by concatenating
* the action class's prefix with the action id
* @throws Exception
*/
public static ArrayList<String> format (
String actionClassPrefix,
Set<String> actionIds) throws Exception {
ArrayList<String> result = new ArrayList<String>();
if (actionClassPrefix == null || actionIds == null) {
return result;
}
for (String actionId : actionIds) {
if (actionId == null) {
continue;
}
result.add(
actionClassPrefix
+ kSeparator
+ actionId);
}
return result;
}
}