directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From trus...@apache.org
Subject svn commit: r354255 - in /directory/network/trunk/src/java/org/apache/mina/handler/chain: Chain.java ChainBase.java Command.java Context.java Filter.java impl/
Date Tue, 06 Dec 2005 01:46:58 GMT
Author: trustin
Date: Mon Dec  5 17:46:50 2005
New Revision: 354255

URL: http://svn.apache.org/viewcvs?rev=354255&view=rev
Log:
Refactoring chain implementation to make it similar to MINA filter interfaces.

Added:
    directory/network/trunk/src/java/org/apache/mina/handler/chain/ChainBase.java   (with
props)
Removed:
    directory/network/trunk/src/java/org/apache/mina/handler/chain/Context.java
    directory/network/trunk/src/java/org/apache/mina/handler/chain/impl/
Modified:
    directory/network/trunk/src/java/org/apache/mina/handler/chain/Chain.java   (contents,
props changed)
    directory/network/trunk/src/java/org/apache/mina/handler/chain/Command.java   (contents,
props changed)
    directory/network/trunk/src/java/org/apache/mina/handler/chain/Filter.java   (contents,
props changed)

Modified: directory/network/trunk/src/java/org/apache/mina/handler/chain/Chain.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/handler/chain/Chain.java?rev=354255&r1=354254&r2=354255&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/handler/chain/Chain.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/handler/chain/Chain.java Mon Dec  5 17:46:50
2005
@@ -16,6 +16,8 @@
  */
 package org.apache.mina.handler.chain;
 
+import org.apache.mina.common.IoSession;
+
 /**
  * <p>A {@link Chain} represents a configured list of
  * {@link Command}s that will be executed in order to perform processing
@@ -108,5 +110,5 @@
      *  of this {@link Context} should be delegated to a subsequent
      *  {@link Command} in an enclosing {@link Chain}
      */
-    boolean execute( Context context ) throws Exception;
+    boolean execute( IoSession session, Object message ) throws Exception;
 }

Propchange: directory/network/trunk/src/java/org/apache/mina/handler/chain/Chain.java
------------------------------------------------------------------------------
    svn:keywords = HeadURL Id LastChangedBy LastChangedDate LastChangedRevision

Added: directory/network/trunk/src/java/org/apache/mina/handler/chain/ChainBase.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/handler/chain/ChainBase.java?rev=354255&view=auto
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/handler/chain/ChainBase.java (added)
+++ directory/network/trunk/src/java/org/apache/mina/handler/chain/ChainBase.java Mon Dec
 5 17:46:50 2005
