directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From trus...@apache.org
Subject svn commit: r164015 - in /directory/network/trunk/src/java/org/apache/mina: common/ io/ io/datagram/ io/filter/ io/socket/ protocol/ protocol/io/ protocol/vmpipe/ util/
Date Thu, 21 Apr 2005 09:19:33 GMT
Author: trustin
Date: Thu Apr 21 02:19:31 2005
New Revision: 164015

URL: http://svn.apache.org/viewcvs?rev=164015&view=rev
Log:
* Added SessionManager interface that all acceptors and connectors extend
* Moved ExceptionMonitor and its default implementation to common package
* Added SessionInitializer

Added:
    directory/network/trunk/src/java/org/apache/mina/common/DefaultExceptionMonitor.java   (with props)
    directory/network/trunk/src/java/org/apache/mina/common/ExceptionMonitor.java   (with props)
    directory/network/trunk/src/java/org/apache/mina/common/SessionInitializer.java   (with props)
    directory/network/trunk/src/java/org/apache/mina/common/SessionManager.java   (with props)
    directory/network/trunk/src/java/org/apache/mina/util/BaseSessionManager.java   (with props)
    directory/network/trunk/src/java/org/apache/mina/util/ExceptionUtil.java   (with props)
Removed:
    directory/network/trunk/src/java/org/apache/mina/io/DefaultExceptionMonitor.java
    directory/network/trunk/src/java/org/apache/mina/io/ExceptionMonitor.java
Modified:
    directory/network/trunk/src/java/org/apache/mina/io/IoAcceptor.java
    directory/network/trunk/src/java/org/apache/mina/io/IoConnector.java
    directory/network/trunk/src/java/org/apache/mina/io/datagram/DatagramAcceptor.java
    directory/network/trunk/src/java/org/apache/mina/io/datagram/DatagramConnector.java
    directory/network/trunk/src/java/org/apache/mina/io/datagram/DatagramProcessor.java
    directory/network/trunk/src/java/org/apache/mina/io/filter/SSLFilter.java
    directory/network/trunk/src/java/org/apache/mina/io/socket/SocketAcceptor.java
    directory/network/trunk/src/java/org/apache/mina/io/socket/SocketConnector.java
    directory/network/trunk/src/java/org/apache/mina/protocol/ProtocolAcceptor.java
    directory/network/trunk/src/java/org/apache/mina/protocol/ProtocolConnector.java
    directory/network/trunk/src/java/org/apache/mina/protocol/io/IoProtocolAcceptor.java
    directory/network/trunk/src/java/org/apache/mina/protocol/io/IoProtocolConnector.java
    directory/network/trunk/src/java/org/apache/mina/protocol/vmpipe/VmPipeAcceptor.java
    directory/network/trunk/src/java/org/apache/mina/protocol/vmpipe/VmPipeConnector.java
    directory/network/trunk/src/java/org/apache/mina/protocol/vmpipe/VmPipeSession.java

Added: directory/network/trunk/src/java/org/apache/mina/common/DefaultExceptionMonitor.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/common/DefaultExceptionMonitor.java?rev=164015&view=auto
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/common/DefaultExceptionMonitor.java (added)
+++ directory/network/trunk/src/java/org/apache/mina/common/DefaultExceptionMonitor.java Thu Apr 21 02:19:31 2005
@@ -0,0 +1,115 @@
+/*
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 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.common;
+
+import java.lang.reflect.Method;
+import java.text.DateFormat;
+import java.util.Date;
+
+/**
+ * A default {@link ExceptionMonitor} implementation.  It logs uncaught
+ * exceptions using <a href="http://jakarta.apache.org/commons/logging/">Apache
+ * Jakarta Commons Logging</a> if available.  If not available, it prints it
+ * out to {@link System#err}.
+ * 
+ * @author Trustin Lee (trustin@apache.org)
+ * @version $Rev$, $Date$
+ */
+public class DefaultExceptionMonitor implements ExceptionMonitor
+{
+    private static final Object log;
+
+    private static final Method errorMethod;
+
+    static
+    {
+        Object tempLog = null;
+        Method tempErrorMethod = null;
+
+        try
+        {
+            Class logCls = Class.forName( "org.apache.commons.logging.Log" );
+            Class logFactoryCls = Class
+                    .forName( "org.apache.commons.logging.LogFactory" );
+            Method getLogMethod = logFactoryCls
+                    .getMethod( "getLog", new Class[] { String.class } );
+            tempLog = getLogMethod
+                    .invoke( null,
+                             new Object[] { DefaultExceptionMonitor.class
+                                     .getPackage().getName() } );
+            tempErrorMethod = logCls
+                    .getMethod( "error", new Class[] { Object.class,
+                                                      Throwable.class } );
+        }
+        catch( Exception e )
+        {
+            tempLog = null;
+            tempErrorMethod = null;
+        }
+
+        log = tempLog;
+        errorMethod = tempErrorMethod;
+    }
+
+    private final DateFormat df = DateFormat
+            .getDateTimeInstance( DateFormat.MEDIUM, DateFormat.MEDIUM );
+
+    private final Date date = new Date();
+
+    public void exceptionCaught( Object source, Throwable cause )
+    {
+        if( log == null )
+        {
+            logToStdErr( cause );
+        }
+        else
+        {
+            logToCommonsLogging( cause );
+        }
+    }
+
+    private void logToCommonsLogging( Throwable cause )
+    {
+        try
+        {
+            errorMethod.invoke( log, new Object[] { "Uncaught exception: ",
+                                                   cause } );
+        }
+        catch( Exception e )
+        {
+            logToStdErr( cause );
+        }
+    }
+
+    private void logToStdErr( Throwable cause )
+    {
+        synchronized( System.err )
+        {
+            date.setTime( System.currentTimeMillis() );
+
+            System.err.print( '[' );
+            System.err.print( df.format( date ) );
+            System.err.print( "] [" );
+            System.err.print( Thread.currentThread().getName() );
+            System.err.print( "] Uncaught exception: " );
+            cause.printStackTrace();
+        }
+    }
+
+}
\ No newline at end of file

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

Added: directory/network/trunk/src/java/org/apache/mina/common/ExceptionMonitor.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/common/ExceptionMonitor.java?rev=164015&view=auto
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/common/ExceptionMonitor.java (added)
+++ directory/network/trunk/src/java/org/apache/mina/common/ExceptionMonitor.java Thu Apr 21 02:19:31 2005
@@ -0,0 +1,36 @@
+/*
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 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.common;
+
+/**
+ * Monitors uncaught exceptions.  {@link #exceptionCaught(Object, Throwable)} is
+ * invoked when there are any uncaught exceptions.
+ * 
+ * @author Trustin Lee (trustin@apache.org)
+ * @version $Rev$, $Date$
+ * 
+ * @see DefaultExceptionMonitor
+ */
+public interface ExceptionMonitor
+{
+    /**
+     * Invoked when there are any uncaught exceptions.
+     */
+    void exceptionCaught( Object source, Throwable cause );
+}
\ No newline at end of file

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

