From 0267ae3617ed26312a22b680940cf6a4714c444c Mon Sep 17 00:00:00 2001 From: snoop Date: Wed, 28 Jun 2017 18:50:42 +0900 Subject: [PATCH] fixed servcice Proxy --- src/main/java/com/loafle/overflow/OFMain.java | 18 +++++- .../loafle/overflow/proxy/MethodSeeker.java | 37 +++++++++++++ .../loafle/overflow/proxy/ServiceProxy.java | 55 ++++++++++++++++--- 3 files changed, 99 insertions(+), 11 deletions(-) create mode 100644 src/main/java/com/loafle/overflow/proxy/MethodSeeker.java diff --git a/src/main/java/com/loafle/overflow/OFMain.java b/src/main/java/com/loafle/overflow/OFMain.java index 7fbecb6..52afa95 100644 --- a/src/main/java/com/loafle/overflow/OFMain.java +++ b/src/main/java/com/loafle/overflow/OFMain.java @@ -1,7 +1,21 @@ package com.loafle.overflow; +import com.loafle.overflow.proxy.ServiceProxy; + +import java.io.IOException; + public class OFMain { - public static void main(String[] args) { - System.out.println("Hello World!"); + public static void main(String[] args) throws IOException, InterruptedException { + if(args.length <= 0) { + System.out.println("Port args"); + System.out.println("first parameter is Port Number"); + return; + } + + int port = Integer.valueOf(args[0]); + + final ServiceProxy server = new ServiceProxy(); + server.start(port); + server.blockUntilShutdown(); } } \ No newline at end of file diff --git a/src/main/java/com/loafle/overflow/proxy/MethodSeeker.java b/src/main/java/com/loafle/overflow/proxy/MethodSeeker.java new file mode 100644 index 0000000..55388d0 --- /dev/null +++ b/src/main/java/com/loafle/overflow/proxy/MethodSeeker.java @@ -0,0 +1,37 @@ +package com.loafle.overflow.proxy; + +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; + +/** + * Created by root on 17. 6. 23. + */ +public class MethodSeeker { + + public static Method getMethod(Class cls, String methodName, List paramTypes) { + + Method method = null; + + if(paramTypes != null) { + method = MethodSeeker.getMethodType(cls, methodName, paramTypes.toArray(new Class[paramTypes.size()])); + } + + return method; + + } + + private static Method getMethodType(Class jpa, String methodName, Class ...type) { + if (jpa == null) { + return null; + } + Method m = null; + try { + m = jpa.getMethod(methodName, type); + } catch (NoSuchMethodException e) { + return MethodSeeker.getMethodType(jpa.getSuperclass(), methodName, type); + } + + return m; + } +} diff --git a/src/main/java/com/loafle/overflow/proxy/ServiceProxy.java b/src/main/java/com/loafle/overflow/proxy/ServiceProxy.java index 80a3221..efc67ac 100644 --- a/src/main/java/com/loafle/overflow/proxy/ServiceProxy.java +++ b/src/main/java/com/loafle/overflow/proxy/ServiceProxy.java @@ -15,11 +15,14 @@ import com.loafle.overflow.module.sensor.service.SensorItemService; import com.loafle.overflow.module.sensor.service.SensorService; import com.loafle.overflow.module.target.service.TargetService; import io.grpc.ServerBuilder; +import org.codehaus.jackson.map.DeserializationConfig; import org.codehaus.jackson.map.ObjectMapper; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import java.io.IOException; +import java.lang.reflect.Method; +import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -120,18 +123,52 @@ public class ServiceProxy { String methodName = request.getMethod(); List params = request.getParamsList(); ObjectMapper mapper = new ObjectMapper(); + mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); + Object retObj = null; - //nigagara hawai - //nigagara hawai - //nigagara hawai - //nigagara hawai - //nigagara hawai - //nigagara hawai - //nigagara hawai - //nigagara hawai - //nigagara hawai + 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); }