Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1969412
  • 博文数量: 606
  • 博客积分: 9991
  • 博客等级: 中将
  • 技术积分: 5725
  • 用 户 组: 普通用户
  • 注册时间: 2008-07-17 19:07
文章分类

全部博文(606)

文章存档

2011年(10)

2010年(67)

2009年(155)

2008年(386)

分类: Java

2009-08-25 16:11:14

 
 

package com.jeecms.auxiliary.logger;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.jeecms.auxiliary.entity.SystermOperationLog;
import com.jeecms.auxiliary.manager.SystermOperationLogMng;
import com.ponyjava.common.util.ComUtils;

/**
 * @author fisher 2009-8-24 下午02:32:03
 *
 * 拦截指定的操作,并写操作日志 基类。
 */

@Aspect
@Service("BaseLogger")
public abstract class BaseLogger extends com.jeecms.cms.CmsSysAction{

    private static final Logger log = LoggerFactory.getLogger(BaseLogger.class);

    @Resource
    private SystermOperationLogMng systermOperationLogMng;

    /**
     * 方法说明
     *
     * @param call
     *             回调参数
     * @param logType
     *             日志类型
     * @param operaType
     *             操作类型
     * @param logContent
     *             日志内容
     * @return
     * @throws Throwable
     *
     * @author fisher
     */

    public void profile(ProceedingJoinPoint call, String logType, String operaType, String logContent)
            throws Throwable {

        // 获取客户端request对象

        HttpServletRequest request = ServletActionContext.getRequest();
        /*
         * 构建log对象。
         */

        SystermOperationLog entity = new SystermOperationLog();
        entity.setLogType(logType);
        entity.setOperaType(operaType);
        entity.setOperaIP(getClientAddress(request));
        entity.setOperatorName(getUser().getLoginName());
        entity.setOperatorID(getUser().getId());
        entity.setOperaDT(ComUtils.now());
        entity.setLogContent(logContent);
        
        /*
         * 写log。
         */

        try {
            systermOperationLogMng.save(entity);
        } catch (Exception e) {
            log.error("cannot write system operation log.", e);
        }
    }
    

    /**
     * 获取客户端调用的IP
     *
     * @param request
     * @return
     *
     * @author fisher
     */

    private final String getClientAddress(HttpServletRequest request) {
        String address = request.getHeader("X-Forwarded-For");
     if (address != null ) {
     return address;
     }
     return request.getRemoteAddr();
    }

}

 


package com.jeecms.auxiliary.logger;

import java.util.Iterator;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.jeecms.cms.entity.DataSynOption;

/**
 * @author fisher 2009-8-24 下午02:32:03
 *
 * 数据采集操作日志记录
 */
@Aspect
@Service
public class CollFilterLogger extends com.jeecms.auxiliary.logger.BaseLogger {

 private static final Logger log = LoggerFactory.getLogger(CollFilterLogger.class);
 
 private static final String CF_PARAMITEMCODE = "110";


 /**
  * 数据采集保存日志记录(拦截器)
  *
  * @param call
  *    回调参数
  * @return
  *    返回被拦截的方法的返回值。
  * @throws Throwable
  *
  * @author fisher
  */
 @Around("execution(* com.ponyjava.common.hibernate3.BaseManager.save(*)) && target(com.jeecms.cms.manager.DataSynOptionMng)")
 public Object saveProfile(ProceedingJoinPoint call)
   throws Throwable {
  /*
   * 执行被拦截的方法
   */
  Object value = call.proceed();

  DataSynOption dataSynOption = (DataSynOption) value;

  profile(call, CF_PARAMITEMCODE, "C", String.format("dataSynOption id: %s, targetCode: %s, sourceCode: %s",
    dataSynOption.getId(), dataSynOption.getTargetCode(), dataSynOption.getSourceCode()));
  
  return value;
 }
 
 /**
  * 数据采集更新日志记录(拦截器)
  *
  * @param call
  *    回调参数
  * @return
  *    返回被拦截的方法的返回值。
  * @throws Throwable
  *
  * @author fisher
  */

 @Around("execution(* com.ponyjava.common.hibernate3.BaseManager.update(*)) && target(com.jeecms.cms.manager.DataSynOptionMng)")
 public Object updateProfile(ProceedingJoinPoint call)
   throws Throwable {
  /*
   * 执行被拦截的方法
   */
  Object value = call.proceed();

  DataSynOption dataSynOption = (DataSynOption) value;
  
  profile(call, CF_PARAMITEMCODE, "U", String.format("dataSynOption id: %s, targetCode: %s, sourceCode: %s",
    dataSynOption.getId(), dataSynOption.getTargetCode(), dataSynOption.getSourceCode()));
  
  return value;
 }
 
 /**
  * 数据采集删除日志记录(拦截器)
  *
  * @param call
  *    回调参数
  * @return
  *    返回被拦截的方法的返回值。
  * @throws Throwable
  *
  * @author fisher
  */
 @Around("execution(* com.ponyjava.common.hibernate3.BaseManager.delete*(java.io.Serializable)) && target(com.jeecms.cms.manager.DataSynOptionMng) ")
 public Object deleteProfile(ProceedingJoinPoint call)
   throws Throwable {
  /*
   * 执行被拦截的方法
   */
  Object value = call.proceed();

  DataSynOption dataSynOption = (DataSynOption) value;

  profile(call, CF_PARAMITEMCODE, "D", String.format("dataSynOption id: %s, targetCode: %s, sourceCode: %s",
    dataSynOption.getId(), dataSynOption.getTargetCode(), dataSynOption.getSourceCode()));
  
  return value;
 }
 