Added: directory/network/trunk/src/java/org/apache/mina/common/SessionInitializer.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/common/SessionInitializer.java?rev=164015&view=auto
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/common/SessionInitializer.java (added)
+++ directory/network/trunk/src/java/org/apache/mina/common/SessionInitializer.java Thu Apr 21 02:19:31 2005
@@ -0,0 +1,64 @@
+/*
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 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.common;
+
+import java.io.IOException;
+
+import org.apache.mina.io.IoAcceptor;
+import org.apache.mina.io.IoConnector;
+import org.apache.mina.protocol.ProtocolAcceptor;
+import org.apache.mina.protocol.ProtocolConnector;
+
+
+/**
+ * Initializes session just after it is created.
+ * You can adjust {@link SessionConfig} or set pre-define user-defined attributes
+ * using this before MINA actually starts communication.
+ * <p>
+ * Please specify your initializer when you call:
+ * <ul>
+ *   <li>{@link IoAcceptor}<tt>.bind(...)</tt></li>
+ *   <li>{@link IoConnector}<tt>.connect(...)</tt></li>
+ *   <li>{@link ProtocolAcceptor}<tt>.bind(...)</tt></li>
+ *   <li>{@link ProtocolConnector}<tt>.connect(...)</tt></li>
+ * </ul>
+ * <p>
+ * In case of bind, session is closed immediately and no event is fired if
+ * {@link #initializeSession(Session)} throws any exception.  The exception
+ * is notified to <tt>ExceptionMonitor</tt>.
+ * <p>
+ * In case of connect, session is closed immediately and caught exception
+ * is forwarded to user application context which called <tt>connect(...)</tt>
+ * method.
+ 
+ * @author Trustin Lee (trustin@apache.org)
+ * @version $Rev$, $Date$
+ */
+public interface SessionInitializer {
+
+    /**
+     * Initializes session just after it is created.
+     * You can adjust {@link SessionConfig} or set pre-define user-defined
+     * attributes using this before MINA actually starts communication. 
+     * Session is closed immediately and no event is fired if
+     * {@link #initializeSession(Session)} throws any exception.
+     * The exception is notified to <tt>ExceptionMonitor</tt>.
+     */
+    void initializeSession( Session session ) throws IOException;
+}

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

Added: directory/network/trunk/src/java/org/apache/mina/common/SessionManager.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/common/SessionManager.java?rev=164015&view=auto
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/common/SessionManager.java (added)
+++ directory/network/trunk/src/java/org/apache/mina/common/SessionManager.java Thu Apr 21 02:19:31 2005
@@ -0,0 +1,44 @@
+/*
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 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.common;
+
+/**
+ * Base interface for all acceptors and connectors that manage
+ * sessions.
+ * <p>
+ * You can monitor any uncaught exceptions by setting {@link ExceptionMonitor}
+ * by calling {@link #setExceptionMonitor(ExceptionMonitor)}.  The default
+ * monitor is {@link DefaultExceptionMonitor}.
+ * 
+ * @author Trustin Lee (trustin@apache.org)
+ * @version $Rev$, $Date$
+ */
+public interface SessionManager {
+    
+    /**
+     * Returns the current exception monitor.
+     */
+    ExceptionMonitor getExceptionMonitor();
+
+    /**
+     * Sets the uncaught exception monitor.  If <code>null</code> is specified,
+     * a new instance of {@link DefaultExceptionMonitor} will be set.
+     */
+    void setExceptionMonitor( ExceptionMonitor monitor );
+}

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

Modified: directory/network/trunk/src/java/org/apache/mina/io/IoAcceptor.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/io/IoAcceptor.java?rev=164015&r1=164014&r2=164015&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/io/IoAcceptor.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/io/IoAcceptor.java Thu Apr 21 02:19:31 2005
@@ -21,6 +21,9 @@
 import java.io.IOException;
 import java.net.SocketAddress;
 
+import org.apache.mina.common.SessionInitializer;
+import org.apache.mina.common.SessionManager;
+
 /**
  * Accepts incoming connection, communicates with clients, and fires events to
  * {@link IoHandler}s.
@@ -39,15 +42,13 @@
  * <p>
  * {@link IoHandlerFilter}s can be added and removed at any time to filter
  * events just like Servlet filters and they are effective immediately.
- * <p>
- * You can monitor any uncaught exceptions by setting {@link ExceptionMonitor}
- * by calling {@link #setExceptionMonitor(ExceptionMonitor)}.  The default
- * monitor is {@link DefaultExceptionMonitor}.
  * 
  * @author Trustin Lee (trustin@apache.org)
  * @version $Rev$, $Date$
+ * 
+ * @see SessionInitializer
  */
-public interface IoAcceptor
+public interface IoAcceptor extends SessionManager
 {
     /**
      * Binds to the specified <code>address</code> and handles incoming
@@ -58,20 +59,17 @@
     void bind( SocketAddress address, IoHandler handler ) throws IOException;
 
     /**
-     * Unbinds from the specified <code>address</code>.
+     * Binds to the specified <code>address</code> and handles incoming
+     * connections with the specified <code>handler</code>.
+     * 
+     * @throws IOException if failed to bind
      */
-    void unbind( SocketAddress address );
+    void bind( SocketAddress address, IoHandler handler, SessionInitializer initializer ) throws IOException;
 
-    IoHandlerFilterChain getFilterChain();
-    
     /**
-     * Returns the current exception monitor.
+     * Unbinds from the specified <code>address</code>.
      */
-    ExceptionMonitor getExceptionMonitor();
+    void unbind( SocketAddress address );
 
-    /**
-     * Sets the uncaught exception monitor.  If <code>null</code> is specified,
-     * a new instance of {@link DefaultExceptionMonitor} will be set.
-     */
-    void setExceptionMonitor( ExceptionMonitor monitor );
+    IoHandlerFilterChain getFilterChain();
 }

Modified: directory/network/trunk/src/java/org/apache/mina/io/IoConnector.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/io/IoConnector.java?rev=164015&r1=164014&r2=164015&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/io/IoConnector.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/io/IoConnector.java Thu Apr 21 02:19:31 2005
@@ -21,6 +21,9 @@
 import java.io.IOException;
 import java.net.SocketAddress;
 
+import org.apache.mina.common.SessionInitializer;
+import org.apache.mina.common.SessionManager;
+
 /**
  * Connects to endpoint, communicates with the server, and fires events to
  * {@link IoHandler}s.
@@ -39,15 +42,13 @@
  * <p>
  * {@link IoHandlerFilter}s can be added and removed at any time to filter
  * events just like Servlet filters and they are effective immediately.
- * <p>
- * You can monitor any uncaught exceptions by setting {@link ExceptionMonitor}
- * by calling {@link #setExceptionMonitor(ExceptionMonitor)}.  The default
- * monitor is {@link DefaultExceptionMonitor}.
  * 
  * @author Trustin Lee (trustin@apache.org)
  * @version $Rev$, $Date$
+ * 
+ * @see SessionInitializer
  */
-public interface IoConnector
+public interface IoConnector extends SessionManager
 {
     /**
      * Connects to the specified <code>address</code>.  If communication starts
@@ -91,16 +92,48 @@
     IoSession connect( SocketAddress address, SocketAddress localAddress,
                        int timeout, IoHandler handler ) throws IOException;
 
-    IoHandlerFilterChain getFilterChain();
+    /**
+     * Connects to the specified <code>address</code>.  If communication starts
+     * successfully, events are fired to the specified
+     * <code>handler</code>.  This method blocks.
+     * 
+     * @throws IOException if failed to connect
+     */
+    IoSession connect( SocketAddress address, IoHandler handler,
+                       SessionInitializer initializer ) throws IOException;
 
     /**
-     * Returns the current exception monitor.
+     * Connects to the specified <code>address</code>.  If communication starts
+     * successfully, events are fired to the specified
+     * <code>handler</code>.  This method blocks.
+     * 
+     * @param localAddress the local address the channel is bound to
+     * @throws IOException if failed to connect
      */
-    ExceptionMonitor getExceptionMonitor();
+    IoSession connect( SocketAddress address, SocketAddress localAddress,
+                       IoHandler handler, SessionInitializer initializer ) throws IOException;
 
     /**
-     * Sets the uncaught exception monitor.  If <code>null</code> is specified,
-     * a new instance of {@link DefaultExceptionMonitor} will be set.
+     * Connects to the specified <code>address</code> with timeout.  If
+     * communication starts successfully, events are fired to the specified
+     * <code>handler</code>.  This method blocks.
+     * 
+     * @throws IOException if failed to connect
      */
-    void setExceptionMonitor( ExceptionMonitor monitor );
+    IoSession connect( SocketAddress address, int timeout, IoHandler handler,
+                       SessionInitializer initializer ) throws IOException;
+    
+    /**
+     * Connects to the specified <code>address</code> with timeout.  If
+     * communication starts successfully, events are fired to the specified
+     * <code>handler</code>.  This method blocks.
+     * 
+     * @param localAddress the local address the channel is bound to
+     * @throws IOException if failed to connect
+     */
+    IoSession connect( SocketAddress address, SocketAddress localAddress,
+                       int timeout, IoHandler handler,
+                       SessionInitializer initializer ) throws IOException;
+
+    IoHandlerFilterChain getFilterChain();
 }