@@ -0,0 +1,255 @@
+/*
+ *   Copyright 2005 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.mina.handler.chain;
+
+import java.util.Collection;
+import java.util.Iterator;
+
+import org.apache.mina.common.IoSession;
+import org.apache.mina.handler.chain.Chain;
+import org.apache.mina.handler.chain.Command;
+import org.apache.mina.handler.chain.Context;
+import org.apache.mina.handler.chain.Filter;
+
+/**
+ * <p>A {@link Chain} represents a configured list of
+ * {@link Command}s that will be executed in order to perform processing
+ * on a specified {@link Context}.  Each included {@link Command} will be
+ * executed in turn, until either one of them returns <code>true</code>,
+ * one of the executed {@link Command}s throws an exception,
+ * or the end of the chain has been reached.  The {@link Chain} itself will
+ * return the return value of the last {@link Command} that was executed
+ * (if no exception was thrown), or rethrow the thrown exception.</p>
+ *
+ * <p>Note that {@link Chain} extends {@link Command}, so that the two can
+ * be used interchangeably when a {@link Command} is expected.  This makes it
+ * easy to assemble workflows in a hierarchical manner by combining subchains
+ * into an overall processing chain.</p>
+ *
+ * <p>To protect applications from evolution of this interface, specialized
+ * implementations of {@link Chain} should generally be created by extending
+ * the provided base class {@link org.apache.mina.handler.chain.impl.ChainBase})
+ * rather than directly implementing this interface.</p>
+ *
+ * <p>{@link Chain} implementations should be designed in a thread-safe
+ * manner, suitable for execution on multiple threads simultaneously.  In
+ * general, this implies that the state information identifying which
+ * {@link Command} is currently being executed should be maintained in a
+ * local variable inside the <code>execute()</code> method, rather than
+ * in an instance variable.  The {@link Command}s in a {@link Chain} may be
+ * configured (via calls to <code>addCommand()</code>) at any time before
+ * the <code>execute()</code> method of the {@link Chain} is first called.
+ * After that, the configuration of the {@link Chain} is frozen.</p>
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public class ChainBase implements Command
+{
+    // ----------------------------------------------------------- Constructors
+
+    /**
+     * <p>Construct a {@link Chain} with no configured {@link Command}s.</p>
+     */
+    public ChainBase()
+    {
+
+    }
+
+    /**
+     * <p>Construct a {@link Chain} configured with the specified
+     * {@link Command}.</p>
+     *
+     * @param command The {@link Command} to be configured
+     *
+     * @exception IllegalArgumentException if <code>command</code>
+     *  is <code>null</code>
+     */
+    public ChainBase( Command command )
+    {
+        addCommand( command );
+    }
+
+    /**
+     * <p>Construct a {@link Chain} configured with the specified
+     * {@link Command}s.</p>
+     *
+     * @param commands The {@link Command}s to be configured
+     *
+     * @exception IllegalArgumentException if <code>commands</code>,
+     *  or one of the individual {@link Command} elements,
+     *  is <code>null</code>
+     */
+    public ChainBase( Command[] commands )
+    {
+        if ( commands == null )
+        {
+            throw new IllegalArgumentException();
+        }
+
+        for ( int i = 0; i < commands.length; i++ )
+        {
+            addCommand( commands[ i ] );
+        }
+    }
+
+    /**
+     * <p>Construct a {@link Chain} configured with the specified
+     * {@link Command}s.</p>
+     *
+     * @param commands The {@link Command}s to be configured
+     *
+     * @exception IllegalArgumentException if <code>commands</code>,
+     *  or one of the individual {@link Command} elements,
+     *  is <code>null</code>
+     */
+    public ChainBase( Collection commands )
+    {
+        if ( commands == null )
+        {
+            throw new IllegalArgumentException();
+        }
+
+        Iterator elements = commands.iterator();
+        while ( elements.hasNext() )
+        {
+            addCommand( (Command) elements.next() );
+        }
+    }
+
+    // ----------------------------------------------------- Instance Variables
+
+    /**
+     * <p>The list of {@link Command}s configured for this {@link Chain}, in
+     * the order in which they may delegate processing to the remainder of
+     * the {@link Chain}.</p>
+     */
+    protected Command[] commands = new Command[ 0 ];
+
+    /**
+     * <p>Flag indicating whether the configuration of our commands list
+     * has been frozen by a call to the <code>execute()</code> method.</p>
+     */
+    protected boolean frozen = false;
+
+    // ---------------------------------------------------------- Chain Methods
+
+    // Documented in Chain interface
+    public void addCommand( Command command )
+    {
+        if ( command == null )
+        {
+            throw new IllegalArgumentException();
+        }
+
+        if ( frozen )
+        {
+            throw new IllegalStateException();
+        }
+
+        Command[] results = new Command[ commands.length + 1 ];
+        System.arraycopy( commands, 0, results, 0, commands.length );
+        results[ commands.length ] = command;
+        commands = results;
+    }
+
+    // Documented in Chain interface
+    public boolean execute( IoSession session, Object message ) throws Exception
+    {
+        // Verify our parameters
+        if ( context == null )
+        {
+            throw new IllegalArgumentException();
+        }
+
+        // Freeze the configuration of the command list
+        frozen = true;
+
+        // Execute the commands in this list until one returns true
+        // or throws an exception
+        boolean saveResult = false;
+        Exception saveException = null;
+        int i = 0;
+        int n = commands.length;
+
+        for ( i = 0; i < n; i++ )
+        {
+            try
+            {
+                saveResult = commands[ i ].execute( context );
+                if ( saveResult )
+                {
+                    break;
+                }
+            }
+            catch ( Exception e )
+            {
+                saveException = e;
+                break;
+            }
+        }
+
+        // Call postprocess methods on Filters in reverse order
+        if ( i >= n )
+        { // Fell off the end of the chain
+            i--;
+        }
+        boolean handled = false;
+        boolean result = false;
+        for ( int j = i; j >= 0; j-- )
+        {
+            if ( commands[ j ] instanceof Filter )
+            {
+                try
+                {
+                    result = ( (Filter) commands[ j ] ).postprocess( context, saveException
);
+                    if ( result )
+                    {
+                        handled = true;
+                    }
+                }
+                catch ( Exception e )
+                {
+                    // Silently ignore
+                }
+            }
+        }
+
+        // Return the exception or result state from the last execute()
+        if ( ( saveException != null ) && !handled )
+        {
+            throw saveException;
+        }
+
+        return ( saveResult );
+    }
+
+    // -------------------------------------------------------- Package Methods
+
+    /**
+     * <p>Return an array of the configured {@link Command}s for this
+     * {@link Chain}.  This method is package private, and is used only
+     * for the unit tests.</p>
+     */
+    Command[] getCommands()
+    {
+        return ( commands );
+    }
+}

