add auto type convert in ArgSchema

This commit is contained in:
CaiHQ 2022-11-08 17:37:53 +08:00
parent 64cdfecd67
commit 9317385bd9
7 changed files with 33 additions and 30 deletions

View File

@ -6,7 +6,7 @@ plugins {
} }
group = "org.bdware.sc" group = "org.bdware.sc"
version = "1.6.7" version = "1.6.91"
repositories { repositories {
mavenCentral() mavenCentral()
mavenLocal() mavenLocal()
@ -42,8 +42,8 @@ dependencies {
implementation 'org.jsoup:jsoup:1.14.2' implementation 'org.jsoup:jsoup:1.14.2'
implementation 'com.sun.mail:javax.mail:1.6.2' implementation 'com.sun.mail:javax.mail:1.6.2'
implementation 'com.squareup.okhttp3:okhttp:4.9.1' implementation 'com.squareup.okhttp3:okhttp:4.9.1'
implementation 'org.bdware.bdcontract:sdk-java:1.0.0' implementation 'org.bdware.bdcontract:sdk-java:1.0.2'
implementation 'org.bdware.doip:doip-audit-tool:1.0.8' implementation 'org.bdware.doip:doip-audit-tool:1.1.3'
implementation fileTree(dir: 'lib', include: '*.jar') implementation fileTree(dir: 'lib', include: '*.jar')
testImplementation 'junit:junit:4.13.2' testImplementation 'junit:junit:4.13.2'
} }
@ -61,8 +61,8 @@ jar {
// uncomment this when publish, // uncomment this when publish,
//while develop at local use "false" //while develop at local use "false"
configurations.runtimeClasspath.filter { configurations.runtimeClasspath.filter {
it.getAbsolutePath().contains("/lib/") // it.getAbsolutePath().contains("/lib/")
// false false
}.collect { }.collect {
it.isDirectory() ? it : zipTree(it) it.isDirectory() ? it : zipTree(it)
} }

View File

@ -1,7 +1,6 @@
package org.bdware.sc.boundry; package org.bdware.sc.boundry;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import groovy.util.ScriptException;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.bdware.sc.ContractProcess; import org.bdware.sc.ContractProcess;
@ -19,7 +18,6 @@ import org.bdware.sc.util.HashUtil;
import org.bdware.sc.util.JsonUtil; import org.bdware.sc.util.JsonUtil;
import org.zz.gmhelper.SM2KeyPair; import org.zz.gmhelper.SM2KeyPair;
import wrp.jdk.nashorn.api.scripting.NashornScriptEngine; import wrp.jdk.nashorn.api.scripting.NashornScriptEngine;
import wrp.jdk.nashorn.api.scripting.ScriptObjectMirror;
import wrp.jdk.nashorn.internal.objects.Global; import wrp.jdk.nashorn.internal.objects.Global;
import wrp.jdk.nashorn.internal.runtime.PropertyMap; import wrp.jdk.nashorn.internal.runtime.PropertyMap;
import wrp.jdk.nashorn.internal.runtime.ScriptFunction; import wrp.jdk.nashorn.internal.runtime.ScriptFunction;
@ -391,10 +389,10 @@ public class JavaScriptEntry {
* @author Kaidong Wu * @author Kaidong Wu
*/ */
public static String subscribe(String topic, ScriptFunction fun) { public static String subscribe(String topic, ScriptFunction fun) {
subscribe(topic, fun, false);
if (topic_handlers.containsKey(topic)) { if (topic_handlers.containsKey(topic)) {
ContractProcess.instance.unSubscribe(topic_handlers.get(topic).getName()); ContractProcess.instance.unSubscribe(topic_handlers.get(topic).getName());
} }
subscribe(topic, fun, false);
topic_handlers.put(topic, fun); topic_handlers.put(topic, fun);
return topic; return topic;
} }

View File

@ -4,18 +4,9 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
public class ScriptReturnException extends IllegalStateException { public class ScriptReturnException extends IllegalStateException {
// public String message1;
// public int code;
public JsonObject message; public JsonObject message;
public ScriptReturnException(JsonElement jsonElement) {
message=jsonElement.getAsJsonObject();
// message=jo.get("msg").getAsString();
// code=jo.get("code").getAsInt();
}
// @Override public ScriptReturnException(JsonElement jsonElement) {
// public String getMessage() { message = jsonElement.getAsJsonObject();
// return this.message1; }
//
// }
} }

View File

@ -271,7 +271,6 @@ public class DesktopEngine extends JSEngine {
ScriptEngine.FILENAME, ScriptEngine.FILENAME,
fun.getFileName(), fun.getFileName(),
ScriptContext.ENGINE_SCOPE); ScriptContext.ENGINE_SCOPE);
LOGGER.info("loadFun:" + str);
compileFunction(fun, str, isInsnLimit); compileFunction(fun, str, isInsnLimit);
} catch (ScriptException e) { } catch (ScriptException e) {
return wrapperException(e, fun); return wrapperException(e, fun);
@ -427,7 +426,14 @@ public class DesktopEngine extends JSEngine {
Method getGeneric = ScriptFunctionData.class.getDeclaredMethod("getGenericInvoker", ScriptObject.class); Method getGeneric = ScriptFunctionData.class.getDeclaredMethod("getGenericInvoker", ScriptObject.class);
getGeneric.setAccessible(true); getGeneric.setAccessible(true);
MethodHandle methodHandle = (MethodHandle) getGeneric.invoke(scriptFunctioNData, scope); MethodHandle methodHandle = (MethodHandle) getGeneric.invoke(scriptFunctioNData, scope);
if (methodHandle.getClass() != Class.forName("java.lang.invoke.DirectMethodHandle")) {
Field argL0 = methodHandle.getClass().getDeclaredField("argL0");
argL0.setAccessible(true);
methodHandle = (MethodHandle) argL0.get(methodHandle);
}
Field memberNameField = methodHandle.getClass().getDeclaredField("member"); Field memberNameField = methodHandle.getClass().getDeclaredField("member");
memberNameField.setAccessible(true); memberNameField.setAccessible(true);
Object memberName = memberNameField.get(methodHandle); Object memberName = memberNameField.get(methodHandle);
Field clazz = memberName.getClass().getDeclaredField("clazz"); Field clazz = memberName.getClass().getDeclaredField("clazz");
@ -437,6 +443,7 @@ public class DesktopEngine extends JSEngine {
functionNode.compiledClazz = clazz2; functionNode.compiledClazz = clazz2;
//functionNode==null --> event functions //functionNode==null --> event functions
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} finally { } finally {
if (globalChanged) Context.setGlobal(oldGlobal); if (globalChanged) Context.setGlobal(oldGlobal);

View File

@ -20,7 +20,6 @@ public class ArgSchemaHandler implements AnnotationHook {
public ArgSchemaHandler(AnnotationNode annoNode) { public ArgSchemaHandler(AnnotationNode annoNode) {
a = annoNode; a = annoNode;
String arg = a.getArgs().get(0); String arg = a.getArgs().get(0);
// if (arg.startsWith("/")){ // if (arg.startsWith("/")){
// ;//ContractProcess.instance.engine.getResources().loadAsString(arg); // ;//ContractProcess.instance.engine.getResources().loadAsString(arg);
// }else { // }else {
@ -39,8 +38,20 @@ public class ArgSchemaHandler implements AnnotationHook {
ArgSchemaVisitor visitor; ArgSchemaVisitor visitor;
if (je.isJsonObject()) if (je.isJsonObject())
visitor = new ArgSchemaVisitor(input.getArg().getAsJsonObject()); visitor = new ArgSchemaVisitor(input.getArg().getAsJsonObject());
else else {
visitor = new ArgSchemaVisitor(JsonParser.parseString(input.getArg().getAsString())); try {
JsonElement obj = JsonParser.parseString(input.getArg().getAsString());
visitor = new ArgSchemaVisitor(obj);
//IMPORTANT automatically convert arg type here
input.setArg(obj);
} catch (Exception e) {
e.printStackTrace();
JsonObject jo = new JsonObject();
jo.addProperty("msg", "[Illegal Type] argument should be JSON");
jo.addProperty("code", 1004);
throw new ScriptReturnException(jo);
}
}
if (je.toString().isEmpty() && !a.getArgs().get(0).equals("")) { if (je.toString().isEmpty() && !a.getArgs().get(0).equals("")) {
JsonObject jo = new JsonObject(); JsonObject jo = new JsonObject();
jo.addProperty("msg", "[Empty argument] argument should not be empty"); jo.addProperty("msg", "[Empty argument] argument should not be empty");

View File

@ -1,13 +1,9 @@
package org.bdware.sc.engine.hook; package org.bdware.sc.engine.hook;
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonPrimitive;
import org.bdware.sc.JSEngine; import org.bdware.sc.JSEngine;
import org.bdware.sc.bean.ContractRequest; import org.bdware.sc.bean.ContractRequest;
import org.bdware.sc.engine.JSONTool; import org.bdware.sc.engine.JSONTool;
import org.bdware.sc.node.AnnotationHook; import org.bdware.sc.node.AnnotationHook;
import wrp.jdk.nashorn.api.scripting.ScriptObjectMirror;
public class ObjToJsonHandler implements AnnotationHook { public class ObjToJsonHandler implements AnnotationHook {
@Override @Override

View File

@ -5,6 +5,6 @@ appender.console.name=STDOUT
appender.console.layout.type=PatternLayout appender.console.layout.type=PatternLayout
appender.console.layout.pattern=%highlight{[%-5p] %d{HH:mm:ss.SSS} %m (%F:%L)[%M]%n}{FATAL=Bright Red,ERROR=Red,WARN=Yellow,INFO=Green,DEBUG=Blue,TRACE=White} appender.console.layout.pattern=%highlight{[%-5p] %d{HH:mm:ss.SSS} %m (%F:%L)[%M]%n}{FATAL=Bright Red,ERROR=Red,WARN=Yellow,INFO=Green,DEBUG=Blue,TRACE=White}
rootLogger.level=error rootLogger.level=info
rootLogger.appenderRef.stdout.ref=STDOUT rootLogger.appenderRef.stdout.ref=STDOUT
rootLogger.appenderRef.stdout.level=error rootLogger.appenderRef.stdout.level=info