CGLIB动态代理应用-java使用记录操作日志

来源:互联网 发布:淘宝天猫优惠券秒杀 编辑:程序博客网 时间:2024/06/13 10:56

1.调用动态代理记录操作。

public class CommandPublishSearchAction extends Action{

    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        SearchForm spform=(SearchForm)form;
        String clear=request.getParameter("clear");
        
        if(clear!=null){
            spform.setKeyWord(null);
            spform.setPageCount(null);
        }
        
        String pageNum=spform.getPageCount();
        
        if(pageNum==null||"".equals(pageNum)){
            pageNum="1";
        }
        
    
        HttpSession session = request.getSession();
        LoginDto loginDto=(LoginDto)session.getAttribute("admin");

   


        //动态代理记录操作

        OperationProxyUtil factory=new OperationProxyUtil();
        CommandDao dao=(CommandDao)factory.createProxyIntance(new CommandDao(), 17, loginDto);
        Page page=dao.selectCommandPublishlogList(spform.getKeyWord(), 1, pageNum);        

       


        request.setAttribute("page", page);

        request.setAttribute("commadPublishlogList", page.getList());
        
        return mapping.findForward("list");
    }

}





2.CGLIB动态代理应用

mport java.lang.reflect.Method;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import com.streetlight.log.dao.UserOperationLogDao;
import com.streetlight.login.model.dto.LoginDto;

public class OperationProxyUtil implements MethodInterceptor {

    private Object targetObject;

    private int proxyType = 0;

    private LoginDto dto = null;

    public Object createProxyIntance(Object targetObj, int proxyType,
            LoginDto loginDto) {
        this.dto = loginDto;
        this.proxyType = proxyType;
        this.targetObject = targetObj;

        Enhancer enh = new Enhancer();
        enh.setSuperclass(this.targetObject.getClass());
        enh.setCallback(this);

        return enh.create();
    }

    public Object intercept(Object proxy, Method method, Object[] arg,
            MethodProxy methodProxy) throws Throwable {

        Object result = null;
        result = methodProxy.invoke(targetObject, arg);

        operation(method, result);

        return result;
    }

