ITeye博客

Struts2 自定义拦截器

hwpok · 2008-10-13 14:29 · Struts · Markdown

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->package org.roadway.wisp.zd.util;

import org.apache.log4j.Logger;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

/**
 * @author Huyvanpull
 * 
 */
@SuppressWarnings("serial")
public class ExceptionInterceptor extends AbstractInterceptor
{
    private Logger logger = Logger.getLogger(ExceptionInterceptor.class);
    
    private String interceptorName;
    
    @Override
    public String intercept(ActionInvocation invocation) throws Exception
    {
        this.logger.debug("进入" + this.getInterceptorName());
        String result = null;
        try
        {
            result = invocation.invoke();
        }
        catch (Exception exception)
        {
            this.logger.error(this.getExceptionInfo(exception));
            throw exception;
        }
        return result;
    }
    
    private String getExceptionInfo(Exception exception)
    {
        StringBuffer bExceptionInfo = new StringBuffer();
        bExceptionInfo.append(exception.toString());
        bExceptionInfo.append("\n\t");
        
        StackTraceElement[] stackTraceElements = exception.getStackTrace();
        for (int i = 0; i < stackTraceElements.length; i++)
        {
            bExceptionInfo.append("[" + this.getInterceptorName() + "] "
                    + stackTraceElements[i].toString() + "\n\t");
        }
        return bExceptionInfo.toString();
    }
    
    public String getInterceptorName()
    {
        return interceptorName;
    }
    
    public void setInterceptorName(String interceptorName)
    {
        this.interceptorName = interceptorName;
    }
    
}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。