随笔-6  评论-0  文章-3  trackbacks-0

package com.gxlu.common.web.webwork.intercepter;

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() {
 }
}

posted on 2006-11-22 22:57 frogfool 阅读(347) 评论(0)  编辑  收藏 所属分类: WebWork

只有注册用户登录后才能发表评论。


网站导航: