Search in sources :

Example 6 with MotanBizException

use of com.weibo.api.motan.exception.MotanBizException in project motan by weibocom.

the class YarProtocolUtil method convert.

public static YarResponse convert(Response response, String packagerName) {
    YarResponse yarResponse = new YarResponse();
    yarResponse.setId(response.getRequestId());
    yarResponse.setPackagerName(packagerName);
    if (response.getException() != null) {
        if (response.getException() instanceof MotanBizException) {
            yarResponse.setError(response.getException().getCause().getMessage());
        } else {
            yarResponse.setError(response.getException().getMessage());
        }
    } else {
        yarResponse.setRet(response.getValue());
    }
    return yarResponse;
}
Also used : YarResponse(com.weibo.yar.YarResponse) MotanBizException(com.weibo.api.motan.exception.MotanBizException)

Example 7 with MotanBizException

use of com.weibo.api.motan.exception.MotanBizException in project motan by weibocom.

the class ServiceMockFilter method filter.

@Override
public Response filter(Caller<?> caller, Request request) {
    // Do nothing when mock is empty.
    String mockServiceName = caller.getUrl().getParameter(URLParamType.mock.getName());
    if (StringUtils.isEmpty(mockServiceName) || "false".equals(mockServiceName)) {
        return caller.call(request);
    }
    MockInfo info = getServiceStat(caller.getUrl());
    DefaultResponse response = new DefaultResponse();
    if (mockServiceName.startsWith(RETURN_PREFIX)) {
        String value = mockServiceName.substring(RETURN_PREFIX.length());
        try {
            info.callNum.addAndGet(1);
            long sleepTime = caclSleepTime(info);
            Thread.sleep(sleepTime);
            response.setValue(parseMockValue(value));
        } catch (RuntimeException e) {
            if (e.getCause() != null) {
                response.setException(new MotanBizException("mock service call process error", e.getCause()));
            } else {
                response.setException(new MotanBizException("mock service call process error", e));
            }
        } catch (Exception e) {
            throw new IllegalStateException("Illegal mock json value in <motan:service ... mock=\"" + mockServiceName + "\" />");
        }
    } else {
        try {
            Class<?> mockClass = isDefault(mockServiceName) ? ReflectUtil.forName(caller.getInterface().getName() + "Mock") : ReflectUtil.forName(mockServiceName);
            if (!caller.getInterface().isAssignableFrom(mockClass)) {
                throw new MotanFrameworkException("The mock implemention class " + mockClass.getName() + " not implement interface " + caller.getInterface().getName());
            }
            try {
                mockClass.getConstructor();
            } catch (NoSuchMethodException e) {
                throw new IllegalStateException("No such empty constructor \"public " + mockClass.getSimpleName() + "()\" in mock implemention class " + mockClass.getName());
            }
            String methodDesc = ReflectUtil.getMethodDesc(request.getMethodName(), request.getParamtersDesc());
            Method[] methods = mockClass.getMethods();
            boolean invoke = false;
            for (Method method : methods) {
                if (methodDesc.equals(ReflectUtil.getMethodDesc(method))) {
                    Object value = invoke(mockClass.newInstance(), method, request.getArguments(), info);
                    response.setValue(value);
                    invoke = true;
                    break;
                }
            }
            if (!invoke) {
                throw new MotanFrameworkException("Mock method is not found." + methodDesc);
            }
        } catch (ClassNotFoundException e) {
            throw new MotanFrameworkException("Mock service is not found." + (isDefault(mockServiceName) ? caller.getInterface().getName() + "Mock" : mockServiceName));
        } catch (Exception e) {
            if (e.getCause() != null) {
                response.setException(new MotanBizException("mock service call process error", e.getCause()));
            } else {
                response.setException(new MotanBizException("mock service call process error", e));
            }
        }
    }
    return response;
}
Also used : MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) Method(java.lang.reflect.Method) MotanBizException(com.weibo.api.motan.exception.MotanBizException) MotanBizException(com.weibo.api.motan.exception.MotanBizException) InvocationTargetException(java.lang.reflect.InvocationTargetException) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException)

Example 8 with MotanBizException

use of com.weibo.api.motan.exception.MotanBizException in project motan by weibocom.

the class AccessStatisticFilterTest method testFilter.

