From bdd1f31c2ba10a351bf9681dd2d1d1a5bad726e5 Mon Sep 17 00:00:00 2001 From: snoop Date: Fri, 30 Jun 2017 14:32:36 +0900 Subject: [PATCH] fixed params -> bytestring --- .../loafle/overflow/proxy/ServiceInvoker.java | 42 +++- .../loafle/overflow/proxy/ServiceProxy.java | 193 ++++++++++-------- 2 files changed, 152 insertions(+), 83 deletions(-) diff --git a/src/main/java/com/loafle/overflow/proxy/ServiceInvoker.java b/src/main/java/com/loafle/overflow/proxy/ServiceInvoker.java index 12d6d26..1a7073b 100644 --- a/src/main/java/com/loafle/overflow/proxy/ServiceInvoker.java +++ b/src/main/java/com/loafle/overflow/proxy/ServiceInvoker.java @@ -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 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 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 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 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; + } } diff --git a/src/main/java/com/loafle/overflow/proxy/ServiceProxy.java b/src/main/java/com/loafle/overflow/proxy/ServiceProxy.java index a384666..8123157 100644 --- a/src/main/java/com/loafle/overflow/proxy/ServiceProxy.java +++ b/src/main/java/com/loafle/overflow/proxy/ServiceProxy.java @@ -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 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 params = request.getParamsList(); - ObjectMapper mapper = new ObjectMapper(); - mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); - - Object retObj = null; - - List paramTypes = new ArrayList(); - List valueList = new ArrayList(); - - 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 params = request.getParamsList(); +// ObjectMapper mapper = new ObjectMapper(); +// mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); +// +// Object retObj = null; +// +// List paramTypes = new ArrayList(); +// List valueList = new ArrayList(); +// +// 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); +// } } }