Modified: directory/network/trunk/src/java/org/apache/mina/io/datagram/DatagramAcceptor.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/io/datagram/DatagramAcceptor.java?rev=164015&r1=164014&r2=164015&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/io/datagram/DatagramAcceptor.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/io/datagram/DatagramAcceptor.java Thu Apr 21 02:19:31 2005
@@ -30,11 +30,11 @@
 import java.util.Set;
 
 import org.apache.mina.common.ByteBuffer;
-import org.apache.mina.io.DefaultExceptionMonitor;
-import org.apache.mina.io.ExceptionMonitor;
+import org.apache.mina.common.SessionInitializer;
 import org.apache.mina.io.IoAcceptor;
 import org.apache.mina.io.IoHandler;
 import org.apache.mina.io.IoHandlerFilterChain;
+import org.apache.mina.util.ExceptionUtil;
 import org.apache.mina.util.Queue;
 
 /**
@@ -61,8 +61,6 @@
 
     private final Queue flushingSessions = new Queue();
 
-    private ExceptionMonitor exceptionMonitor = new DefaultExceptionMonitor();
-
     private Worker worker;
 
     /**
@@ -78,6 +76,12 @@
     public void bind( SocketAddress address, IoHandler handler )
             throws IOException
     {
+        bind( address, handler, null );
+    }
+
+    public void bind( SocketAddress address, IoHandler handler, SessionInitializer initializer )
+            throws IOException
+    {
         if( address == null )
             throw new NullPointerException( "address" );
         if( handler == null )
@@ -89,7 +93,7 @@
         if( ( ( InetSocketAddress ) address ).getPort() == 0 )
             throw new IllegalArgumentException( "Unsupported port number: 0" );
 
-        RegistrationRequest request = new RegistrationRequest( address, handler );
+        RegistrationRequest request = new RegistrationRequest( address, handler, initializer );
         synchronized( this )
         {
             synchronized( registerQueue )
@@ -117,22 +121,7 @@
         if( request.exception != null )
         {
             request.exception.fillInStackTrace();
-            if( request.exception instanceof IOException )
-            {
-                throw ( IOException ) request.exception;
-            }
-            else if( request.exception instanceof RuntimeException )
-            {
-                throw ( RuntimeException ) request.exception;
-            }
-            else if( request.exception instanceof Error )
-            {
-                throw ( Error ) request.exception;
-            }
-            else
-            {
-                throw new IllegalStateException();
-            }
+            ExceptionUtil.throwException( request.exception );
         }
     }
 
@@ -266,18 +255,31 @@
 
             DatagramChannel ch = ( DatagramChannel ) key.channel();
 
+            RegistrationRequest req = ( RegistrationRequest ) key.attachment();
             DatagramSession session = new DatagramSession(
-                    filters, ch, ( IoHandler ) key.attachment() );
+                    filters, ch, req.handler );
             session.setSelectionKey( key );
-
-            if( key.isReadable() )
+            
+            try
             {
-                readSession( session );
-            }
+                if( req.initializer != null )
+                {
+                    req.initializer.initializeSession( session );
+                }
 
-            if( key.isWritable() )
+                if( key.isReadable() )
+                {
+                    readSession( session );
+                }
+
+                if( key.isWritable() )
+                {
+                    scheduleFlush( session );
+                }
+            }
+            catch( Throwable t )
             {
-                scheduleFlush( session );
+                exceptionMonitor.exceptionCaught( this, t );
             }
         }
     }
@@ -432,7 +434,7 @@
                 ch = DatagramChannel.open();
                 ch.configureBlocking( false );
                 ch.socket().bind( req.address );
-                ch.register( selector, SelectionKey.OP_READ, req.handler );
+                ch.register( selector, SelectionKey.OP_READ, req );
                 channels.put( req.address, ch );
             }
             catch( Throwable t )
@@ -523,14 +525,18 @@
         
         private final IoHandler handler;
         
+        private final SessionInitializer initializer;
+        
         private Throwable exception; 
         
         private boolean done;
         
-        private RegistrationRequest( SocketAddress address, IoHandler handler )
+        private RegistrationRequest( SocketAddress address, IoHandler handler,
+                                     SessionInitializer initializer )
         {
             this.address = address;
             this.handler = handler;
+            this.initializer = initializer;
         }
     }
 
@@ -544,20 +550,5 @@
         {
             this.address = address;
         }
-    }
-
-    public ExceptionMonitor getExceptionMonitor()
-    {
-        return exceptionMonitor;
-    }
-
-    public void setExceptionMonitor( ExceptionMonitor monitor )
-    {
-        if( monitor == null )
-        {
-            monitor = new DefaultExceptionMonitor();
-        }
-
-        this.exceptionMonitor = monitor;
     }
 }

Modified: directory/network/trunk/src/java/org/apache/mina/io/datagram/DatagramConnector.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/io/datagram/DatagramConnector.java?rev=164015&r1=164014&r2=164015&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/io/datagram/DatagramConnector.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/io/datagram/DatagramConnector.java Thu Apr 21 02:19:31 2005
@@ -21,7 +21,6 @@
 import java.io.IOException;
 import java.net.InetSocketAddress;
 import java.net.SocketAddress;
-import java.nio.channels.ClosedChannelException;
 import java.nio.channels.DatagramChannel;
 import java.nio.channels.SelectionKey;
 import java.nio.channels.Selector;
@@ -29,12 +28,12 @@
 import java.util.Set;
 
 import org.apache.mina.common.ByteBuffer;
-import org.apache.mina.io.DefaultExceptionMonitor;
-import org.apache.mina.io.ExceptionMonitor;
+import org.apache.mina.common.SessionInitializer;
 import org.apache.mina.io.IoConnector;
 import org.apache.mina.io.IoHandler;
 import org.apache.mina.io.IoHandlerFilterChain;
 import org.apache.mina.io.IoSession;
+import org.apache.mina.util.ExceptionUtil;
 import org.apache.mina.util.Queue;
 
 /**
@@ -43,8 +42,7 @@
  * @author Trustin Lee (trustin@apache.org)
  * @version $Rev$, $Date$
  */
