dev@grizzly.java.net

[Fwd: [Issue 43] need a way to pass the exceptions generated in filters to the Grizzly user]

From: Oleksiy Stashok <Oleksiy.Stashok_at_Sun.COM>
Date: Fri, 11 Jan 2008 11:50:20 +0100

Hi,

I want to propose fix for reviewing. It's attached.
I'm proposing to apply this fix on DefaultProtocolChain class, not
introducing new API for common ProtocolChain interface... at least for now.

Will appreciate your feedback.

WBR,
Alexey.


Index: DefaultProtocolChain.java
===================================================================
--- DefaultProtocolChain.java (revision 695)
+++ DefaultProtocolChain.java (working copy)
@@ -23,8 +23,10 @@
 
 package com.sun.grizzly;
 
-import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
 import java.util.logging.Level;
 
 /**
@@ -34,11 +36,18 @@
  */
 public class DefaultProtocolChain implements ProtocolChain {
     
+ public enum Phase {EXECUTE, POST_EXECUTE};
+
     /**
      * The list of ProtocolFilter this chain will invoke.
      */
- protected ArrayList<ProtocolFilter> protocolFilters;
+ protected List<ProtocolFilter> protocolFilters;
     
+ /**
+ * The list of <code>EventHandler</code>s, which will be notified about this
+ * <code>ProtocolChain</code> events
+ */
+ protected Collection<EventHandler> eventHandlers;
     
     /**
      * <tt>true</tt> if a pipelined execution is required. A pipelined execution
@@ -52,6 +61,7 @@
     
     public DefaultProtocolChain() {
         protocolFilters = new ArrayList<ProtocolFilter>();
+ eventHandlers = new HashSet<EventHandler>();
     }
     
     
@@ -84,15 +94,18 @@
         boolean invokeNext = true;
         int size = protocolFilters.size();
         int currentPosition = 0;
+ ProtocolFilter protocolFilter = null;
         
- for (int i=0; i < size; i++){
- try{
- invokeNext = protocolFilters.get(i).execute(ctx);
+ for (int i=0; i < size; i++) {
+ try {
+ protocolFilter = protocolFilters.get(i);
+ invokeNext = protocolFilter.execute(ctx);
             } catch (Exception ex){
                 invokeNext = false;
                 i--;
                 Controller.logger().log(Level.SEVERE,
                         "ProtocolChain exception",ex);
+ notifyException(Phase.EXECUTE, protocolFilter, ex);
             }
             
             currentPosition = i;
@@ -110,7 +123,7 @@
      */
     protected boolean postExecuteProtocolFilter(int currentPosition,Context ctx) {
         boolean invokeNext = true;
- ProtocolFilter tmpHandler;
+ ProtocolFilter tmpHandler = null;
         boolean reinvokeChain = false;
         for (int i = currentPosition; i > -1; i--){
             try{
@@ -119,6 +132,7 @@
             } catch (Exception ex){
                 Controller.logger().log(Level.SEVERE,
                         "ProtocolChain exception",ex);
+ notifyException(Phase.POST_EXECUTE, tmpHandler, ex);
             }
             if ( !invokeNext ) {
                break;
@@ -147,8 +161,8 @@
     
     
     /**
- * Return the <code>List</code> of available <code>ProtocolFilter</code>
- * @param protocolFilter
+ * Add the <code>ProtocolFilter</code> to this <code>ProtocolChain</code>
+ * @param protocolFilter to add
      * @return
      */
     public boolean addFilter(ProtocolFilter protocolFilter) {
@@ -207,4 +221,51 @@
         return continousExecution;
     }
     
+
+ /**
+ * Add the <code>EventHandler</code>
+ * @param eventHandler
+ * @return true, if <code>EventHandler</code> was added, false otherwise
+ */
+ public boolean addEventHandler(EventHandler eventHandler) {
+ return eventHandlers.add(eventHandler);
+ }
+
+
+ /**
+ * Remove the <code>EventHandler</code>.
+ * @param eventHandler the <code>ProtocolFilter<code> to remove
+ * @return true, if <code>EventHandler</code> was removed, false otherwise
+ */
+ public boolean removeEventHandler(EventHandler eventHandler) {
+ return eventHandlers.remove(eventHandler);
+ }
+
+ /**
+ * Notifies all <code>EventHandler</code>s about exception, which occured
+ * @param phase execution <code>Phase</code>, where exception occured
+ * @param filter <code>ProtocolFilter</code>, where exception occured
+ * @param throwable actual exception
+ */
+ protected void notifyException(Phase phase, ProtocolFilter filter,
+ Throwable throwable) {
+ for(EventHandler eventHandler : eventHandlers) {
+ try {
+ eventHandler.onException(phase, filter, throwable);
+ } catch(Exception e) {
+ Controller.logger().log(Level.SEVERE,
+ "ProtocolChain notifyException exception", e);
+
+ }
+ }
+ }
+
+ /**
+ * Interface, which introduces handler, which will be notified about event,
+ * happened on <code>ProtocolChain</code>
+ */
+ public interface EventHandler {
+ public void onException(Phase phase, ProtocolFilter filter,
+ Throwable throwable);
+ }
 }

attached mail follows:



https://grizzly.dev.java.net/issues/show_bug.cgi?id=43



User jfarcand changed the following:

                What |Old value |New value
================================================================================
             Assigned to|issues_at_grizzly |oleksiys
--------------------------------------------------------------------------------
              Issue type|ENHANCEMENT |DEFECT
--------------------------------------------------------------------------------
                Priority|P3 |P2
--------------------------------------------------------------------------------
        Target milestone|milestone 1 |not determined
--------------------------------------------------------------------------------
                 Version|1.5.2 |current
--------------------------------------------------------------------------------




------- Additional comments from jfarcand_at_dev.java.net Wed Jan 9 23:11:26 +0000 2008 -------
I agree this is important :-) I'm turning it into a bug and a p2 so we can a fix
sooner that later :-)

Thanks!

---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe_at_grizzly.dev.java.net
For additional commands, e-mail: issues-help_at_grizzly.dev.java.net