[seam-commits] Seam SVN: r13120 - in modules/faces/branches/exception_handling: impl/src/main/java/org/jboss/seam/exceptionhandling and 1 other directory.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Thu Jun 10 16:06:48 EDT 2010


Author: lightguard
Date: 2010-06-10 16:06:46 -0400 (Thu, 10 Jun 2010)
New Revision: 13120

Added:
   modules/faces/branches/exception_handling/api/src/main/java/org/jboss/seam/exceptionhandling/ExceptionEvent.java
   modules/faces/branches/exception_handling/impl/src/main/java/org/jboss/seam/exceptionhandling/ExceptionHandlerExecutor.java
Modified:
   modules/faces/branches/exception_handling/api/src/main/java/org/jboss/seam/exceptionhandling/ExceptionHandler.java
   modules/faces/branches/exception_handling/impl/src/main/java/org/jboss/seam/exceptionhandling/SeamExceptionHandler.java
Log:
Changing the design based on IRC conversations.
Things compile :)

I don't know how to test this though, I'll need some help from Dan or Lincoln in getting it setup

Added: modules/faces/branches/exception_handling/api/src/main/java/org/jboss/seam/exceptionhandling/ExceptionEvent.java
===================================================================
--- modules/faces/branches/exception_handling/api/src/main/java/org/jboss/seam/exceptionhandling/ExceptionEvent.java	                        (rev 0)
+++ modules/faces/branches/exception_handling/api/src/main/java/org/jboss/seam/exceptionhandling/ExceptionEvent.java	2010-06-10 20:06:46 UTC (rev 13120)
@@ -0,0 +1,73 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.seam.exceptionhandling;
+
+/**
+ * Payload for an exception to be handled.
+ */
+public class ExceptionEvent
+{
+   private State state;
+   private Throwable exception;
+   private boolean exceptionHandled;
+
+   public ExceptionEvent(Throwable exception, State state)
+   {
+      this.exception = exception;
+      this.state = state;
+      this.exceptionHandled = false;
+   }
+
+   /**
+    * @return the exception to be handled.
+    */
+   public Throwable getException()
+   {
+      return exception;
+   }
+
+   /**
+    * @return State instance related to the environment. This will often need to be cast to the correct sub class.
+    */
+   public State getState()
+   {
+      return state;
+   }
+
+   /**
+    * @return flag indicating the exception has been handled.
+    */
+   public boolean isExceptionHandled()
+   {
+      return exceptionHandled;
+   }
+
+   /**
+    * This should be set if the exception has been handled in an event observer or handler.
+    * @param exceptionHandled new value
+    */
+   public void setExceptionHandled(boolean exceptionHandled)
+   {
+      this.exceptionHandled = exceptionHandled;
+   }
+}


Property changes on: modules/faces/branches/exception_handling/api/src/main/java/org/jboss/seam/exceptionhandling/ExceptionEvent.java
___________________________________________________________________
Name: svn:mime-type
   + text/x-java-source
Name: svn:keywords
   + Author Date Id Revision URL
Name: svn:eol-style
   + native

Modified: modules/faces/branches/exception_handling/api/src/main/java/org/jboss/seam/exceptionhandling/ExceptionHandler.java
===================================================================
--- modules/faces/branches/exception_handling/api/src/main/java/org/jboss/seam/exceptionhandling/ExceptionHandler.java	2010-06-10 18:12:42 UTC (rev 13119)
+++ modules/faces/branches/exception_handling/api/src/main/java/org/jboss/seam/exceptionhandling/ExceptionHandler.java	2010-06-10 20:06:46 UTC (rev 13120)
@@ -25,7 +25,7 @@
  * Registers an exception handler for a specific exception and state, this
  * is the main entry point for using Seam's exception handling infrastructure.
  */