-public class DatagramConnector extends DatagramProcessor implements
-        IoConnector
+public class DatagramConnector extends DatagramProcessor implements IoConnector
 {
     private static volatile int nextId = 0;
 
@@ -60,8 +58,6 @@
 
     private final Queue flushingSessions = new Queue();
 
-    private ExceptionMonitor exceptionMonitor = new DefaultExceptionMonitor();
-
     private Worker worker;
 
     /**
@@ -74,14 +70,43 @@
         selector = Selector.open();
     }
 
-    public IoSession connect( SocketAddress address, IoHandler handler )
-            throws IOException
+    public IoSession connect( SocketAddress address, IoHandler handler ) throws IOException
     {
-        return connect( address, null, handler );
+        return connect( address, null, handler, null );
     }
-    
+
+    public IoSession connect( SocketAddress address, SocketAddress localAddress, IoHandler handler ) throws IOException
+    {
+        return connect( address, localAddress, handler, null );
+    }
+
+    public IoSession connect( SocketAddress address, int timeout, IoHandler handler ) throws IOException
+    {
+        return connect( address, null, handler, null );
+    }
+
+    public IoSession connect( SocketAddress address, SocketAddress localAddress, int timeout, IoHandler handler ) throws IOException
+    {
+        return connect( address, localAddress, handler, null );
+    }
+
+    public IoSession connect( SocketAddress address, IoHandler handler, SessionInitializer initializer ) throws IOException
+    {
+        return connect( address, null, handler, initializer );
+    }
+
+    public IoSession connect( SocketAddress address, int timeout, IoHandler handler, SessionInitializer initializer ) throws IOException
+    {
+        return connect( address, null, handler, initializer );
+    }
+
+    public IoSession connect( SocketAddress address, SocketAddress localAddress, int timeout, IoHandler handler, SessionInitializer initializer ) throws IOException
+    {
+        return connect( address, localAddress, handler, initializer );
+    }
+
     public IoSession connect( SocketAddress address, SocketAddress localAddress,
-                              IoHandler handler ) throws IOException
+                              IoHandler handler, SessionInitializer initializer ) throws IOException
     {
         if( address == null )
             throw new NullPointerException( "address" );
@@ -118,7 +143,7 @@
             }
         }
 
-        RegistrationRequest request = new RegistrationRequest( ch, handler );
+        RegistrationRequest request = new RegistrationRequest( ch, handler, initializer );
         synchronized( this )
         {
             synchronized( registerQueue )
@@ -132,7 +157,7 @@
 
         synchronized( request )
         {
-            while( request.session == null )
+            while( !request.done )
             {
                 try
                 {
@@ -143,6 +168,12 @@
                 }
             }
         }
+        
+        if( request.exception != null )
+        {
+            request.exception.fillInStackTrace();
+            ExceptionUtil.throwException( request.exception );
+        }
 
         return request.session;
     }
@@ -156,18 +187,6 @@
         }
     }
 
-    public IoSession connect( SocketAddress address,
-                              int timeout, IoHandler handler ) throws IOException
-    {
-        return connect( address, null, timeout, handler );
-    }
-
-    public IoSession connect( SocketAddress address, SocketAddress localAddress,
-                              int timeout, IoHandler handler ) throws IOException
-    {
-        return connect( address, localAddress, handler );
-    }
-
     void closeSession( DatagramSession session )
     {
         synchronized( this )
@@ -396,7 +415,7 @@
         }
     }
 
-    private void registerNew() throws ClosedChannelException
+    private void registerNew()
     {
         if( registerQueue.isEmpty() )
             return;
@@ -415,15 +434,43 @@
             DatagramSession session = new DatagramSession(
                     filters, req.channel, req.handler );
 
-            SelectionKey key = req.channel.register( selector,
-                    SelectionKey.OP_READ, session );
+            try
+            {
+                SelectionKey key = req.channel.register( selector,
+                        SelectionKey.OP_READ, session );
+    
+                session.setSelectionKey( key );
 
-            session.setSelectionKey( key );
+                if( req.initializer != null )
+                {
+                    req.initializer.initializeSession( session );
+                }
 
-            synchronized( req )
+            }
+            catch( Throwable t )
             {
-                req.session = session;
-                req.notify();
+                req.exception = t;
+            }
+            finally 
+            {
+                synchronized( req )
+                {
+                    req.done = true;
+                    req.session = session;
+                    req.notify();
+                }
+                
+                if( req.exception != null )
+                {
+                    try
+                    {
+                        req.channel.close();
+                    }
+                    catch (IOException e)
+                    {
+                        exceptionMonitor.exceptionCaught( this, e );
+                    }
+                }
             }
         }
     }
@@ -469,29 +516,21 @@
         private final DatagramChannel channel;
 
         private final IoHandler handler;
+        
+        private final SessionInitializer initializer;
 
+        private boolean done;
+        
         private DatagramSession session;
+        
+        private Throwable exception;
 
         private RegistrationRequest( DatagramChannel channel,
-                                    IoHandler handler )
+                                     IoHandler handler, SessionInitializer initializer )
         {
             this.channel = channel;
             this.handler = handler;
+            this.initializer = initializer;
         }
-    }
-
-    public ExceptionMonitor getExceptionMonitor()
-    {
-        return exceptionMonitor;
-    }
-
-    public void setExceptionMonitor( ExceptionMonitor monitor )
-    {
-        if( monitor == null )
-        {
-            monitor = new DefaultExceptionMonitor();
-        }
-
-        this.exceptionMonitor = monitor;
     }
 }

Modified: directory/network/trunk/src/java/org/apache/mina/io/datagram/DatagramProcessor.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/io/datagram/DatagramProcessor.java?rev=164015&r1=164014&r2=164015&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/io/datagram/DatagramProcessor.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/io/datagram/DatagramProcessor.java Thu Apr 21 02:19:31 2005
@@ -18,6 +18,8 @@
  */
 package org.apache.mina.io.datagram;
 
+import org.apache.mina.util.BaseSessionManager;
+
 /**
  * A base class for {@link DatagramAcceptor} and {@link DatagramConnector}.
  * Session interacts with this abstract class instead of those two concrete
@@ -26,7 +28,7 @@
  * @author Trustin Lee (trustin@apache.org)
  * @version $Rev$, $Date$
  */
-abstract class DatagramProcessor
+abstract class DatagramProcessor extends BaseSessionManager
 {
     /**
      * Requests this processor to flush the write buffer of the specified

Modified: directory/network/trunk/src/java/org/apache/mina/io/filter/SSLFilter.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/io/filter/SSLFilter.java?rev=164015&r1=164014&r2=164015&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/io/filter/SSLFilter.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/io/filter/SSLFilter.java Thu Apr 21 02:19:31 2005
@@ -30,7 +30,7 @@
 import javax.net.ssl.SSLHandshakeException;
 
 import org.apache.mina.common.ByteBuffer;
-import org.apache.mina.io.DefaultExceptionMonitor;
+import org.apache.mina.common.DefaultExceptionMonitor;
 import org.apache.mina.io.IoHandlerFilterAdapter;
 import org.apache.mina.io.IoSession;
 

Modified: directory/network/trunk/src/java/org/apache/mina/io/socket/SocketAcceptor.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/io/socket/SocketAcceptor.java?rev=164015&r1=164014&r2=164015&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/io/socket/SocketAcceptor.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/io/socket/SocketAcceptor.java Thu Apr 21 02:19:31 2005
@@ -30,11 +30,11 @@
 import java.util.Map;
 import java.util.Set;
 
-import org.apache.mina.io.DefaultExceptionMonitor;
-import org.apache.mina.io.ExceptionMonitor;
+import org.apache.mina.common.SessionInitializer;
 import org.apache.mina.io.IoAcceptor;
 import org.apache.mina.io.IoHandler;
 import org.apache.mina.io.IoHandlerFilterChain;
+import org.apache.mina.util.BaseSessionManager;
 import org.apache.mina.util.Queue;
 
 /**
@@ -43,7 +43,7 @@
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  * @version $Rev$, $Date$
  */
-public class SocketAcceptor implements IoAcceptor
+public class SocketAcceptor extends BaseSessionManager implements IoAcceptor
 {
     private static volatile int nextId = 0;
 
@@ -61,8 +61,6 @@
     
     private int backlog = 50;
 
-    private ExceptionMonitor exceptionMonitor = new DefaultExceptionMonitor();
-
     private Worker worker;
 
 
@@ -76,7 +74,6 @@
         selector = Selector.open();
     }
 
-
     /**
      * Binds to the specified <code>address</code> and handles incoming
      * connections with the specified <code>handler</code>.  Backlog value
@@ -86,6 +83,19 @@
      */
     public void bind( SocketAddress address, IoHandler handler ) throws IOException
     {
+        bind( address, handler, null );
+    }
+
+    /**
+     * Binds to the specified <code>address</code> and handles incoming
+     * connections with the specified <code>handler</code>.  Backlog value
+     * is configured to the value of <code>backlog</code> property.
+     *
+     * @throws IOException if failed to bind
+     */
+    public void bind( SocketAddress address, IoHandler handler,
+                      SessionInitializer initializer ) throws IOException
+    {
         if( address == null )
         {
             throw new NullPointerException( "address" );
@@ -106,7 +116,7 @@
             throw new IllegalArgumentException( "Unsupported port number: 0" );
         }
 
-        RegistrationRequest request = new RegistrationRequest( address, backlog, handler );
+        RegistrationRequest request = new RegistrationRequest( address, backlog, handler, initializer );
 
         synchronized( this )
         {
@@ -136,7 +146,6 @@
         if( request.exception != null )
         {
             request.exception.fillInStackTrace();
-
             throw request.exception;
         }
     }
@@ -291,9 +300,31 @@
                     continue;
                 }
    
-                SocketSession session = new SocketSession( filters, ch, ( IoHandler ) key.attachment() );
-   
-                SocketIoProcessor.getInstance().addSession( session );
+                boolean success = false;
+                try
+                {
+                    RegistrationRequest req = ( RegistrationRequest ) key.attachment();
+                    SocketSession session = new SocketSession( filters, ch, req.handler );
+                    
+                    if( req.initializer != null )
+                    {
+                        req.initializer.initializeSession( session );
+                    }
+                    
+                    SocketIoProcessor.getInstance().addSession( session );
+                    success = true;
+                }
+                catch( Throwable t )
+                {
+                    exceptionMonitor.exceptionCaught( SocketAcceptor.this, t );
+                }
+                finally
+                {
+                    if( !success )
+                    {
+                        ch.close();
+                    }
+                }
             }
         }
     }
@@ -330,7 +361,7 @@
 
                 ssc.socket().bind( req.address, req.backlog );
 
-                ssc.register( selector, SelectionKey.OP_ACCEPT, req.handler );
+                ssc.register( selector, SelectionKey.OP_ACCEPT, req );
 
                 channels.put( req.address, ssc );
             }
@@ -433,17 +464,19 @@
 
         private final IoHandler handler;
         
+        private final SessionInitializer initializer;
+        
         private IOException exception; 
         
         private boolean done;
         
-        private RegistrationRequest( SocketAddress address, int backlog, IoHandler handler )
+        private RegistrationRequest( SocketAddress address, int backlog, IoHandler handler,
+                                     SessionInitializer initializer )
         {
             this.address = address;
-
             this.backlog = backlog;
-
             this.handler = handler;
+            this.initializer = initializer;
         }
     }
 
@@ -460,22 +493,5 @@
         {
             this.address = address;
         }
-    }
-
-    
-    public ExceptionMonitor getExceptionMonitor()
-    {
-        return exceptionMonitor;
-    }
-
-
-    public void setExceptionMonitor( ExceptionMonitor monitor )
-    {
-        if( monitor == null )
-        {
-            monitor = new DefaultExceptionMonitor();
-        }
-
-        this.exceptionMonitor = monitor;
     }
 }

Modified: directory/network/trunk/src/java/org/apache/mina/io/socket/SocketConnector.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/io/socket/SocketConnector.java?rev=164015&r1=164014&r2=164015&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/io/socket/SocketConnector.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/io/socket/SocketConnector.java Thu Apr 21 02:19:31 2005
@@ -28,12 +28,12 @@
 import java.util.Iterator;
 import java.util.Set;
 
-import org.apache.mina.io.DefaultExceptionMonitor;
-import org.apache.mina.io.ExceptionMonitor;
+import org.apache.mina.common.SessionInitializer;
 import org.apache.mina.io.IoConnector;
 import org.apache.mina.io.IoHandler;
 import org.apache.mina.io.IoHandlerFilterChain;
 import org.apache.mina.io.IoSession;
+import org.apache.mina.util.BaseSessionManager;
 import org.apache.mina.util.Queue;
 
 /**
@@ -42,7 +42,7 @@
  * @author Trustin Lee (trustin@apache.org)
  * @version $Rev$, $Date$
  */
-public class SocketConnector implements IoConnector
+public class SocketConnector extends BaseSessionManager implements IoConnector
 {
     private static volatile int nextId = 0;
 
@@ -52,8 +52,6 @@
 
     private final Selector selector;
 
-    private ExceptionMonitor exceptionMonitor = new DefaultExceptionMonitor();
-
     private final Queue connectQueue = new Queue();
 
     private Worker worker;
@@ -68,26 +66,44 @@
         selector = Selector.open();
     }
 
-    public IoSession connect( SocketAddress address, IoHandler handler )
-            throws IOException
+    public IoSession connect( SocketAddress address, IoHandler handler ) throws IOException
     {
-        return connect( address, null, handler );
+        return connect( address, null, Integer.MAX_VALUE, handler, null);
     }
 
-    public IoSession connect( SocketAddress address, SocketAddress localAddress,
-                              IoHandler handler ) throws IOException
+    public IoSession connect( SocketAddress address, SocketAddress localAddress, IoHandler handler ) throws IOException
+    {
+        return connect( address, localAddress, Integer.MAX_VALUE, handler, null);
+    }
+
+    public IoSession connect( SocketAddress address, int timeout, IoHandler handler ) throws IOException
+    {
+        return connect( address, null, timeout, handler, null);
+    }
+
+    public IoSession connect( SocketAddress address, SocketAddress localAddress, int timeout, IoHandler handler ) throws IOException
+    {
+        return connect( address, localAddress, timeout, handler, null);
+    }
+
+    public IoSession connect( SocketAddress address, IoHandler handler, SessionInitializer initializer ) throws IOException
+    {
+        return connect( address, null, Integer.MAX_VALUE, handler, initializer );
+    }
+
+    public IoSession connect( SocketAddress address, SocketAddress localAddress, IoHandler handler, SessionInitializer initializer ) throws IOException
     {
-        return connect( address, localAddress, Integer.MAX_VALUE, handler );
+        return connect( address, localAddress, Integer.MAX_VALUE, handler, initializer );
     }
 
-    public IoSession connect( SocketAddress address, int timeout,
-                              IoHandler handler ) throws IOException
+    public IoSession connect( SocketAddress address, int timeout, IoHandler handler, SessionInitializer initializer ) throws IOException
     {
-        return connect( address, null, timeout, handler );
+        return connect( address, null, timeout, handler, initializer );
     }
 
     public IoSession connect( SocketAddress address, SocketAddress localAddress,
-                              int timeout, IoHandler handler ) throws IOException
+                              int timeout, IoHandler handler,
+                              SessionInitializer initializer ) throws IOException
     {
         if( address == null )
             throw new NullPointerException( "address" );
@@ -106,7 +122,7 @@
                                                 + localAddress.getClass() );
 
         SocketChannel ch = SocketChannel.open();
-        boolean initialized = false;
+        boolean success = false;
         try
         {
             if( localAddress != null )
@@ -115,23 +131,22 @@
             }
             ch.configureBlocking( false );
     
-            boolean connected = ch.connect( address );
-            initialized = true;
-
-            if( connected )
+            if( ch.connect( address ) )
             {
-                return newSession( ch, handler );
+                return newSession( ch, handler, initializer );
             }
+            
+            success = true;
         }
         finally
         {
-            if( !initialized )
+            if( !success )
             {
                 ch.close();
             }
         }
 
-        ConnectionRequest request = new ConnectionRequest( ch, timeout, handler );
+        ConnectionRequest request = new ConnectionRequest( ch, timeout, handler, initializer );
         synchronized( this )
         {
             synchronized( connectQueue )
@@ -224,7 +239,7 @@
             try
             {
                 ch.finishConnect();
-                SocketSession session = newSession( ch, entry.handler );
+                SocketSession session = newSession( ch, entry.handler, entry.initializer );
                 entry.session = session;
             }
             catch( IOException e )
@@ -275,9 +290,13 @@
         }
     }
 
