コード例 #1
0
  @Override
  public Result invoke(Invoker<?> invoker, Invocation invocation) throws CallerException {
    String reqSV = invoker.getInterface().getName();
    String reqMethod = invocation.getMethodName();
    Object[] requestParams = invocation.getArguments();
    // 交易序列
    String tradeSeq = UUIDUtil.genId32();
    // 打印请求参数明细
    if (CollectionUtil.isEmpty(requestParams)) {
      LOG.info(
          "TRADE_SEQ:{},拦截类型:{},请求类(接口):{},请求方法:{},请求参数:{}",
          tradeSeq,
          "Dubbo请求参数拦截(无参数)",
          reqSV,
          reqMethod,
          "");
    } else {
      LOG.info(
          "TRADE_SEQ:{},拦截类型:{},请求类(接口):{},请求方法:{},请求参数:{}",
          tradeSeq,
          "Dubbo请求参数拦截(有参数)",
          reqSV,
          reqMethod,
          JSON.toJSONString(requestParams));
    }
    // ThreadLocalUtils.set(REQUEST_PARAM, tradeSeq);
    // 执行结果
    Result result = null;
    try {
      // 校验租户ID是否存在
      /*boolean isOK=validateTenantId(requestParams);
      if(!isOK){
      	throw new BusinessException(MyXAppPaaSConstant.ExceptionCode.PARAM_IS_NULL, "租户ID(tenantId)不能为空!");
      }*/
      result = invoker.invoke(invocation);
      if (result.hasException()) {
        LOG.error(
            "TRADE_SEQ:{},Dubbo服务端执行{}类中的{}方法发生异常",
            tradeSeq,
            reqSV,
            reqMethod,
            result.getException());
        throw DubboExceptAssembler.assemble(result.getException());
      }
      LOG.info(
          "TRADE_SEQ:{},拦截类型:{},请求结果:{}",
          tradeSeq,
          "Dubbo请求返回结果拦截",
          JSON.toJSONString(result.getValue()));

      return result;
    } catch (Exception ex) {
      LOG.error("TRADE_SEQ:{},执行{}类中的{}方法发生异常:", tradeSeq, reqSV, reqMethod, ex);
      RpcResult r = new RpcResult();
      r.setException(DubboExceptAssembler.assemble(ex));
      return r;
    }
  }
コード例 #2
0
ファイル: GenericImplFilter.java プロジェクト: rx78zlm/dubbo
  public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    String generic = invoker.getUrl().getParameter(Constants.GENERIC_KEY);
    if (ProtocolUtils.isGeneric(generic)
        && !Constants.$INVOKE.equals(invocation.getMethodName())
        && invocation instanceof RpcInvocation) {
      RpcInvocation invocation2 = (RpcInvocation) invocation;
      String methodName = invocation2.getMethodName();
      Class<?>[] parameterTypes = invocation2.getParameterTypes();
      Object[] arguments = invocation2.getArguments();

      String[] types = new String[parameterTypes.length];
      for (int i = 0; i < parameterTypes.length; i++) {
        types[i] = ReflectUtils.getName(parameterTypes[i]);
      }
      Object[] args = PojoUtils.generalize(arguments);

      invocation2.setMethodName(Constants.$INVOKE);
      invocation2.setParameterTypes(GENERIC_PARAMETER_TYPES);
      invocation2.setArguments(new Object[] {methodName, types, args});
      Result result = invoker.invoke(invocation2);

      if (!result.hasException()) {
        Object value = result.getValue();
        try {
          Method method = invoker.getInterface().getMethod(methodName, parameterTypes);
          return new RpcResult(
              PojoUtils.realize(value, method.getReturnType(), method.getGenericReturnType()));
        } catch (NoSuchMethodException e) {
          throw new RpcException(e.getMessage(), e);
        }
      } else if (result.getException() instanceof GenericException) {
        GenericException exception = (GenericException) result.getException();
        try {
          String className = exception.getExceptionClass();
          Class<?> clazz = ReflectUtils.forName(className);
          Throwable targetException = null;
          Throwable lastException = null;
          try {
            targetException = (Throwable) clazz.newInstance();
          } catch (Throwable e) {
            lastException = e;
            for (Constructor<?> constructor : clazz.getConstructors()) {
              try {
                targetException =
                    (Throwable)
                        constructor.newInstance(new Object[constructor.getParameterTypes().length]);
                break;
              } catch (Throwable e1) {
                lastException = e1;
              }
            }
          }
          if (targetException != null) {
            try {
              Field field = Throwable.class.getDeclaredField("detailMessage");
              if (!field.isAccessible()) {
                field.setAccessible(true);
              }
              field.set(targetException, exception.getExceptionMessage());
            } catch (Throwable e) {
              logger.warn(e.getMessage(), e);
            }
            result = new RpcResult(targetException);
          } else if (lastException != null) {
            throw lastException;
          }
        } catch (Throwable e) {
          throw new RpcException(
              "Can not deserialize exception "
                  + exception.getExceptionClass()
                  + ", message: "
                  + exception.getExceptionMessage(),
              e);
        }
      }
      return result;
    }

    if (invocation.getMethodName().equals(Constants.$INVOKE)
        && invocation.getArguments() != null
        && invocation.getArguments().length == 3
        && ProtocolUtils.isGeneric(generic)) {

      if (ProtocolUtils.isJavaGenericSerialization(generic)) {
        Object[] args = (Object[]) invocation.getArguments()[2];

        for (Object arg : args) {
          if (!(byte[].class == arg.getClass())) {
            error(arg.getClass().getName());
          }
        }
      }

      ((RpcInvocation) invocation)
          .setAttachment(
              Constants.GENERIC_KEY, invoker.getUrl().getParameter(Constants.GENERIC_KEY));
    }
    return invoker.invoke(invocation);
  }