servcice Proxy
This commit is contained in:
snoop 2017-06-28 18:50:42 +09:00
parent 4d6ff514b5
commit 0267ae3617
3 changed files with 99 additions and 11 deletions

View File

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

View File

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

View File

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