-    private SocketSession newSession( SocketChannel ch, IoHandler handler )
+    private SocketSession newSession( SocketChannel ch, IoHandler handler, SessionInitializer initializer ) throws IOException
     {
         SocketSession session = new SocketSession( filters, ch, handler );
+        if( initializer != null )
+        {
+            initializer.initializeSession( session );
+        }
         SocketIoProcessor.getInstance().addSession( session );
         return session;
     }
@@ -342,6 +361,8 @@
         private final long deadline;
 
         private final IoHandler handler;
+        
+        private final SessionInitializer initializer;
 
         private SocketSession session;
 
@@ -349,31 +370,17 @@
 
         private IOException exception;
 
-        private ConnectionRequest( SocketChannel channel, int timeout, IoHandler handler )
+        private ConnectionRequest( SocketChannel channel, int timeout, IoHandler handler, SessionInitializer initializer )
         {
             this.channel = channel;
             this.deadline = System.currentTimeMillis() + timeout * 1000L;
             this.handler = handler;
+            this.initializer = initializer;
         }
     }
 
     public IoHandlerFilterChain getFilterChain()
     {
         return filters;
-    }
-
-    public ExceptionMonitor getExceptionMonitor()
-    {
-        return exceptionMonitor;
-    }
-
-    public void setExceptionMonitor( ExceptionMonitor monitor )
-    {
-        if( monitor == null )
-        {
-            monitor = new DefaultExceptionMonitor();
-        }
-
-        this.exceptionMonitor = monitor;
     }
 }

Modified: directory/network/trunk/src/java/org/apache/mina/protocol/ProtocolAcceptor.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/protocol/ProtocolAcceptor.java?rev=164015&r1=164014&r2=164015&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/protocol/ProtocolAcceptor.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/protocol/ProtocolAcceptor.java Thu Apr 21 02:19:31 2005
@@ -21,6 +21,9 @@
 import java.io.IOException;
 import java.net.SocketAddress;
 
+import org.apache.mina.common.SessionInitializer;
+import org.apache.mina.common.SessionManager;
+
 /**
  * Accepts incoming connection, communicates with clients, and fires events to
  * {@link ProtocolHandler}s.
@@ -42,8 +45,10 @@
  * 
  * @author Trustin Lee (trustin@apache.org)
  * @version $Rev$, $Date$
+ * 
+ * @see SessionInitializer
  */
-public interface ProtocolAcceptor
+public interface ProtocolAcceptor extends SessionManager
 {
     /**
      * Binds to the specified <code>address</code> and handles incoming
@@ -52,6 +57,15 @@
      * @throws IOException if failed to bind
      */
     void bind( SocketAddress address, ProtocolProvider protocolProvider )
+            throws IOException;
+
+    /**
+     * Binds to the specified <code>address</code> and handles incoming
+     * connections with the specified <code>protocolProvider</code>.
+     * 
+     * @throws IOException if failed to bind
+     */
+    void bind( SocketAddress address, ProtocolProvider protocolProvider, SessionInitializer initializer )
             throws IOException;
 
     /**

Modified: directory/network/trunk/src/java/org/apache/mina/protocol/ProtocolConnector.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/protocol/ProtocolConnector.java?rev=164015&r1=164014&r2=164015&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/protocol/ProtocolConnector.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/protocol/ProtocolConnector.java Thu Apr 21 02:19:31 2005
@@ -21,6 +21,9 @@
 import java.io.IOException;
 import java.net.SocketAddress;
 
+import org.apache.mina.common.SessionInitializer;
+import org.apache.mina.common.SessionManager;
+
 /**
  * Connects to endpoint, communicates with the server, and fires events to
  * {@link ProtocolProvider}s.
@@ -42,8 +45,10 @@
  * 
  * @author Trustin Lee (trustin@apache.org)
  * @version $Rev$, $Date$
+ * 
+ * @see SessionInitializer
  */
-public interface ProtocolConnector
+public interface ProtocolConnector extends SessionManager
 {
     /**
      * Connects to the specified <code>address</code>.  If communication starts
@@ -86,6 +91,52 @@
      */
     ProtocolSession connect( SocketAddress address, SocketAddress localAddress,
                              int timeout, ProtocolProvider protocolProvider ) throws IOException;
+
+    /**
+     * Connects to the specified <code>address</code>.  If communication starts
+     * successfully, events are fired to the specified
+     * <code>protocolProvider</code>.  This method blocks.
+     * 
+     * @throws IOException if failed to connect
+     */
+    ProtocolSession connect( SocketAddress address,
+                             ProtocolProvider protocolProvider,
+                             SessionInitializer initializer ) throws IOException;
+
+    /**
+     * Connects to the specified <code>address</code>.  If communication starts
+     * successfully, events are fired to the specified
+     * <code>protocolProvider</code>.  This method blocks.
+     * 
+     * @param localAddress the local address the channel is bound to
+     * @throws IOException if failed to connect
+     */
+    ProtocolSession connect( SocketAddress address, SocketAddress localAddress,
+                             ProtocolProvider protocolProvider,
+                             SessionInitializer initializer ) throws IOException;
+
+    /**
+     * Connects to the specified <code>address</code> with timeout.  If
+     * communication starts successfully, events are fired to the specified
+     * <code>protocolProvider</code>.  This method blocks.
+     * 
+     * @throws IOException if failed to connect
+     */
+    ProtocolSession connect( SocketAddress address, int timeout,
+                             ProtocolProvider protocolProvider,
+                             SessionInitializer initializer ) throws IOException;
+
+    /**
+     * Connects to the specified <code>address</code> with timeout.  If
+     * communication starts successfully, events are fired to the specified
+     * <code>protocolProvider</code>.  This method blocks.
+     * 
+     * @param localAddress the local address the channel is bound to
+     * @throws IOException if failed to connect
+     */
+    ProtocolSession connect( SocketAddress address, SocketAddress localAddress,
+                             int timeout, ProtocolProvider protocolProvider,
+                             SessionInitializer initializer ) throws IOException;
 
     ProtocolHandlerFilterChain getFilterChain();
 }

Modified: directory/network/trunk/src/java/org/apache/mina/protocol/io/IoProtocolAcceptor.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/protocol/io/IoProtocolAcceptor.java?rev=164015&r1=164014&r2=164015&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/protocol/io/IoProtocolAcceptor.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/protocol/io/IoProtocolAcceptor.java Thu Apr 21 02:19:31 2005
@@ -6,6 +6,8 @@
 import java.io.IOException;
 import java.net.SocketAddress;
 
+import org.apache.mina.common.ExceptionMonitor;
+import org.apache.mina.common.SessionInitializer;
 import org.apache.mina.io.IoAcceptor;
 import org.apache.mina.protocol.ProtocolAcceptor;
 import org.apache.mina.protocol.ProtocolHandlerFilterChain;
@@ -49,6 +51,12 @@
     {
         acceptor.bind( address, adapter.adapt( provider ) );
     }
+    
+    public void bind( SocketAddress address, ProtocolProvider provider,
+                      SessionInitializer initializer ) throws IOException
+    {
+        acceptor.bind( address, adapter.adapt( provider ), initializer );
+    }
 
     public void unbind( SocketAddress address )
     {
@@ -58,5 +66,15 @@
     public ProtocolHandlerFilterChain getFilterChain()
     {
         return adapter.getFilterChain();
+    }
+
+    public ExceptionMonitor getExceptionMonitor()
+    {
+        return acceptor.getExceptionMonitor();
+    }
+
+    public void setExceptionMonitor( ExceptionMonitor monitor )
+    {
+        acceptor.setExceptionMonitor( monitor );
     }
 }

Modified: directory/network/trunk/src/java/org/apache/mina/protocol/io/IoProtocolConnector.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/protocol/io/IoProtocolConnector.java?rev=164015&r1=164014&r2=164015&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/protocol/io/IoProtocolConnector.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/protocol/io/IoProtocolConnector.java Thu Apr 21 02:19:31 2005
@@ -6,6 +6,8 @@
 import java.io.IOException;
 import java.net.SocketAddress;
 
+import org.apache.mina.common.ExceptionMonitor;
+import org.apache.mina.common.SessionInitializer;
 import org.apache.mina.io.IoConnector;
 import org.apache.mina.io.IoSession;
 import org.apache.mina.protocol.ProtocolConnector;
@@ -77,8 +79,51 @@
         return adapter.toProtocolSession( session );
     }
 
