Exception of GRPC has been added.

This commit is contained in:
crusader 2017-08-14 16:56:27 +09:00
parent e9d9b7eee4
commit 8786d944cb
15 changed files with 133 additions and 80 deletions

View File

@ -1,4 +1,10 @@
package com.loafle.overflow.commons.exception;
public class OverflowException extends Exception {
import io.grpc.Status;
import io.grpc.StatusException;
public class OverflowException extends StatusException {
public OverflowException(Status status) {
super(status);
}
}

View File

@ -0,0 +1,10 @@
package com.loafle.overflow.commons.exception;
import io.grpc.Status;
import io.grpc.StatusRuntimeException;
public class OverflowRuntimeException extends StatusRuntimeException {
public OverflowRuntimeException(Class<? extends OverflowRuntimeException> clazz) {
super(Status.fromCode(Status.Code.UNKNOWN).withDescription(clazz.getSimpleName()));
}
}

View File

@ -1,6 +1,9 @@
package com.loafle.overflow.module.member.exception;
import com.loafle.overflow.commons.exception.OverflowException;
import com.loafle.overflow.commons.exception.OverflowRuntimeException;
public class EmailNotConfirmedException extends OverflowException {
}
public class EmailNotConfirmedException extends OverflowRuntimeException {
public EmailNotConfirmedException() {
super(EmailNotConfirmedException.class);
}
}

View File

@ -1,6 +1,9 @@
package com.loafle.overflow.module.member.exception;
import com.loafle.overflow.commons.exception.OverflowException;
import com.loafle.overflow.commons.exception.OverflowRuntimeException;
public class SignInIdNotExistException extends OverflowException {
public class SignInIdNotExistException extends OverflowRuntimeException {
public SignInIdNotExistException() {
super(SignInIdNotExistException.class);
}
}

View File

@ -1,6 +0,0 @@
package com.loafle.overflow.module.member.exception;
import com.loafle.overflow.commons.exception.OverflowException;
public class SignInPwNotExactException extends OverflowException {
}

View File

@ -0,0 +1,9 @@
package com.loafle.overflow.module.member.exception;
import com.loafle.overflow.commons.exception.OverflowRuntimeException;
public class SignInPwNotMatchException extends OverflowRuntimeException {
public SignInPwNotMatchException() {
super(SignInPwNotMatchException.class);
}
}

View File

@ -4,7 +4,7 @@ import com.loafle.overflow.module.email.service.EmailAuthService;
import com.loafle.overflow.module.member.dao.MemberDAO;
import com.loafle.overflow.module.member.exception.EmailNotConfirmedException;
import com.loafle.overflow.module.member.exception.SignInIdNotExistException;
import com.loafle.overflow.module.member.exception.SignInPwNotExactException;
import com.loafle.overflow.module.member.exception.SignInPwNotMatchException;
import com.loafle.overflow.module.member.model.Member;
import com.loafle.overflow.module.meta.model.MetaMemberStatus;
import org.springframework.beans.factory.annotation.Autowired;
@ -23,7 +23,7 @@ public class MemberService {
@Autowired
private EmailAuthService emailAuthService;
public Member signin(String signinId, String signinPw) throws SignInIdNotExistException, EmailNotConfirmedException, SignInPwNotExactException {
public Member signin(String signinId, String signinPw) throws SignInIdNotExistException, EmailNotConfirmedException, SignInPwNotMatchException {
Member m = this.memberDAO.findByEmail(signinId);
if ( null == m ) {
@ -37,7 +37,7 @@ public class MemberService {
BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
Boolean match = passwordEncoder.matches(signinPw, m.getPw());
if(!match) {
throw new SignInPwNotExactException();
throw new SignInPwNotMatchException();
}
return m;

View File

@ -1,36 +0,0 @@
package com.loafle.overflow.proxy;
import java.lang.reflect.Method;
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 cls, String methodName, Class<?> ...type) {
if (cls == null) {
return null;
}
Method m = null;
try {
m = cls.getMethod(methodName, type);
} catch (NoSuchMethodException e) {
return MethodSeeker.getMethodType(cls.getSuperclass(), methodName, type);
}
return m;
}
}

View File

@ -2,10 +2,17 @@ package com.loafle.overflow.proxy;
import com.google.gson.internal.Primitives;
import com.google.protobuf.ByteString;
import com.loafle.overflow.proxy.exception.InvalidParameterException;
import com.loafle.overflow.proxy.exception.InvalidRequestException;
import com.loafle.overflow.proxy.exception.NoSuchMethodException;
import com.loafle.overflow.proxy.exception.NoSuchServiceException;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.JavaType;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
@ -50,7 +57,7 @@ public class ServiceInvoker {
return serviceCache.bean;
}
private MethodCache getMethodCache(String methodName) throws NoSuchMethodException, ClassNotFoundException {
private MethodCache getMethodCache(String methodName) throws NoSuchMethodException {
MethodCache methodCache = methodCacheMap.get(methodName);
if (null != methodCache) {
return methodCache;
@ -101,27 +108,36 @@ public class ServiceInvoker {
}
}
private Cache getServiceCache(String serviceName) {
private Cache getServiceCache(String serviceName) throws NoSuchServiceException {
Cache serviceCache = serviceCacheMap.get(serviceName);
if (null == serviceCache) {
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();
}
return serviceCache;
}
private Object getValue(Type parameterType, String json) throws IOException {
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())) {
return objectMapper.readValue(json, targetType);
try {
return objectMapper.readValue(json, targetType);
} catch (IOException e) {
throw new InvalidParameterException();
}
}
return objectMapper.convertValue(json, targetType);
}
private Object[] getParameters(Type[] parameterTypes, List<ByteString> params) throws IOException {
private Object[] getParameters(Type[] parameterTypes, List<ByteString> params) throws InvalidParameterException {
if (null == parameterTypes || null == params) {
return null;
}
@ -140,18 +156,40 @@ public class ServiceInvoker {
}
public String invoke(String serviceName, String methodName, List<ByteString> params) throws NoSuchMethodException, ClassNotFoundException, IOException, InvocationTargetException, IllegalAccessException {
public String invoke(String serviceName, String methodName, List<ByteString> params) throws Throwable {
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 = methodCache.method.invoke(serviceCache.getBean(), parameters);
Object result = null;
String jsonInString = null;
try {
result = methodCache.method.invoke(serviceCache.getBean(), parameters);
} catch (IllegalAccessException e) {
throw new InvalidRequestException();
} catch (InvocationTargetException e) {
throw e.getTargetException();
}
String jsonInString = objectMapper.writeValueAsString(result);
try {
jsonInString = objectMapper.writeValueAsString(result);
} catch (IOException e) {
throw new InvalidRequestException();
}
return jsonInString;
}

View File

@ -4,11 +4,11 @@ import com.loafle.overflow.api.OverflowApiServerGrpc;
import com.loafle.overflow.api.ServerInput;
import com.loafle.overflow.api.ServerOutput;
import io.grpc.ServerBuilder;
import io.grpc.StatusException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.logging.Logger;
/**
@ -66,34 +66,20 @@ public class ServiceProxy {
String result = null;
try {
result =this.serviceInvoker.invoke(request.getTarget(), request.getMethod(), request.getParamsList().asByteStringList());
result = this.serviceInvoker.invoke(request.getTarget(), request.getMethod(), request.getParamsList().asByteStringList());
ServerOutput reply = ServerOutput.newBuilder()
.setResult(result)
.build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (StatusException e) {
logger.warning(e.toString());
responseObserver.onError(e);
} catch (ClassNotFoundException e) {
e.printStackTrace();
responseObserver.onError(e);
} catch (IOException e) {
e.printStackTrace();
responseObserver.onError(e);
} catch (InvocationTargetException e) {
e.printStackTrace();
responseObserver.onError(e);
} catch (IllegalAccessException e) {
e.printStackTrace();
responseObserver.onError(e);
} catch (Exception e) {
e.printStackTrace();
} catch (Throwable e) {
responseObserver.onError(e);
}
}
}

View File

@ -0,0 +1,10 @@
package com.loafle.overflow.proxy.exception;
import com.loafle.overflow.commons.exception.OverflowException;
import io.grpc.Status;
public class InvalidParameterException extends OverflowException {
public InvalidParameterException() {
super(Status.fromCode(Status.Code.INVALID_ARGUMENT).withDescription(InvalidParameterException.class.getSimpleName()));
}
}

View File

@ -0,0 +1,10 @@
package com.loafle.overflow.proxy.exception;
import com.loafle.overflow.commons.exception.OverflowException;
import io.grpc.Status;
public class InvalidRequestException extends OverflowException {
public InvalidRequestException() {
super(Status.fromCode(Status.Code.INTERNAL).withDescription(InvalidRequestException.class.getSimpleName()));
}
}

View File

@ -0,0 +1,10 @@
package com.loafle.overflow.proxy.exception;
import com.loafle.overflow.commons.exception.OverflowException;
import io.grpc.Status;
public class NoSuchMethodException extends OverflowException {
public NoSuchMethodException() {
super(Status.fromCode(Status.Code.UNIMPLEMENTED).withDescription(NoSuchMethodException.class.getSimpleName()));
}
}

View File

@ -0,0 +1,10 @@
package com.loafle.overflow.proxy.exception;
import com.loafle.overflow.commons.exception.OverflowException;
import io.grpc.Status;
public class NoSuchServiceException extends OverflowException {
public NoSuchServiceException() {
super(Status.fromCode(Status.Code.UNAVAILABLE).withDescription(NoSuchServiceException.class.getSimpleName()));
}
}

View File

@ -27,7 +27,7 @@ public class ServiceInvokerTest {
private ServiceInvoker serviceInvoker;
@Test
public void invoke() throws Exception {
public void invoke() throws Throwable {
// List<ByteString> params = new ArrayList<>(2);
// params.add(ByteString.copyFromUtf8("[{\"id\": 1," +