add doipOperationMap in DOOPRequestHandler and merge DOOPRepoHandler

This commit is contained in:
haoeliu@foxmail.com 2022-11-16 14:58:26 +08:00
parent 37ad6c35d7
commit 9686de6ba9
4 changed files with 97 additions and 123 deletions

View File

@ -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);
// 维护ContractNodefunctionName is useless, use BasicOperation to map the corresponding functionNode
contractNode.updateFunctionMap(functionNode.functionName, functionNode.getDoipOperationInfo().operationType.toString());
}
}

View File

@ -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;
}
}

View File

@ -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<String, Method> handlers;
public class DOOPRequestHandler implements DoipRequestHandler, RepositoryHandler {
public Map<String, FunctionNode> 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;
}
}

View File

@ -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;