+    public ProtocolSession connect( SocketAddress address,
+                                    ProtocolProvider provider,
+                                    SessionInitializer initializer ) throws IOException
+    {
+        IoSession session = connector.connect(
+                address, adapter.adapt( provider ), initializer );
+        return adapter.toProtocolSession( session );
+    }
+
+    public ProtocolSession connect( SocketAddress address, SocketAddress localAddress,
+                                    ProtocolProvider provider, SessionInitializer initializer ) throws IOException
+    {
+        IoSession session = connector.connect(
+                address, localAddress, adapter.adapt( provider ), initializer );
+        return adapter.toProtocolSession( session );
+    }
+
+    public ProtocolSession connect( SocketAddress address, int timeout,
+                                    ProtocolProvider provider, SessionInitializer initializer ) throws IOException
+    {
+        IoSession session = connector.connect(
+                address, timeout, adapter.adapt( provider ), initializer );
+        return adapter.toProtocolSession( session );
+    }
+
+    public ProtocolSession connect( SocketAddress address, SocketAddress localAddress,
+                                    int timeout, ProtocolProvider provider, SessionInitializer initializer ) throws IOException
+    {
+        IoSession session = connector.connect(
+                address, localAddress, timeout, adapter.adapt( provider ), initializer );
+        return adapter.toProtocolSession( session );
+    }
+
     public ProtocolHandlerFilterChain getFilterChain()
     {
         return adapter.getFilterChain();
+    }
+
+    public ExceptionMonitor getExceptionMonitor()
+    {
+        return connector.getExceptionMonitor();
+    }
+
+    public void setExceptionMonitor( ExceptionMonitor monitor )
+    {
+        connector.setExceptionMonitor( monitor );
     }
 }

Modified: directory/network/trunk/src/java/org/apache/mina/protocol/vmpipe/VmPipeAcceptor.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/protocol/vmpipe/VmPipeAcceptor.java?rev=164015&r1=164014&r2=164015&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/protocol/vmpipe/VmPipeAcceptor.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/protocol/vmpipe/VmPipeAcceptor.java Thu Apr 21 02:19:31 2005
@@ -8,10 +8,12 @@
 import java.util.HashMap;
 import java.util.Map;
 
+import org.apache.mina.common.SessionInitializer;
 import org.apache.mina.protocol.ProtocolAcceptor;
 import org.apache.mina.protocol.ProtocolHandler;
 import org.apache.mina.protocol.ProtocolHandlerFilterChain;
 import org.apache.mina.protocol.ProtocolProvider;
+import org.apache.mina.util.BaseSessionManager;
 
 /**
  * Binds the specified {@link ProtocolProvider} to the specified
@@ -20,7 +22,7 @@
  * @author Trustin Lee (trustin@apache.org)
  * @version $Rev$, $Date$
  */
-public class VmPipeAcceptor implements ProtocolAcceptor
+public class VmPipeAcceptor extends BaseSessionManager implements ProtocolAcceptor
 {
     static final Map boundHandlers = new HashMap();
 
@@ -33,9 +35,14 @@
     {
         filters.addLast( "VMPipe", new VmPipeFilter() );
     }
+    
+    public void bind( SocketAddress address, ProtocolProvider protocolProvider ) throws IOException
+    {
+        bind( address, protocolProvider, null );
+    }
 
-    public void bind( SocketAddress address, ProtocolProvider protocolProvider )
-            throws IOException
+    public void bind( SocketAddress address, ProtocolProvider protocolProvider,
+                      SessionInitializer initializer ) throws IOException
     {
         if( address == null )
             throw new NullPointerException( "address" );
@@ -52,8 +59,12 @@
                 throw new IOException( "Address already bound: " + address );
             }
 
-            boundHandlers.put( address, new Entry( ( VmPipeAddress ) address,
-                    filters, protocolProvider.getHandler() ) );
+            boundHandlers.put( address, 
+                               new Entry( this,
+                                          ( VmPipeAddress ) address,
+                                          filters,
+                                          protocolProvider.getHandler(),
+                                          initializer ) );
         }
     }
 
@@ -75,19 +86,27 @@
 
     static class Entry
     {
+        final VmPipeAcceptor acceptor;
+        
         final VmPipeAddress address;
 
         final VmPipeFilterChain filters;
 
         final ProtocolHandler handler;
+        
+        final SessionInitializer initializer;
 
-        private Entry( VmPipeAddress address,
+        private Entry( VmPipeAcceptor acceptor,
+                       VmPipeAddress address,
                        VmPipeFilterChain filters,
-                       ProtocolHandler handler )
+                       ProtocolHandler handler,
+                       SessionInitializer initializer )
         {
+            this.acceptor = acceptor;
             this.address = address;
             this.filters = filters;
             this.handler = handler;
+            this.initializer = initializer;
         }
     }
 }

Modified: directory/network/trunk/src/java/org/apache/mina/protocol/vmpipe/VmPipeConnector.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/protocol/vmpipe/VmPipeConnector.java?rev=164015&r1=164014&r2=164015&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/protocol/vmpipe/VmPipeConnector.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/protocol/vmpipe/VmPipeConnector.java Thu Apr 21 02:19:31 2005
@@ -6,11 +6,13 @@
 import java.io.IOException;
 import java.net.SocketAddress;
 
+import org.apache.mina.common.SessionInitializer;
 import org.apache.mina.protocol.ProtocolConnector;
 import org.apache.mina.protocol.ProtocolHandlerFilterChain;
 import org.apache.mina.protocol.ProtocolProvider;
 import org.apache.mina.protocol.ProtocolSession;
 import org.apache.mina.protocol.vmpipe.VmPipeAcceptor.Entry;
+import org.apache.mina.util.BaseSessionManager;
 
 /**
  * Connects to {@link ProtocolProvider}s which is bound on the specified
@@ -19,7 +21,7 @@
  * @author Trustin Lee (trustin@apache.org)
  * @version $Rev$, $Date$
  */
-public class VmPipeConnector implements ProtocolConnector
+public class VmPipeConnector extends BaseSessionManager implements ProtocolConnector
 {
     private final VmPipeFilterChain filters = new VmPipeFilterChain();
 
@@ -36,14 +38,44 @@
         return filters;
     }
 
