mirror of
https://gitee.com/BDWare/cp.git
synced 2025-01-10 01:44:08 +00:00
add doipOperationMap in DOOPRequestHandler and merge DOOPRepoHandler
This commit is contained in:
parent
37ad6c35d7
commit
9686de6ba9
@ -1,19 +1,32 @@
|
|||||||
package org.bdware.sc.compiler.ap;
|
package org.bdware.sc.compiler.ap;
|
||||||
|
|
||||||
|
import org.bdware.doip.codec.operations.BasicOperations;
|
||||||
import org.bdware.sc.bean.DoipOperationInfo;
|
import org.bdware.sc.bean.DoipOperationInfo;
|
||||||
import org.bdware.sc.compiler.AnnotationProcessor;
|
import org.bdware.sc.compiler.AnnotationProcessor;
|
||||||
|
import org.bdware.sc.handler.DOOPRequestHandler;
|
||||||
import org.bdware.sc.node.AnnotationNode;
|
import org.bdware.sc.node.AnnotationNode;
|
||||||
import org.bdware.sc.node.ContractNode;
|
import org.bdware.sc.node.ContractNode;
|
||||||
import org.bdware.sc.node.FunctionNode;
|
import org.bdware.sc.node.FunctionNode;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
// DOOP is designed for DoipModule which contains specific functions for RepositoryHandler
|
// DOOP is designed for DoipModule which contains specific functions for RepositoryHandler
|
||||||
public class DOOP extends AnnotationProcessor {
|
public class DOOP extends AnnotationProcessor {
|
||||||
@Override
|
@Override
|
||||||
public void processFunction(AnnotationNode anno, ContractNode contractNode, FunctionNode functionNode) {
|
public void processFunction(AnnotationNode anno, ContractNode contractNode, FunctionNode functionNode) {
|
||||||
// 通过DOOP注解,解析对应的值,并放进对应的FunctionNode中
|
// 通过DOOP注解,解析对应的值,并放进对应的FunctionNode中
|
||||||
|
// 注解必须暴露出来昂!!!
|
||||||
|
functionNode.setIsExport(true);
|
||||||
functionNode.setDoipOperationInfo(DoipOperationInfo.create(anno, contractNode));
|
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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,5 +1,7 @@
|
|||||||
package org.bdware.sc.handler;
|
package org.bdware.sc.handler;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import com.google.gson.JsonElement;
|
||||||
import io.netty.channel.ChannelHandlerContext;
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
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.codec.operations.BasicOperations;
|
||||||
import org.bdware.doip.endpoint.server.DoipRequestHandler;
|
import org.bdware.doip.endpoint.server.DoipRequestHandler;
|
||||||
import org.bdware.doip.endpoint.server.NettyServerHandler;
|
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.lang.reflect.Method;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public class DOOPRequestHandler implements DoipRequestHandler {
|
public class DOOPRequestHandler implements DoipRequestHandler, RepositoryHandler {
|
||||||
Map<String, Method> handlers;
|
public Map<String, FunctionNode> doipOperationsMap;
|
||||||
static Logger logger = LogManager.getLogger(NettyServerHandler.class);
|
static Logger logger = LogManager.getLogger(NettyServerHandler.class);
|
||||||
protected DOOPRepositoryHandler doopRepoHandler;
|
Gson gson;
|
||||||
|
|
||||||
public DOOPRequestHandler(DOOPRepositoryHandler doopRepoHandler) {
|
public static DOOPRequestHandler instance;
|
||||||
handlers = new HashMap<>();
|
|
||||||
this.doopRepoHandler = doopRepoHandler;
|
|
||||||
Class handlerClass = doopRepoHandler.getClass();
|
|
||||||
|
|
||||||
// 建立doopRepoHandler这个类中,方法名和对应的Method的对应关系
|
public DOOPRequestHandler() {
|
||||||
while (handlerClass != Object.class) {
|
if(instance == null) {
|
||||||
putDoipHandlerMethod(handlerClass);
|
instance = new DOOPRequestHandler();
|
||||||
Class[] interfaces = handlerClass.getInterfaces();
|
instance.doipOperationsMap = new HashMap<>();
|
||||||
for (Class clz : interfaces) {
|
gson = new Gson();
|
||||||
putDoipHandlerMethod(clz);
|
|
||||||
}
|
|
||||||
handlerClass = handlerClass.getSuperclass();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void putDoipHandlerMethod(Class handlerClass) {
|
public void addDoipOperation(FunctionNode function) {
|
||||||
Method[] methods = handlerClass.getDeclaredMethods();
|
doipOperationsMap.put(function.getDoipOperationInfo().operationType.getName(), function);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -65,12 +43,14 @@ public class DOOPRequestHandler implements DoipRequestHandler {
|
|||||||
String str = msg.header.parameters.operation;
|
String str = msg.header.parameters.operation;
|
||||||
logger.debug("[Call operation] name: " + str);
|
logger.debug("[Call operation] name: " + str);
|
||||||
if (str != null) {
|
if (str != null) {
|
||||||
Method m;
|
FunctionNode fn;
|
||||||
m = handlers.get(str);
|
fn = doipOperationsMap.get(str);
|
||||||
if (m == null) m = handlers.get(BasicOperations.Unknown.getName());
|
if (fn == null) fn = doipOperationsMap.get(BasicOperations.Unknown.getName());
|
||||||
if (m != null) {
|
if (fn != null) {
|
||||||
try {
|
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) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
@ -78,4 +58,61 @@ public class DOOPRequestHandler implements DoipRequestHandler {
|
|||||||
}
|
}
|
||||||
return null;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,16 +3,11 @@ package org.bdware.sc.server;
|
|||||||
import org.bdware.doip.audit.EndpointConfig;
|
import org.bdware.doip.audit.EndpointConfig;
|
||||||
import org.bdware.doip.audit.EndpointInfo;
|
import org.bdware.doip.audit.EndpointInfo;
|
||||||
import org.bdware.doip.audit.client.AuditIrpClient;
|
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.audit.writer.ConfigurableAuditConfig;
|
||||||
import org.bdware.doip.endpoint.server.DoipListenerConfig;
|
import org.bdware.doip.endpoint.server.DoipListenerConfig;
|
||||||
import org.bdware.doip.endpoint.server.DoipServerImpl;
|
import org.bdware.doip.endpoint.server.DoipServerImpl;
|
||||||
import org.bdware.doip.endpoint.server.DoipServiceInfo;
|
import org.bdware.doip.endpoint.server.DoipServiceInfo;
|
||||||
import org.bdware.doip.endpoint.server.RepositoryHandler;
|
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.lang.reflect.Field;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
Loading…
Reference in New Issue
Block a user