All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mockrunner.struts.DefaultExceptionHandlerConfig Maven / Gradle / Ivy

package com.mockrunner.struts;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ExceptionHandler;
import org.apache.struts.config.ExceptionConfig;

import com.mockrunner.base.NestedApplicationException;

/**
 * The default implementation of {@link ExceptionHandlerConfig}. It uses the Struts
 * exception handling mechanism. Use the various constructors to provide your subclass
 * of ExceptionHandler or to configure exception handling using an
 * instance of ExceptionConfig. The ExceptionConfig class
 * allows you to set the handler class an exception type.
 * Use {@link ActionTestModule#addExceptionHandler} to register an exception handler.
 */
public class DefaultExceptionHandlerConfig implements ExceptionHandlerConfig
{
    private ExceptionConfig exceptionConfig;
    private ExceptionHandler exceptionHandler;
    private Class exceptionClass;
    
    public DefaultExceptionHandlerConfig(ExceptionConfig exceptionConfig)
    {
        this.exceptionConfig = exceptionConfig;
        try
        {
            Class handlerClass = exceptionConfig.getClass().getClassLoader().loadClass(exceptionConfig.getHandler());
            exceptionHandler = (ExceptionHandler)handlerClass.newInstance();
            exceptionClass = exceptionConfig.getClass().getClassLoader().loadClass(exceptionConfig.getType());
        } 
        catch(Exception exc)
        {
            throw new NestedApplicationException(exc);
        }
    }
    
    public DefaultExceptionHandlerConfig(ExceptionHandler exceptionHandler, ExceptionConfig exceptionConfig)
    {
        this.exceptionHandler = exceptionHandler;
        this.exceptionConfig = exceptionConfig;
        this.exceptionConfig.setHandler(exceptionHandler.getClass().getName());
        try
        {
            exceptionClass = exceptionConfig.getClass().getClassLoader().loadClass(exceptionConfig.getType());
        } 
        catch(Exception exc)
        {
            throw new NestedApplicationException(exc);
        }
    }
    
    public DefaultExceptionHandlerConfig(ExceptionHandler exceptionHandler, Class exceptionClass)
    {
        this.exceptionHandler = exceptionHandler;
        this.exceptionClass = exceptionClass;
        this.exceptionConfig = new ExceptionConfig();
        this.exceptionConfig.setHandler(exceptionHandler.getClass().getName());
        this.exceptionConfig.setType(exceptionClass.getName());
    }
    
    public DefaultExceptionHandlerConfig(Class exceptionClass)
    {
        this.exceptionHandler = new ExceptionHandler();
        this.exceptionClass = exceptionClass;
        this.exceptionConfig = new ExceptionConfig();
        this.exceptionConfig.setHandler(ExceptionHandler.class.getName());
        this.exceptionConfig.setType(exceptionClass.getName());
    }
    
    public boolean canHandle(Exception exception)
    {
        return exceptionClass.isInstance(exception);
    }

    public Object handle(Exception exception, ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException
    {
        if(!canHandle(exception)) return null;
        if(null == exceptionHandler) return null;
        return exceptionHandler.execute(exception, exceptionConfig, mapping, form, request, response);
    }

    /**
     * Get the underlying ExceptionConfig. If you did not provide
     * an instance of ExceptionConfig, this class will create one
     * internally.
     * @return the ExceptionConfig
     */
    public ExceptionConfig getExceptionConfig()
    {
        return exceptionConfig;
    }

    /**
     * Get the underlying ExceptionHandler.
     * @return the ExceptionHandler
     */
    public ExceptionHandler getExceptionHandler()
    {
        return exceptionHandler;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy