directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From trus...@apache.org
Subject svn commit: r164156 - in /directory/network/trunk/src/java/org/apache/mina: common/ io/datagram/ io/socket/ protocol/io/ protocol/vmpipe/ util/
Date Fri, 22 Apr 2005 01:11:40 GMT
Author: trustin
Date: Thu Apr 21 18:11:38 2005
New Revision: 164156

URL: http://svn.apache.org/viewcvs?rev=164156&view=rev
Log:
* Added SessionManager.defaultSessionInitializer property,
* and added DefaultSessionInitializer

Added:
    directory/network/trunk/src/java/org/apache/mina/common/DefaultSessionInitializer.java
  (with props)
Modified:
    directory/network/trunk/src/java/org/apache/mina/common/DefaultExceptionMonitor.java
    directory/network/trunk/src/java/org/apache/mina/common/SessionManager.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/socket/SocketAcceptor.java
    directory/network/trunk/src/java/org/apache/mina/io/socket/SocketConnector.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
    directory/network/trunk/src/java/org/apache/mina/util/BaseSessionManager.java

Modified: 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=164156&r1=164155&r2=164156&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/common/DefaultExceptionMonitor.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/common/DefaultExceptionMonitor.java Thu
Apr 21 18:11:38 2005
@@ -23,10 +23,13 @@
 import java.util.Date;
 
 /**
- * A default {@link ExceptionMonitor} implementation.  It logs uncaught
+ * A default {@link ExceptionMonitor} implementation that 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}.
+ * <p>
+ * All {@link SessionManager}s have this implementation as a default exception
+ * monitor.
  * 
  * @author Trustin Lee (trustin@apache.org)
  * @version $Rev$, $Date$

Added: directory/network/trunk/src/java/org/apache/mina/common/DefaultSessionInitializer.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/common/DefaultSessionInitializer.java?rev=164156&view=auto
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/common/DefaultSessionInitializer.java
(added)
+++ directory/network/trunk/src/java/org/apache/mina/common/DefaultSessionInitializer.java
Thu Apr 21 18:11:38 2005
@@ -0,0 +1,116 @@
+/*
+ *   @(#) $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 java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+import java.util.Map.Entry;
+
+import org.apache.mina.io.datagram.DatagramSessionConfig;
+import org.apache.mina.io.socket.SocketSessionConfig;
+
+/**
+ * A default {@link SessionInitializer} implementation that initializes
+ * default socket parameters and user-defined attributes.
+ * <ul>
+ *   <li><tt>ReuseAddress</tt> is set to <tt>true</tt></li>
+ *   <li><tt>KeepAlive</tt> is set to <tt>true</tt></li>
+ *   <li>All user-defined attributes are copied to new session's attribute map.</li>
+ * </ul>
+ * <p>
+ * All {@link SessionManager}s have this implementation as a default session
+ * initializer.
+ * 
+ * @author The Apache Directory Project (dev@directory.apache.org)
+ * @author Trustin Lee (trustin@apache.org)
+ * @version $Rev$, $Date$
+ */
+public class DefaultSessionInitializer implements SessionInitializer {
+    
+    private final Map attributes = new HashMap();
+    
+    /**
+     * Creates a new instance.
+     */
+    public DefaultSessionInitializer()
+    {
+    }
+    
+    /**
+     * Returns default attribute value with the specified key.
+     */
+    public Object getAttribute( String key )
+    {
+        return attributes.get( key );
+    }
+    
+    /**
+     * Sets default attribute value with the specified key and value.
+     * 
+     * @return The old value
+     */
+    public Object setAttribute( String key, Object value )
+    {
+        return attributes.put( key, value );
+    }
+    
+    /**
+     * Removed default attribute value with the specified key.
+     * 
+     * @return The old value
+     */
+    public Object removeAttribute( String key )
+    {
+        return attributes.remove( key );
+    }
+    
+    /**
+     * Returns the set of keys of all default attributes.
+     */
+    public Set getAttributeKeys()
+    {
+        return attributes.keySet();
+    }
+
+    public void initializeSession( Session session ) throws IOException {
+        
+        SessionConfig config = session.getConfig();
+        if( config instanceof SocketSessionConfig )
+        {
+            SocketSessionConfig ssc = ( SocketSessionConfig ) config;
+            ssc.setReuseAddress( true );
+            ssc.setKeepAlive( true );
+        }
+        else if( config instanceof DatagramSessionConfig )
+        {
+            DatagramSessionConfig dsc = ( DatagramSessionConfig ) config;
+            dsc.setReuseAddress( true );
+        }
+        
+        Iterator it = attributes.entrySet().iterator();
+        while( it.hasNext() )
+        {
+            Entry e = ( Entry ) it.next();
+            session.setAttribute( ( String ) e.getKey(), e.getValue() );
+        }
+    }
+}

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