 /**
  * 数据采集数组删除日志记录(拦截器)
  *
  * @param call
  *    回调参数
  * @return
  *    返回被拦截的方法的返回值。
  * @throws Throwable
  *
  * @author fisher
  */
 @SuppressWarnings("unchecked")
 @Around("execution(java.util.List<*> com.ponyjava.common.hibernate3.BaseManager.delete*(java.io.Serializable[])) && target(com.jeecms.cms.manager.DataSynOptionMng)")
 public Object deletesProfile(ProceedingJoinPoint call)
   throws Throwable {
  /*
   * 执行被拦截的方法
   */
  Object value = call.proceed();

  java.util.List dataSynOptionList = (java.util.List) value;
  DataSynOption dataSynOption;
  for(Iterator it = dataSynOptionList.iterator(); it.hasNext();) {
   dataSynOption = (DataSynOption) it.next();
   profile(call, CF_PARAMITEMCODE, "D", String.format("dataSynOption id: %s, targetCode: %s, sourceCode: %s",
     dataSynOption.getId(), dataSynOption.getTargetCode(), dataSynOption.getSourceCode()));
  }
  
  return value;
 }

}

 

package com.ponyjava.common.hibernate3;

import java.io.Serializable;
import java.util.List;

import com.ponyjava.common.page.Pagination;

public interface BaseManager<T extends Serializable> {
    /**
     * 通过ID查找对象
     *
     * @param id
     * 记录的ID
     * @return 实体对象
     */

    public T findById(Serializable id);

    public T load(Serializable id);

    /**
     * 查找所有对象
     *
     * @return 对象列表
     */

    public List<T> findAll();

    public Pagination findAll(int pageNo, int pageSize, OrderBy... orderBys);

    /**
     * 通过示例对象查找对象列表
     *
     * @param eg
     * 示例对象
     * @param anyWhere
     * 是否模糊查询。默认false。
     * @param conds
     * 排序及is null。分别为OrderBy和String。
     * @param exclude
     * 排除属性
     * @return
     */

    public List<T> findByEgList(T eg, boolean anyWhere, Condition[] conds,
            String... exclude);

    public List<T> findByEgList(T eg, boolean anyWhere, String... exclude);

    public List<T> findByEgList(T eg, Condition[] conds, String... exclude);

    public List<T> findByEgList(T eg, boolean anyWhere, Condition[] conds,
            int firstResult, int maxResult, String... exclude);

    public List<T> findByEgList(T eg, boolean anyWhere, int firstResult,
            int maxResult, String... exclude);

    public List<T> findByEgList(T eg, Condition[] conds, int firstResult,
            int maxResult, String... exclude);

    public List<T> findByEgList(T eg, String... exclude);

    public Pagination findByEg(T eg, boolean anyWhere, Condition[] conds,
            int pageNo, int pageSize, String... exclude);

    public Pagination findByEg(T eg, boolean anyWhere, int pageNo,
            int pageSize, String... exclude);

    public Pagination findByEg(T eg, Condition[] conds, int pageNo,
            int pageSize, String... exclude);

    public Pagination findByEg(T eg, int pageNo, int pageSize,
            String... exclude);

    /**
     * 根据Updater更新对象
     *
     * @param updater
     */

    public Object updateByUpdater(Updater updater);

    public Object updateDefault(Object entity);

    /**
     * 保存对象
     *
     * @param entity
     * 实体对象
     * @return 操作信息
     */

    public T save(T entity);

    public Object update(Object o);

    public Object saveOrUpdate(Object o);

    public void delete(Object o);

    /**
     * 根据ID删除记录
     *
     * @param id
     * 记录ID
     */

    public T deleteById(Serializable id);

    /**
     * 根据ID数组删除记录,当发生异常时,操作终止并回滚
     *
     * @param ids
     * 记录ID数组
     * @return 删除的对象
     */

    public List<T> deleteById(Serializable[] ids);

    /**
     * 保存并刷新对象,避免many-to-one属性不完整
     *
     * @param entity
     */

    public T saveAndRefresh(T entity);
    
    /**
     *
     * 用SQL直接查询
     * @param sql
     * @return
     */

    public List executeQuery(String sql, List paramsList);
    
    /**
     *
     * 用SQL直接查询
     * @param sql
     * @param params
     * @param object
     * @return
     */

    public List getListByExecuteSQLPagination(String sql,String[] params,
            Object[] object);
    
    /**
     *
     * 用SQL直接查询
     * @param sql
     * @param params
     * @param object
     * @param pageNum
     * @param pageSize
     * @param count
     * @return
     */

    public List getListByExecuteSQLPagination(String sql,String[] params,
            Object[] object,int pageNum,int pageSize,int count);
    
    /**
     *
     * @param sql
     * @param values
     * @return
     */

    public Object executeSQL(final String sql, final Object[] values);
    
    /**
     * 使对象变为脱管状态
     * @param obj
     */

    public void evict(Object obj);
}

其中BaseManager类下的方法都是泛型的,所以,必须定义target 对象,否则不能拦截到方法。 target(子类) 

target - 限定匹配特定的连接点(使用Spring AOP的时候方法的执行),其中目标对象(被代理的应用对象)是指定类型的实例。

阅读(4567) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~