-public interface ExceptionHandler<E extends Class<Throwable>, S extends State>
+public interface ExceptionHandler<E extends Throwable, S extends State>
 {
    /**
     * @return the numeric priority of this handler in relationship to

Added: modules/faces/branches/exception_handling/impl/src/main/java/org/jboss/seam/exceptionhandling/ExceptionHandlerExecutor.java
===================================================================
--- modules/faces/branches/exception_handling/impl/src/main/java/org/jboss/seam/exceptionhandling/ExceptionHandlerExecutor.java	                        (rev 0)
+++ modules/faces/branches/exception_handling/impl/src/main/java/org/jboss/seam/exceptionhandling/ExceptionHandlerExecutor.java	2010-06-10 20:06:46 UTC (rev 13120)
@@ -0,0 +1,86 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.seam.exceptionhandling;
+
+import org.jboss.seam.faces.util.BeanManagerUtils;
+
+import javax.enterprise.event.Observes;
+import javax.enterprise.inject.spi.BeanManager;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * Finds and invokes all {@link ExceptionHandler} instants for a particular
+ * exception and {@link State}.
+ * <p>
+ * If any handlers are found and invoked the the {@link ExceptionEvent#setExceptionHandled(boolean)} is set to true.
+ * </p>
+ */
+public class ExceptionHandlerExecutor
+{
+   /**
+    * Observes the event, finds the correct exception handler(s) and invokes them.
+    * @param event Event Payload
+    */
+   @SuppressWarnings("unchecked")
+   public void executeHandlers(@Observes ExceptionEvent event)
+   {
+      final HandlerChain chain = new HandlerChainImpl();
+      final Throwable exception = event.getException();
+      final State state = event.getState();
+      final BeanManager beanManager = state.getBeanManager();
+      final BeanManagerUtils managerUtils = BeanManagerUtils.getContextualInstance(beanManager, BeanManagerUtils.class);
+      ExceptionHandler bean;
+      final List<ExceptionHandler> beans = managerUtils.getContextualInstances(ExceptionHandler.class);
+
+      // Finding the correct exception handlers using reflection based on the method
+      // to determine if it's the correct 
+      for (Iterator<ExceptionHandler> iter = beans.iterator(); iter.hasNext();)
+      {
+         bean = iter.next();
+         try
+         {
+            bean.getClass().getMethod("handle", HandlerChain.class, FacesState.class, exception.getClass());
+         }
+         catch (NoSuchMethodException e)
+         {
+            iter.remove();
+         }
+      }
+
+      Collections.sort(beans, new Comparator<ExceptionHandler>()
+      {
+         public int compare(ExceptionHandler lhs, ExceptionHandler rhs)
+         {
+            return lhs.getPriority() - rhs.getPriority();
+         }
+      });
+
+      for (ExceptionHandler handler : beans)
+      {
+         handler.handle(chain, state, exception);
+         event.setExceptionHandled(true);
+      }
+   }
+}


Property changes on: modules/faces/branches/exception_handling/impl/src/main/java/org/jboss/seam/exceptionhandling/ExceptionHandlerExecutor.java
___________________________________________________________________
Name: svn:mime-type
   + text/x-java-source
Name: svn:keywords
   + Author Date Id Revision URL
Name: svn:eol-style
   + native

Modified: modules/faces/branches/exception_handling/impl/src/main/java/org/jboss/seam/exceptionhandling/SeamExceptionHandler.java
===================================================================
--- modules/faces/branches/exception_handling/impl/src/main/java/org/jboss/seam/exceptionhandling/SeamExceptionHandler.java	2010-06-10 18:12:42 UTC (rev 13119)
+++ modules/faces/branches/exception_handling/impl/src/main/java/org/jboss/seam/exceptionhandling/SeamExceptionHandler.java	2010-06-10 20:06:46 UTC (rev 13120)
@@ -23,8 +23,11 @@
 package org.jboss.seam.exceptionhandling;
 
 import org.jboss.seam.faces.util.BeanManagerUtils;
+import org.jboss.weld.extensions.beanManager.BeanManagerAccessor;
 
 import javax.enterprise.context.ApplicationScoped;
+import javax.enterprise.context.spi.CreationalContext;
+import javax.enterprise.event.Event;
 import javax.enterprise.inject.spi.Bean;
 import javax.enterprise.inject.spi.BeanManager;
 import javax.enterprise.util.TypeLiteral;
@@ -49,9 +52,10 @@
 
    private ExceptionHandler wrapped;
 
+
    public SeamExceptionHandler() throws NamingException
    {
-      this.beanManager = (BeanManager) new InitialContext().lookup("java:comp/BeanManager");
+      this.beanManager = BeanManagerAccessor.getManager();
       this.managerUtils = new BeanManagerUtils();
    }
 
@@ -68,7 +72,8 @@
       ExceptionQueuedEvent event;
       Throwable exception;
       HandlerChain chain;
-      FacesState state = this.managerUtils.getContextualInstance(this.beanManager, FacesStateImpl.class);
+      FacesState state = BeanManagerUtils.getContextualInstance(this.beanManager, FacesStateImpl.class);
+      Bean<?> bean;
 
       for (Iterator<ExceptionQueuedEvent> i = this.getUnhandledExceptionQueuedEvents().iterator(); i.hasNext();)
       {
@@ -77,26 +82,14 @@
 
          final Throwable realException = (this.getRootCause(exception) != null) ? this.getRootCause(exception) : exception;
 
-         chain = new HandlerChainImpl();
-         beans = this.beanManager.getBeans(new ParameterizedType() {
-            public Type[] getActualTypeArguments()
-            {
-               return new Type[] {realException.getClass(), FacesState.class};
-            }
+         ExceptionEvent exceptionEventObject = new ExceptionEvent(realException, this.managerUtils.getContextualInstance(FacesState.class));
 
-            public Type getRawType()
-            {
-               return org.jboss.seam.exceptionhandling.ExceptionHandler.class;
-            }
+         this.beanManager.fireEvent(exceptionEventObject);
 
-            public Type getOwnerType()
-            {
-               return null;
-            }
-         });
-         // TODO: Find handlers for the exception type
-         // TODO: execute handler
-
+         if (exceptionEventObject.isExceptionHandled())
+         {
+            i.remove();
+         }
       }
 
       if (this.wrapped != null)



More information about the seam-commits mailing list