From 9686de6ba9a48a4e412af65ce637558b435efa88 Mon Sep 17 00:00:00 2001 From: "haoeliu@foxmail.com" Date: Wed, 16 Nov 2022 14:58:26 +0800 Subject: [PATCH] add doipOperationMap in DOOPRequestHandler and merge DOOPRepoHandler --- .../java/org/bdware/sc/compiler/ap/DOOP.java | 17 ++- .../sc/handler/DOOPRepositoryHandler.java | 71 ---------- .../bdware/sc/handler/DOOPRequestHandler.java | 127 +++++++++++------- .../org/bdware/sc/server/DOOPDoipServer.java | 5 - 4 files changed, 97 insertions(+), 123 deletions(-) delete mode 100644 src/main/java/org/bdware/sc/handler/DOOPRepositoryHandler.java 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 806a2f7..6529401 100644 --- a/src/main/java/org/bdware/sc/compiler/ap/DOOP.java +++ b/src/main/java/org/bdware/sc/compiler/ap/DOOP.java @@ -1,19 +1,32 @@ package org.bdware.sc.compiler.ap; +import org.bdware.doip.codec.operations.BasicOperations; import org.bdware.sc.bean.DoipOperationInfo; import org.bdware.sc.compiler.AnnotationProcessor; +import org.bdware.sc.handler.DOOPRequestHandler; import org.bdware.sc.node.AnnotationNode; import org.bdware.sc.node.ContractNode; import org.bdware.sc.node.FunctionNode; +import java.util.List; + // DOOP is designed for DoipModule which contains specific functions for RepositoryHandler public class DOOP extends AnnotationProcessor { @Override public void processFunction(AnnotationNode anno, ContractNode contractNode, FunctionNode functionNode) { // 通过DOOP注解,解析对应的值,并放进对应的FunctionNode中 + // 注解必须暴露出来昂!!! + functionNode.setIsExport(true); functionNode.setDoipOperationInfo(DoipOperationInfo.create(anno, contractNode)); - // 维护DoipOperation维 - contractNode.addDoipOperation(functionNode); + + // 维护DOOPRequestHandler + if(DOOPRequestHandler.instance == null) { + new DOOPRequestHandler(); + } + DOOPRequestHandler.instance.addDoipOperation(functionNode); + + // 维护ContractNode,functionName is useless, use BasicOperation to map the corresponding functionNode + contractNode.updateFunctionMap(functionNode.functionName, functionNode.getDoipOperationInfo().operationType.toString()); } } diff --git a/src/main/java/org/bdware/sc/handler/DOOPRepositoryHandler.java b/src/main/java/org/bdware/sc/handler/DOOPRepositoryHandler.java deleted file mode 100644 index f67df99..0000000 --- a/src/main/java/org/bdware/sc/handler/DOOPRepositoryHandler.java +++ /dev/null @@ -1,71 +0,0 @@ -package org.bdware.sc.handler; - -import com.google.gson.JsonPrimitive; -import org.bdware.doip.codec.doipMessage.DoipMessage; -import org.bdware.doip.codec.operations.BasicOperations; -import org.bdware.doip.endpoint.server.RepositoryHandler; -import org.bdware.sc.ContractProcess; -import org.bdware.sc.ContractResult; -import org.bdware.sc.node.ContractNode; -import org.bdware.sc.node.FunctionNode; -import org.bdware.sc.node.YjsType; - -public class DOOPRepositoryHandler implements RepositoryHandler { - private ContractNode cn; - private boolean isDoipModule; - - public DOOPRepositoryHandler(ContractNode cn) { - if(cn.getYjsType() == YjsType.DoipModule) { - isDoipModule = true; - this.cn = cn; - } - } - - @Override - public DoipMessage handleHello(DoipMessage request) { - FunctionNode fn = cn.getDoipOperation(BasicOperations.Hello); - ContractProcess.instance.executeContract(""); - return null; - } - - @Override - public DoipMessage handleListOps(DoipMessage request) { - FunctionNode fn = cn.getDoipOperation(BasicOperations.ListOps); - - return null; - } - - @Override - public DoipMessage handleCreate(DoipMessage request) { - FunctionNode fn = cn.getDoipOperation(BasicOperations.Create); - return null; - } - - @Override - public DoipMessage handleUpdate(DoipMessage request) { - FunctionNode fn = cn.getDoipOperation(BasicOperations.Update); - return null; - } - - @Override - public DoipMessage handleDelete(DoipMessage request) { - FunctionNode fn = cn.getDoipOperation(BasicOperations.Delete); - return null; - } - - @Override - public DoipMessage handleRetrieve(DoipMessage request) { - FunctionNode fn = cn.getDoipOperation(BasicOperations.Retrieve); - return null; - } - - private ContractResult invokeFunction(FunctionNode fun, BasicOperations basicOperation) { - if (fun == null) { - return new ContractResult( - ContractResult.Status.Exception, - new JsonPrimitive("DoipOperation " + basicOperation + " is not exists")); - } - - return null; - } -} diff --git a/src/main/java/org/bdware/sc/handler/DOOPRequestHandler.java b/src/main/java/org/bdware/sc/handler/DOOPRequestHandler.java index 3130380..a40af26 100644 --- a/src/main/java/org/bdware/sc/handler/DOOPRequestHandler.java +++ b/src/main/java/org/bdware/sc/handler/DOOPRequestHandler.java @@ -1,5 +1,7 @@ package org.bdware.sc.handler; +import com.google.gson.Gson; +import com.google.gson.JsonElement; import io.netty.channel.ChannelHandlerContext; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -7,57 +9,33 @@ import org.bdware.doip.codec.doipMessage.DoipMessage; import org.bdware.doip.codec.operations.BasicOperations; import org.bdware.doip.endpoint.server.DoipRequestHandler; import org.bdware.doip.endpoint.server.NettyServerHandler; -import org.bdware.doip.endpoint.server.Op; +import org.bdware.doip.endpoint.server.RepositoryHandler; +import org.bdware.sc.ContractProcess; +import org.bdware.sc.ContractResult; +import org.bdware.sc.bean.ContractRequest; +import org.bdware.sc.node.FunctionNode; import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; -public class DOOPRequestHandler implements DoipRequestHandler { - Map handlers; +public class DOOPRequestHandler implements DoipRequestHandler, RepositoryHandler { + public Map doipOperationsMap; static Logger logger = LogManager.getLogger(NettyServerHandler.class); - protected DOOPRepositoryHandler doopRepoHandler; + Gson gson; - public DOOPRequestHandler(DOOPRepositoryHandler doopRepoHandler) { - handlers = new HashMap<>(); - this.doopRepoHandler = doopRepoHandler; - Class handlerClass = doopRepoHandler.getClass(); + public static DOOPRequestHandler instance; - // 建立doopRepoHandler这个类中,方法名和对应的Method的对应关系 - while (handlerClass != Object.class) { - putDoipHandlerMethod(handlerClass); - Class[] interfaces = handlerClass.getInterfaces(); - for (Class clz : interfaces) { - putDoipHandlerMethod(clz); - } - handlerClass = handlerClass.getSuperclass(); + public DOOPRequestHandler() { + if(instance == null) { + instance = new DOOPRequestHandler(); + instance.doipOperationsMap = new HashMap<>(); + gson = new Gson(); } } - private void putDoipHandlerMethod(Class handlerClass) { - Method[] methods = handlerClass.getDeclaredMethods(); - for (Method m : methods) { - Op a = m.getAnnotation(Op.class); - if (a != null) { -// logger.debug("method annotation: " + a.op().getName()); -// logger.debug("method annotation: " + a.name()); - if (a.op() != BasicOperations.Extension) { - putHandler(a.op().getName(), m); - } else { - putHandler(a.name(), m); - } - } - } - } - - private void putHandler(String name, Method m) { - if (handlers.containsKey(name)) { -// logger.error("Duplicated operation handler:" + name + " methodName:" + m.getName()); - return; - } - logger.debug("[Register operation] name: " + name); - m.setAccessible(true); - handlers.put(name, m); + public void addDoipOperation(FunctionNode function) { + doipOperationsMap.put(function.getDoipOperationInfo().operationType.getName(), function); } @Override @@ -65,12 +43,14 @@ public class DOOPRequestHandler implements DoipRequestHandler { String str = msg.header.parameters.operation; logger.debug("[Call operation] name: " + str); if (str != null) { - Method m; - m = handlers.get(str); - if (m == null) m = handlers.get(BasicOperations.Unknown.getName()); - if (m != null) { + FunctionNode fn; + fn = doipOperationsMap.get(str); + if (fn == null) fn = doipOperationsMap.get(BasicOperations.Unknown.getName()); + if (fn != null) { try { - return (DoipMessage) m.invoke(doopRepoHandler, msg); + ContractRequest contractRequest = constructContractRequest(fn, msg); + ContractResult res = ContractProcess.instance.engine.executeContract(contractRequest); + return gson.fromJson(res.toString(), DoipMessage.class); } catch (Exception e) { e.printStackTrace(); } @@ -78,4 +58,61 @@ public class DOOPRequestHandler implements DoipRequestHandler { } return null; } + + @Override + public DoipMessage handleHello(DoipMessage request) { + FunctionNode fn = doipOperationsMap.get(BasicOperations.Hello.getName()); + ContractRequest cr = constructContractRequest(fn, request); + JsonElement res = ContractProcess.instance.engine.executeContract(cr).result; + return gson.fromJson(res.toString(), DoipMessage.class); + } + + @Override + public DoipMessage handleListOps(DoipMessage request) { + FunctionNode fn = doipOperationsMap.get(BasicOperations.ListOps.getName()); + ContractRequest cr = constructContractRequest(fn, request); + JsonElement res = ContractProcess.instance.engine.executeContract(cr).result; + return gson.fromJson(res.toString(), DoipMessage.class); + } + + @Override + public DoipMessage handleCreate(DoipMessage request) { + FunctionNode fn = doipOperationsMap.get(BasicOperations.Create.getName()); + ContractRequest cr = constructContractRequest(fn, request); + JsonElement res = ContractProcess.instance.engine.executeContract(cr).result; + return gson.fromJson(res.toString(), DoipMessage.class); + } + + @Override + public DoipMessage handleUpdate(DoipMessage request) { + FunctionNode fn = doipOperationsMap.get(BasicOperations.Update.getName()); + ContractRequest cr = constructContractRequest(fn, request); + JsonElement res = ContractProcess.instance.engine.executeContract(cr).result; + return gson.fromJson(res.toString(), DoipMessage.class); + } + + @Override + public DoipMessage handleDelete(DoipMessage request) { + FunctionNode fn = doipOperationsMap.get(BasicOperations.Delete.getName()); + ContractRequest cr = constructContractRequest(fn, request); + JsonElement res = ContractProcess.instance.engine.executeContract(cr).result; + return gson.fromJson(res.toString(), DoipMessage.class); + } + + @Override + public DoipMessage handleRetrieve(DoipMessage request) { + FunctionNode fn = doipOperationsMap.get(BasicOperations.Retrieve.getName()); + ContractRequest cr = constructContractRequest(fn, request); + JsonElement res = ContractProcess.instance.engine.executeContract(cr).result; + return gson.fromJson(res.toString(), DoipMessage.class); + } + + public ContractRequest constructContractRequest(FunctionNode fn, DoipMessage request) { + ContractRequest cr = new ContractRequest(); + cr.setContractID(""); + cr.setRequester(""); + cr.setAction(""); + cr.setArg(""); + return cr; + } } diff --git a/src/main/java/org/bdware/sc/server/DOOPDoipServer.java b/src/main/java/org/bdware/sc/server/DOOPDoipServer.java index fe0db45..b509bc3 100644 --- a/src/main/java/org/bdware/sc/server/DOOPDoipServer.java +++ b/src/main/java/org/bdware/sc/server/DOOPDoipServer.java @@ -3,16 +3,11 @@ package org.bdware.sc.server; import org.bdware.doip.audit.EndpointConfig; import org.bdware.doip.audit.EndpointInfo; import org.bdware.doip.audit.client.AuditIrpClient; -import org.bdware.doip.audit.server.AuditRequestHandler; import org.bdware.doip.audit.writer.ConfigurableAuditConfig; 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.RepositoryHandler; -import org.bdware.sc.ContractProcess; -import org.bdware.sc.handler.DOOPRepositoryHandler; -import org.bdware.sc.handler.DOOPRequestHandler; -import org.bdware.sc.node.ContractNode; import java.lang.reflect.Field; import java.util.ArrayList;