Skip to content

Commit

Permalink
fix: fix hashmap thread unsafe (#294)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
imp2002 authored Sep 11, 2022
1 parent 41bf788 commit 1281720
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions src/main/java/org/casbin/jcasbin/util/BuiltInFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

import com.googlecode.aviator.AviatorEvaluator;
import com.googlecode.aviator.AviatorEvaluatorInstance;
import com.googlecode.aviator.runtime.RuntimeUtils;
import com.googlecode.aviator.runtime.function.AbstractFunction;
import com.googlecode.aviator.runtime.function.AbstractVariadicFunction;
import com.googlecode.aviator.runtime.function.FunctionUtils;
import com.googlecode.aviator.runtime.type.*;
Expand All @@ -27,6 +25,7 @@
import org.casbin.jcasbin.rbac.RoleManager;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
Expand Down Expand Up @@ -153,7 +152,7 @@ public static boolean keyMatch4(String key1, String key2) {
Map<String, String> params = new HashMap<>();
for (String token : tokens) {
if (p.matcher(token).matches()) {
while (i < values.length && values[i].equals("")) {
while (i < values.length && "".equals(values[i])) {
i++;
}
if (i == values.length) {
Expand Down Expand Up @@ -242,7 +241,7 @@ public static String keyGet2Func(String key1, String key2, String pathVar) {
}
for (int i = 0; i < keysList.size(); i++) {
if (pathVar.equals(keysList.get(i).substring(1))) {
return valuesList.get(i+1);
return valuesList.get(i + 1);
}
}
return "";
Expand Down Expand Up @@ -334,26 +333,26 @@ public static boolean allMatch(String key1, String key2) {
}


public static class GenerateGFunctionClass{
public static class GenerateGFunctionClass {
// key:name such as g,g2 value:user-role mapping
private static Map<String, Map<String, AviatorBoolean>> memorizedMap = new HashMap<>();
private static Map<String, Map<String, AviatorBoolean>> memorizedMap = new ConcurrentHashMap<>();

/**
* generateGFunction is the factory method of the g(_, _) function.
*
* @param name the name of the g(_, _) function, can be "g", "g2", ..
* @param rm the role manager used by the function.
* @param rm the role manager used by the function.
* @return the function.
*/
public static AviatorFunction generateGFunction(String name, RoleManager rm) {
memorizedMap.put(name,new HashMap<>());
memorizedMap.put(name, new HashMap<>());

return new AbstractVariadicFunction() {
@Override
public AviatorObject variadicCall(Map<String, Object> env, AviatorObject... args) {
Map<String, AviatorBoolean> memorized = memorizedMap.get(name);
int len = args.length;
if(len < 2){
if (len < 2) {
return AviatorBoolean.valueOf(false);
}
String name1 = FunctionUtils.getStringValue(args[0], env);
Expand All @@ -364,11 +363,12 @@ public AviatorObject variadicCall(Map<String, Object> env, AviatorObject... args
String name = FunctionUtils.getStringValue(arg, env);
key += ";" + name;
}
if (memorized.containsKey(key)) {
return memorized.get(key);

AviatorBoolean value = memorized.get(key);
if (value != null) {
return value;
}

AviatorBoolean value;
if (rm == null) {
value = AviatorBoolean.valueOf(name1.equals(name2));
} else if (len == 2) {
Expand All @@ -390,6 +390,7 @@ public String getName() {
};
}
}

/**
* eval calculates the stringified boolean expression and return its result.
*
Expand Down

0 comments on commit 1281720

Please sign in to comment.