Propchange: directory/network/trunk/src/java/org/apache/mina/handler/chain/ChainBase.java
------------------------------------------------------------------------------
    svn:keywords = HeadURL Id LastChangedBy LastChangedDate LastChangedRevision

Modified: directory/network/trunk/src/java/org/apache/mina/handler/chain/Command.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/handler/chain/Command.java?rev=354255&r1=354254&r2=354255&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/handler/chain/Command.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/handler/chain/Command.java Mon Dec  5
17:46:50 2005
@@ -16,6 +16,8 @@
  */
 package org.apache.mina.handler.chain;
 
+import org.apache.mina.common.IoSession;
+
 /**
  * <p>A {@link Command} encapsulates a unit of processing work to be
  * performed, whose purpose is to examine and/or modify the state of a
@@ -97,5 +99,5 @@
      *  of this {@link Context} should be delegated to a subsequent
      *  {@link Command} in an enclosing {@link Chain}
      */
-    boolean execute( Context context ) throws Exception;
+    boolean execute( IoSession session ) throws Exception;
 }

Propchange: directory/network/trunk/src/java/org/apache/mina/handler/chain/Command.java
------------------------------------------------------------------------------
    svn:keywords = HeadURL Id LastChangedBy LastChangedDate LastChangedRevision

Modified: directory/network/trunk/src/java/org/apache/mina/handler/chain/Filter.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/handler/chain/Filter.java?rev=354255&r1=354254&r2=354255&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/handler/chain/Filter.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/handler/chain/Filter.java Mon Dec  5
17:46:50 2005
@@ -16,6 +16,8 @@
  */
 package org.apache.mina.handler.chain;
 
+import org.apache.mina.common.IoSession;
+
 /**
  * <p>A {@link Filter} is a specialized {@link Command} that also expects
  * the {@link Chain} that is executing it to call the
@@ -59,5 +61,5 @@
      *  method (and therefore need not be rethrown), return <code>true</code>;
      *  otherwise return <code>false</code>
      */
-    boolean postprocess( Context context, Exception exception );
+    boolean postprocess( IoSession session, Object message, Exception exception );
 }

Propchange: directory/network/trunk/src/java/org/apache/mina/handler/chain/Filter.java
------------------------------------------------------------------------------
    svn:keywords = HeadURL Id LastChangedBy LastChangedDate LastChangedRevision



Mime
View raw message