[jboss-cvs] jboss-seam/src/main/org/jboss/seam/exceptions ...

Gavin King gavin.king at jboss.com
Sun Dec 17 14:46:41 EST 2006


  User: gavin   
  Date: 06/12/17 14:46:41

  Added:       src/main/org/jboss/seam/exceptions           
                        AnnotationErrorHandler.java
                        AnnotationRedirectHandler.java
                        AnnotationRenderHandler.java
                        ConfigErrorHandler.java ConfigRedirectHandler.java
                        ConfigRenderHandler.java DebugPageHandler.java
                        ErrorHandler.java ExceptionHandler.java
                        RedirectHandler.java RenderHandler.java
  Log:
  refactor all the inner classes
  
  Revision  Changes    Path
  1.1      date: 2006/12/17 19:46:41;  author: gavin;  state: Exp;jboss-seam/src/main/org/jboss/seam/exceptions/AnnotationErrorHandler.java
  
  Index: AnnotationErrorHandler.java
  ===================================================================
  /**
   * 
   */
  package org.jboss.seam.exceptions;
  
  import org.jboss.seam.annotations.HttpError;
  
  public class AnnotationErrorHandler extends ErrorHandler
  {
     @Override
     public boolean isHandler(Exception e)
     {
        return e.getClass().isAnnotationPresent(HttpError.class);
     }
     
     @Override
     protected String getMessage(Exception e)
     {
        return e.getClass().getAnnotation(HttpError.class).message();
     }
     
     @Override
     protected int getCode(Exception e)
     {
        return e.getClass().getAnnotation(HttpError.class).errorCode();
     }
     
     @Override
     protected boolean isEnd(Exception e)
     {
        return e.getClass().getAnnotation(HttpError.class).end();
     }
     
     @Override
     protected boolean isRollback(Exception e)
     {
        return e.getClass().getAnnotation(HttpError.class).rollback();
     }
     
     @Override
     public String toString()
     {
        return "ErrorHandler";
     }
  }
  
  
  1.1      date: 2006/12/17 19:46:41;  author: gavin;  state: Exp;jboss-seam/src/main/org/jboss/seam/exceptions/AnnotationRedirectHandler.java
  
  Index: AnnotationRedirectHandler.java
  ===================================================================
  /**
   * 
   */
  package org.jboss.seam.exceptions;
  
  import javax.faces.event.PhaseId;
  
  import org.jboss.seam.annotations.Redirect;
  import org.jboss.seam.contexts.Lifecycle;
  
  public class AnnotationRedirectHandler extends RedirectHandler
  {
     @Override
     public boolean isHandler(Exception e)
     {
        return e.getClass().isAnnotationPresent(Redirect.class) && 
              Lifecycle.getPhaseId()!=PhaseId.RENDER_RESPONSE && 
              Lifecycle.getPhaseId()!=null;
     }
     
     @Override
     protected String getMessage(Exception e)
     {
        return e.getClass().getAnnotation(Redirect.class).message();
     }
     
     @Override
     protected String getViewId(Exception e)
     {
        return e.getClass().getAnnotation(Redirect.class).viewId();
     }
     
     @Override
     protected boolean isEnd(Exception e)
     {
        return e.getClass().getAnnotation(Redirect.class).end();
     } 
  
     @Override
     protected boolean isRollback(Exception e)
     {
        return e.getClass().getAnnotation(Redirect.class).rollback();
     }
     
     @Override
     public String toString()
     {
        return "RedirectHandler";
     }
  }
  
  
  1.1      date: 2006/12/17 19:46:41;  author: gavin;  state: Exp;jboss-seam/src/main/org/jboss/seam/exceptions/AnnotationRenderHandler.java
  
  Index: AnnotationRenderHandler.java
  ===================================================================
  /**
   * 
   */
  package org.jboss.seam.exceptions;
  
  import javax.faces.event.PhaseId;
  
  import org.jboss.seam.annotations.Render;
  import org.jboss.seam.contexts.Lifecycle;
  
  public class AnnotationRenderHandler extends RenderHandler
  {
     @Override
     public boolean isHandler(Exception e)
     {
        return e.getClass().isAnnotationPresent(Render.class) && 
              Lifecycle.getPhaseId()==PhaseId.INVOKE_APPLICATION;
     }
  
     @Override
     protected String getMessage(Exception e)
     {
        return e.getClass().getAnnotation(Render.class).message();
     }
     
     @Override
     protected String getViewId(Exception e)
     {
        return e.getClass().getAnnotation(Render.class).viewId();
     }
  
     @Override
     protected boolean isEnd(Exception e)
     {
        return e.getClass().getAnnotation(Render.class).end();
     }
  
     @Override
     protected boolean isRollback(Exception e)
     {
        return e.getClass().getAnnotation(Render.class).rollback();
     }
     
     @Override
     public String toString()
     {
        return "RenderHandler";
     }
  }
  
  
  1.1      date: 2006/12/17 19:46:41;  author: gavin;  state: Exp;jboss-seam/src/main/org/jboss/seam/exceptions/ConfigErrorHandler.java
  
  Index: ConfigErrorHandler.java
  ===================================================================
  /**
   * 
   */
  package org.jboss.seam.exceptions;
  
  
  public final class ConfigErrorHandler extends ErrorHandler
  {
     private final String message;
     private final boolean conversation;
     private final Class clazz;
     private final int code;
     private final boolean rollback;
  
     public ConfigErrorHandler(String message, boolean conversation, Class clazz, int code, boolean rollback)
     {
        this.message = message;
        this.conversation = conversation;
        this.clazz = clazz;
        this.code = code;
        this.rollback = rollback;
     }
  
     @Override
     protected String getMessage(Exception e)
     {
        return message;
     }
  
     @Override
     protected int getCode(Exception e)
     {
        return code;
     }
  
     @Override
     public boolean isHandler(Exception e)
     {
        return clazz.isInstance(e);
     }
  
     @Override
     protected boolean isEnd(Exception e)
     {
        return conversation;
     }
  
     @Override
     protected boolean isRollback(Exception e)
     {
        return rollback;
     }
  }
  
  
  1.1      date: 2006/12/17 19:46:41;  author: gavin;  state: Exp;jboss-seam/src/main/org/jboss/seam/exceptions/ConfigRedirectHandler.java
  
  Index: ConfigRedirectHandler.java
  ===================================================================
  /**
   * 
   */
  package org.jboss.seam.exceptions;
  
  import javax.faces.event.PhaseId;
  
  import org.jboss.seam.contexts.Lifecycle;
  
  public final class ConfigRedirectHandler extends RedirectHandler
  {
     private final String id;
     private final Class clazz;
     private final boolean conversation;
     private final boolean rollback;
     private final String message;
  
     public ConfigRedirectHandler(String id, Class clazz, boolean conversation, boolean rollback, String message)
     {
        this.id = id;
        this.clazz = clazz;
        this.conversation = conversation;
        this.rollback = rollback;
        this.message = message;
     }
  
     @Override
     protected String getMessage(Exception e)
     {
        return message;
     }
  
     @Override
     protected String getViewId(Exception e)
     {
        return id;
     }
  
     @Override
     public boolean isHandler(Exception e)
     {
        return clazz.isInstance(e) && 
              Lifecycle.getPhaseId()!=PhaseId.RENDER_RESPONSE;
     }
  
     @Override
     protected boolean isEnd(Exception e)
     {
        return conversation;
     }
  
     @Override
     protected boolean isRollback(Exception e)
     {
        return rollback;
     }
  }
  
  
  1.1      date: 2006/12/17 19:46:41;  author: gavin;  state: Exp;jboss-seam/src/main/org/jboss/seam/exceptions/ConfigRenderHandler.java
  
  Index: ConfigRenderHandler.java
  ===================================================================
  /**
   * 
   */
  package org.jboss.seam.exceptions;
  
  import javax.faces.event.PhaseId;
  
  import org.jboss.seam.contexts.Lifecycle;
  
  public final class ConfigRenderHandler extends RenderHandler
  {
     private final String message;
     private final String id;
     private final Class clazz;
     private final boolean rollback;
     private final boolean conversation;
  
     public ConfigRenderHandler(String message, String id, Class clazz, boolean rollback, boolean conversation)
     {
        this.message = message;
        this.id = id;
        this.clazz = clazz;
        this.rollback = rollback;
        this.conversation = conversation;
     }
  
     @Override
     protected String getMessage(Exception e)
     {
        return message;
     }
  
     @Override
     protected String getViewId(Exception e)
     {
        return id;
     }
  
     @Override
     public boolean isHandler(Exception e)
     {
        return clazz.isInstance(e) && 
              Lifecycle.getPhaseId()==PhaseId.INVOKE_APPLICATION;
     }
  
     @Override
     protected boolean isEnd(Exception e)
     {
        return conversation;
     }
  
     @Override
     protected boolean isRollback(Exception e)
     {
        return rollback;
     }
  }
  
  
  1.1      date: 2006/12/17 19:46:41;  author: gavin;  state: Exp;jboss-seam/src/main/org/jboss/seam/exceptions/DebugPageHandler.java
  
  Index: DebugPageHandler.java
  ===================================================================
  /**
   * 
   */
  package org.jboss.seam.exceptions;
  
  import javax.faces.event.PhaseId;
  
  import org.jboss.seam.contexts.Context;
  import org.jboss.seam.contexts.Contexts;
  import org.jboss.seam.contexts.Lifecycle;
  import org.jboss.seam.core.Exceptions;
  import org.jboss.seam.core.Manager;
  
  public class DebugPageHandler extends ExceptionHandler
  {
  
     @Override
     public Object handle(Exception e) throws Exception
     {
        Exceptions.log.error("redirecting to debug page", e);
        Context conversationContext = Contexts.getConversationContext();
        conversationContext.set("org.jboss.seam.debug.lastException", e);
        conversationContext.set("org.jboss.seam.debug.phaseId", Lifecycle.getPhaseId().toString());
        org.jboss.seam.core.Redirect redirect = org.jboss.seam.core.Redirect.instance();
        redirect.setViewId("/debug.xhtml");
        Manager manager = Manager.instance();
        manager.beforeRedirect();
        redirect.setParameter( manager.getConversationIdParameter(), manager.getCurrentConversationId() );
        redirect.execute();
        conversationContext.flush();
        return rethrow(e);
     }
  
     @Override
     public boolean isHandler(Exception e)
     {
        return Lifecycle.getPhaseId()!=PhaseId.RENDER_RESPONSE && 
              Lifecycle.getPhaseId()!=null;
     }
     
     @Override
     public String toString()
     {
        return "Debug";
     }
  }
  
  
  1.1      date: 2006/12/17 19:46:41;  author: gavin;  state: Exp;jboss-seam/src/main/org/jboss/seam/exceptions/ErrorHandler.java
  
  Index: ErrorHandler.java
  ===================================================================
  package org.jboss.seam.exceptions;
  
  import org.jboss.seam.core.Conversation;
  import org.jboss.seam.core.Interpolator;
  import org.jboss.seam.util.Transactions;
  
  public abstract class ErrorHandler extends ExceptionHandler
  {
  
     @Override
     public Object handle(Exception e) throws Exception
     {
        if ( isEnd(e) ) Conversation.instance().end();
        if ( isRollback(e) ) Transactions.setTransactionRollbackOnly();
        String message = getMessage(e);
        //addFacesMessage(e, message);
        error( getCode(e), Interpolator.instance().interpolate( getDisplayMessage(e, message) ) );
        return rethrow(e);
     }
  
  }
  
  
  1.1      date: 2006/12/17 19:46:41;  author: gavin;  state: Exp;jboss-seam/src/main/org/jboss/seam/exceptions/ExceptionHandler.java
  
  Index: ExceptionHandler.java
  ===================================================================
  /**
   * 
   */
  package org.jboss.seam.exceptions;
  
  import javax.faces.context.FacesContext;
  
  import org.jboss.seam.core.FacesMessages;
  import org.jboss.seam.core.Navigator;
  import org.jboss.seam.util.Strings;
  
  public abstract class ExceptionHandler extends Navigator
  {
     public abstract Object handle(Exception e) throws Exception;
     public abstract boolean isHandler(Exception e);
     
     protected String getMessage(Exception e)
     {
        throw new UnsupportedOperationException();
     }
     protected String getViewId(Exception e)
     {
        throw new UnsupportedOperationException();
     }
     protected boolean isEnd(Exception e)
     {
        throw new UnsupportedOperationException();
     }
     protected boolean isRollback(Exception e)
     {
        throw new UnsupportedOperationException();
     }
     protected int getCode(Exception e)
     {
        throw new UnsupportedOperationException();
     }
  
     public static String getDisplayMessage(Exception e, String message)
     {
        if ( Strings.isEmpty(message) && e.getMessage()!=null ) 
        {
           return e.getMessage();
        }
        else
        {
           return message;
        }
     }
     
     public static void addFacesMessage(Exception e, String message)
     {
        message = getDisplayMessage(e, message);
        if ( !Strings.isEmpty(message) )
        {
           FacesMessages.instance().add(message);
        }
     }
     
     public static Object rethrow(Exception e) throws Exception
     {
        //SeamExceptionFilter does *not* do these things, which 
        //would normally happen in the phase listener after a 
        //responseComplete() call, but because we are rethrowing,
        //the phase listener might not get called (due to a bug!)
        /*FacesMessages.afterPhase();
        if ( Contexts.isConversationContextActive() )
        {
           Manager.instance().endRequest( ContextAdaptor.getSession( externalContext, true ) );
        }*/
        
        FacesContext facesContext = FacesContext.getCurrentInstance();
        facesContext.responseComplete();
        facesContext.getExternalContext().getRequestMap().put("org.jboss.seam.exceptionHandled", e);
        throw e;
     }
  
  }
  
  
  1.1      date: 2006/12/17 19:46:41;  author: gavin;  state: Exp;jboss-seam/src/main/org/jboss/seam/exceptions/RedirectHandler.java
  
  Index: RedirectHandler.java
  ===================================================================
  package org.jboss.seam.exceptions;
  
  import org.jboss.seam.core.Conversation;
  import org.jboss.seam.util.Transactions;
  
  public abstract class RedirectHandler extends ExceptionHandler
  {
     
     @Override
     public Object handle(Exception e) throws Exception
     {
        addFacesMessage( e, getMessage(e) );
        if ( isEnd(e) ) Conversation.instance().end();
        if ( isRollback(e) ) Transactions.setTransactionRollbackOnly();
        redirect( getViewId(e), null );
        return rethrow(e);
     }
  
  }
  
  
  1.1      date: 2006/12/17 19:46:41;  author: gavin;  state: Exp;jboss-seam/src/main/org/jboss/seam/exceptions/RenderHandler.java
  
  Index: RenderHandler.java
  ===================================================================
  package org.jboss.seam.exceptions;
  
  import org.jboss.seam.core.Conversation;
  import org.jboss.seam.util.Transactions;
  
  public abstract class RenderHandler extends ExceptionHandler
  {
  
     @Override
     public Object handle(Exception e) throws Exception
     {
        addFacesMessage( e, getMessage(e) );
        if ( isEnd(e) ) Conversation.instance().end();
        if ( isRollback(e) ) Transactions.setTransactionRollbackOnly();
        render( getViewId(e) );
        return null;
     }
  
  }
  
  



More information about the jboss-cvs-commits mailing list