From commits-return-7168-apmail-directory-commits-archive=directory.apache.org@directory.apache.org Tue Dec 06 01:47:26 2005 Return-Path: Delivered-To: apmail-directory-commits-archive@www.apache.org Received: (qmail 5733 invoked from network); 6 Dec 2005 01:47:25 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 6 Dec 2005 01:47:25 -0000 Received: (qmail 5996 invoked by uid 500); 6 Dec 2005 01:47:22 -0000 Delivered-To: apmail-directory-commits-archive@directory.apache.org Received: (qmail 5888 invoked by uid 500); 6 Dec 2005 01:47:21 -0000 Mailing-List: contact commits-help@directory.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@directory.apache.org Delivered-To: mailing list commits@directory.apache.org Received: (qmail 5665 invoked by uid 99); 6 Dec 2005 01:47:20 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 05 Dec 2005 17:47:20 -0800 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [209.237.227.194] (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.29) with SMTP; Mon, 05 Dec 2005 17:47:19 -0800 Received: (qmail 5617 invoked by uid 65534); 6 Dec 2005 01:46:59 -0000 Message-ID: <20051206014659.5616.qmail@minotaur.apache.org> Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: commits@directory.apache.org From: trustin@apache.org X-Mailer: svnmailer-1.0.5 X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N 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; + /** *

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; + +/** + *

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 true, + * 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.

+ * + *

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.

+ * + *

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.

+ * + *

{@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 execute() method, rather than + * in an instance variable. The {@link Command}s in a {@link Chain} may be + * configured (via calls to addCommand()) at any time before + * the execute() method of the {@link Chain} is first called. + * After that, the configuration of the {@link Chain} is frozen.

+ * + * @author Apache Directory Project + * @version $Rev$, $Date$ + * + * @author Apache Directory Project + * @version $Rev$, $Date$ + */ +public class ChainBase implements Command +{ + // ----------------------------------------------------------- Constructors + + /** + *

Construct a {@link Chain} with no configured {@link Command}s.

+ */ + public ChainBase() + { + + } + + /** + *

Construct a {@link Chain} configured with the specified + * {@link Command}.

+ * + * @param command The {@link Command} to be configured + * + * @exception IllegalArgumentException if command + * is null + */ + public ChainBase( Command command ) + { + addCommand( command ); + } + + /** + *

Construct a {@link Chain} configured with the specified + * {@link Command}s.

+ * + * @param commands The {@link Command}s to be configured + * + * @exception IllegalArgumentException if commands, + * or one of the individual {@link Command} elements, + * is null + */ + public ChainBase( Command[] commands ) + { + if ( commands == null ) + { + throw new IllegalArgumentException(); + } + + for ( int i = 0; i < commands.length; i++ ) + { + addCommand( commands[ i ] ); + } + } + + /** + *

Construct a {@link Chain} configured with the specified + * {@link Command}s.

+ * + * @param commands The {@link Command}s to be configured + * + * @exception IllegalArgumentException if commands, + * or one of the individual {@link Command} elements, + * is null + */ + public ChainBase( Collection commands ) + { + if ( commands == null ) + { + throw new IllegalArgumentException(); + } + + Iterator elements = commands.iterator(); + while ( elements.hasNext() ) + { + addCommand( (Command) elements.next() ); + } + } + + // ----------------------------------------------------- Instance Variables + + /** + *

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}.

+ */ + protected Command[] commands = new Command[ 0 ]; + + /** + *

Flag indicating whether the configuration of our commands list + * has been frozen by a call to the execute() method.

+ */ + 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 + + /** + *

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.

+ */ + 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; + /** *

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; + /** *

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 true; * otherwise return false */ - 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