Modified: 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=164156&r1=164155&r2=164156&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/common/SessionManager.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/common/SessionManager.java Thu Apr 21
18:11:38 2005
@@ -22,6 +22,8 @@
  * Base interface for all acceptors and connectors that manage
  * sessions.
  * <p>
+ * The default {@link SessionInitializer} is {@link DefaultSessionInitializer}.
+ * <p>
  * You can monitor any uncaught exceptions by setting {@link ExceptionMonitor}
  * by calling {@link #setExceptionMonitor(ExceptionMonitor)}.  The default
  * monitor is {@link DefaultExceptionMonitor}.
@@ -32,6 +34,23 @@
 public interface SessionManager {
     
     /**
+     * Returns default {@link SessionInitializer} when no initializer
+     * is specified as a parameter when user call <tt>bind(...)</tt>
+     * and <tt>connect(...)</tt> methods.
+     */
+    SessionInitializer getDefaultSessionInitializer();
+    
+    /**
+     * Sets default {@link SessionInitializer} when no initializer
+     * is specified as a parameter when user call <tt>bind(...)</tt>
+     * and <tt>connect(...)</tt> methods.
+     * 
+     * @param initializer A new instance of {@link DefaultSessionInitializer}
+     *                    is set if <tt>null</tt> is specified.
+     */
+    void setDefaultSessionInitializer( SessionInitializer initializer );
+    
+    /**
      * Returns the current exception monitor.
      */
     ExceptionMonitor getExceptionMonitor();
@@ -39,6 +58,9 @@
     /**
      * Sets the uncaught exception monitor.  If <code>null</code> is specified,
      * a new instance of {@link DefaultExceptionMonitor} will be set.
+     * 
+     * @param monitor A new instance of {@link DefaultExceptionMonitor} is set
+     *                if <tt>null</tt> is specified.
      */
     void setExceptionMonitor( ExceptionMonitor monitor );
 }

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=164156&r1=164155&r2=164156&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 18:11:38 2005
@@ -92,6 +92,11 @@
                                                 + address.getClass() );
         if( ( ( InetSocketAddress ) address ).getPort() == 0 )
             throw new IllegalArgumentException( "Unsupported port number: 0" );
+        
+        if( initializer == null )
+        {
+            initializer = defaultInitializer;
+        }
 
         RegistrationRequest request = new RegistrationRequest( address, handler, initializer
);
         synchronized( this )