    private void operation(Method method, Object returnValue) {
        UserOperationLogDao logDao = new UserOperationLogDao();
        String logContext = "", model = "";
        boolean isExecu = true;
        
        //proxyType=16
        
        if (proxyType == 0) { // 策略控制 CommandDao
            if ("add".equals(method.getName())) {
                logContext = "添加";
                isExecu = (Boolean) returnValue;
            } else if ("update".equals(method.getName())) {
                logContext = "修改";
                isExecu = (Boolean) returnValue;
            } else if ("delComamdByName".equals(method.getName())) {
                logContext = "删除";
                isExecu = (Boolean) returnValue;
            }
            model = "策略控制";
        }else if(proxyType == 11){//策略控制
            if("selectCommandList".equals(method.getName())){
                logContext = "查询";
            }
             model = "策略控制";
        }else if(proxyType == 1||proxyType == 2){//能耗统计 EnergyManageDao
            if("selectAreaStatisticsInfo".equals(method.getName())){
                logContext = "区域查询";
            }else if("selectRoadStatisticsInfo".equals(method.getName())){
                logContext = "路段查询";
            }else if("selectLampStatisticsInfo".equals(method.getName())){
                logContext = "灯杆查询";
            
            }
            model = proxyType==1?"能耗统计":"能耗分析";
            
        }else if(proxyType == 15||proxyType == 16){//能耗打印            
             logContext = "打印";
             model = proxyType==15?"能耗统计":"能耗分析";
            
        }else if(proxyType == 3){//寿命统计 LifeStatisticsDao
            if("selectLightLiefInfo".equals(method.getName())){
                logContext = "查询";
            }
            model = "寿命统计";
        }else if(proxyType == 4){//寿命统计打印 LifeStatisticsDao
            if("selectLightLiefInfo".equals(method.getName())){
                logContext = "打印";
            
            }
            model = "寿命统计";
        }else if(proxyType == 5){//手动控制
            if("addHandExecCommand".equals(method.getName())){
                logContext = "命令下发";
                isExecu = (Boolean) returnValue;
            }
            model = "手动控制";
        }else if(proxyType == 6){//策略发布 CommandDao
            if("addExecCommand".equals(method.getName())){
                logContext = "策略发布";
                isExecu = (Boolean) returnValue;
            }
            model = "策略控制";
        }else if(proxyType==7){//维修记录管理
            if ("add".equals(method.getName())) {
                logContext = "添加";
                isExecu = (Boolean) returnValue;
            } else if ("update".equals(method.getName())) {
                logContext = "修改";
                isExecu = (Boolean) returnValue;
            } else if ("del".equals(method.getName())) {
                logContext = "删除";
                isExecu = (Boolean) returnValue;
            }else if ("selectRepairList".equals(method.getName())){
                logContext = "查询";
            }
            model = "维修记录管理";
        }else if(proxyType==8){//维修记录管理
            if ("selectRepairById".equals(method.getName())) {
                logContext = "打印";
            }
            model = "维修记录管理";
        }else if(proxyType==9){//灯具管理
            isExecu=false;
            if ("add".equals(method.getName())) {
                logContext = "添加";
                isExecu = (Boolean) returnValue;
            } else if ("update".equals(method.getName())) {
                logContext = "修改";
                isExecu = (Boolean) returnValue;
            } else if ("del".equals(method.getName())) {
                logContext = "删除";
                isExecu = (Boolean) returnValue;
            }
            model = "灯具管理";
        }else if (proxyType==10){//灯具管理
            if ("selectLightList".equals(method.getName())) {
                logContext = "查询";
            }
            model = "灯具管理";
        }else if (proxyType==12){//系统設置
            if ("upadte".equals(method.getName())) {
                logContext = "参数设定";
                isExecu = (Boolean) returnValue;
            }
            model = "系统设置";
        }else if (proxyType==13){//密码管理
            if ("upadte".equals(method.getName())) {
                logContext = "密码变更";
                isExecu = (Boolean) returnValue;
            }
            model = "密码管理";
        }else if (proxyType==14){//警报处理
            if ("update".equals(method.getName())) {
                logContext = "销警";
                isExecu = (Boolean) returnValue;
            }else if ("selectWarnList".equals(method.getName())) {
                logContext = "查询";
            }
            model = "警报管理";
            
        }else if(proxyType == 17){//已发布策略查询
            if("selectCommandPublishlogList".equals(method.getName())){
                logContext = "查询";
            }
             model = "已发布策略";
        }
        
        //-------------------------------
        if (isExecu) {
            logDao.operationLog(dto.getId(), dto.getUserName(), logContext,
                    model, dto.getIp());
        }
    }

}






3.日志记录

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.streetlight.conn.DBConnection;
import com.streetlight.log.dto.UserOpertionDto;
import com.streetlight.utils.BaseUtils;
import com.streetlight.utils.Page;

public class UserOperationLogDao {

    public UserOperationLogDao() {
    }

    private static Logger log = Logger.getLogger(UserOperationLogDao.class);

    private Connection con = null,con1 = null;

    private PreparedStatement ps = null,ps1=null;

    private ResultSet rs = null,rs1 = null;

    private Page page = new Page();

    public boolean operationLog(UserOpertionDto dto, Connection con) {
        try {
            String sql = "insert into useroperationlog(op_userid,op_username,op_context,op_model,Op_ipaddress) values(?,?,?,?,?)";
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setString(1, dto.getUserId());
            ps.setString(2, dto.getUserName());
            ps.setString(3, dto.getContext());
            ps.setString(4, dto.getModel());
            ps.setString(5, dto.getIpAddress());

            ps.executeUpdate();

            log.info("执行日志添加操作!");
        } catch (Exception ex) {
            log.info("操作useroperationlog发生错误!");
            ex.printStackTrace();
        }
        return true;
    }

    public boolean operationLog(String userId, String userName, String context,
            String model, String ip) {
        try {
            String sql = "insert into useroperationlog(op_userid,op_username,op_context,op_model,Op_ipaddress) values(?,?,?,?,?)";

            con = DBConnection.getConnection();

            ps = con.prepareStatement(sql);
            ps.setString(1, userId);
            ps.setString(2, userName);
            ps.setString(3, context);
            ps.setString(4, model);
            ps.setString(5, ip);

            ps.executeUpdate();

            log.info("执行日志添加操作!");
        } catch (Exception ex) {
            log.info("操作useroperationlog发生错误!");
            ex.printStackTrace();
        } finally {
            DBConnection.free(null, ps, con);
        }
        return true;
    }