diff --git a/build.gradle b/build.gradle index e6364ce..400a8bb 100644 --- a/build.gradle +++ b/build.gradle @@ -6,7 +6,7 @@ plugins { } group = "org.bdware.sc" -version = "1.8.1" +version = "1.8.2" tasks.withType(JavaCompile) { // options.compilerArgs << '-Xlint:none' // options.compilerArgs << '-Xlint:deprecation' << "-Werror" @@ -67,7 +67,7 @@ jar { // uncomment this when publish, // while develop at local use "false" configurations.runtimeClasspath.filter { - // it.getAbsolutePath().contains("/lib/") + // it.getAbsolutePath().contains("/lib/") false }.collect { it.isDirectory() ? it : zipTree(it) diff --git a/src/main/java/org/bdware/sc/ContractProcess.java b/src/main/java/org/bdware/sc/ContractProcess.java index 8a1b965..8b9901e 100644 --- a/src/main/java/org/bdware/sc/ContractProcess.java +++ b/src/main/java/org/bdware/sc/ContractProcess.java @@ -31,6 +31,7 @@ import org.bdware.sc.engine.DesktopEngine; import org.bdware.sc.engine.JSONTool; import org.bdware.sc.engine.hook.*; import org.bdware.sc.handler.ContractHandler; +import org.bdware.sc.handler.DOOPRequestHandler; import org.bdware.sc.index.TimeSerialIndex; import org.bdware.sc.node.*; import org.bdware.sc.server.DoipClusterServer; @@ -80,6 +81,7 @@ public class ContractProcess { private TimeSerialIndex logIndex; private RocksDBUtil edion; private String pid; + public DOOPRequestHandler doopRequestHandler; public ContractProcess(int port, String cmi) { handler = new ContractHandler(this); @@ -542,7 +544,7 @@ public class ContractProcess { } } - private void injectHandlers() { + private void injectHandlers() throws Exception { // 正式启动 if (!this.contract.isDebug()) { // this.engine.getResources().loadAsString("/maskConfig.json"); @@ -555,6 +557,9 @@ public class ContractProcess { } } } + DOOPBeforeExecHandler doopBeforeExecHandler = null; + DOOPAfterExecHandler doopAfterExecHandler = null; + doopRequestHandler = null; for (FunctionNode fun : cn.getFunctions()) { if (fun.isConfidential()) { fun.appendBeforeInvokeHandler(new ConfidentialHandler(fun)); @@ -591,8 +596,12 @@ public class ContractProcess { } if (fun.isDoipOperation()) { - fun.appendBeforeInvokeHandler(DOOPBeforeExecHandler.createDOOPHandler()); - fun.appendAfterInvokeHandler(DOOPAfterExecHandler.createDOOPHandler()); + if (doopRequestHandler == null) { + doopRequestHandler = new DOOPRequestHandler(); + } + fun.appendBeforeInvokeHandler(new DOOPBeforeExecHandler(fun.getDoipOperationInfo().operation)); + fun.appendAfterInvokeHandler(new DOOPAfterExecHandler(fun.getDoipOperationInfo().operation)); + doopRequestHandler.addDoipOperation(fun); } } } diff --git a/src/main/java/org/bdware/sc/compiler/ap/DOOP.java b/src/main/java/org/bdware/sc/compiler/ap/DOOP.java index 0e254b5..16e0451 100644 --- a/src/main/java/org/bdware/sc/compiler/ap/DOOP.java +++ b/src/main/java/org/bdware/sc/compiler/ap/DOOP.java @@ -2,9 +2,6 @@ package org.bdware.sc.compiler.ap; import org.bdware.sc.bean.DoipOperationInfo; import org.bdware.sc.compiler.AnnotationProcessor; -import org.bdware.sc.engine.hook.DOOPAfterExecHandler; -import org.bdware.sc.engine.hook.DOOPBeforeExecHandler; -import org.bdware.sc.handler.DOOPRequestHandler; import org.bdware.sc.node.AnnotationNode; import org.bdware.sc.node.ContractNode; import org.bdware.sc.node.FunctionNode; @@ -15,23 +12,8 @@ public class DOOP extends AnnotationProcessor { @Override public void processFunction(AnnotationNode anno, ContractNode contractNode, FunctionNode functionNode) throws Exception { // 通过DOOP注解,解析对应的值,并放进对应的FunctionNode中 - // 注解必须暴露出来昂!!! functionNode.setIsExport(true); functionNode.setIsDoipOperation(true); functionNode.setDoipOperationInfo(DoipOperationInfo.create(anno, contractNode)); - // functionNode.setFunctionName(functionNode.getDoipOperationInfo().operationName); - - // 维护DOOPRequestHandler - DOOPRequestHandler.createHandler(); - DOOPRequestHandler.instance.addDoipOperation(functionNode); - - // 维护DOOPHandler - DOOPBeforeExecHandler.createDOOPHandler(); - DOOPBeforeExecHandler.instance.putFuncNameAndDoipOperationsMapping(functionNode); - DOOPAfterExecHandler.createDOOPHandler(); - DOOPAfterExecHandler.instance.putFuncNameAndDoipOperationsMapping(functionNode); - - // 维护ContractNode,functionName is useless, use BasicOperation to map the corresponding functionNode - // contractNode.updateFunctionMap(functionNode.functionName, functionNode.getDoipOperationInfo().operationName); } } diff --git a/src/main/java/org/bdware/sc/engine/hook/DOOPAfterExecHandler.java b/src/main/java/org/bdware/sc/engine/hook/DOOPAfterExecHandler.java index 3bcf474..623d848 100644 --- a/src/main/java/org/bdware/sc/engine/hook/DOOPAfterExecHandler.java +++ b/src/main/java/org/bdware/sc/engine/hook/DOOPAfterExecHandler.java @@ -3,10 +3,12 @@ package org.bdware.sc.engine.hook; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; -import org.bdware.doip.codec.doipMessage.*; +import org.bdware.doip.codec.doipMessage.DoipMessage; +import org.bdware.doip.codec.doipMessage.DoipResponseCode; +import org.bdware.doip.codec.doipMessage.HeaderParameter; +import org.bdware.doip.codec.doipMessage.MessageHeader; import org.bdware.doip.codec.operations.BasicOperations; import org.bdware.sc.JSEngine; -import org.bdware.sc.bean.ContractRequest; import org.bdware.sc.boundry.ScriptReturnException; import org.bdware.sc.engine.JSONTool; import org.bdware.sc.entity.DoipMessagePacker; @@ -16,39 +18,15 @@ import org.bdware.sc.node.FunctionNode; import org.bdware.sc.util.JsonUtil; import java.nio.charset.StandardCharsets; -import java.util.HashMap; -import java.util.Map; -import java.util.Set; public class DOOPAfterExecHandler implements AnnotationHook { - public static Map funcNameToDoipOperations; - public static DOOPAfterExecHandler instance; - public DOOPAfterExecHandler() { - funcNameToDoipOperations = new HashMap<>(); + private JsonElement jsonResponseRules; + + public DOOPAfterExecHandler(BasicOperations operations) { + jsonResponseRules = getRulesForJsonResponse(operations); } - - public static DOOPAfterExecHandler createDOOPHandler() { - if(instance == null) { - instance = new DOOPAfterExecHandler(); - } - - return instance; - } - - public void putFuncNameAndDoipOperationsMapping(FunctionNode fn) { - String basicOperationsString = fn.getDoipOperationInfo().operationType; - BasicOperations operation = BasicOperations.Unknown; - for(BasicOperations basicOperation : BasicOperations.values()) { - if(basicOperation.toString().equals(basicOperationsString)) { - operation = basicOperation; - } - } - funcNameToDoipOperations.put(fn.getFunctionName(), operation); - } - @Override public ArgPacks handle(JSEngine desktopEngine, ArgPacks argPacks) { - BasicOperations curOp = funcNameToDoipOperations.get(argPacks.request.getAction()); Object originDoipMsgPacker = argPacks.arg; DoipMessage originDoipMsg = null; if (originDoipMsgPacker instanceof DoipMessagePacker) { @@ -61,27 +39,25 @@ public class DOOPAfterExecHandler implements AnnotationHook { } else { // pack JsonObject jsonObjectRes = JSONTool.convertMirrorToJson(argPacks.ret).getAsJsonObject(); - // validate json response - JsonElement jsonResponseRules = getRulesForJsonResponse(curOp); ArgSchemaVisitor visitor = new ArgSchemaVisitor(jsonObjectRes); validateJsonElementRulesByArgSchemaVisitor(jsonResponseRules, visitor); - JsonObject header = jsonObjectRes.get("header") != null ? jsonObjectRes.get("header").getAsJsonObject() : null; String body = jsonObjectRes.get("body") != null ? jsonObjectRes.get("body").getAsString() : null; // 和HTTP一样,所有需要的字段自己封装,校验规则也比较简单,这里只做简单的包装返回即可!!! - if(header != null) { + if (header != null) { originDoipMsg.header = JsonUtil.fromJson(header, MessageHeader.class); // response字段根据白皮书上的规定,处于header下,人为包装到parameters的response中 String headerRespCode = header.get("response") != null ? header.get("response").getAsString() : null; - if(headerRespCode != null) { - if (originDoipMsg.header.parameters == null) originDoipMsg.header.parameters = new HeaderParameter(null, null); + if (headerRespCode != null) { + if (originDoipMsg.header.parameters == null) + originDoipMsg.header.parameters = new HeaderParameter(null, null); originDoipMsg.header.parameters.response = DoipResponseCode.valueOf(headerRespCode); } } - if(body != null) { + if (body != null) { originDoipMsg.body.encodedData = body.getBytes(StandardCharsets.UTF_8); } @@ -94,7 +70,6 @@ public class DOOPAfterExecHandler implements AnnotationHook { } - public static JsonElement getRulesForJsonResponse(BasicOperations basicOperations) { switch (basicOperations) { case Hello: @@ -114,31 +89,27 @@ public class DOOPAfterExecHandler implements AnnotationHook { } // old convert jsonResponse from argPack's ret to Doip response in doip chain logic - public static DoipMessage convertJsonResponseToDoipMessage(FunctionNode fn, JsonElement jsonResponse, DoipMessage msg) { - BasicOperations curOp = funcNameToDoipOperations.get(fn.getFunctionName()); + public DoipMessage convertJsonResponseToDoipMessage(FunctionNode fn, JsonElement jsonResponse, DoipMessage msg) { JsonObject jsonParams = jsonResponse.getAsJsonObject(); - // validate json response - JsonElement jsonResponseRules = getRulesForJsonResponse(curOp); ArgSchemaVisitor visitor = new ArgSchemaVisitor(jsonResponse); validateJsonElementRulesByArgSchemaVisitor(jsonResponseRules, visitor); JsonObject header = jsonParams.get("header") != null ? jsonParams.get("header").getAsJsonObject() : null; String body = jsonParams.get("body") != null ? jsonParams.get("body").getAsString() : null; - if(header != null) { + if (header != null) { String headerRespCode = header.get("response") != null ? header.get("response").getAsString() : null; - if(headerRespCode != null) { + if (headerRespCode != null) { for (DoipResponseCode responseCode : DoipResponseCode.values()) { - if(responseCode.toString().equals(headerRespCode)) { + if (responseCode.toString().equals(headerRespCode)) { msg.header.parameters.response = responseCode; break; } } } } - - if(body != null) { + if (body != null) { msg.body.encodedData = body.getBytes(StandardCharsets.UTF_8); } diff --git a/src/main/java/org/bdware/sc/engine/hook/DOOPBeforeExecHandler.java b/src/main/java/org/bdware/sc/engine/hook/DOOPBeforeExecHandler.java index 0437d1f..8fcba4c 100644 --- a/src/main/java/org/bdware/sc/engine/hook/DOOPBeforeExecHandler.java +++ b/src/main/java/org/bdware/sc/engine/hook/DOOPBeforeExecHandler.java @@ -12,37 +12,19 @@ import org.bdware.sc.boundry.ScriptReturnException; import org.bdware.sc.entity.DoipMessagePacker; import org.bdware.sc.node.AnnotationHook; import org.bdware.sc.node.ArgPacks; -import org.bdware.sc.node.FunctionNode; import java.nio.charset.StandardCharsets; -import java.util.HashMap; import java.util.Map; import java.util.Set; public class DOOPBeforeExecHandler implements AnnotationHook { - public static Map funcNameToDoipOperations; - public static DOOPBeforeExecHandler instance; - public DOOPBeforeExecHandler() { - funcNameToDoipOperations = new HashMap<>(); - } - public static DOOPBeforeExecHandler createDOOPHandler() { - if(instance == null) { - instance = new DOOPBeforeExecHandler(); - } + private final BasicOperations httpOperation; + private JsonElement httpArgsRules; - return instance; - } - - public void putFuncNameAndDoipOperationsMapping(FunctionNode fn) { - String basicOperationsString = fn.getDoipOperationInfo().operationType; - BasicOperations operation = BasicOperations.Unknown; - for(BasicOperations basicOperation : BasicOperations.values()) { - if(basicOperation.toString().equals(basicOperationsString)) { - operation = basicOperation; - } - } - funcNameToDoipOperations.put(fn.getFunctionName(), operation); + public DOOPBeforeExecHandler(BasicOperations operations) { + httpOperation = operations; + httpArgsRules = getRulesForHTTPRequest(operations); } @Override @@ -50,8 +32,8 @@ public class DOOPBeforeExecHandler implements AnnotationHook { Object arg = argPacks.arg; DoipMessagePacker doipMsgPackerArg = new DoipMessagePacker(); - if (arg instanceof DoipMessagePacker){ - doipMsgPackerArg = (DoipMessagePacker)arg; + if (arg instanceof DoipMessagePacker) { + doipMsgPackerArg = (DoipMessagePacker) arg; } else { // validate http request's params ContractRequest httpReq = argPacks.request; @@ -66,19 +48,16 @@ public class DOOPBeforeExecHandler implements AnnotationHook { return argPacks; } - public static void validateHTTPRequestArgs(ContractRequest httpReq) { + public void validateHTTPRequestArgs(ContractRequest httpReq) { JsonElement originArgs = httpReq.getArg(); JsonElement httpArgs = JsonParser.parseString(originArgs.getAsString()); - BasicOperations curOp = funcNameToDoipOperations.get(httpReq.getAction()); - // get args rules and validate http args - JsonElement httpArgsRules = getRulesForHTTPRequest(curOp); ArgSchemaVisitor visitor = new ArgSchemaVisitor(httpArgs); validateJsonElementRulesByArgSchemaVisitor(httpArgsRules, visitor); } public static JsonElement getRulesForHTTPRequest(BasicOperations basicOperation) { - switch(basicOperation) { + switch (basicOperation) { case Hello: case Delete: case ListOps: @@ -97,14 +76,13 @@ public class DOOPBeforeExecHandler implements AnnotationHook { } } - public static DoipMessage convertHttpRequestToDoipMessage(ContractRequest httpReq) { - BasicOperations httpOperation = funcNameToDoipOperations.get(httpReq.getAction()); + public DoipMessage convertHttpRequestToDoipMessage(ContractRequest httpReq) { JsonObject jsonParams = JsonParser.parseString(httpReq.getArg().getAsString()).getAsJsonObject(); // taking Extension into consideration JsonObject header = jsonParams.get("header") != null ? jsonParams.get("header").getAsJsonObject() : null; String body = jsonParams.get("body") != null ? jsonParams.get("body").getAsString() : null; DoipMessage doipMessage = null; - switch(httpOperation) { + switch (httpOperation) { case Hello: case Delete: case ListOps: @@ -121,13 +99,13 @@ public class DOOPBeforeExecHandler implements AnnotationHook { DoipMessageFactory.DoipMessageBuilder searchBuilder = new DoipMessageFactory.DoipMessageBuilder() .createRequest(header.get("identifier").getAsString(), httpOperation.getName()); JsonElement query = header.get("query"); - if(query != null) searchBuilder.addAttributes("query", query.getAsString()); + if (query != null) searchBuilder.addAttributes("query", query.getAsString()); JsonElement pageNum = header.get("pageNum"); - if(pageNum != null) searchBuilder.addAttributes("pageNum", pageNum.getAsInt()); + if (pageNum != null) searchBuilder.addAttributes("pageNum", pageNum.getAsInt()); JsonElement pageSize = header.get("pageSize"); - if(pageSize != null) searchBuilder.addAttributes("pageSize", pageSize.getAsInt()); + if (pageSize != null) searchBuilder.addAttributes("pageSize", pageSize.getAsInt()); JsonElement type = header.get("type"); - if(type != null) searchBuilder.addAttributes("type", type.getAsString()); + if (type != null) searchBuilder.addAttributes("type", type.getAsString()); doipMessage = searchBuilder.create(); break; @@ -135,13 +113,14 @@ public class DOOPBeforeExecHandler implements AnnotationHook { doipMessage = new DoipMessageFactory.DoipMessageBuilder().createRequest(header.get("identifier").getAsString(), httpOperation.getName()).create(); JsonElement element = header.get("element"); JsonElement includeElementData = header.get("includeElementData"); - if(element != null) doipMessage.header.parameters.addAttribute("element", element.getAsString()); - if(includeElementData != null && includeElementData.getAsBoolean()) doipMessage.header.parameters.addAttribute("includeElementData", "true"); + if (element != null) doipMessage.header.parameters.addAttribute("element", element.getAsString()); + if (includeElementData != null && includeElementData.getAsBoolean()) + doipMessage.header.parameters.addAttribute("includeElementData", "true"); break; case Extension: DoipMessageFactory.DoipMessageBuilder extensionBuilder = new DoipMessageFactory.DoipMessageBuilder(); - if(header != null) { - if(header.get("identifier") != null) { + if (header != null) { + if (header.get("identifier") != null) { extensionBuilder = extensionBuilder .createRequest(header.get("identifier").getAsString(), httpOperation.getName()); } @@ -152,7 +131,7 @@ public class DOOPBeforeExecHandler implements AnnotationHook { } } - if(body != null) { + if (body != null) { extensionBuilder.setBody(body.getBytes(StandardCharsets.UTF_8)); } diff --git a/src/main/java/org/bdware/sc/handler/DOOPRequestHandler.java b/src/main/java/org/bdware/sc/handler/DOOPRequestHandler.java index bef2021..8d49723 100644 --- a/src/main/java/org/bdware/sc/handler/DOOPRequestHandler.java +++ b/src/main/java/org/bdware/sc/handler/DOOPRequestHandler.java @@ -1,6 +1,5 @@ package org.bdware.sc.handler; -import com.google.gson.Gson; import com.google.gson.JsonObject; import io.netty.channel.ChannelHandlerContext; import org.apache.logging.log4j.LogManager; @@ -21,22 +20,11 @@ import java.util.HashMap; import java.util.Map; public class DOOPRequestHandler implements DoipRequestHandler, RepositoryHandler { - public Map doipFunctionNodeMap; static Logger logger = LogManager.getLogger(NettyServerHandler.class); - static Gson gson; - - public static DOOPRequestHandler instance; + public Map doipFunctionNodeMap; public DOOPRequestHandler() { doipFunctionNodeMap = new HashMap<>(); - gson = new Gson(); - } - - public static DOOPRequestHandler createHandler() { - if(instance == null) { - instance = new DOOPRequestHandler(); - } - return instance; } public void addDoipOperation(FunctionNode function) throws Exception { @@ -145,10 +133,10 @@ public class DOOPRequestHandler implements DoipRequestHandler, RepositoryHandler Object ret = ContractProcess.instance.engine.executeWithoutLock(fn, contractRequest, arg); DoipMessage finalDoipMsg = (DoipMessage) ret; finalDoipMsg.requestID = msg.requestID; - if(finalDoipMsg.header.parameters == null) { + if (finalDoipMsg.header.parameters == null) { finalDoipMsg.header.parameters = new HeaderParameter("", ""); } - if(finalDoipMsg.header.parameters.attributes == null) { + if (finalDoipMsg.header.parameters.attributes == null) { finalDoipMsg.header.parameters.attributes = new JsonObject(); } finalDoipMsg.header.parameters.attributes.addProperty("nodeID", String.valueOf(JavaScriptEntry.shardingID)); @@ -162,9 +150,9 @@ public class DOOPRequestHandler implements DoipRequestHandler, RepositoryHandler } public ContractRequest constructContractRequest(FunctionNode fn, DoipMessage request) { - ContractRequest cr = new ContractRequest(); + ContractRequest cr = new ContractRequest(); cr.setContractID(""); - if(request.credential == null) { + if (request.credential == null) { cr.setRequester(null); } else { cr.setRequester(request.credential.getSigner()); diff --git a/src/main/java/org/bdware/sc/server/DoipLocalSingleton.java b/src/main/java/org/bdware/sc/server/DoipLocalSingleton.java index db7165b..54c2845 100644 --- a/src/main/java/org/bdware/sc/server/DoipLocalSingleton.java +++ b/src/main/java/org/bdware/sc/server/DoipLocalSingleton.java @@ -6,6 +6,7 @@ import org.bdware.doip.endpoint.server.DoipListenerConfig; import org.bdware.doip.endpoint.server.DoipServerImpl; import org.bdware.doip.endpoint.server.DoipServiceInfo; import org.bdware.doip.endpoint.server.StartServerCallback; +import org.bdware.sc.ContractProcess; import org.bdware.sc.handler.DOOPRequestHandler; import java.net.URI; @@ -54,7 +55,7 @@ public class DoipLocalSingleton { } DoipServiceInfo info = new DoipServiceInfo("aibd.govdata.tj/do.3f9c41e6-9f8e-48a0-9220-53f438d40e43", "ownerDEF", "gateRepo", infos); server = new DoipServerImpl(info); - DOOPRequestHandler handler = DOOPRequestHandler.createHandler(); + DOOPRequestHandler handler = ContractProcess.instance.doopRequestHandler; server.setRepositoryHandler(handler); ResultChecker checker = new ResultChecker(); server.start(checker);