@@ -262,10 +267,7 @@
             
             try
             {
-                if( req.initializer != null )
-                {
-                    req.initializer.initializeSession( session );
-                }
+                req.initializer.initializeSession( session );
 
                 if( key.isReadable() )
                 {

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=164156&r1=164155&r2=164156&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 18:11:38 2005
@@ -122,6 +122,11 @@
             throw new IllegalArgumentException( "Unexpected local address type: "
                                                 + localAddress.getClass() );
         }
+        
+        if( initializer == null )
+        {
+            initializer = defaultInitializer;
+        }
 
         DatagramChannel ch = DatagramChannel.open();
         boolean initialized = false;
@@ -436,16 +441,12 @@
 
             try
             {
+                req.initializer.initializeSession( session );
+
                 SelectionKey key = req.channel.register( selector,
                         SelectionKey.OP_READ, session );
     
                 session.setSelectionKey( key );
-
-                if( req.initializer != null )
-                {
-                    req.initializer.initializeSession( session );
-                }
-
             }
             catch( Throwable t )
             {

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=164156&r1=164155&r2=164156&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 18:11:38 2005
@@ -115,6 +115,11 @@
         {
             throw new IllegalArgumentException( "Unsupported port number: 0" );
         }
+        
+        if( initializer == null )
+        {
+            initializer = defaultInitializer;
+        }
 
         RegistrationRequest request = new RegistrationRequest( address, backlog, handler,
initializer );
 
@@ -305,12 +310,7 @@
                 {
                     RegistrationRequest req = ( RegistrationRequest ) key.attachment();
                     SocketSession session = new SocketSession( filters, ch, req.handler );
-                    
-                    if( req.initializer != null )
-                    {
-                        req.initializer.initializeSession( session );
-                    }
-                    
+                    req.initializer.initializeSession( session );
                     SocketIoProcessor.getInstance().addSession( session );
                     success = true;
                 }

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=164156&r1=164155&r2=164156&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 18:11:38 2005
@@ -121,6 +121,11 @@
             throw new IllegalArgumentException( "Unexpected local address type: "
                                                 + localAddress.getClass() );
 
+        if( initializer == null )
+        {
+            initializer = defaultInitializer;
+        }
+
         SocketChannel ch = SocketChannel.open();
         boolean success = false;
         try
@@ -307,10 +312,7 @@
     private SocketSession newSession( SocketChannel ch, IoHandler handler, SessionInitializer
initializer ) throws IOException
     {
         SocketSession session = new SocketSession( filters, ch, handler );
-        if( initializer != null )
-        {
-            initializer.initializeSession( session );
-        }
+        initializer.initializeSession( session );
         SocketIoProcessor.getInstance().addSession( session );
         return session;
     }

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=164156&r1=164155&r2=164156&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 18:11:38 2005
@@ -72,6 +72,16 @@
     {
         return adapter.getFilterChain();
     }
+    
+    public SessionInitializer getDefaultSessionInitializer()
+    {
+        return acceptor.getDefaultSessionInitializer();
+    }
+    
+    public void setDefaultSessionInitializer( SessionInitializer defaultInitializer )
+    {
+        acceptor.setDefaultSessionInitializer( defaultInitializer );
+    }
 
     public ExceptionMonitor getExceptionMonitor()
     {

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=164156&r1=164155&r2=164156&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 18:11:38 2005
@@ -120,6 +120,16 @@
         return adapter.getFilterChain();
     }
 
+    public SessionInitializer getDefaultSessionInitializer()
+    {
+        return connector.getDefaultSessionInitializer();
+    }
+    
+    public void setDefaultSessionInitializer( SessionInitializer defaultInitializer )
+    {
+        connector.setDefaultSessionInitializer( defaultInitializer );
+    }
+
     public ExceptionMonitor getExceptionMonitor()
     {
         return connector.getExceptionMonitor();

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=164156&r1=164155&r2=164156&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 18:11:38 2005
@@ -51,6 +51,10 @@
         if( !( address instanceof VmPipeAddress ) )
             throw new IllegalArgumentException(
                     "address must be VmPipeAddress." );
+        if( initializer == null )
+        {
+            initializer = defaultInitializer;
+        }
 
         synchronized( boundHandlers )
         {

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=164156&r1=164155&r2=164156&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 18:11:38 2005
@@ -85,6 +85,11 @@
             throw new IllegalArgumentException(
                                                 "address must be VmPipeAddress." );
 
+        if( initializer == null )
+        {
+            initializer = defaultInitializer;
+        }
+
         Entry entry = ( Entry ) VmPipeAcceptor.boundHandlers.get( address );
         if( entry == null )
             throw new IOException( "Endpoint unavailable: " + address );

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=164156&r1=164155&r2=164156&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 18:11:38 2005
@@ -59,22 +59,22 @@
         this.remoteFilters = remoteEntry.filters;
 
         remoteSession = new VmPipeSession( this, remoteEntry.handler );
-        if( remoteEntry.initializer != null )
+        
+        // initialize remote session
+        try
         {
-            try
-            {
-                remoteEntry.initializer.initializeSession( remoteSession );
-            }
-            catch( Throwable t )
-            {
-                remoteEntry.acceptor.getExceptionMonitor().exceptionCaught( remoteEntry.acceptor,
t );
-                IOException e = new IOException( "Failed to initialize remote session." );
-                e.initCause( t );
-                throw e;
-            }
-            
-            initializer.initializeSession( this );
+            remoteEntry.initializer.initializeSession( remoteSession );
         }
+        catch( Throwable t )
+        {
+            remoteEntry.acceptor.getExceptionMonitor().exceptionCaught( remoteEntry.acceptor,
t );
+            IOException e = new IOException( "Failed to initialize remote session." );
+            e.initCause( t );
+            throw e;
+        }
+        
+        // initialize client session
+        initializer.initializeSession( this );
 
         remoteEntry.filters.sessionOpened( remoteSession );
         localFilters.sessionOpened( this );

Modified: 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=164156&r1=164155&r2=164156&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/util/BaseSessionManager.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/util/BaseSessionManager.java Thu Apr
21 18:11:38 2005
@@ -19,7 +19,9 @@
 package org.apache.mina.util;
 
 import org.apache.mina.common.DefaultExceptionMonitor;
+import org.apache.mina.common.DefaultSessionInitializer;
 import org.apache.mina.common.ExceptionMonitor;
+import org.apache.mina.common.SessionInitializer;
 import org.apache.mina.common.SessionManager;
 
 /**
@@ -31,12 +33,32 @@
 public class BaseSessionManager implements SessionManager {
 
     /**
+     * Default session initializer.
+     */
+    protected SessionInitializer defaultInitializer = new DefaultSessionInitializer();
+
+    /**
      * Current exception monitor.
      */
     protected ExceptionMonitor exceptionMonitor = new DefaultExceptionMonitor();
-
+    
     protected BaseSessionManager()
     {
+    }
+
+    public SessionInitializer getDefaultSessionInitializer()
+    {
+        return defaultInitializer;
+    }
+
+    public void setDefaultSessionInitializer( SessionInitializer defaultInitializer )
+    {
+        if( defaultInitializer == null )
+        {
+            defaultInitializer = new DefaultSessionInitializer();
+        }
+        
+        this.defaultInitializer = defaultInitializer;
     }
 
     public ExceptionMonitor getExceptionMonitor()



Mime
View raw message