directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From trus...@apache.org
Subject svn commit: r371285 - in /directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support: BaseIoSession.java IoSessionAttributeOverrider.java
Date Sun, 22 Jan 2006 13:17:19 GMT
Author: trustin
Date: Sun Jan 22 05:17:13 2006
New Revision: 371285

URL: http://svn.apache.org/viewcvs?rev=371285&view=rev
Log:
* Added a framework that users can implement transport-type specific properties

Added:
    directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/IoSessionAttributeOverrider.java
  (with props)
Modified:
    directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/BaseIoSession.java

Modified: directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/BaseIoSession.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/BaseIoSession.java?rev=371285&r1=371284&r2=371285&view=diff
==============================================================================
--- directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/BaseIoSession.java
(original)
+++ directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/BaseIoSession.java
Sun Jan 22 05:17:13 2006
@@ -26,6 +26,7 @@
 import org.apache.mina.common.CloseFuture;
 import org.apache.mina.common.IdleStatus;
 import org.apache.mina.common.IoSession;
+import org.apache.mina.common.RuntimeIOException;
 import org.apache.mina.common.TrafficMask;
 import org.apache.mina.common.WriteFuture;
 import org.apache.mina.common.IoFilter.WriteRequest;
@@ -38,6 +39,9 @@
  */
 public abstract class BaseIoSession implements IoSession
 {
+    private static final IoSessionAttributeOverrider[] OVERRIDER_DEFAULT =
+        new IoSessionAttributeOverrider[ 0 ];
+
     private final Map attributes = new HashMap();
     private final long creationTime;
 
@@ -69,7 +73,8 @@
     private long lastIdleTimeForBoth;
     private long lastIdleTimeForRead;
     private long lastIdleTimeForWrite;
-
+    
+    protected IoSessionAttributeOverrider[] attributeOverriders = OVERRIDER_DEFAULT;
 
     protected BaseIoSession()
     {
@@ -150,22 +155,36 @@
     
     public Object getAttachment()
     {
-        synchronized( attributes )
-        {
-            return attributes.get( "" );
-        }
+        return getAttribute( "" );
     }
 
     public Object setAttachment( Object attachment )
     {
-        synchronized( attributes )
-        {
-            return attributes.put( "", attachment );
-        }
+        return setAttribute( "", attachment );
     }
 
     public Object getAttribute( String key )
     {
+        for( int i = attributeOverriders.length - 1; i >= 0; i -- )
+        {
+            IoSessionAttributeOverrider overrider = attributeOverriders[ i ];
+            if( overrider.getKey().equals( key ) )
+            {
+                try
+                {
+                    return overrider.getValue();
+                }
+                catch( RuntimeException e )
+                {
+                    return e;
+                }
+                catch( Exception e )
+                {
+                    throw new RuntimeIOException( "Failed to get an attribute: " + key, e
);
+                }
+            }
+        }
+
         synchronized( attributes )
         {
             return attributes.get( key );
@@ -174,6 +193,26 @@
 
     public Object setAttribute( String key, Object value )
     {
+        for( int i = attributeOverriders.length - 1; i >= 0; i -- )
+        {
+            IoSessionAttributeOverrider overrider = attributeOverriders[ i ];
+            if( overrider.getKey().equals( key ) )
+            {
+                try
+                {
+                    return overrider.setValue( value );
+                }
+                catch( RuntimeException e )
+                {
+                    return e;
+                }
+                catch( Exception e )
+                {
+                    throw new RuntimeIOException( "Failed to set an attribute: " + key, e
);
+                }
+            }
+        }
+
         synchronized( attributes )
         {
             return attributes.put( key, value );
@@ -200,10 +239,19 @@
 
     public Set getAttributeKeys()
     {
+        Set keys = new HashSet( attributes.size() + attributeOverriders.length );
+
         synchronized( attributes )
         {
-            return new HashSet( attributes.keySet() );
+            keys.addAll( attributes.keySet() );
+        }
+        
+        for( int i = attributeOverriders.length - 1; i >= 0; i -- )
+        {
+            keys.add( attributeOverriders[ i ].getKey() );
         }
+
+        return keys;
     }
     
     public int getIdleTime( IdleStatus status )

Added: directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/IoSessionAttributeOverrider.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/IoSessionAttributeOverrider.java?rev=371285&view=auto
==============================================================================
--- directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/IoSessionAttributeOverrider.java
(added)
+++ directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/IoSessionAttributeOverrider.java
Sun Jan 22 05:17:13 2006
@@ -0,0 +1,46 @@
+/*
+ *   @(#) $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.support;
+
+/**
+ * Overrides the behavior of user-defined attribute getters and setters in 
+ * {@link BaseIoSession}.
+ *
+ * @author The Apache Directory Project (dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ */
+public interface IoSessionAttributeOverrider
+{
+    /**
+     * Returns the key of the attribute to override.
+     */
+    String getKey();
+    
+    /**
+     * Invoked when user reads the attribute.
+     */
+    Object getValue() throws Exception;
+    
+    /**
+     * Invoked when user sets the attribute.
+     * 
+     * @return the old attribute
+     */
+    Object setValue( Object value ) throws Exception;
+}

Propchange: directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/IoSessionAttributeOverrider.java
------------------------------------------------------------------------------
    svn:keywords = HeadURL Id LastChangedBy LastChangedDate LastChangedRevision



Mime
View raw message