package cn.com.css.common.interceptor;
import java.io.IOException;
import java.sql.SQLException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.DataAccessException;
import cn.com.css.common.exception.MSIPSysException;
import cn.com.css.util.SysConstants;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
/**
* 此类是struts2的拦截器当发生错误时<br/>
* 由此类进行拦截并调用系统的异常处理类<br/>
* 生成页面错误信息和打印LOG日志<br/>
*
* @version 1.0
*
*/
@SuppressWarnings("serial")
public class MSIPSysExceptionInterceptor extends AbstractInterceptor {
//log4j实例名
private static final Log errorLog = LogFactory.getLog(SysConstants.LOG_ERROR);
//构造方法
private MSIPSysExceptionInterceptor() {
}
//初始化realPath只在服务器启动时执行一次
@Override
public void init() {
}
//主拦截方法,拦截常用的错误异常,当有异常发生时进行捕捉并打印LOG日志和生成页面的错误提示信息
@Override
public String intercept(ActionInvocation invocation) throws Exception {
String method=invocation.getProxy().getMethod();
String nameSpace = invocation.getProxy().getNamespace().substring(invocation.getProxy().getNamespace().lastIndexOf("/")+1);
String result = "";
String mes = ""
+ "<br><br>nameSpace:" + invocation.getProxy().getNamespace()
+ "<br>action:" + invocation.getProxy().getAction()
+ "<br>method:" + invocation.getProxy().getMethod()
+ "<br><br>" + "具体的错误信息为:";
try {
result = invocation.invoke();
} catch (DataAccessException ex) {
this.logging(method, nameSpace, ex);
invocation.getInvocationContext().put("invocationError","数据库操作失败!" );
throw new MSIPSysException(mes+ ex.getMessage());
} catch (NullPointerException ex) {
this.logging(method, nameSpace, ex);
invocation.getInvocationContext().put("invocationError","调用了未经初始化的对象或者是不存在的对象!" );
throw new MSIPSysException(mes + ex.getMessage());
} catch (IOException ex) {
this.logging(method, nameSpace, ex);
invocation.getInvocationContext().put("invocationError","IO异常!" );
throw new MSIPSysException(mes + ex.getMessage());
} catch (ClassNotFoundException ex) {
this.logging(method, nameSpace, ex);
invocation.getInvocationContext().put("invocationError", "指定的类不存在!" );
throw new MSIPSysException(mes + ex.getMessage());
} catch (ArithmeticException ex) {
this.logging(method, nameSpace, ex);
invocation.getInvocationContext().put("invocationError", "数学运算异常!");
throw new MSIPSysException(mes + ex.getMessage());
} catch (ArrayIndexOutOfBoundsException ex) {
this.logging(method, nameSpace, ex);
invocation.getInvocationContext().put("invocationError", "数组下标越界!" );
throw new MSIPSysException(mes + ex.getMessage());
} catch (IllegalArgumentException ex) {
this.logging(method, nameSpace, ex);
invocation.getInvocationContext().put("invocationError", "方法的参数错误!");
throw new MSIPSysException(mes + ex.getMessage());
} catch (ClassCastException ex) {
this.logging(method, nameSpace, ex);
invocation.getInvocationContext().put("invocationError","类型强制转换错误!");
throw new MSIPSysException(mes + ex.getMessage());
} catch (SecurityException ex) {
this.logging(method, nameSpace, ex);
invocation.getInvocationContext().put("invocationError","违背安全原则异常!");
throw new MSIPSysException(mes + ex.getMessage());
} catch (SQLException ex) {
this.logging(method, nameSpace, ex);
invocation.getInvocationContext().put("invocationError","操作数据库异常!" );
throw new MSIPSysException(mes + ex.getMessage());
} catch (NoSuchMethodError ex) {
this.logging(method, nameSpace, ex);
invocation.getInvocationContext().put("invocationError","方法末找到异常!" );
throw new MSIPSysException(mes + ex.getMessage());
} catch (InternalError ex) {
this.logging(method, nameSpace, ex);
invocation.getInvocationContext().put("invocationError","Java虚拟机发生了内部错误" );
throw new MSIPSysException(mes + ex.getMessage());
} catch (Exception ex) {
this.logging(method, nameSpace, ex);
invocation.getInvocationContext().put("invocationError","程序内部错误,操作失败!" );
throw new MSIPSysException(mes + ex.getMessage());
}
return result;
}
//打印LOG日志
private void logging(String method,String nameSpace,Exception ex)
{
try {
errorLog.debug("-------------------"+nameSpace+"提交的method="+method+"---------------\n");
errorLog.fatal("错误提示:"+ex.getMessage(),ex);
} catch (Exception e) {
errorLog.debug("-------------------loggingMeg.xml文件解析错误---------------\n");
errorLog.fatal("错误提示:"+ex.getMessage(),ex);
}
}
//打印LOG日志
private void logging(String method,String nameSpace,NoSuchMethodError ex)
{
try {
errorLog.debug("-------------------"+nameSpace+"提交的method="+method+"---------------\n");
errorLog.fatal("错误提示:"+ex.getMessage(),ex);
} catch (Exception e) {
errorLog.debug("-------------------loggingMeg.xml文件解析错误---------------\n");
errorLog.fatal("错误提示:"+ex.getMessage(),ex);
}
}
//打印LOG日志
private void logging(String method,String nameSpace,InternalError ex)
{
try {
errorLog.debug("-------------------"+nameSpace+"提交的method="+method+"---------------\n");
errorLog.fatal("错误提示:"+ex.getMessage(),ex);
} catch (Exception e) {
errorLog.debug("-------------------loggingMeg.xml文件解析错误---------------\n");
errorLog.fatal("错误提示:"+ex.getMessage(),ex);
}
}
}