16
16
17
17
import com .googlecode .aviator .AviatorEvaluator ;
18
18
import com .googlecode .aviator .AviatorEvaluatorInstance ;
19
- import com .googlecode .aviator .runtime .RuntimeUtils ;
20
- import com .googlecode .aviator .runtime .function .AbstractFunction ;
21
19
import com .googlecode .aviator .runtime .function .AbstractVariadicFunction ;
22
20
import com .googlecode .aviator .runtime .function .FunctionUtils ;
23
21
import com .googlecode .aviator .runtime .type .*;
27
25
import org .casbin .jcasbin .rbac .RoleManager ;
28
26
29
27
import java .util .*;
28
+ import java .util .concurrent .ConcurrentHashMap ;
30
29
import java .util .regex .Matcher ;
31
30
import java .util .regex .Pattern ;
32
31
import java .util .regex .PatternSyntaxException ;
@@ -153,7 +152,7 @@ public static boolean keyMatch4(String key1, String key2) {
153
152
Map <String , String > params = new HashMap <>();
154
153
for (String token : tokens ) {
155
154
if (p .matcher (token ).matches ()) {
156
- while (i < values .length && values [i ]. equals ( "" )) {
155
+ while (i < values .length && "" . equals ( values [i ])) {
157
156
i ++;
158
157
}
159
158
if (i == values .length ) {
@@ -242,7 +241,7 @@ public static String keyGet2Func(String key1, String key2, String pathVar) {
242
241
}
243
242
for (int i = 0 ; i < keysList .size (); i ++) {
244
243
if (pathVar .equals (keysList .get (i ).substring (1 ))) {
245
- return valuesList .get (i + 1 );
244
+ return valuesList .get (i + 1 );
246
245
}
247
246
}
248
247
return "" ;
@@ -334,26 +333,26 @@ public static boolean allMatch(String key1, String key2) {
334
333
}
335
334
336
335
337
- public static class GenerateGFunctionClass {
336
+ public static class GenerateGFunctionClass {
338
337
// key:name such as g,g2 value:user-role mapping
339
- private static Map <String , Map <String , AviatorBoolean >> memorizedMap = new HashMap <>();
338
+ private static Map <String , Map <String , AviatorBoolean >> memorizedMap = new ConcurrentHashMap <>();
340
339
341
340
/**
342
341
* generateGFunction is the factory method of the g(_, _) function.
343
342
*
344
343
* @param name the name of the g(_, _) function, can be "g", "g2", ..
345
- * @param rm the role manager used by the function.
344
+ * @param rm the role manager used by the function.
346
345
* @return the function.
347
346
*/
348
347
public static AviatorFunction generateGFunction (String name , RoleManager rm ) {
349
- memorizedMap .put (name ,new HashMap <>());
348
+ memorizedMap .put (name , new HashMap <>());
350
349
351
350
return new AbstractVariadicFunction () {
352
351
@ Override
353
352
public AviatorObject variadicCall (Map <String , Object > env , AviatorObject ... args ) {
354
353
Map <String , AviatorBoolean > memorized = memorizedMap .get (name );
355
354
int len = args .length ;
356
- if (len < 2 ){
355
+ if (len < 2 ) {
357
356
return AviatorBoolean .valueOf (false );
358
357
}
359
358
String name1 = FunctionUtils .getStringValue (args [0 ], env );
@@ -364,11 +363,12 @@ public AviatorObject variadicCall(Map<String, Object> env, AviatorObject... args
364
363
String name = FunctionUtils .getStringValue (arg , env );
365
364
key += ";" + name ;
366
365
}
367
- if (memorized .containsKey (key )) {
368
- return memorized .get (key );
366
+
367
+ AviatorBoolean value = memorized .get (key );
368
+ if (value != null ) {
369
+ return value ;
369
370
}
370
371
371
- AviatorBoolean value ;
372
372
if (rm == null ) {
373
373
value = AviatorBoolean .valueOf (name1 .equals (name2 ));
374
374
} else if (len == 2 ) {
@@ -390,6 +390,7 @@ public String getName() {
390
390
};
391
391
}
392
392
}
393
+
393
394
/**
394
395
* eval calculates the stringified boolean expression and return its result.
395
396
*
0 commit comments