Skip to content

Commit 1281720

Browse files
authored
fix: fix hashmap thread unsafe (casbin#294)
* fix: fix hashmap thread unsafe Signed-off-by: imp2002 <[email protected]> * fix: use ConcurrentHashMap() Signed-off-by: imp2002 <[email protected]> Signed-off-by: imp2002 <[email protected]>
1 parent 41bf788 commit 1281720

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

src/main/java/org/casbin/jcasbin/util/BuiltInFunctions.java

+13-12
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
import com.googlecode.aviator.AviatorEvaluator;
1818
import com.googlecode.aviator.AviatorEvaluatorInstance;
19-
import com.googlecode.aviator.runtime.RuntimeUtils;
20-
import com.googlecode.aviator.runtime.function.AbstractFunction;
2119
import com.googlecode.aviator.runtime.function.AbstractVariadicFunction;
2220
import com.googlecode.aviator.runtime.function.FunctionUtils;
2321
import com.googlecode.aviator.runtime.type.*;
@@ -27,6 +25,7 @@
2725
import org.casbin.jcasbin.rbac.RoleManager;
2826

2927
import java.util.*;
28+
import java.util.concurrent.ConcurrentHashMap;
3029
import java.util.regex.Matcher;
3130
import java.util.regex.Pattern;
3231
import java.util.regex.PatternSyntaxException;
@@ -153,7 +152,7 @@ public static boolean keyMatch4(String key1, String key2) {
153152
Map<String, String> params = new HashMap<>();
154153
for (String token : tokens) {
155154
if (p.matcher(token).matches()) {
156-
while (i < values.length && values[i].equals("")) {
155+
while (i < values.length && "".equals(values[i])) {
157156
i++;
158157
}
159158
if (i == values.length) {
@@ -242,7 +241,7 @@ public static String keyGet2Func(String key1, String key2, String pathVar) {
242241
}
243242
for (int i = 0; i < keysList.size(); i++) {
244243
if (pathVar.equals(keysList.get(i).substring(1))) {
245-
return valuesList.get(i+1);
244+
return valuesList.get(i + 1);
246245
}
247246
}
248247
return "";
@@ -334,26 +333,26 @@ public static boolean allMatch(String key1, String key2) {
334333
}
335334

336335

337-
public static class GenerateGFunctionClass{
336+
public static class GenerateGFunctionClass {
338337
// 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<>();
340339

341340
/**
342341
* generateGFunction is the factory method of the g(_, _) function.
343342
*
344343
* @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.
346345
* @return the function.
347346
*/
348347
public static AviatorFunction generateGFunction(String name, RoleManager rm) {
349-
memorizedMap.put(name,new HashMap<>());
348+
memorizedMap.put(name, new HashMap<>());
350349

351350
return new AbstractVariadicFunction() {
352351
@Override
353352
public AviatorObject variadicCall(Map<String, Object> env, AviatorObject... args) {
354353
Map<String, AviatorBoolean> memorized = memorizedMap.get(name);
355354
int len = args.length;
356-
if(len < 2){
355+
if (len < 2) {
357356
return AviatorBoolean.valueOf(false);
358357
}
359358
String name1 = FunctionUtils.getStringValue(args[0], env);
@@ -364,11 +363,12 @@ public AviatorObject variadicCall(Map<String, Object> env, AviatorObject... args
364363
String name = FunctionUtils.getStringValue(arg, env);
365364
key += ";" + name;
366365
}
367-
if (memorized.containsKey(key)) {
368-
return memorized.get(key);
366+
367+
AviatorBoolean value = memorized.get(key);
368+
if (value != null) {
369+
return value;
369370
}
370371

371-
AviatorBoolean value;
372372
if (rm == null) {
373373
value = AviatorBoolean.valueOf(name1.equals(name2));
374374
} else if (len == 2) {
@@ -390,6 +390,7 @@ public String getName() {
390390
};
391391
}
392392
}
393+
393394
/**
394395
* eval calculates the stringified boolean expression and return its result.
395396
*

0 commit comments

Comments
 (0)