锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.gxlu.common.web.webwork.action.AjaxJSONAction;
import com.gxlu.common.web.webwork.action.AjaxUpdaterAction;
import com.gxlu.common.web.webwork.action.AjaxXMLAction;
import com.opensymphony.webwork.ServletActionContext;
import com.opensymphony.xwork.Action;
import com.opensymphony.xwork.ActionInvocation;
import com.opensymphony.xwork.interceptor.Interceptor;
/**
聽* Interceptor to handle Exceptions thrown by Actions.
聽* <p>
聽* <a href="ExceptionHandlerInterceptor.java.html"> <i>View Source </i> </a>
聽* </p>
聽*聽
聽*/
public class ExceptionInterceptor implements Interceptor {
聽public static final String EXCEPTION = "exception";
聽protected final Log logger = LogFactory.getLog(getClass());
聽private Map exceptionMappings;
聽/**
聽 * Set the mappings between exception class names and result names.
聽 *
聽 * @param mappings
聽 *聽聽聽聽聽聽聽 fully qualified exception class names as keys, and result names as
聽 *聽聽聽聽聽聽聽 values
聽 */
聽public void setExceptionMappings(Properties mappings) throws ClassNotFoundException {
聽聽this.exceptionMappings = new HashMap();
聽聽for (Iterator it = mappings.keySet().iterator(); it.hasNext();) {
聽聽聽String exceptionClassName = (String) it.next();
聽聽聽String viewName = mappings.getProperty(exceptionClassName);
聽聽聽Class exceptionClass = Class.forName(exceptionClassName, true, Thread.currentThread().getContextClassLoader());
聽聽聽this.exceptionMappings.put(exceptionClass, viewName);
聽聽}
聽}
聽/**
聽 * Invoke action and if an exception occurs, route it to the mapped result.
聽 */
聽public String intercept(ActionInvocation invocation) throws Exception {
聽聽String result = null;
聽聽try {
聽聽聽result = invocation.invoke();
聽聽} catch (Throwable e) {
聽聽聽logger.error(e);
聽聽聽//
聽聽聽result = EXCEPTION;
聽聽聽//
聽聽聽StringWriter writer = new StringWriter();
聽聽聽e.printStackTrace(new java.io.PrintWriter(writer));
聽聽聽ServletActionContext.getRequest().setAttribute("_exception_stack_trace_", writer.toString());
聽聽聽ServletActionContext.getRequest().setAttribute("_exception_message_", e.getMessage());
聽聽聽writer = null;
聽聽聽Action action = invocation.getAction();
聽聽聽if(action instanceof AjaxJSONAction) {聽聽聽聽
聽聽聽聽return "exception.json";
聽聽聽} else if(action instanceof AjaxXMLAction) {
聽聽聽聽return "exception.xml";
聽聽聽} else if(action instanceof AjaxUpdaterAction) {
聽聽聽聽return "exception.updater";
聽聽聽}
聽聽聽/**
聽聽聽 * // check for specific mappings if (this.exceptionMappings !=
聽聽聽 * null) { for (Iterator it =
聽聽聽 * this.exceptionMappings.keySet().iterator(); it.hasNext();) {
聽聽聽 * Class exceptionClass = (Class) it.next(); if
聽聽聽 * (exceptionClass.isInstance(ex)) { result = (String)
聽聽聽 * this.exceptionMappings.get(exceptionClass); } } } if (null ==
聽聽聽 * result || "".equals(result)) { result = EXCEPTION; }
聽聽聽 */
聽聽}
聽聽return result;
聽}
聽public void destroy() {
聽}
聽public void init() {
聽}
}