ing
This commit is contained in:
parent
2b4aeea3e6
commit
8625e10e00
|
@ -1,12 +1,14 @@
|
||||||
package com.loafle.overflow.central.commons.service;
|
package com.loafle.overflow.central.commons.service;
|
||||||
|
|
||||||
|
import com.loafle.overflow.core.exception.OverflowException;
|
||||||
|
|
||||||
public interface MessagePublisher {
|
public interface MessagePublisher {
|
||||||
void publishToDomainMembers(final long domainID, 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);
|
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 publishToMember(final String memberID, final String method, final Object... params) throws OverflowException;
|
||||||
void publishToMemberSession(final String memberSessionID, final String method, final Object... params);
|
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 publishToNoAuthProbe(final String tempProbeKey, final String method, final Object... params) throws OverflowException;
|
||||||
void publishToProbe(final String probeKey, final String method, final Object... params);
|
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("Start Discovery");
|
||||||
System.out.println(json);
|
System.out.println(json);
|
||||||
// this.messagePublisher.publishToProbe("/auth", noAuthProbe.getTempProbeKey(), "NoAuthProbeService.acceptNoAuthProbe", probe.getProbeKey());
|
// 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
|
@WebappAPI
|
||||||
public void discoverZone(String probeID, DiscoverZone discoveryZone) {
|
public void discoverZone(String probeID, DiscoverZone discoveryZone) throws OverflowException {
|
||||||
String requesterSessionID = SessionMetadata.getSessionID();
|
String requesterSessionID = SessionMetadata.getSessionID();
|
||||||
|
|
||||||
messagePublisher.publishToProbe(probeID, "DiscoveryService.DiscoverZone", requesterSessionID, discoveryZone);
|
messagePublisher.publishToProbe(probeID, "DiscoveryService.DiscoverZone", requesterSessionID, discoveryZone);
|
||||||
}
|
}
|
||||||
|
|
||||||
@WebappAPI
|
@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();
|
String requesterSessionID = SessionMetadata.getSessionID();
|
||||||
|
|
||||||
messagePublisher.publishToProbe(probeID, "DiscoveryService.DiscoverHost", requesterSessionID, zone, discoveryHost);
|
messagePublisher.publishToProbe(probeID, "DiscoveryService.DiscoverHost", requesterSessionID, zone, discoveryHost);
|
||||||
}
|
}
|
||||||
|
|
||||||
@WebappAPI
|
@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();
|
String requesterSessionID = SessionMetadata.getSessionID();
|
||||||
|
|
||||||
messagePublisher.publishToProbe(probeID, "DiscoveryService.DiscoverPort", requesterSessionID, host, discoveryPort);
|
messagePublisher.publishToProbe(probeID, "DiscoveryService.DiscoverPort", requesterSessionID, host, discoveryPort);
|
||||||
}
|
}
|
||||||
|
|
||||||
@WebappAPI
|
@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();
|
String requesterSessionID = SessionMetadata.getSessionID();
|
||||||
|
|
||||||
messagePublisher.publishToProbe(probeID, "DiscoveryService.DiscoverService", requesterSessionID, port, discoveryService);
|
messagePublisher.publishToProbe(probeID, "DiscoveryService.DiscoverService", requesterSessionID, port, discoveryService);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ProbeAPI
|
@ProbeAPI
|
||||||
public void discoverStart(String requesterSessionID, Date startDate) {
|
public void discoverStart(String requesterSessionID, Date startDate) throws OverflowException {
|
||||||
messagePublisher.publishToMemberSession(requesterSessionID, "DiscoveryService.discoveryStart", startDate);
|
messagePublisher.publishToMemberSession(requesterSessionID, "DiscoveryService.discoveryStart", startDate);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ProbeAPI
|
@ProbeAPI
|
||||||
public void discoverStop(String requesterSessionID, Date stopDate) {
|
public void discoverStop(String requesterSessionID, Date stopDate) throws OverflowException {
|
||||||
messagePublisher.publishToMemberSession(requesterSessionID, "DiscoveryService.discoveryStop", stopDate);
|
messagePublisher.publishToMemberSession(requesterSessionID, "DiscoveryService.discoveryStop", stopDate);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ProbeAPI
|
@ProbeAPI
|
||||||
public void discoveredZone(String requesterSessionID, Zone zone) {
|
public void discoveredZone(String requesterSessionID, Zone zone) throws OverflowException {
|
||||||
messagePublisher.publishToMemberSession(requesterSessionID, "DiscoveryService.discoveredZone", zone);
|
messagePublisher.publishToMemberSession(requesterSessionID, "DiscoveryService.discoveredZone", zone);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ProbeAPI
|
@ProbeAPI
|
||||||
public void discoveredHost(String requesterSessionID, Host host) {
|
public void discoveredHost(String requesterSessionID, Host host) throws OverflowException {
|
||||||
messagePublisher.publishToMemberSession(requesterSessionID, "DiscoveryService.discoveredHost", host);
|
messagePublisher.publishToMemberSession(requesterSessionID, "DiscoveryService.discoveredHost", host);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ProbeAPI
|
@ProbeAPI
|
||||||
public void discoveredPort(String requesterSessionID, Port port) {
|
public void discoveredPort(String requesterSessionID, Port port) throws OverflowException {
|
||||||
messagePublisher.publishToMemberSession(requesterSessionID, "DiscoveryService.discoveredPort", port);
|
messagePublisher.publishToMemberSession(requesterSessionID, "DiscoveryService.discoveredPort", port);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ProbeAPI
|
@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);
|
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.gson.internal.Primitives;
|
||||||
import com.google.protobuf.ByteString;
|
import com.google.protobuf.ByteString;
|
||||||
import com.loafle.overflow.central.proxy.exception.InvalidParameterException;
|
import com.loafle.overflow.core.exception.OverflowException;
|
||||||
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 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.codehaus.jackson.type.JavaType;
|
import org.codehaus.jackson.type.JavaType;
|
||||||
|
@ -27,175 +23,177 @@ import java.util.Map;
|
||||||
*/
|
*/
|
||||||
@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;
|
||||||
|
|
||||||
public ServiceInvoker(ApplicationContext context) {
|
public ServiceInvoker(ApplicationContext context) {
|
||||||
this.context = 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);
|
||||||
|
|
||||||
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 Object getBean() {
|
||||||
private ServiceCache serviceCache;
|
return serviceCache.bean;
|
||||||
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 Cache getServiceCache(String serviceName) throws NoSuchServiceException {
|
private MethodCache getMethodCache(String methodName) throws NoSuchMethodException {
|
||||||
Cache serviceCache = serviceCacheMap.get(serviceName);
|
MethodCache methodCache = methodCacheMap.get(methodName);
|
||||||
if (null != serviceCache) {
|
if (null != methodCache) {
|
||||||
return serviceCache;
|
return methodCache;
|
||||||
}
|
}
|
||||||
try {
|
|
||||||
Object bean = context.getBean(serviceName);
|
|
||||||
serviceCache = new Cache(bean);
|
|
||||||
serviceCacheMap.put(serviceName, serviceCache);
|
|
||||||
} catch (BeansException e) {
|
|
||||||
throw new NoSuchServiceException();
|
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
private Method getMethod(String methodName) {
|
||||||
JavaType targetType = objectMapper.getTypeFactory().constructType(parameterType);
|
Class<?> clazz = AopUtils.getTargetClass(serviceCache.bean);
|
||||||
if (!Primitives.isPrimitive(parameterType) && !parameterType.getTypeName().equals(String.class.getName())) {
|
Method[] methods = clazz.getMethods();
|
||||||
try {
|
|
||||||
return objectMapper.readValue(json, targetType);
|
Method targetMethod = null;
|
||||||
} catch (IOException e) {
|
for (Method m : methods) {
|
||||||
throw new InvalidParameterException();
|
if (methodName.equals(m.getName())) {
|
||||||
}
|
targetMethod = m;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
return objectMapper.convertValue(json, targetType);
|
}
|
||||||
|
return targetMethod;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static class ServiceCache {
|
||||||
private Object[] getParameters(Type[] parameterTypes, List<ByteString> params) throws InvalidParameterException {
|
private Object bean;
|
||||||
if (null == parameterTypes || null == params) {
|
private Map<String, MethodCache> methodCacheMap;
|
||||||
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 MethodCache {
|
||||||
public String invoke(String serviceName, String methodName, List<ByteString> params) throws OverflowException, OverflowRuntimeException {
|
private Method method;
|
||||||
Cache serviceCache = getServiceCache(serviceName);
|
private Class returnClazz;
|
||||||
Cache.MethodCache methodCache = serviceCache.getMethodCache(methodName);
|
private Type[] parameterTypes;
|
||||||
|
|
||||||
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 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.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;
|
||||||
|
import com.loafle.overflow.core.exception.OverflowException;
|
||||||
|
|
||||||
import io.grpc.*;
|
import io.grpc.*;
|
||||||
|
import io.grpc.Status.Code;
|
||||||
|
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
|
|
||||||
|
@ -14,91 +18,83 @@ import java.util.logging.Logger;
|
||||||
* Created by insanity on 17. 6. 28.
|
* Created by insanity on 17. 6. 28.
|
||||||
*/
|
*/
|
||||||
public class ServiceProxy {
|
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;
|
||||||
private ApplicationContext ctx;
|
private ApplicationContext ctx;
|
||||||
|
|
||||||
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");
|
||||||
|
|
||||||
ProxyServerInterceptor proxyServerInterceptor = new ProxyServerInterceptor();
|
ProxyServerInterceptor proxyServerInterceptor = new ProxyServerInterceptor();
|
||||||
|
|
||||||
server = ServerBuilder.forPort(port)
|
server = ServerBuilder.forPort(port)
|
||||||
.addService(ServerInterceptors.intercept(new ServiceImpl(new ServiceInvoker(ctx)), proxyServerInterceptor))
|
.addService(ServerInterceptors.intercept(new ServiceImpl(new ServiceInvoker(ctx)), proxyServerInterceptor))
|
||||||
.build()
|
.build().start();
|
||||||
.start();
|
logger.info("Server started, listening on " + port);
|
||||||
logger.info("Server started, listening on " + port);
|
Runtime.getRuntime().addShutdownHook(new Thread() {
|
||||||
Runtime.getRuntime().addShutdownHook(new Thread() {
|
@Override
|
||||||
@Override
|
public void run() {
|
||||||
public void run() {
|
// Use stderr here since the logger may have been reset by its JVM shutdown hook.
|
||||||
// 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");
|
||||||
System.err.println("*** shutting down gRPC server since JVM is shutting down");
|
ServiceProxy.this.stop();
|
||||||
ServiceProxy.this.stop();
|
System.err.println("*** server shut down");
|
||||||
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() {
|
@Override
|
||||||
if (server != null) {
|
public void exec(ServerInput request, io.grpc.stub.StreamObserver<ServerOutput> responseObserver) {
|
||||||
server.shutdown();
|
|
||||||
}
|
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 {
|
private StatusException getException(ServerInput request, Code code, Throwable cause) {
|
||||||
if (server != null) {
|
|
||||||
server.awaitTermination();
|
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);
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
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();
|
PublishMessage message = new PublishMessage();
|
||||||
message.setTargetType(PublishMessage.TargetType.MEMBER);
|
message.setTargetType(PublishMessage.TargetType.MEMBER);
|
||||||
message.setTargets(getMemberListByDomainID(domainID));
|
message.setTargets(getMemberListByDomainID(domainID));
|
||||||
this.publish(CHANNEL_WEBAPP, message, method, params);
|
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();
|
PublishMessage message = new PublishMessage();
|
||||||
message.setTargetType(PublishMessage.TargetType.MEMBER);
|
message.setTargetType(PublishMessage.TargetType.MEMBER);
|
||||||
message.setTargets(getMemberListByProbeKey(probeKey));
|
message.setTargets(getMemberListByProbeKey(probeKey));
|
||||||
this.publish(CHANNEL_WEBAPP, message, method, params);
|
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();
|
PublishMessage message = new PublishMessage();
|
||||||
message.setTargetType(PublishMessage.TargetType.MEMBER);
|
message.setTargetType(PublishMessage.TargetType.MEMBER);
|
||||||
message.addTarget(memberID);
|
message.addTarget(memberID);
|
||||||
this.publish(CHANNEL_WEBAPP, message, method, params);
|
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();
|
PublishMessage message = new PublishMessage();
|
||||||
message.setTargetType(PublishMessage.TargetType.MEMBER_SESSION);
|
message.setTargetType(PublishMessage.TargetType.MEMBER_SESSION);
|
||||||
message.addTarget(memberSessionID);
|
message.addTarget(memberSessionID);
|
||||||
this.publish(CHANNEL_WEBAPP, message, method, params);
|
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();
|
PublishMessage message = new PublishMessage();
|
||||||
message.setTargetType(PublishMessage.TargetType.PROBE);
|
message.setTargetType(PublishMessage.TargetType.PROBE);
|
||||||
message.addTarget(tempProbeKey);
|
message.addTarget(tempProbeKey);
|
||||||
this.publish(CHANNEL_NOAUTH_PROBE, message, method, params);
|
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();
|
PublishMessage message = new PublishMessage();
|
||||||
message.setTargetType(PublishMessage.TargetType.PROBE);
|
message.setTargetType(PublishMessage.TargetType.PROBE);
|
||||||
message.addTarget(probeKey);
|
message.addTarget(probeKey);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user