Merge branch 'master' of gitea.internetapi.cn:iod/registry
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
package org.bdware.sc.registry;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import org.bdware.sc.ContractProcess;
|
||||
import org.bdware.sc.boundry.JavaScriptEntry;
|
||||
import org.bdware.sc.engine.JSONTool;
|
||||
import wrp.jdk.nashorn.api.scripting.NashornScriptEngine;
|
||||
import wrp.jdk.nashorn.api.scripting.ScriptObjectMirror;
|
||||
import wrp.jdk.nashorn.internal.runtime.PropertyMap;
|
||||
import wrp.jdk.nashorn.internal.runtime.ScriptFunction;
|
||||
import wrp.jdk.nashorn.internal.scripts.JO;
|
||||
|
||||
public class RuleExecutor {
|
||||
private static int executeCode;
|
||||
|
||||
public static Object executeRule(ScriptObjectMirror som) {
|
||||
executeCode = 0;
|
||||
JsonObject jo = JSONTool.convertMirrorToJson(som).getAsJsonObject();
|
||||
Object val = som.get("localDataSample");
|
||||
JsonObject rule = jo.get("mapRule").getAsJsonObject();
|
||||
JO ret = new JO(PropertyMap.newMap());
|
||||
for (String str : rule.keySet()) {
|
||||
ret.put(str, getMapResult(val, rule.get(str).getAsString()), false);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
//{"localDataSample":{"owner":"abcdef","size":"1234"},"mapRule":{"所有者":"${val.owner}","大小":"${val.size*1000}","偏移过的大小":"${edp(val.size,30.0)}" }}
|
||||
private static Object getMapResult(Object val, String fun) {
|
||||
try {
|
||||
NashornScriptEngine engine = ContractProcess.instance.engine.engine;
|
||||
if (fun.matches("\\$\\{.*\\}")) {
|
||||
fun = "function(val){ return " + fun.substring(2);
|
||||
}
|
||||
ScriptObjectMirror function = (ScriptObjectMirror) engine.eval(fun);
|
||||
function.compileScriptFunction();
|
||||
return JavaScriptEntry.executeFunction((ScriptFunction) function.getScriptObject(), val);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
//TODO GREP....
|
||||
executeCode = 1;
|
||||
return "exception:" + e.getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
public static int getLastExecuteStatus() {
|
||||
return executeCode;
|
||||
}
|
||||
|
||||
}
|
||||
27
backend/src/main/java/org/bdware/sc/registry/RuleHelper.java
Normal file
27
backend/src/main/java/org/bdware/sc/registry/RuleHelper.java
Normal file
@@ -0,0 +1,27 @@
|
||||
package org.bdware.sc.registry;
|
||||
|
||||
import com.alibaba.datax.transport.transformer.maskingMethods.differentialPrivacy.EpsilonDifferentialPrivacyImpl;
|
||||
import com.alibaba.datax.transport.transformer.maskingMethods.irreversibleInterference.MD5EncryptionImpl;
|
||||
|
||||
public class RuleHelper {
|
||||
public static String md5(Object obj) {
|
||||
try {
|
||||
MD5EncryptionImpl masker = new MD5EncryptionImpl();
|
||||
return masker.execute(obj.toString());
|
||||
} catch (Exception e) {
|
||||
return "*****" + e.getMessage() + "****";
|
||||
}
|
||||
}
|
||||
|
||||
public static Object edp(Object obj, double epsilon) {
|
||||
try {
|
||||
EpsilonDifferentialPrivacyImpl masker = new EpsilonDifferentialPrivacyImpl();
|
||||
double result = 0;
|
||||
result = masker.maskOne(Double.valueOf(obj.toString()), epsilon);
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return "***" + e.getMessage() + "***";
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user