ing
This commit is contained in:
parent
2b4aeea3e6
commit
8625e10e00
|
@ -1,12 +1,14 @@
|
|||
package com.loafle.overflow.central.commons.service;
|
||||
|
||||
import com.loafle.overflow.core.exception.OverflowException;
|
||||
|
||||
public interface MessagePublisher {
|
||||
void publishToDomainMembers(final long domainID, final String method, final Object... params);
|
||||
void publishToDomainMembersByProbeKey(final String probeKey, final String method, final Object... params);
|
||||
void publishToDomainMembers(final long domainID, final String method, final Object... params) throws OverflowException;
|
||||
void publishToDomainMembersByProbeKey(final String probeKey, final String method, final Object... params) throws OverflowException;
|
||||
|
||||
void publishToMember(final String memberID, final String method, final Object... params);
|
||||
void publishToMemberSession(final String memberSessionID, final String method, final Object... params);
|
||||
void publishToMember(final String memberID, final String method, final Object... params) throws OverflowException;
|
||||
void publishToMemberSession(final String memberSessionID, final String method, final Object... params) throws OverflowException;
|
||||
|
||||
void publishToNoAuthProbe(final String tempProbeKey, final String method, final Object... params);
|
||||
void publishToProbe(final String probeKey, final String method, final Object... params);
|
||||
void publishToNoAuthProbe(final String tempProbeKey, final String method, final Object... params) throws OverflowException;
|
||||
void publishToProbe(final String probeKey, final String method, final Object... params) throws OverflowException;
|
||||
}
|
||||
|
|
|
@ -35,85 +35,63 @@ public class CentralDiscoveryService implements DiscoveryService {
|
|||
System.out.println("Start Discovery");
|
||||
System.out.println(json);
|
||||
// this.messagePublisher.publishToProbe("/auth", noAuthProbe.getTempProbeKey(), "NoAuthProbeService.acceptNoAuthProbe", probe.getProbeKey());
|
||||
}
|
||||
}
|
||||
|
||||
// public boolean testDiscovery(int types, String obj) {
|
||||
|
||||
// Domain domain = new Domain();
|
||||
// domain.setId(1);
|
||||
|
||||
// // int typeInt = Integer.valueOf(types);
|
||||
// switch (types) {
|
||||
// case 1 :
|
||||
// messagePublisher.publishToDomainMembers(domain, "DiscoveryService.discoveryIngHost", obj);
|
||||
// break;
|
||||
// case 2 :
|
||||
// messagePublisher.publishToDomain("/webapp", domain, "DiscoveryService.discoveryIngPort", obj);
|
||||
// break;
|
||||
// case 3 :
|
||||
// messagePublisher.publishToDomain("/webapp", domain, "DiscoveryService.discoveryIngService", obj);
|
||||
// break;
|
||||
// }
|
||||
|
||||
|
||||
// return true;
|
||||
// }
|
||||
|
||||
@WebappAPI
|
||||
public void discoverZone(String probeID, DiscoverZone discoveryZone) {
|
||||
public void discoverZone(String probeID, DiscoverZone discoveryZone) throws OverflowException {
|
||||
String requesterSessionID = SessionMetadata.getSessionID();
|
||||
|
||||
messagePublisher.publishToProbe(probeID, "DiscoveryService.DiscoverZone", requesterSessionID, discoveryZone);
|
||||
}
|
||||
|
||||
@WebappAPI
|
||||
public void discoverHost(String probeID, Zone zone, DiscoverHost discoveryHost) {
|
||||
public void discoverHost(String probeID, Zone zone, DiscoverHost discoveryHost) throws OverflowException {
|
||||
String requesterSessionID = SessionMetadata.getSessionID();
|
||||
|
||||
messagePublisher.publishToProbe(probeID, "DiscoveryService.DiscoverHost", requesterSessionID, zone, discoveryHost);
|
||||
}
|
||||
|
||||
@WebappAPI
|
||||
public void discoverPort(String probeID, Host host, DiscoverPort discoveryPort) {
|
||||
public void discoverPort(String probeID, Host host, DiscoverPort discoveryPort) throws OverflowException {
|
||||
String requesterSessionID = SessionMetadata.getSessionID();
|
||||
|
||||
messagePublisher.publishToProbe(probeID, "DiscoveryService.DiscoverPort", requesterSessionID, host, discoveryPort);
|
||||
}
|
||||
|
||||
@WebappAPI
|
||||
public void discoverService(String probeID, Port port, com.loafle.overflow.model.discovery.DiscoverService discoveryService) {
|
||||
public void discoverService(String probeID, Port port, com.loafle.overflow.model.discovery.DiscoverService discoveryService) throws OverflowException {
|
||||
String requesterSessionID = SessionMetadata.getSessionID();
|
||||
|
||||
messagePublisher.publishToProbe(probeID, "DiscoveryService.DiscoverService", requesterSessionID, port, discoveryService);
|
||||
}
|
||||
|
||||
@ProbeAPI
|
||||
public void discoverStart(String requesterSessionID, Date startDate) {
|
||||
public void discoverStart(String requesterSessionID, Date startDate) throws OverflowException {
|
||||
messagePublisher.publishToMemberSession(requesterSessionID, "DiscoveryService.discoveryStart", startDate);
|
||||
}
|
||||
|
||||
@ProbeAPI
|
||||
public void discoverStop(String requesterSessionID, Date stopDate) {
|
||||
public void discoverStop(String requesterSessionID, Date stopDate) throws OverflowException {
|
||||
messagePublisher.publishToMemberSession(requesterSessionID, "DiscoveryService.discoveryStop", stopDate);
|
||||
}
|
||||
|
||||
@ProbeAPI
|
||||
public void discoveredZone(String requesterSessionID, Zone zone) {
|
||||
public void discoveredZone(String requesterSessionID, Zone zone) throws OverflowException {
|
||||
messagePublisher.publishToMemberSession(requesterSessionID, "DiscoveryService.discoveredZone", zone);
|
||||
}
|
||||
|
||||
@ProbeAPI
|
||||
public void discoveredHost(String requesterSessionID, Host host) {
|
||||
public void discoveredHost(String requesterSessionID, Host host) throws OverflowException {
|
||||
messagePublisher.publishToMemberSession(requesterSessionID, "DiscoveryService.discoveredHost", host);
|
||||
}
|
||||
|
||||
@ProbeAPI
|
||||
public void discoveredPort(String requesterSessionID, Port port) {
|
||||
public void discoveredPort(String requesterSessionID, Port port) throws OverflowException {
|
||||
messagePublisher.publishToMemberSession(requesterSessionID, "DiscoveryService.discoveredPort", port);
|
||||
}
|
||||
|
||||
@ProbeAPI
|
||||
public void discoveredService(String requesterSessionID, com.loafle.overflow.model.discovery.Service service) {
|
||||
public void discoveredService(String requesterSessionID, com.loafle.overflow.model.discovery.Service service) throws OverflowException {
|
||||
messagePublisher.publishToMemberSession(requesterSessionID, "DiscoveryService.discoveredService", service);
|
||||
}
|
||||
|
||||
|
|
|
@ -2,12 +2,8 @@ package com.loafle.overflow.central.proxy;
|
|||
|
||||
import com.google.gson.internal.Primitives;
|
||||
import com.google.protobuf.ByteString;
|
||||
import com.loafle.overflow.central.proxy.exception.InvalidParameterException;
|
||||
import com.loafle.overflow.central.proxy.exception.InvalidRequestException;
|
||||
import com.loafle.overflow.central.proxy.exception.NoSuchMethodException;
|
||||
import com.loafle.overflow.central.proxy.exception.NoSuchServiceException;
|
||||
import io.grpc.Status;
|
||||
import io.grpc.StatusRuntimeException;
|
||||
import com.loafle.overflow.core.exception.OverflowException;
|
||||
|
||||
import org.codehaus.jackson.map.DeserializationConfig;
|
||||
import org.codehaus.jackson.map.ObjectMapper;
|
||||
import org.codehaus.jackson.type.JavaType;
|
||||
|
@ -27,175 +23,177 @@ import java.util.Map;
|
|||
*/
|
||||
@Component
|
||||
public class ServiceInvoker {
|
||||
// @Autowired
|
||||
private ApplicationContext context;
|
||||
// @Autowired
|
||||
private ApplicationContext context;
|
||||
|
||||
private ObjectMapper objectMapper;
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
private Map<String, Cache> serviceCacheMap;
|
||||
private Map<String, Cache> serviceCacheMap;
|
||||
|
||||
public ServiceInvoker(ApplicationContext context) {
|
||||
this.context = context;
|
||||
objectMapper = new ObjectMapper();
|
||||
objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
public ServiceInvoker(ApplicationContext context) {
|
||||
this.context = context;
|
||||
objectMapper = new ObjectMapper();
|
||||
objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
|
||||
serviceCacheMap = new HashMap<>();
|
||||
serviceCacheMap = new HashMap<>();
|
||||
}
|
||||
|
||||
private static class Cache {
|
||||
private ServiceCache serviceCache;
|
||||
private Map<String, MethodCache> methodCacheMap;
|
||||
|
||||
private Cache(Object bean) {
|
||||
serviceCache = new ServiceCache();
|
||||
serviceCache.bean = bean;
|
||||
|
||||
methodCacheMap = new HashMap<>(10);
|
||||
}
|
||||
|
||||
private static class Cache {
|
||||
private ServiceCache serviceCache;
|
||||
private Map<String, MethodCache> methodCacheMap;
|
||||
|
||||
private Cache(Object bean) {
|
||||
serviceCache = new ServiceCache();
|
||||
serviceCache.bean = bean;
|
||||
|
||||
methodCacheMap = new HashMap<>(10);
|
||||
}
|
||||
|
||||
private Object getBean() {
|
||||
return serviceCache.bean;
|
||||
}
|
||||
|
||||
private MethodCache getMethodCache(String methodName) throws NoSuchMethodException {
|
||||
MethodCache methodCache = methodCacheMap.get(methodName);
|
||||
if (null != methodCache) {
|
||||
return methodCache;
|
||||
}
|
||||
|
||||
Method method = getMethod(methodName);
|
||||
if (null == method) {
|
||||
throw new NoSuchMethodException();
|
||||
}
|
||||
methodCache = new MethodCache();
|
||||
methodCacheMap.put(methodName, methodCache);
|
||||
|
||||
methodCache.method = method;
|
||||
methodCache.returnClazz = method.getReturnType();
|
||||
|
||||
int paramCount = method.getParameterCount();
|
||||
if (0 == paramCount) {
|
||||
return methodCache;
|
||||
}
|
||||
|
||||
methodCache.parameterTypes = method.getGenericParameterTypes();
|
||||
|
||||
return methodCache;
|
||||
}
|
||||
|
||||
private Method getMethod(String methodName) {
|
||||
Class<?> clazz = AopUtils.getTargetClass(serviceCache.bean);
|
||||
Method[] methods = clazz.getMethods();
|
||||
|
||||
Method targetMethod = null;
|
||||
for(Method m : methods){
|
||||
if (methodName.equals(m.getName())) {
|
||||
targetMethod = m;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return targetMethod;
|
||||
}
|
||||
|
||||
private static class ServiceCache {
|
||||
private Object bean;
|
||||
private Map<String, MethodCache> methodCacheMap;
|
||||
}
|
||||
private static class MethodCache {
|
||||
private Method method;
|
||||
private Class returnClazz;
|
||||
private Type[] parameterTypes;
|
||||
}
|
||||
private Object getBean() {
|
||||
return serviceCache.bean;
|
||||
}
|
||||
|
||||
private Cache getServiceCache(String serviceName) throws NoSuchServiceException {
|
||||
Cache serviceCache = serviceCacheMap.get(serviceName);
|
||||
if (null != serviceCache) {
|
||||
return serviceCache;
|
||||
}
|
||||
try {
|
||||
Object bean = context.getBean(serviceName);
|
||||
serviceCache = new Cache(bean);
|
||||
serviceCacheMap.put(serviceName, serviceCache);
|
||||
} catch (BeansException e) {
|
||||
throw new NoSuchServiceException();
|
||||
}
|
||||
private MethodCache getMethodCache(String methodName) throws NoSuchMethodException {
|
||||
MethodCache methodCache = methodCacheMap.get(methodName);
|
||||
if (null != methodCache) {
|
||||
return methodCache;
|
||||
}
|
||||
|
||||
return serviceCache;
|
||||
Method method = getMethod(methodName);
|
||||
if (null == method) {
|
||||
throw new NoSuchMethodException();
|
||||
}
|
||||
methodCache = new MethodCache();
|
||||
methodCacheMap.put(methodName, methodCache);
|
||||
|
||||
methodCache.method = method;
|
||||
methodCache.returnClazz = method.getReturnType();
|
||||
|
||||
int paramCount = method.getParameterCount();
|
||||
if (0 == paramCount) {
|
||||
return methodCache;
|
||||
}
|
||||
|
||||
methodCache.parameterTypes = method.getGenericParameterTypes();
|
||||
|
||||
return methodCache;
|
||||
}
|
||||
|
||||
private Object getValue(Type parameterType, String json) throws InvalidParameterException {
|
||||
JavaType targetType = objectMapper.getTypeFactory().constructType(parameterType);
|
||||
if (!Primitives.isPrimitive(parameterType) && !parameterType.getTypeName().equals(String.class.getName())) {
|
||||
try {
|
||||
return objectMapper.readValue(json, targetType);
|
||||
} catch (IOException e) {
|
||||
throw new InvalidParameterException();
|
||||
}
|
||||
private Method getMethod(String methodName) {
|
||||
Class<?> clazz = AopUtils.getTargetClass(serviceCache.bean);
|
||||
Method[] methods = clazz.getMethods();
|
||||
|
||||
Method targetMethod = null;
|
||||
for (Method m : methods) {
|
||||
if (methodName.equals(m.getName())) {
|
||||
targetMethod = m;
|
||||
break;
|
||||
}
|
||||
return objectMapper.convertValue(json, targetType);
|
||||
}
|
||||
return targetMethod;
|
||||
}
|
||||
|
||||
|
||||
private Object[] getParameters(Type[] parameterTypes, List<ByteString> params) throws InvalidParameterException {
|
||||
if (null == parameterTypes || null == params) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (parameterTypes.length != params.size()) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
Object[] result = new Object[parameterTypes.length];
|
||||
|
||||
for (int i = 0; i < parameterTypes.length; i++) {
|
||||
result[i] = getValue(parameterTypes[i], params.get(i).toStringUtf8());
|
||||
}
|
||||
|
||||
return result;
|
||||
private static class ServiceCache {
|
||||
private Object bean;
|
||||
private Map<String, MethodCache> methodCacheMap;
|
||||
}
|
||||
|
||||
|
||||
public String invoke(String serviceName, String methodName, List<ByteString> params) throws OverflowException, OverflowRuntimeException {
|
||||
Cache serviceCache = getServiceCache(serviceName);
|
||||
Cache.MethodCache methodCache = serviceCache.getMethodCache(methodName);
|
||||
|
||||
if (null == methodCache.parameterTypes) {
|
||||
if (null != params && 0 < params.size()) {
|
||||
throw new InvalidParameterException();
|
||||
}
|
||||
} else {
|
||||
if (methodCache.parameterTypes.length != params.size()) {
|
||||
throw new InvalidParameterException();
|
||||
}
|
||||
}
|
||||
|
||||
Object[] parameters = null;
|
||||
if (null != methodCache.parameterTypes && 0 < methodCache.parameterTypes.length) {
|
||||
parameters = getParameters(methodCache.parameterTypes, params);
|
||||
}
|
||||
|
||||
Object result = null;
|
||||
String jsonInString = null;
|
||||
try {
|
||||
result = methodCache.method.invoke(serviceCache.getBean(), parameters);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new InvalidRequestException();
|
||||
} catch (InvocationTargetException e) {
|
||||
Throwable t = e.getTargetException();
|
||||
if (t instanceof OverflowRuntimeException) {
|
||||
throw (OverflowRuntimeException)t;
|
||||
}
|
||||
throw new InvalidRequestException();
|
||||
}
|
||||
|
||||
try {
|
||||
jsonInString = objectMapper.writeValueAsString(result);
|
||||
} catch (IOException e) {
|
||||
throw new InvalidRequestException();
|
||||
}
|
||||
|
||||
return jsonInString;
|
||||
private static class MethodCache {
|
||||
private Method method;
|
||||
private Class returnClazz;
|
||||
private Type[] parameterTypes;
|
||||
}
|
||||
}
|
||||
|
||||
private Cache getServiceCache(String serviceName) throws java.lang.NoSuchMethodException {
|
||||
Cache serviceCache = serviceCacheMap.get(serviceName);
|
||||
if (null != serviceCache) {
|
||||
return serviceCache;
|
||||
}
|
||||
try {
|
||||
Object bean = context.getBean(serviceName);
|
||||
serviceCache = new Cache(bean);
|
||||
serviceCacheMap.put(serviceName, serviceCache);
|
||||
} catch (BeansException e) {
|
||||
throw new NoSuchMethodException();
|
||||
}
|
||||
|
||||
return serviceCache;
|
||||
}
|
||||
|
||||
private Object getValue(Type parameterType, String json) throws IllegalArgumentException {
|
||||
JavaType targetType = objectMapper.getTypeFactory().constructType(parameterType);
|
||||
if (!Primitives.isPrimitive(parameterType) && !parameterType.getTypeName().equals(String.class.getName())) {
|
||||
try {
|
||||
return objectMapper.readValue(json, targetType);
|
||||
} catch (IOException e) {
|
||||
throw new IllegalArgumentException("");
|
||||
}
|
||||
}
|
||||
return objectMapper.convertValue(json, targetType);
|
||||
}
|
||||
|
||||
private Object[] getParameters(Type[] parameterTypes, List<ByteString> params) throws IllegalArgumentException {
|
||||
if (null == parameterTypes || null == params) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (parameterTypes.length != params.size()) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
Object[] result = new Object[parameterTypes.length];
|
||||
|
||||
for (int i = 0; i < parameterTypes.length; i++) {
|
||||
result[i] = getValue(parameterTypes[i], params.get(i).toStringUtf8());
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public String invoke(String serviceName, String methodName, List<ByteString> params)
|
||||
throws NoSuchMethodException, IllegalAccessException, OverflowException {
|
||||
Cache serviceCache = getServiceCache(serviceName);
|
||||
Cache.MethodCache methodCache = serviceCache.getMethodCache(methodName);
|
||||
|
||||
if (null == methodCache.parameterTypes) {
|
||||
if (null != params && 0 < params.size()) {
|
||||
throw new IllegalArgumentException(
|
||||
String.format("size of parameters is not valid target[%d], source[%d]", 0, params.size()));
|
||||
}
|
||||
} else {
|
||||
if (methodCache.parameterTypes.length != params.size()) {
|
||||
throw new IllegalArgumentException(String.format("size of parameters is not valid target[%d], source[%d]",
|
||||
methodCache.parameterTypes.length, params.size()));
|
||||
}
|
||||
}
|
||||
|
||||
Object[] parameters = null;
|
||||
if (null != methodCache.parameterTypes && 0 < methodCache.parameterTypes.length) {
|
||||
parameters = getParameters(methodCache.parameterTypes, params);
|
||||
}
|
||||
|
||||
Object result = null;
|
||||
String jsonInString = null;
|
||||
try {
|
||||
result = methodCache.method.invoke(serviceCache.getBean(), parameters);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new IllegalAccessException(String.format("method[%s.%s] is not valid", serviceName, methodName));
|
||||
} catch (InvocationTargetException e) {
|
||||
Throwable t = e.getTargetException();
|
||||
if (t instanceof OverflowException) {
|
||||
throw (OverflowException) t;
|
||||
}
|
||||
throw new OverflowException("internal error", e);
|
||||
}
|
||||
|
||||
try {
|
||||
jsonInString = objectMapper.writeValueAsString(result);
|
||||
} catch (IOException e) {
|
||||
throw new OverflowException("internal error", e);
|
||||
}
|
||||
|
||||
return jsonInString;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -3,7 +3,11 @@ package com.loafle.overflow.central.proxy;
|
|||
import com.loafle.overflow.api.OverflowApiServerGrpc;
|
||||
import com.loafle.overflow.api.ServerInput;
|
||||
import com.loafle.overflow.api.ServerOutput;
|
||||
import com.loafle.overflow.core.exception.OverflowException;
|
||||
|
||||
import io.grpc.*;
|
||||
import io.grpc.Status.Code;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
|
||||
|
@ -14,91 +18,83 @@ import java.util.logging.Logger;
|
|||
* Created by insanity on 17. 6. 28.
|
||||
*/
|
||||
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 ApplicationContext ctx;
|
||||
private io.grpc.Server server;
|
||||
private ApplicationContext ctx;
|
||||
|
||||
public void start(int port) throws IOException {
|
||||
ctx = new AnnotationConfigApplicationContext("com.loafle.overflow");
|
||||
public void start(int port) throws IOException {
|
||||
ctx = new AnnotationConfigApplicationContext("com.loafle.overflow");
|
||||
|
||||
ProxyServerInterceptor proxyServerInterceptor = new ProxyServerInterceptor();
|
||||
ProxyServerInterceptor proxyServerInterceptor = new ProxyServerInterceptor();
|
||||
|
||||
server = ServerBuilder.forPort(port)
|
||||
.addService(ServerInterceptors.intercept(new ServiceImpl(new ServiceInvoker(ctx)), proxyServerInterceptor))
|
||||
.build()
|
||||
.start();
|
||||
logger.info("Server started, listening on " + port);
|
||||
Runtime.getRuntime().addShutdownHook(new Thread() {
|
||||
@Override
|
||||
public void run() {
|
||||
// Use stderr here since the logger may have been reset by its JVM shutdown hook.
|
||||
System.err.println("*** shutting down gRPC server since JVM is shutting down");
|
||||
ServiceProxy.this.stop();
|
||||
System.err.println("*** server shut down");
|
||||
}
|
||||
});
|
||||
server = ServerBuilder.forPort(port)
|
||||
.addService(ServerInterceptors.intercept(new ServiceImpl(new ServiceInvoker(ctx)), proxyServerInterceptor))
|
||||
.build().start();
|
||||
logger.info("Server started, listening on " + port);
|
||||
Runtime.getRuntime().addShutdownHook(new Thread() {
|
||||
@Override
|
||||
public void run() {
|
||||
// Use stderr here since the logger may have been reset by its JVM shutdown hook.
|
||||
System.err.println("*** shutting down gRPC server since JVM is shutting down");
|
||||
ServiceProxy.this.stop();
|
||||
System.err.println("*** server shut down");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
if (server != null) {
|
||||
server.shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
public void blockUntilShutdown() throws InterruptedException {
|
||||
if (server != null) {
|
||||
server.awaitTermination();
|
||||
}
|
||||
}
|
||||
|
||||
static class ServiceImpl extends OverflowApiServerGrpc.OverflowApiServerImplBase {
|
||||
|
||||
private ServiceInvoker serviceInvoker;
|
||||
|
||||
ServiceImpl(ServiceInvoker serviceInvoker) {
|
||||
this.serviceInvoker = serviceInvoker;
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
if (server != null) {
|
||||
server.shutdown();
|
||||
}
|
||||
@Override
|
||||
public void exec(ServerInput request, io.grpc.stub.StreamObserver<ServerOutput> responseObserver) {
|
||||
|
||||
String result = null;
|
||||
|
||||
try {
|
||||
result = this.serviceInvoker.invoke(request.getTarget(), request.getMethod(),
|
||||
request.getParamsList().asByteStringList());
|
||||
|
||||
ServerOutput reply = ServerOutput.newBuilder().setResult(result).build();
|
||||
responseObserver.onNext(reply);
|
||||
responseObserver.onCompleted();
|
||||
|
||||
} catch (OverflowException e) {
|
||||
responseObserver.onError(this.getException(request, Status.Code.INTERNAL, e));
|
||||
return;
|
||||
} catch (NoSuchMethodException e) {
|
||||
responseObserver.onError(this.getException(request, Status.Code.NOT_FOUND, e));
|
||||
return;
|
||||
} catch (IllegalAccessException e) {
|
||||
responseObserver.onError(this.getException(request, Status.Code.INVALID_ARGUMENT, e));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public void blockUntilShutdown() throws InterruptedException {
|
||||
if (server != null) {
|
||||
server.awaitTermination();
|
||||
}
|
||||
}
|
||||
|
||||
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 result = null;
|
||||
try {
|
||||
result = this.serviceInvoker.invoke(request.getTarget(), request.getMethod(), request.getParamsList().asByteStringList());
|
||||
|
||||
ServerOutput reply = ServerOutput.newBuilder()
|
||||
.setResult(result)
|
||||
.build();
|
||||
responseObserver.onNext(reply);
|
||||
responseObserver.onCompleted();
|
||||
} catch (OverflowException e) {
|
||||
logger.warning(getExceptionMessage(request, e));
|
||||
responseObserver.onError(convertException(e));
|
||||
} catch (OverflowRuntimeException e) {
|
||||
logger.warning(getExceptionMessage(request, e));
|
||||
responseObserver.onError(convertException(e));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private String getExceptionMessage(ServerInput request, Exception e) {
|
||||
return String.format("Target: %s, Method:%s, Params:%s, ex:%s", request.getTarget(), request.getMethod(), request.getParamsList().asByteStringList(), e.toString());
|
||||
}
|
||||
|
||||
protected StatusRuntimeException convertException(OverflowRuntimeException e) {
|
||||
String message = String.format("%s|%s", e.getClass().getSimpleName(), e.getMessage());
|
||||
Status status = Status.fromCode(Status.Code.INTERNAL).withDescription(message);
|
||||
return new StatusRuntimeException(status);
|
||||
}
|
||||
|
||||
protected StatusException convertException(OverflowException e) {
|
||||
String message = String.format("%s|%s", e.getClass().getSimpleName(), e.getMessage());
|
||||
Status status = Status.fromCode(e.getCode()).withDescription(message);
|
||||
return new StatusException(status);
|
||||
}
|
||||
|
||||
private StatusException getException(ServerInput request, Code code, Throwable cause) {
|
||||
|
||||
String msg = String.format("Target: %s, Method:%s, Params:%s, message:%s", request.getTarget(),
|
||||
request.getMethod(), request.getParamsList().asByteStringList(), cause.getMessage());
|
||||
Status status = Status.fromCode(code).withDescription(msg).withCause(cause);
|
||||
return new StatusException(status);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,15 +0,0 @@
|
|||
package com.loafle.overflow.central.proxy.exception;
|
||||
|
||||
import com.loafle.overflow.central.commons.exception.OverflowException;
|
||||
import io.grpc.Status;
|
||||
|
||||
public class InvalidParameterException extends OverflowException {
|
||||
private static final Status.Code code = Status.Code.INVALID_ARGUMENT;
|
||||
public InvalidParameterException() {
|
||||
super(code);
|
||||
}
|
||||
|
||||
public InvalidParameterException(String message) {
|
||||
super(code, message);
|
||||
}
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
package com.loafle.overflow.central.proxy.exception;
|
||||
|
||||
import com.loafle.overflow.central.commons.exception.OverflowException;
|
||||
import io.grpc.Status;
|
||||
|
||||
public class InvalidRequestException extends OverflowException {
|
||||
private static final Status.Code code = Status.Code.UNKNOWN;
|
||||
public InvalidRequestException() {
|
||||
super(code);
|
||||
}
|
||||
|
||||
public InvalidRequestException(String message) {
|
||||
super(code, message);
|
||||
}
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
package com.loafle.overflow.central.proxy.exception;
|
||||
|
||||
import com.loafle.overflow.central.commons.exception.OverflowException;
|
||||
import io.grpc.Status;
|
||||
|
||||
public class NoSuchMethodException extends OverflowException {
|
||||
private static final Status.Code code = Status.Code.UNIMPLEMENTED;
|
||||
public NoSuchMethodException() {
|
||||
super(code);
|
||||
}
|
||||
|
||||
public NoSuchMethodException(String message) {
|
||||
super(code, message);
|
||||
}
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
package com.loafle.overflow.central.proxy.exception;
|
||||
|
||||
import com.loafle.overflow.central.commons.exception.OverflowException;
|
||||
import io.grpc.Status;
|
||||
|
||||
public class NoSuchServiceException extends OverflowException {
|
||||
private static final Status.Code code = Status.Code.UNIMPLEMENTED;
|
||||
public NoSuchServiceException() {
|
||||
super(code);
|
||||
}
|
||||
|
||||
public NoSuchServiceException(String message) {
|
||||
super(code, message);
|
||||
}
|
||||
}
|
|
@ -41,40 +41,40 @@ public class RedisMessagePublisher implements MessagePublisher {
|
|||
this.topics = topics;
|
||||
}
|
||||
|
||||
public void publishToDomainMembers(final long domainID, final String method, final Object... params) {
|
||||
public void publishToDomainMembers(final long domainID, final String method, final Object... params) throws OverflowException {
|
||||
PublishMessage message = new PublishMessage();
|
||||
message.setTargetType(PublishMessage.TargetType.MEMBER);
|
||||
message.setTargets(getMemberListByDomainID(domainID));
|
||||
this.publish(CHANNEL_WEBAPP, message, method, params);
|
||||
}
|
||||
public void publishToDomainMembersByProbeKey(final String probeKey, final String method, final Object... params) {
|
||||
public void publishToDomainMembersByProbeKey(final String probeKey, final String method, final Object... params) throws OverflowException {
|
||||
PublishMessage message = new PublishMessage();
|
||||
message.setTargetType(PublishMessage.TargetType.MEMBER);
|
||||
message.setTargets(getMemberListByProbeKey(probeKey));
|
||||
this.publish(CHANNEL_WEBAPP, message, method, params);
|
||||
}
|
||||
|
||||
public void publishToMember(final String memberID, final String method, final Object... params) {
|
||||
public void publishToMember(final String memberID, final String method, final Object... params) throws OverflowException {
|
||||
PublishMessage message = new PublishMessage();
|
||||
message.setTargetType(PublishMessage.TargetType.MEMBER);
|
||||
message.addTarget(memberID);
|
||||
this.publish(CHANNEL_WEBAPP, message, method, params);
|
||||
}
|
||||
public void publishToMemberSession(final String memberSessionID, final String method, final Object... params) {
|
||||
public void publishToMemberSession(final String memberSessionID, final String method, final Object... params) throws OverflowException {
|
||||
PublishMessage message = new PublishMessage();
|
||||
message.setTargetType(PublishMessage.TargetType.MEMBER_SESSION);
|
||||
message.addTarget(memberSessionID);
|
||||
this.publish(CHANNEL_WEBAPP, message, method, params);
|
||||
}
|
||||
|
||||
public void publishToNoAuthProbe(final String tempProbeKey, final String method, final Object... params) {
|
||||
public void publishToNoAuthProbe(final String tempProbeKey, final String method, final Object... params) throws OverflowException {
|
||||
PublishMessage message = new PublishMessage();
|
||||
message.setTargetType(PublishMessage.TargetType.PROBE);
|
||||
message.addTarget(tempProbeKey);
|
||||
this.publish(CHANNEL_NOAUTH_PROBE, message, method, params);
|
||||
}
|
||||
|
||||
public void publishToProbe(final String probeKey, final String method, final Object... params) {
|
||||
public void publishToProbe(final String probeKey, final String method, final Object... params) throws OverflowException {
|
||||
PublishMessage message = new PublishMessage();
|
||||
message.setTargetType(PublishMessage.TargetType.PROBE);
|
||||
message.addTarget(probeKey);
|
||||
|
|
Loading…
Reference in New Issue
Block a user