@SuppressWarnings("unchecked")
public void testFilter() {
    final Request request = mockery.mock(Request.class);
    final Response response = mockery.mock(Response.class);
    final Caller<IHello> caller = mockery.mock(Caller.class);
    final URL url = new URL(MotanConstants.PROTOCOL_MOTAN, NetUtils.getLocalAddress().getHostAddress(), 0, RegistryService.class.getName());
    final Map<String, String> attachments = new HashMap<String, String>();
    attachments.put(URLParamType.host.getName(), URLParamType.host.getValue());
    attachments.put(URLParamType.application.getName(), URLParamType.application.getValue());
    attachments.put(URLParamType.module.getName(), URLParamType.module.getValue());
    // TODO 优化重复代码
    mockery.checking(new Expectations() {

        {
            oneOf(caller).call(request);
            will(returnValue(response));
            atLeast(2).of(caller).getUrl();
            will(returnValue(url));
            oneOf(response).getException();
            will(returnValue(null));
            oneOf(response).getProcessTime();
            will(returnValue(1000L));
            allowing(request).getInterfaceName();
            will(returnValue(IHello.class.getName()));
            allowing(request).getParamtersDesc();
            will(returnValue("mock_param_desc"));
            allowing(request).getMethodName();
            will(returnValue("mock_method_name"));
            atLeast(1).of(request).getAttachments();
            will(returnValue(attachments));
        }
    });
    assertEquals(response, accessStatisticFilter.filter(caller, request));
    mockery.checking(new Expectations() {

        {
            oneOf(caller).call(request);
            will(returnValue(null));
            oneOf(caller).getUrl();
            will(returnValue(url));
            allowing(request).getInterfaceName();
            will(returnValue(IHello.class.getName()));
            allowing(request).getParamtersDesc();
            will(returnValue("mock_param_desc"));
            allowing(request).getMethodName();
            will(returnValue("mock_method_name"));
        }
    });
    assertNull(accessStatisticFilter.filter(caller, request));
    mockery.checking(new Expectations() {

        {
            oneOf(caller).call(request);
            will(returnValue(response));
            oneOf(caller).getUrl();
            will(returnValue(url));
            atMost(2).of(response).getException();
            will(returnValue(new MotanBizException()));
            oneOf(response).getProcessTime();
            will(returnValue(1000L));
            allowing(request).getInterfaceName();
            will(returnValue(IHello.class.getName()));
            allowing(request).getParamtersDesc();
            will(returnValue("mock_param_desc"));
            allowing(request).getMethodName();
            will(returnValue("mock_method_name"));
        }
    });
    assertEquals(response, accessStatisticFilter.filter(caller, request));
    mockery.checking(new Expectations() {

        {
            oneOf(caller).call(request);
            will(returnValue(response));
            oneOf(caller).getUrl();
            will(returnValue(url));
            atMost(2).of(response).getException();
            will(returnValue(new MotanServiceException()));
            oneOf(response).getProcessTime();
            will(returnValue(1000L));
            allowing(request).getInterfaceName();
            will(returnValue(IHello.class.getName()));
            allowing(request).getParamtersDesc();
            will(returnValue("mock_param_desc"));
            allowing(request).getMethodName();
            will(returnValue("mock_method_name"));
        }
    });
    assertEquals(response, accessStatisticFilter.filter(caller, request));
}
Also used : Response(com.weibo.api.motan.rpc.Response) Expectations(org.jmock.Expectations) HashMap(java.util.HashMap) Request(com.weibo.api.motan.rpc.Request) RegistryService(com.weibo.api.motan.registry.RegistryService) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException) IHello(com.weibo.api.motan.protocol.example.IHello) URL(com.weibo.api.motan.rpc.URL) MotanBizException(com.weibo.api.motan.exception.MotanBizException)

Aggregations

MotanBizException (com.weibo.api.motan.exception.MotanBizException)8 MotanServiceException (com.weibo.api.motan.exception.MotanServiceException)3 DefaultResponse (com.weibo.api.motan.rpc.DefaultResponse)3 Method (java.lang.reflect.Method)3 Response (com.weibo.api.motan.rpc.Response)2 YarResponse (com.weibo.yar.YarResponse)2 Expectations (org.jmock.Expectations)2 Test (org.junit.Test)2 Cluster (com.weibo.api.motan.cluster.Cluster)1 MotanFrameworkException (com.weibo.api.motan.exception.MotanFrameworkException)1 IHello (com.weibo.api.motan.protocol.example.IHello)1 RegistryService (com.weibo.api.motan.registry.RegistryService)1 Request (com.weibo.api.motan.rpc.Request)1 URL (com.weibo.api.motan.rpc.URL)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1