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

Gavin King gavin.king at jboss.com
Sat Oct 21 21:47:11 EDT 2006


  User: gavin   
  Date: 06/10/21 21:47:11

  Modified:    src/main/org/jboss/seam/core     
                        ManagedHibernateSession.java
                        ManagedJbpmContext.java
                        ManagedPersistenceContext.java Pages.java
  Added:       src/main/org/jboss/seam/core      Exceptions.java
  Log:
  better exception handling
  rolled back change to faces-config
  fix bug where destroy() injected closed EM and tried to register in txn
  
  Revision  Changes    Path
  1.25      +6 -2      jboss-seam/src/main/org/jboss/seam/core/ManagedHibernateSession.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: ManagedHibernateSession.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss-seam/src/main/org/jboss/seam/core/ManagedHibernateSession.java,v
  retrieving revision 1.24
  retrieving revision 1.25
  diff -u -b -r1.24 -r1.25
  --- ManagedHibernateSession.java	17 Oct 2006 14:39:56 -0000	1.24
  +++ ManagedHibernateSession.java	22 Oct 2006 01:47:11 -0000	1.25
  @@ -1,4 +1,4 @@
  -//$Id: ManagedHibernateSession.java,v 1.24 2006/10/17 14:39:56 gavin Exp $
  +//$Id: ManagedHibernateSession.java,v 1.25 2006/10/22 01:47:11 gavin Exp $
   package org.jboss.seam.core;
   
   import static org.jboss.seam.InterceptionType.NEVER;
  @@ -25,6 +25,7 @@
   import org.jboss.seam.annotations.Intercept;
   import org.jboss.seam.annotations.Scope;
   import org.jboss.seam.annotations.Unwrap;
  +import org.jboss.seam.contexts.Lifecycle;
   import org.jboss.seam.core.Expressions.ValueBinding;
   import org.jboss.seam.util.Naming;
   
  @@ -98,7 +99,10 @@
      @Unwrap
      public Session getSession()
      {
  +      if ( !Lifecycle.isDestroying() ) 
  +      {
         session.isOpen();
  +      }
         return session;
      }
      
  
  
  
  1.14      +3 -2      jboss-seam/src/main/org/jboss/seam/core/ManagedJbpmContext.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: ManagedJbpmContext.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss-seam/src/main/org/jboss/seam/core/ManagedJbpmContext.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -b -r1.13 -r1.14
  --- ManagedJbpmContext.java	10 Oct 2006 19:37:37 -0000	1.13
  +++ ManagedJbpmContext.java	22 Oct 2006 01:47:11 -0000	1.14
  @@ -24,6 +24,7 @@
   import org.jboss.seam.annotations.Scope;
   import org.jboss.seam.annotations.Unwrap;
   import org.jboss.seam.contexts.Contexts;
  +import org.jboss.seam.contexts.Lifecycle;
   import org.jboss.seam.util.Transactions;
   import org.jbpm.JbpmContext;
   import org.jbpm.persistence.db.DbPersistenceServiceFactory;
  @@ -33,7 +34,7 @@
    * Manages a reference to a JbpmSession.
    *
    * @author <a href="mailto:steve at hibernate.org">Steve Ebersole </a>
  - * @version $Revision: 1.13 $
  + * @version $Revision: 1.14 $
    */
   @Scope(ScopeType.EVENT)
   @Name("jbpmContext")
  @@ -70,7 +71,7 @@
         {
            throw new IllegalStateException("JbpmContext may only be used inside a transaction");
         }
  -      if ( !synchronizationRegistered && Transactions.isTransactionActive() )
  +      if ( !synchronizationRegistered && !Lifecycle.isDestroying() && Transactions.isTransactionActive() )
         {
            Transactions.registerSynchronization(this);
            synchronizationRegistered = true;
  
  
  
  1.26      +4 -2      jboss-seam/src/main/org/jboss/seam/core/ManagedPersistenceContext.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: ManagedPersistenceContext.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss-seam/src/main/org/jboss/seam/core/ManagedPersistenceContext.java,v
  retrieving revision 1.25
  retrieving revision 1.26
  diff -u -b -r1.25 -r1.26
  --- ManagedPersistenceContext.java	17 Oct 2006 14:39:56 -0000	1.25
  +++ ManagedPersistenceContext.java	22 Oct 2006 01:47:11 -0000	1.26
  @@ -1,4 +1,4 @@
  -//$Id: ManagedPersistenceContext.java,v 1.25 2006/10/17 14:39:56 gavin Exp $
  +//$Id: ManagedPersistenceContext.java,v 1.26 2006/10/22 01:47:11 gavin Exp $
   package org.jboss.seam.core;
   
   import static org.jboss.seam.InterceptionType.NEVER;
  @@ -23,6 +23,8 @@
   import org.jboss.seam.annotations.Intercept;
   import org.jboss.seam.annotations.Scope;
   import org.jboss.seam.annotations.Unwrap;
  +import org.jboss.seam.contexts.Contexts;
  +import org.jboss.seam.contexts.Lifecycle;
   import org.jboss.seam.core.Expressions.ValueBinding;
   import org.jboss.seam.persistence.PersistenceProvider;
   import org.jboss.seam.util.Naming;
  @@ -93,7 +95,7 @@
      @Unwrap
      public EntityManager getEntityManager() throws NamingException, SystemException
      {
  -      if ( Transactions.isTransactionActive() ) 
  +      if ( !Lifecycle.isDestroying() && Transactions.isTransactionActive() )
         {
            entityManager.joinTransaction();
         }
  
  
  
  1.33      +6 -4      jboss-seam/src/main/org/jboss/seam/core/Pages.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: Pages.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss-seam/src/main/org/jboss/seam/core/Pages.java,v
  retrieving revision 1.32
  retrieving revision 1.33
  diff -u -b -r1.32 -r1.33
  --- Pages.java	19 Oct 2006 20:58:19 -0000	1.32
  +++ Pages.java	22 Oct 2006 01:47:11 -0000	1.33
  @@ -356,9 +356,9 @@
         return parameters;
      }
      
  -   public void applyRequestParameterValues(String viewId)
  +   public void applyRequestParameterValues(FacesContext facesContext)
      {
  -      FacesContext context = FacesContext.getCurrentInstance();
  +      String viewId = facesContext.getViewRoot().getViewId();
         Map<String, String[]> requestParameters = Parameters.getRequestParameters();
         for ( PageParameter pageParameter: getPage(viewId).pageParameters )
         {         
  @@ -386,13 +386,15 @@
            
            Object value = converter==null ? 
                  stringValue :
  -               converter.getAsObject( context, context.getViewRoot(), stringValue );
  +               converter.getAsObject( facesContext, facesContext.getViewRoot(), stringValue );
            pageParameter.valueBinding.setValue(value);
         }
      }
   
  -   public void applyViewRootValues(String viewId)
  +   public void applyViewRootValues(FacesContext facesContext)
      {
  +      String viewId = facesContext.getViewRoot().getViewId();
  +      
         Map<String, Object> pageParameters = (Map<String, Object>) Contexts.getPageContext().get(PAGE_PARAMETERS);
         if (pageParameters!=null)
         {
  
  
  
  1.1      date: 2006/10/22 01:47:11;  author: gavin;  state: Exp;jboss-seam/src/main/org/jboss/seam/core/Exceptions.java
  
  Index: Exceptions.java
  ===================================================================
  package org.jboss.seam.core;
  
  import static org.jboss.seam.InterceptionType.NEVER;
  
  import java.util.ArrayList;
  import java.util.List;
  
  import javax.faces.component.UIViewRoot;
  import javax.faces.context.FacesContext;
  import javax.faces.event.PhaseId;
  
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.jboss.seam.Component;
  import org.jboss.seam.ScopeType;
  import org.jboss.seam.annotations.Create;
  import org.jboss.seam.annotations.HttpError;
  import org.jboss.seam.annotations.Intercept;
  import org.jboss.seam.annotations.Name;
  import org.jboss.seam.annotations.Redirect;
  import org.jboss.seam.annotations.Render;
  import org.jboss.seam.annotations.Scope;
  import org.jboss.seam.contexts.Contexts;
  import org.jboss.seam.contexts.Lifecycle;
  import org.jboss.seam.interceptors.ExceptionInterceptor;
  import org.jboss.seam.jsf.AbstractSeamPhaseListener;
  
  /**
   * Holds metadata for pages defined in pages.xml, including
   * page actions and page descriptions.
   * 
   * @author Gavin King
   */
  @Scope(ScopeType.APPLICATION)
  @Intercept(NEVER)
  @Name("org.jboss.seam.core.exceptions")
  public class Exceptions 
  {
     private static final Log log = LogFactory.getLog(ExceptionInterceptor.class);
     
     private List<ExceptionHandler> exceptionHandlers = new ArrayList<ExceptionHandler>();
     
     public ExceptionHandler handle(Exception e) throws Exception
     {
        for (ExceptionHandler eh: exceptionHandlers)
        {
           if ( eh.isHandler(e) )
           {
              eh.handle(e);
              break;
           }
        }
        return null;
     }
     
     @Create
     public void init()
     {
        exceptionHandlers.add( new RenderHandler() );
        exceptionHandlers.add( new RedirectHandler() );
        exceptionHandlers.add( new ErrorHandler() );
        if ( Init.instance().isDebug() ) 
        {
           exceptionHandlers.add( new DebugPageHandler() );
        }
     }
  
     public static interface ExceptionHandler
     {
        public void handle(Exception e) throws Exception;
        public boolean isHandler(Exception e);
     }
     
     public static class RedirectHandler implements ExceptionHandler
     {
        public void handle(Exception e) throws Exception
        {
           addFacesMessage(e, getMessage(e));
           redirect( getViewId(e) );
           handled(e);
           throw e;
        }
  
        public boolean isHandler(Exception e)
        {
           return e.getClass().isAnnotationPresent(Redirect.class) && 
                 Lifecycle.getPhaseId()!=PhaseId.RENDER_RESPONSE;
        }
        
        protected String getMessage(Exception e)
        {
           return e.getClass().getAnnotation(Redirect.class).message();
        }
        
        protected String getViewId(Exception e)
        {
           return e.getClass().getAnnotation(Redirect.class).viewId();
        }
        
     }
     
     public static class RenderHandler implements ExceptionHandler
     {
        public void handle(Exception e)
        {
           addFacesMessage(e, getMessage(e));
           render(getViewId(e));
        }
  
        public boolean isHandler(Exception e)
        {
           return  e.getClass().isAnnotationPresent(Render.class) && 
                 Lifecycle.getPhaseId()==PhaseId.INVOKE_APPLICATION;
        }
  
        protected String getMessage(Exception e)
        {
           return e.getClass().getAnnotation(Redirect.class).message();
        }
        
        protected String getViewId(Exception e)
        {
           return e.getClass().getAnnotation(Redirect.class).viewId();
        }
     }
     
     public static class ErrorHandler implements ExceptionHandler
     {
        public void handle(Exception e) throws Exception
        {
           String message = getMessage(e); 
           message = message==null ? 
                 null : Interpolator.instance().interpolate(message);
           error(getCode(e), message);
           handled(e);
        }  
  
        public boolean isHandler(Exception e)
        {
           return e.getClass().isAnnotationPresent(HttpError.class);
        }
        
        protected String getMessage(Exception e)
        {
           String message = e.getClass().getAnnotation(HttpError.class).message();
           if ( "".equals(message) ) message = e.getMessage();
           return message;
        }
        
        protected int getCode(Exception e)
        {
           return e.getClass().getAnnotation(HttpError.class).errorCode();
        }
        
     }
     
     public static class DebugPageHandler implements ExceptionHandler
     {
  
        public void handle(Exception e)
        {
           log.error("redirecting to debug page", e);
           Contexts.getConversationContext().set("org.jboss.seam.debug.lastException", e);
           Contexts.getConversationContext().set("org.jboss.seam.debug.phaseId", Lifecycle.getPhaseId().toString());
           FacesContext facesContext = FacesContext.getCurrentInstance();
           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();
           FacesMessages.afterPhase();
           AbstractSeamPhaseListener.storeAnyConversationContext(facesContext);
           handled(e);
        }
  
        public boolean isHandler(Exception e)
        {
           return Lifecycle.getPhaseId()!=PhaseId.RENDER_RESPONSE;
        }
        
     }
     
     protected static void addFacesMessage(Exception e, String message)
     {
        String message1 = message;
        message1 = "".equals(message1) ? e.getMessage() : message1;
        message = message1==null ? null : Interpolator.instance().interpolate(message1);
        if (message!=null)
        {
           FacesMessages.instance().add(message);
        }
     }
     
     protected static void error(int code, String message)
     {
        if ( log.isDebugEnabled() ) log.debug("sending error: " + code);
        FacesContext facesContext = FacesContext.getCurrentInstance();
        org.jboss.seam.core.HttpError httpError = org.jboss.seam.core.HttpError.instance();
        if (message==null)
        {
           httpError.send(code);
        }
        else
        {
           httpError.send(code, message);
        }
        FacesMessages.afterPhase();
        AbstractSeamPhaseListener.storeAnyConversationContext(facesContext);
     }
  
     protected static void redirect(String viewId)
     {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        if ( "".equals(viewId) )
        {
           viewId = facesContext.getViewRoot().getViewId();
        }
        if ( log.isDebugEnabled() ) log.debug("redirecting to: " + viewId);
        org.jboss.seam.core.Redirect redirect = org.jboss.seam.core.Redirect.instance();
        redirect.setViewId(viewId);
        redirect.execute();
        FacesMessages.afterPhase();
        AbstractSeamPhaseListener.storeAnyConversationContext(facesContext);
     }
     
     protected static void render(String viewId)
     {
        FacesContext context = FacesContext.getCurrentInstance();
        if ( !"".equals(viewId) )
        {
           if ( log.isDebugEnabled() ) log.debug("rendering: " + viewId);
           UIViewRoot viewRoot = context.getApplication().getViewHandler()
                 .createView(context, viewId);
           context.setViewRoot(viewRoot);
        }
        context.renderResponse();
     }
  
     protected static void handled(Exception e)
     {
        FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("org.jboss.seam.exceptionHandled", e);
     }
  
     public static Exceptions instance()
     {
        if ( !Contexts.isApplicationContextActive() )
        {
           throw new IllegalStateException("No active application context");
        }
        return (Exceptions) Component.getInstance(Exceptions.class, ScopeType.APPLICATION, true);
     }
  
  
  }
  
  
  



More information about the jboss-cvs-commits mailing list