fixed
params -> bytestring
This commit is contained in:
parent
1e840ef024
commit
bdd1f31c2b
|
@ -1,5 +1,6 @@
|
||||||
package com.loafle.overflow.proxy;
|
package com.loafle.overflow.proxy;
|
||||||
|
|
||||||
|
import com.google.protobuf.ByteString;
|
||||||
import org.codehaus.jackson.map.DeserializationConfig;
|
import org.codehaus.jackson.map.DeserializationConfig;
|
||||||
import org.codehaus.jackson.map.ObjectMapper;
|
import org.codehaus.jackson.map.ObjectMapper;
|
||||||
import org.springframework.aop.support.AopUtils;
|
import org.springframework.aop.support.AopUtils;
|
||||||
|
@ -19,16 +20,17 @@ import java.util.Map;
|
||||||
/**
|
/**
|
||||||
* Created by crusader on 17. 6. 30.
|
* Created by crusader on 17. 6. 30.
|
||||||
*/
|
*/
|
||||||
@Component
|
//@Component
|
||||||
public class ServiceInvoker {
|
public class ServiceInvoker {
|
||||||
@Autowired
|
// @Autowired
|
||||||
private ApplicationContext context;
|
private ApplicationContext context;
|
||||||
|
|
||||||
private ObjectMapper objectMapper;
|
private ObjectMapper objectMapper;
|
||||||
|
|
||||||
private Map<String, Cache> serviceCacheMap;
|
private Map<String, Cache> serviceCacheMap;
|
||||||
|
|
||||||
private ServiceInvoker() {
|
public ServiceInvoker(ApplicationContext context) {
|
||||||
|
this.context = context;
|
||||||
objectMapper = new ObjectMapper();
|
objectMapper = new ObjectMapper();
|
||||||
objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||||
|
|
||||||
|
@ -176,6 +178,24 @@ public class ServiceInvoker {
|
||||||
return result;
|
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 {
|
public String invoke(String serviceName, String methodName, List<String> params) throws NoSuchMethodException, ClassNotFoundException, IOException, InvocationTargetException, IllegalAccessException {
|
||||||
Cache serviceCache = getServiceCache(serviceName);
|
Cache serviceCache = getServiceCache(serviceName);
|
||||||
Cache.MethodCache methodCache = serviceCache.getMethodCache(methodName);
|
Cache.MethodCache methodCache = serviceCache.getMethodCache(methodName);
|
||||||
|
@ -191,4 +211,20 @@ public class ServiceInvoker {
|
||||||
|
|
||||||
return jsonInString;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package com.loafle.overflow.proxy;
|
package com.loafle.overflow.proxy;
|
||||||
|
|
||||||
|
import com.google.protobuf.ByteString;
|
||||||
import com.loafle.overflow.api.OverflowApiServerGrpc;
|
import com.loafle.overflow.api.OverflowApiServerGrpc;
|
||||||
import com.loafle.overflow.api.ServerInput;
|
import com.loafle.overflow.api.ServerInput;
|
||||||
import com.loafle.overflow.api.ServerOutput;
|
import com.loafle.overflow.api.ServerOutput;
|
||||||
|
@ -7,10 +8,12 @@ import com.loafle.overflow.api.ServerParam;
|
||||||
import io.grpc.ServerBuilder;
|
import io.grpc.ServerBuilder;
|
||||||
import org.codehaus.jackson.map.DeserializationConfig;
|
import org.codehaus.jackson.map.DeserializationConfig;
|
||||||
import org.codehaus.jackson.map.ObjectMapper;
|
import org.codehaus.jackson.map.ObjectMapper;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -23,16 +26,14 @@ public class ServiceProxy {
|
||||||
private static final Logger logger = Logger.getLogger(ServiceProxy.class.getName());
|
private static final Logger logger = Logger.getLogger(ServiceProxy.class.getName());
|
||||||
|
|
||||||
private io.grpc.Server server;
|
private io.grpc.Server server;
|
||||||
public static ApplicationContext ctx;
|
private ApplicationContext ctx;
|
||||||
|
|
||||||
public ServiceProxy() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void start(int port) throws IOException {
|
public void start(int port) throws IOException {
|
||||||
ctx = new AnnotationConfigApplicationContext("com.loafle.overflow");
|
ctx = new AnnotationConfigApplicationContext("com.loafle.overflow");
|
||||||
|
|
||||||
|
|
||||||
server = ServerBuilder.forPort(port)
|
server = ServerBuilder.forPort(port)
|
||||||
.addService(new ServiceImpl())
|
.addService(new ServiceImpl(new ServiceInvoker(ctx)))
|
||||||
.build()
|
.build()
|
||||||
.start();
|
.start();
|
||||||
logger.info("Server started, listening on " + port);
|
logger.info("Server started, listening on " + port);
|
||||||
|
@ -60,87 +61,119 @@ public class ServiceProxy {
|
||||||
}
|
}
|
||||||
|
|
||||||
static class ServiceImpl extends OverflowApiServerGrpc.OverflowApiServerImplBase {
|
static class ServiceImpl extends OverflowApiServerGrpc.OverflowApiServerImplBase {
|
||||||
|
|
||||||
|
private ServiceInvoker serviceInvoker;
|
||||||
|
|
||||||
|
ServiceImpl(ServiceInvoker serviceInvoker) {
|
||||||
|
this.serviceInvoker = serviceInvoker;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void exec(ServerInput request,
|
public void exec(ServerInput request,
|
||||||
io.grpc.stub.StreamObserver<ServerOutput> responseObserver) {
|
io.grpc.stub.StreamObserver<ServerOutput> responseObserver) {
|
||||||
|
|
||||||
String targetServiceName = request.getTarget();
|
String result = null;
|
||||||
Object service = ctx.getBean(targetServiceName);
|
try {
|
||||||
|
result =this.serviceInvoker.invokeForByteString(request.getTarget(), request.getMethod(), request.getParamsList().asByteStringList());
|
||||||
if(service != null) {
|
} catch (NoSuchMethodException e) {
|
||||||
try {
|
e.printStackTrace();
|
||||||
|
responseObserver.onError(e);
|
||||||
String jsonResult = process(request, service);
|
} catch (ClassNotFoundException e) {
|
||||||
|
e.printStackTrace();
|
||||||
ServerOutput reply = ServerOutput.newBuilder()
|
responseObserver.onError(e);
|
||||||
.setResult(jsonResult)
|
} catch (IOException e) {
|
||||||
.build();
|
e.printStackTrace();
|
||||||
responseObserver.onNext(reply);
|
responseObserver.onError(e);
|
||||||
responseObserver.onCompleted();
|
} catch (InvocationTargetException e) {
|
||||||
}catch(Exception e) {
|
e.printStackTrace();
|
||||||
e.printStackTrace();
|
responseObserver.onError(e);
|
||||||
responseObserver.onError(e);
|
} catch (IllegalAccessException e) {
|
||||||
}
|
e.printStackTrace();
|
||||||
|
responseObserver.onError(e);
|
||||||
}else {
|
|
||||||
responseObserver.onError(new Exception("Not assigned Service :" + service));
|
|
||||||
}
|
}
|
||||||
|
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 {
|
// private String process(ServerInput request, Object service) throws Exception {
|
||||||
|
//
|
||||||
String methodName = request.getMethod();
|
// String methodName = request.getMethod();
|
||||||
List<ServerParam> params = request.getParamsList();
|
// List<ServerParam> params = request.getParamsList();
|
||||||
ObjectMapper mapper = new ObjectMapper();
|
// ObjectMapper mapper = new ObjectMapper();
|
||||||
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
// mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||||
|
//
|
||||||
Object retObj = null;
|
// Object retObj = null;
|
||||||
|
//
|
||||||
List<Class> paramTypes = new ArrayList<Class>();
|
// List<Class> paramTypes = new ArrayList<Class>();
|
||||||
List<Object> valueList = new ArrayList<Object>();
|
// List<Object> valueList = new ArrayList<Object>();
|
||||||
|
//
|
||||||
for( ServerParam param : params ){
|
// for( ServerParam param : params ){
|
||||||
|
//
|
||||||
if(false == param.getIsCollection()) {
|
// if(false == param.getIsCollection()) {
|
||||||
Class<?> cls = Class.forName(param.getType());
|
// Class<?> cls = Class.forName(param.getType());
|
||||||
Object obj = mapper.readValue(param.getData(), cls);
|
// Object obj = mapper.readValue(param.getData(), cls);
|
||||||
paramTypes.add(cls);
|
// paramTypes.add(cls);
|
||||||
valueList.add(obj);
|
// valueList.add(obj);
|
||||||
}else {
|
// }else {
|
||||||
String type = param.getType();
|
// String type = param.getType();
|
||||||
int idx = type.indexOf("|");
|
// int idx = type.indexOf("|");
|
||||||
String firstClassName = type.substring(0, idx);
|
// String firstClassName = type.substring(0, idx);
|
||||||
String lastClassName = type.substring(idx+1);
|
// String lastClassName = type.substring(idx+1);
|
||||||
Class firstCls = Class.forName(firstClassName);
|
// Class firstCls = Class.forName(firstClassName);
|
||||||
Class<?> lastCls = Class.forName(lastClassName);
|
// Class<?> lastCls = Class.forName(lastClassName);
|
||||||
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
// mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||||
Object obj = mapper.readValue(param.getData(), mapper.getTypeFactory().constructCollectionType(firstCls, lastCls));
|
// Object obj = mapper.readValue(param.getData(), mapper.getTypeFactory().constructCollectionType(firstCls, lastCls));
|
||||||
|
//
|
||||||
paramTypes.add(firstCls);
|
// paramTypes.add(firstCls);
|
||||||
valueList.add(obj);
|
// valueList.add(obj);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
|
//
|
||||||
}
|
// }
|
||||||
Method method = null;
|
// Method method = null;
|
||||||
if(params.size() > 0) {
|
// if(params.size() > 0) {
|
||||||
method = MethodSeeker.getMethod(service.getClass(),methodName, paramTypes);
|
// method = MethodSeeker.getMethod(service.getClass(),methodName, paramTypes);
|
||||||
if (method == null) {
|
// if (method == null) {
|
||||||
throw new Exception("Not found method : " + methodName);
|
// throw new Exception("Not found method : " + methodName);
|
||||||
}
|
// }
|
||||||
retObj = method.invoke(service, valueList.toArray(new Object[valueList.size()]));
|
// retObj = method.invoke(service, valueList.toArray(new Object[valueList.size()]));
|
||||||
}
|
// }
|
||||||
else {
|
// else {
|
||||||
method = service.getClass().getMethod(methodName);
|
// method = service.getClass().getMethod(methodName);
|
||||||
retObj = method.invoke(service);
|
// retObj = method.invoke(service);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
if(retObj == null) {
|
// if(retObj == null) {
|
||||||
return "";
|
// return "";
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
return mapper.writeValueAsString(retObj);
|
// return mapper.writeValueAsString(retObj);
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user