hoge66的专栏

-----------------------看到专栏,偷着笑!
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

adapter结构异常记录

Posted on 2017-05-30 16:36  hehoge  阅读(280)  评论(0编辑  收藏  举报

adapter结构异常记录,记录在这个类里,记录数据日志,在148行:

com.creditharmony.adapter.core.service.GeneralHttpService

package com.creditharmony.adapter.core.service;

import java.util.Properties;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.creditharmony.apporveadapter.bean.BaseInfo;
import com.creditharmony.apporveadapter.bean.BaseOutInfo;
import com.creditharmony.apporveadapter.bean.GeneralHttpInfoModel;
import com.creditharmony.apporveadapter.bean.GeneralReturnInfo;
import com.creditharmony.adapter.constant.Constant;
import com.creditharmony.apporveadapter.constant.ErrorType;
import com.creditharmony.adapter.constant.MsgKey;
import com.creditharmony.apporveadapter.exception.AdapterException;
import com.creditharmony.adapter.utils.AdapterUtils;
import com.creditharmony.adapter.utils.Messages;
import com.creditharmony.common.util.PropertyUtil;
import com.creditharmony.common.util.SpringContextHolder;

/**
 * @Class Name GeneralHttpService
 * @author yourname
 * @Create In 2016年12月3日
 */
@Controller
public class GeneralHttpService {
    /** 业务挡板测试区分名. */
    private static final String BAFFLE_FIX = "_Baffle";
    
    /** 日志. */
    private static final Logger logger = Logger.getLogger(GeneralHttpService.class);
    
    /** 属性文件. */
    static Properties properties = PropertyUtil.getProperties(Constant.CONFIG_PROPERTY);
    
    /** HttpServletRequest. */
    @Autowired
    private HttpServletRequest request;
    
    /** 参数记录处理. */
    @Autowired
    private IParamRecord paramRecord;
    
    /**
     * Client端调用统一接口.
     * 实现分发器效果
     * 
     * @param paramObj 调用参数
     * @return 返回参数
     */
    @ResponseBody
    @RequestMapping(value = "http/generalHttpService", method = RequestMethod.POST)
    public String exec(@RequestParam("content") String content) {
        
        // 取得唯一序列号
        String serialNum = AdapterUtils.getSerialNum();
        // 获得调用客户端IP地址
        String clientIp = getClientIP(request, serialNum);
        // 大金融的报文转为Model对象
        GeneralHttpInfoModel paramObj = JSON.parseObject(content, GeneralHttpInfoModel.class);

        logger.info(Messages.get(MsgKey.GENERALSERVICE_INPARAM, new String[] { content }));
        // 参数日志记录: 传入参数
        paramRecord.doInParamRecord(
                serialNum,
                content,
                clientIp,
                paramObj.getServiceName());

        // 取得业务参数Json
        String paramStr = paramObj.getParam();

        logger.info(Messages.get(MsgKey.GENERALSERVICE_INPARAM, new String[] { paramStr }));
        // 参数日志记录: 传入参数
        paramRecord.doInParamRecord(
                serialNum,
                paramStr,
                clientIp,
                paramObj.getServiceName());
        
        /*
         * 处理:取得业务实现类
         * 利用传入的serviceName,通过反射方式取得该业务实际类
         */
        IBaseService baseService = this.initService(paramObj.getServiceName());

        /*
         * 处理:调用实际业务处理
         * 通过父类抽象方法调用实现子类具体业务调用
         */
        GeneralReturnInfo out = new GeneralReturnInfo();
        BaseOutInfo outParam = null;
        // 返回对象参数
        String outParamStr = "";
        try {
            
            // 将传入的JSON报文转为Bean对象
            BaseInfo inBean = (BaseInfo) JSON.parseObject(paramStr, doCreatInObject(paramObj.getInClassName()).getClass());
            inBean.setSerialNum(serialNum);
            outParam = baseService.exec(inBean);
            out.setOutParam(JSON.toJSONString(outParam, SerializerFeature.WriteMapNullValue));

            outParamStr = JSON.toJSONString(out, SerializerFeature.WriteMapNullValue);
            
            /*
             * 处理:返回参数记录处理.
             */
            paramRecord.doOutParamRecord(
                    serialNum,
                    outParamStr);
            logger.info(Messages.get(MsgKey.GENERALSERVICE_OUTPARAM, new String[] { outParamStr }));
        } catch (Exception e) {
            boolean isNewExeption = e instanceof AdapterException;
            
            AdapterException businessException = null;
            // 将新产生的例外封装
            if (isNewExeption == false) {
                businessException = new AdapterException(ErrorType.BUSSINESS_ERROR, e, "接口服务端产生异常.");
            } else {
                businessException = (AdapterException) e;
            }
            
            out.setErrorType(businessException.getErrorType());
            out.setErrorMsg(businessException.getMessage());
            out.setBaseOutInfo(businessException.getInfoOutObj());
            StringBuilder paramSb = new StringBuilder();
            
            // 取得返回对象日志
            paramSb.append("errorType=").append(out.getErrorType())
                    .append(", errorMsg=").append(out.getErrorMsg());
            outParamStr = paramSb.toString();

            /*
             * 处理:异常错误记录处理.
             */
            logger.error(Messages.get(
                    MsgKey.ERROR_SYSTEM_STACK, new String[] { businessException.getExceptionStackTrace() }));
            paramRecord.doExceptionRecord(serialNum, businessException);
            
            /*
             * 处理:返回参数记录处理.
             */
            paramRecord.doOutParamRecord(
                    serialNum,
                    outParamStr);
            logger.info(Messages.get(MsgKey.GENERALSERVICE_OUTPARAM, new String[] { outParamStr }));
        }
        return outParamStr;
    }
    
    /**
     * 反射传入Bean对象.
     * @param className 传入Bean包名+类名
     * @return Bean对象
     */
    private Object doCreatInObject(String className) {
        Object obj = null;
        try {
            Class<?> c = Class.forName(className);
            obj = c.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return obj;
    }
    
    /**
     * 获取客户端IP地址.
     * 
     * @param serialNum 请求唯一序列号
     * @return IP地址
     */
    private String getClientIP(HttpServletRequest request, String serialNum) {
        try {
            if (request.getHeader("x-forwarded-for") == null) {
                return request.getRemoteAddr();
            }
            return request.getHeader("x-forwarded-for");
        } catch (Exception e) {
            // 异常错误记录
            AdapterException businessException = new AdapterException(e);
            paramRecord.doExceptionRecord(serialNum, businessException);
        }
        return "";
    }
    
    /**
     * 业务处理类实现.
     * 通过反射,实现业务对象
     * @param serviceName 业务实现类名
     * @return 业务实现类
     */
    private IBaseService initService(String serviceName) {
        
        String serviceId = "";
        boolean isBaffle = Boolean.parseBoolean(properties.getProperty(serviceName + BAFFLE_FIX));
        // true的场合, 运行挡板程序
        if (isBaffle) {
            // 测试挡板程序
            serviceId = serviceName + BAFFLE_FIX;
        } else {
            serviceId = serviceName;
        }
        IBaseService baseService = SpringContextHolder.getBean(serviceId);
        return baseService;
    }
}