-    public ProtocolSession connect( SocketAddress address, SocketAddress localAddress,
-                                    ProtocolProvider protocolProvider ) throws IOException
+    public ProtocolSession connect( SocketAddress address, ProtocolProvider protocolProvider ) throws IOException 
     {
-        return connect( address, protocolProvider );
+        return connect( address, null, Integer.MAX_VALUE, protocolProvider, null );
+    }
+
+    public ProtocolSession connect( SocketAddress address, SocketAddress localAddress, ProtocolProvider protocolProvider ) throws IOException
+    {
+        return connect( address, localAddress, Integer.MAX_VALUE, protocolProvider, null );
+    }
+
+    public ProtocolSession connect( SocketAddress address, int timeout, ProtocolProvider protocolProvider ) throws IOException
+    {
+        return connect( address, null, timeout, protocolProvider, null );
+    }
+
+    public ProtocolSession connect( SocketAddress address, SocketAddress localAddress, int timeout, ProtocolProvider protocolProvider ) throws IOException
+    {
+        return connect( address, localAddress, timeout, protocolProvider, null );
+    }
+
+    public ProtocolSession connect( SocketAddress address, SocketAddress localAddress, ProtocolProvider protocolProvider, SessionInitializer initializer ) throws IOException
+    {
+        return connect( address, localAddress, Integer.MAX_VALUE, protocolProvider, initializer );
+    }
+
+    public ProtocolSession connect( SocketAddress address, int timeout, ProtocolProvider protocolProvider, SessionInitializer initializer ) throws IOException
+    {
+        return connect( address, null, timeout, protocolProvider, initializer );
+    }
+
+    public ProtocolSession connect( SocketAddress address, SocketAddress localAddress, int timeout, ProtocolProvider protocolProvider, SessionInitializer initializer ) throws IOException
+    {
+        return connect( address, localAddress, timeout, protocolProvider, initializer );
     }
 
     public ProtocolSession connect( SocketAddress address,
-                                    ProtocolProvider protocolProvider ) throws IOException
+                                    ProtocolProvider protocolProvider,
+                                    SessionInitializer initializer ) throws IOException
     {
         if( address == null )
             throw new NullPointerException( "address" );
@@ -57,28 +89,14 @@
         if( entry == null )
             throw new IOException( "Endpoint unavailable: " + address );
 
-        VmPipeSession session = new VmPipeSession(
-                                                   new Object(), // lock
+        VmPipeSession session = new VmPipeSession( new Object(), // lock
                                                    AnonymousVmPipeAddress.INSTANCE,
-                                                   entry.address,
                                                    filters,
-                                                   protocolProvider
-                                                           .getHandler(),
-                                                   entry.filters,
-                                                   entry.handler );
+                                                   protocolProvider.getHandler(),
+                                                   initializer,
+                                                   entry );
+
         VmPipeIdleStatusChecker.INSTANCE.addSession( session );
         return session;
-    }
-
-    public ProtocolSession connect( SocketAddress address, int timeout,
-                                    ProtocolProvider protocolProvider ) throws IOException
-    {
-        return connect( address, protocolProvider );
-    }
-
-    public ProtocolSession connect( SocketAddress address, SocketAddress localAddress,
-                                    int timeout, ProtocolProvider protocolProvider ) throws IOException
-    {
-        return connect( address, protocolProvider );
     }
 }

Modified: directory/network/trunk/src/java/org/apache/mina/protocol/vmpipe/VmPipeSession.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/protocol/vmpipe/VmPipeSession.java?rev=164015&r1=164014&r2=164015&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/protocol/vmpipe/VmPipeSession.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/protocol/vmpipe/VmPipeSession.java Thu Apr 21 02:19:31 2005
@@ -3,14 +3,17 @@
  */
 package org.apache.mina.protocol.vmpipe;
 
+import java.io.IOException;
 import java.net.SocketAddress;
 
 import org.apache.mina.common.SessionConfig;
+import org.apache.mina.common.SessionInitializer;
 import org.apache.mina.common.TransportType;
 import org.apache.mina.protocol.ProtocolDecoder;
 import org.apache.mina.protocol.ProtocolEncoder;
 import org.apache.mina.protocol.ProtocolHandler;
 import org.apache.mina.protocol.ProtocolSession;
+import org.apache.mina.protocol.vmpipe.VmPipeAcceptor.Entry;
 import org.apache.mina.util.BaseSession;
 
 /**
@@ -26,7 +29,7 @@
     private final SocketAddress remoteAddress;
 
     private final ProtocolHandler localHandler;
-
+    
     private final VmPipeSessionConfig config = new VmPipeSessionConfig();
 
     final VmPipeFilterChain localFilters;
@@ -43,22 +46,37 @@
      * Constructor for client-side session.
      */
     VmPipeSession( Object lock, SocketAddress localAddress,
-                  SocketAddress remoteAddress,
-                  VmPipeFilterChain localFilters,
-                  ProtocolHandler localHandler,
-                  VmPipeFilterChain removeFilters,
-                  ProtocolHandler remoteHandler )
+                   VmPipeFilterChain localFilters,
+                   ProtocolHandler localHandler,
+                   SessionInitializer initializer,
+                   Entry remoteEntry ) throws IOException
     {
         this.lock = lock;
         this.localAddress = localAddress;
         this.localHandler = localHandler;
         this.localFilters = localFilters;
-        this.remoteAddress = remoteAddress;
-        this.remoteFilters = removeFilters;
+        this.remoteAddress = remoteEntry.address;
+        this.remoteFilters = remoteEntry.filters;
 
-        remoteSession = new VmPipeSession( this, remoteHandler );
+        remoteSession = new VmPipeSession( this, remoteEntry.handler );
+        if( remoteEntry.initializer != null )
+        {
+            boolean success = false;
+            try
+            {
+                remoteEntry.initializer.initializeSession( remoteSession );
+                success = true;
+            }
+            catch( Throwable t )
+            {
+                remoteEntry.acceptor.getExceptionMonitor().exceptionCaught( remoteEntry.acceptor, t );
+                IOException e = new IOException( "Failed to initialize remote session." );
+                e.initCause( t );
+                throw e;
+            }
+        }
 
-        removeFilters.sessionOpened( remoteSession );
+        remoteEntry.filters.sessionOpened( remoteSession );
         localFilters.sessionOpened( this );
     }
 

Added: directory/network/trunk/src/java/org/apache/mina/util/BaseSessionManager.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/util/BaseSessionManager.java?rev=164015&view=auto
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/util/BaseSessionManager.java (added)
+++ directory/network/trunk/src/java/org/apache/mina/util/BaseSessionManager.java Thu Apr 21 02:19:31 2005
@@ -0,0 +1,56 @@
+/*
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 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.util;
+
+import org.apache.mina.common.DefaultExceptionMonitor;
+import org.apache.mina.common.ExceptionMonitor;
+import org.apache.mina.common.SessionManager;
+
+/**
+ * Base implementation of {@link SessionManager}s.
+ * 
+ * @author Trustin Lee (trustin@apache.org)
+ * @version $Rev$, $Date$
+ */
+public class BaseSessionManager implements SessionManager {
+
+    /**
+     * Current exception monitor.
+     */
+    protected ExceptionMonitor exceptionMonitor = new DefaultExceptionMonitor();
+
+    protected BaseSessionManager()
+    {
+    }
+
+    public ExceptionMonitor getExceptionMonitor()
+    {
+        return exceptionMonitor;
+    }
+
+    public void setExceptionMonitor( ExceptionMonitor monitor )
+    {
+        if( monitor == null )
+        {
+            monitor = new DefaultExceptionMonitor();
+        }
+
+        this.exceptionMonitor = monitor;
+    }
+}

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

Added: directory/network/trunk/src/java/org/apache/mina/util/ExceptionUtil.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/util/ExceptionUtil.java?rev=164015&view=auto
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/util/ExceptionUtil.java (added)
+++ directory/network/trunk/src/java/org/apache/mina/util/ExceptionUtil.java Thu Apr 21 02:19:31 2005
@@ -0,0 +1,54 @@
+/*
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 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.util;
+
+import java.io.IOException;
+
+/**
+ * Exception utility.
+ * 
+ * @author Trustin Lee (trustin@apache.org)
+ * @version $Rev$, $Date$
+ */
+public class ExceptionUtil {
+
+    public static void throwException( Throwable t ) throws IOException
+    {
+        if( t instanceof IOException )
+        {
+            throw ( IOException ) t;
+        }
+        else if( t instanceof RuntimeException )
+        {
+            throw ( RuntimeException ) t;
+        }
+        else if( t instanceof Error )
+        {
+            throw ( Error ) t;
+        }
+        else
+        {
+            throw new RuntimeException( t );
+        }
+    }
+    
+    private ExceptionUtil()
+    {
+    }
+}

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



Mime
View raw message