|
The Interceptor
NB: All my actions override toString() so the log message contains useful state information. /* * Created on 04-Nov-2003 */ package shared.webwork.interceptor; import java.util.logging.Level; import java.util.logging.Logger; import com.opensymphony.xwork.Action; import com.opensymphony.xwork.ActionContext; import com.opensymphony.xwork.ActionInvocation; import com.opensymphony.xwork.interceptor.Interceptor; /** * Catches any exceptions that occur during the invocation and returns * Action.ERROR as the result code. The exception and the Action are logged. * * @author John Patterson */ public class ExceptionHandlerInterceptor implements Interceptor { private static Logger _logger = Logger.getLogger(ExceptionHandlerInterceptor.class.getName()); /* * (non-Javadoc) * * @see com.opensymphony.xwork.interceptor.Interceptor#intercept(com.opensymphony.xwork.ActionInvocation) */ public String intercept(ActionInvocation invocation) throws Exception { try { return invocation.invoke(); } catch (Exception e) { if (!invocation.isExecuted()) { // the result was not executed String newLine = System.getProperty("line.separator"); String message = "Caught exception while invoking action."; message += newLine + "Action was: " + invocation.getAction(); _logger.log(Level.SEVERE, message, e); ActionContext.getContext().put("exception", e); } return Action.ERROR; } } /* * (non-Javadoc) * * @see com.opensymphony.xwork.interceptor.Interceptor#destroy() */ public void destroy() { } /* * (non-Javadoc) * * @see com.opensymphony.xwork.interceptor.Interceptor#init() */ public void init() { } } These two issues are similar and will be addressed in XW 1.1/WW2.2
Here is another flavor of an exception interceptor. Similiar to the one used in Webwork in Action.
However, this one allows the developer to have explainations of exceptions in the properties "resource" for a given action. eg. exception."methodName"."exceptionName" // example key -->exception.save.ServiceException=there is a problem with your service dude or properties names should look like |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/*
* Created on 04-Nov-2003
*/
package com.buildabreak.actions;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import javax.servlet.ServletResponse;
import javax.servlet.jsp.PageContext;
import com.opensymphony.webwork.ServletActionContext;
import com.opensymphony.xwork.ActionContext;
/**
* Displays the SUCCESS page and makes the stack trace available as a string
* property. If the response is already committed then all we can do is print an error
* message on the page.
*
* @author John Patterson
*/
public class ExceptionAction extends BaseAction
{
private Exception _exception;
public ExceptionAction()
{
_exception = (Exception) ActionContext.getContext().get("exception");
}
public String getStackTrace()
{
if (_exception != null)
{
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
_exception.printStackTrace(printWriter);
return stringWriter.toString();
}
else
{
return null;
}
}
/**
* @return Returns the exception.
*/
public Exception getException()
{
return _exception;
}
/*
* (non-Javadoc)
*
* @see com.opensymphony.xwork.Action#execute()
*/
public String execute() throws Exception
{
PageContext context = ServletActionContext.getPageContext();
ServletResponse response = ServletActionContext.getResponse();
if (response.isCommitted())
{
Writer writer;
if (context != null)
{
writer = context.getOut();
}
else
{
writer = response.getWriter();
}
writer.write("<b>An error occured during the request.</b>");
writer.flush();
return NONE;
}
else
{
return SUCCESS;
}
}
}