params -> bytestring
This commit is contained in:
snoop 2017-06-30 14:32:36 +09:00
parent 1e840ef024
commit bdd1f31c2b
2 changed files with 152 additions and 83 deletions

View File

@ -1,5 +1,6 @@
package com.loafle.overflow.proxy;
import com.google.protobuf.ByteString;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.aop.support.AopUtils;
@ -19,16 +20,17 @@ import java.util.Map;
/**
* Created by crusader on 17. 6. 30.
*/
@Component
//@Component
public class ServiceInvoker {
@Autowired
// @Autowired
private ApplicationContext context;
private ObjectMapper objectMapper;
private Map<String, Cache> serviceCacheMap;
private ServiceInvoker() {
public ServiceInvoker(ApplicationContext context) {
this.context = context;
objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
@ -176,6 +178,24 @@ public class ServiceInvoker {
return result;
}
private Object[] getParametersForByteString(Cache.ParameterCache[] parameterCaches, List<ByteString> params) throws IOException {
if (null == parameterCaches || null == params) {
return null;
}
if (parameterCaches.length != params.size()) {
throw new IllegalArgumentException();
}
Object[] result = new Object[parameterCaches.length];
for (int i = 0; i < parameterCaches.length; i++) {
result[i] = getValue(parameterCaches[i], params.get(i).toStringUtf8());
}
return result;
}
public String invoke(String serviceName, String methodName, List<String> params) throws NoSuchMethodException, ClassNotFoundException, IOException, InvocationTargetException, IllegalAccessException {
Cache serviceCache = getServiceCache(serviceName);
Cache.MethodCache methodCache = serviceCache.getMethodCache(methodName);
@ -191,4 +211,20 @@ public class ServiceInvoker {
return jsonInString;
}
public String invokeForByteString(String serviceName, String methodName, List<ByteString> params) throws NoSuchMethodException, ClassNotFoundException, IOException, InvocationTargetException, IllegalAccessException {
Cache serviceCache = getServiceCache(serviceName);
Cache.MethodCache methodCache = serviceCache.getMethodCache(methodName);
Object[] parameters = null;
if (null != methodCache.parameterCaches && 0 < methodCache.parameterCaches.length) {
parameters = getParametersForByteString(methodCache.parameterCaches, params);
}
Object result = methodCache.method.invoke(serviceCache.getBean(), parameters);
String jsonInString = objectMapper.writeValueAsString(result);
return jsonInString;
}
}

View File

@ -1,5 +1,6 @@
package com.loafle.overflow.proxy;
import com.google.protobuf.ByteString;
import com.loafle.overflow.api.OverflowApiServerGrpc;
import com.loafle.overflow.api.ServerInput;
import com.loafle.overflow.api.ServerOutput;
@ -7,10 +8,12 @@ import com.loafle.overflow.api.ServerParam;
import io.grpc.ServerBuilder;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
@ -23,16 +26,14 @@ public class ServiceProxy {
private static final Logger logger = Logger.getLogger(ServiceProxy.class.getName());
private io.grpc.Server server;
public static ApplicationContext ctx;
public ServiceProxy() {
}
private ApplicationContext ctx;
public void start(int port) throws IOException {
ctx = new AnnotationConfigApplicationContext("com.loafle.overflow");
server = ServerBuilder.forPort(port)
.addService(new ServiceImpl())
.addService(new ServiceImpl(new ServiceInvoker(ctx)))
.build()
.start();
logger.info("Server started, listening on " + port);
@ -60,87 +61,119 @@ public class ServiceProxy {
}
static class ServiceImpl extends OverflowApiServerGrpc.OverflowApiServerImplBase {
private ServiceInvoker serviceInvoker;
ServiceImpl(ServiceInvoker serviceInvoker) {
this.serviceInvoker = serviceInvoker;
}
@Override
public void exec(ServerInput request,
io.grpc.stub.StreamObserver<ServerOutput> responseObserver) {
String targetServiceName = request.getTarget();
Object service = ctx.getBean(targetServiceName);
if(service != null) {
try {
String jsonResult = process(request, service);
ServerOutput reply = ServerOutput.newBuilder()
.setResult(jsonResult)
.build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
}catch(Exception e) {
e.printStackTrace();
responseObserver.onError(e);
}
}else {
responseObserver.onError(new Exception("Not assigned Service :" + service));
String result = null;
try {
result =this.serviceInvoker.invokeForByteString(request.getTarget(), request.getMethod(), request.getParamsList().asByteStringList());
} catch (NoSuchMethodException e) {
e.printStackTrace();
responseObserver.onError(e);
} catch (ClassNotFoundException e) {
e.printStackTrace();
responseObserver.onError(e);
} catch (IOException e) {
e.printStackTrace();
responseObserver.onError(e);
} catch (InvocationTargetException e) {
e.printStackTrace();
responseObserver.onError(e);
} catch (IllegalAccessException e) {
e.printStackTrace();
responseObserver.onError(e);
}
ServerOutput reply = ServerOutput.newBuilder()
.setResult(result)
.build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
// String targetServiceName = request.getTarget();
// Object service = ctx.getBean(targetServiceName);
//
// if(service != null) {
// try {
//
// String jsonResult = process(request, service);
//
// ServerOutput reply = ServerOutput.newBuilder()
// .setResult(jsonResult)
// .build();
// responseObserver.onNext(reply);
// responseObserver.onCompleted();
// }catch(Exception e) {
// e.printStackTrace();
// responseObserver.onError(e);
// }
//
// }else {
// responseObserver.onError(new Exception("Not assigned Service :" + service));
// }
}
private String process(ServerInput request, Object service) throws Exception {
String methodName = request.getMethod();
List<ServerParam> params = request.getParamsList();
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Object retObj = null;
List<Class> paramTypes = new ArrayList<Class>();
List<Object> valueList = new ArrayList<Object>();
for( ServerParam param : params ){
if(false == param.getIsCollection()) {
Class<?> cls = Class.forName(param.getType());
Object obj = mapper.readValue(param.getData(), cls);
paramTypes.add(cls);
valueList.add(obj);
}else {
String type = param.getType();
int idx = type.indexOf("|");
String firstClassName = type.substring(0, idx);
String lastClassName = type.substring(idx+1);
Class firstCls = Class.forName(firstClassName);
Class<?> lastCls = Class.forName(lastClassName);
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Object obj = mapper.readValue(param.getData(), mapper.getTypeFactory().constructCollectionType(firstCls, lastCls));
paramTypes.add(firstCls);
valueList.add(obj);
}
}
Method method = null;
if(params.size() > 0) {
method = MethodSeeker.getMethod(service.getClass(),methodName, paramTypes);
if (method == null) {
throw new Exception("Not found method : " + methodName);
}
retObj = method.invoke(service, valueList.toArray(new Object[valueList.size()]));
}
else {
method = service.getClass().getMethod(methodName);
retObj = method.invoke(service);
}
if(retObj == null) {
return "";
}
return mapper.writeValueAsString(retObj);
}
// private String process(ServerInput request, Object service) throws Exception {
//
// String methodName = request.getMethod();
// List<ServerParam> params = request.getParamsList();
// ObjectMapper mapper = new ObjectMapper();
// mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
//
// Object retObj = null;
//
// List<Class> paramTypes = new ArrayList<Class>();
// List<Object> valueList = new ArrayList<Object>();
//
// for( ServerParam param : params ){
//
// if(false == param.getIsCollection()) {
// Class<?> cls = Class.forName(param.getType());
// Object obj = mapper.readValue(param.getData(), cls);
// paramTypes.add(cls);
// valueList.add(obj);
// }else {
// String type = param.getType();
// int idx = type.indexOf("|");
// String firstClassName = type.substring(0, idx);
// String lastClassName = type.substring(idx+1);
// Class firstCls = Class.forName(firstClassName);
// Class<?> lastCls = Class.forName(lastClassName);
// mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// Object obj = mapper.readValue(param.getData(), mapper.getTypeFactory().constructCollectionType(firstCls, lastCls));
//
// paramTypes.add(firstCls);
// valueList.add(obj);
// }
//
//
// }
// Method method = null;
// if(params.size() > 0) {
// method = MethodSeeker.getMethod(service.getClass(),methodName, paramTypes);
// if (method == null) {
// throw new Exception("Not found method : " + methodName);
// }
// retObj = method.invoke(service, valueList.toArray(new Object[valueList.size()]));
// }
// else {
// method = service.getClass().getMethod(methodName);
// retObj = method.invoke(service);
// }
//
// if(retObj == null) {
// return "";
// }
//
// return mapper.writeValueAsString(retObj);
// }
}
}