You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by tr...@apache.org on 2006/04/18 08:00:06 UTC

svn commit: r394852 - in /directory/trunks/mina/core/src/main/java/org/apache/mina/transport/socket/nio/support: DatagramSessionConfigImpl.java DatagramSessionImpl.java SocketSessionConfigImpl.java SocketSessionImpl.java

Author: trustin
Date: Mon Apr 17 23:00:01 2006
New Revision: 394852

URL: http://svn.apache.org/viewcvs?rev=394852&view=rev
Log:
Fixed issue: DIRMINA-200 (IBM iSeries throws unexpected SocketException)
* Socket/DatagramSessionConfigImpl now pre-calculates default values and availability of socket options

Modified:
    directory/trunks/mina/core/src/main/java/org/apache/mina/transport/socket/nio/support/DatagramSessionConfigImpl.java
    directory/trunks/mina/core/src/main/java/org/apache/mina/transport/socket/nio/support/DatagramSessionImpl.java
    directory/trunks/mina/core/src/main/java/org/apache/mina/transport/socket/nio/support/SocketSessionConfigImpl.java
    directory/trunks/mina/core/src/main/java/org/apache/mina/transport/socket/nio/support/SocketSessionImpl.java

Modified: directory/trunks/mina/core/src/main/java/org/apache/mina/transport/socket/nio/support/DatagramSessionConfigImpl.java
URL: http://svn.apache.org/viewcvs/directory/trunks/mina/core/src/main/java/org/apache/mina/transport/socket/nio/support/DatagramSessionConfigImpl.java?rev=394852&r1=394851&r2=394852&view=diff
==============================================================================
--- directory/trunks/mina/core/src/main/java/org/apache/mina/transport/socket/nio/support/DatagramSessionConfigImpl.java (original)
+++ directory/trunks/mina/core/src/main/java/org/apache/mina/transport/socket/nio/support/DatagramSessionConfigImpl.java Mon Apr 17 23:00:01 2006
@@ -31,11 +31,99 @@
  */
 public class DatagramSessionConfigImpl extends BaseIoSessionConfig implements DatagramSessionConfig
 {
-    private boolean broadcast;
-    private boolean reuseAddress;
-    private int receiveBufferSize;
-    private int sendBufferSize;
-    private int trafficClass;
+	private static boolean SET_RECEIVE_BUFFER_SIZE_AVAILABLE = false;
+	private static boolean SET_SEND_BUFFER_SIZE_AVAILABLE = false;
+	private static boolean GET_TRAFFIC_CLASS_AVAILABLE = false;
+	private static boolean SET_TRAFFIC_CLASS_AVAILABLE = false;
+
+	private static boolean DEFAULT_BROADCAST;
+	private static boolean DEFAULT_REUSE_ADDRESS;
+    private static int DEFAULT_RECEIVE_BUFFER_SIZE;
+    private static int DEFAULT_SEND_BUFFER_SIZE;
+    private static int DEFAULT_TRAFFIC_CLASS;
+    
+    static
+    {
+    	initialize();
+    }
+    
+    private static void initialize()
+    {
+    	DatagramSocket socket = null;
+
+		try
+		{
+			socket = new DatagramSocket();
+			DEFAULT_BROADCAST = socket.getBroadcast();
+			DEFAULT_REUSE_ADDRESS = socket.getReuseAddress();
+			DEFAULT_RECEIVE_BUFFER_SIZE = socket.getReceiveBufferSize();
+			DEFAULT_SEND_BUFFER_SIZE = socket.getSendBufferSize();
+			
+			// Check if setReceiveBufferSize is supported.
+			try
+			{
+				socket.setReceiveBufferSize(DEFAULT_RECEIVE_BUFFER_SIZE);
+				SET_RECEIVE_BUFFER_SIZE_AVAILABLE = true;
+			}
+			catch( SocketException e )
+			{
+				SET_RECEIVE_BUFFER_SIZE_AVAILABLE = false;
+			}
+			
+			// Check if setSendBufferSize is supported.
+			try
+			{
+				socket.setSendBufferSize(DEFAULT_SEND_BUFFER_SIZE);
+				SET_SEND_BUFFER_SIZE_AVAILABLE = true;
+			}
+			catch( SocketException e )
+			{
+				SET_SEND_BUFFER_SIZE_AVAILABLE = false;
+			}
+			
+			// Check if getTrafficClass is supported.
+			try
+			{
+				DEFAULT_TRAFFIC_CLASS = socket.getTrafficClass();
+				GET_TRAFFIC_CLASS_AVAILABLE = true;
+			}
+			catch( SocketException e )
+			{
+				GET_TRAFFIC_CLASS_AVAILABLE = false;
+				DEFAULT_TRAFFIC_CLASS = 0;
+			}
+		} catch (SocketException e) {
+			throw new ExceptionInInitializerError(e);
+		}
+		finally {
+			if( socket != null )
+			{
+				socket.close();
+			}
+		}
+    }
+    
+    public static boolean isSetReceiveBufferSizeAvailable() {
+    	return SET_RECEIVE_BUFFER_SIZE_AVAILABLE;
+    }
+	
+    public static boolean isSetSendBufferSizeAvailable() {
+    	return SET_SEND_BUFFER_SIZE_AVAILABLE;
+    }
+	
+    public static boolean isGetTrafficClassAvailable() {
+    	return GET_TRAFFIC_CLASS_AVAILABLE;
+    }
+	
+    public static boolean isSetTrafficClassAvailable() {
+    	return SET_TRAFFIC_CLASS_AVAILABLE;
+    }
+
+    private boolean broadcast = DEFAULT_BROADCAST;
+    private boolean reuseAddress = DEFAULT_REUSE_ADDRESS;
+    private int receiveBufferSize = DEFAULT_RECEIVE_BUFFER_SIZE;
+    private int sendBufferSize = DEFAULT_SEND_BUFFER_SIZE;
+    private int trafficClass = DEFAULT_TRAFFIC_CLASS;
 
     /**
      * Creates a new instance.

Modified: directory/trunks/mina/core/src/main/java/org/apache/mina/transport/socket/nio/support/DatagramSessionImpl.java
URL: http://svn.apache.org/viewcvs/directory/trunks/mina/core/src/main/java/org/apache/mina/transport/socket/nio/support/DatagramSessionImpl.java?rev=394852&r1=394851&r2=394852&view=diff
==============================================================================
--- directory/trunks/mina/core/src/main/java/org/apache/mina/transport/socket/nio/support/DatagramSessionImpl.java (original)
+++ directory/trunks/mina/core/src/main/java/org/apache/mina/transport/socket/nio/support/DatagramSessionImpl.java Mon Apr 17 23:00:01 2006
@@ -45,7 +45,7 @@
 class DatagramSessionImpl extends BaseIoSession
 {
     private final IoService wrapperManager;
-    private final DatagramSessionConfig config = new DatagramSessionConfigImpl();
+    private final DatagramSessionConfig config = new SessionConfigImpl();
     private final DatagramService managerDelegate;
     private final DatagramFilterChain filterChain;
     private final DatagramChannel ch;
@@ -199,7 +199,7 @@
         return readBufferSize;
     }
     
-    private class DatagramSessionConfigImpl extends BaseIoSessionConfig implements DatagramSessionConfig
+    private class SessionConfigImpl extends BaseIoSessionConfig implements DatagramSessionConfig
     {
         public int getReceiveBufferSize()
         {
@@ -215,15 +215,18 @@
 
         public void setReceiveBufferSize( int receiveBufferSize )
         {
-            try
-            {
-                ch.socket().setReceiveBufferSize( receiveBufferSize );
-                DatagramSessionImpl.this.readBufferSize = receiveBufferSize;
-            }
-            catch( SocketException e )
-            {
-                throw new RuntimeIOException( e );
-            }
+        	if( DatagramSessionConfigImpl.isSetReceiveBufferSizeAvailable() )
+        	{
+	            try
+	            {
+	                ch.socket().setReceiveBufferSize( receiveBufferSize );
+	                DatagramSessionImpl.this.readBufferSize = receiveBufferSize;
+	            }
+	            catch( SocketException e )
+	            {
+	                throw new RuntimeIOException( e );
+	            }
+        	}
         }
 
         public boolean isBroadcast()
@@ -264,14 +267,17 @@
 
         public void setSendBufferSize( int sendBufferSize )
         {
-            try
-            {
-                ch.socket().setSendBufferSize( sendBufferSize );
-            }
-            catch( SocketException e )
-            {
-                throw new RuntimeIOException( e );
-            }
+        	if( DatagramSessionConfigImpl.isSetSendBufferSizeAvailable() )
+        	{
+	            try
+	            {
+	                ch.socket().setSendBufferSize( sendBufferSize );
+	            }
+	            catch( SocketException e )
+	            {
+	                throw new RuntimeIOException( e );
+	            }
+        	}
         }
 
         public boolean isReuseAddress()
@@ -300,26 +306,36 @@
 
         public int getTrafficClass()
         {
-            try
-            {
-                return ch.socket().getTrafficClass();
-            }
-            catch( SocketException e )
-            {
-                throw new RuntimeIOException( e );
-            }
+        	if( DatagramSessionConfigImpl.isGetTrafficClassAvailable() )
+        	{
+	            try
+	            {
+	                return ch.socket().getTrafficClass();
+	            }
+	            catch( SocketException e )
+	            {
+	                throw new RuntimeIOException( e );
+	            }
+        	}
+        	else
+        	{
+        		return 0;
+        	}
         }
 
         public void setTrafficClass( int trafficClass )
         {
-            try
-            {
-                ch.socket().setTrafficClass( trafficClass );
-            }
-            catch( SocketException e )
-            {
-                throw new RuntimeIOException( e );
-            }
+        	if( DatagramSessionConfigImpl.isSetTrafficClassAvailable() )
+        	{
+	            try
+	            {
+	                ch.socket().setTrafficClass( trafficClass );
+	            }
+	            catch( SocketException e )
+	            {
+	                throw new RuntimeIOException( e );
+	            }
+        	}
         }
     }
 }

Modified: directory/trunks/mina/core/src/main/java/org/apache/mina/transport/socket/nio/support/SocketSessionConfigImpl.java
URL: http://svn.apache.org/viewcvs/directory/trunks/mina/core/src/main/java/org/apache/mina/transport/socket/nio/support/SocketSessionConfigImpl.java?rev=394852&r1=394851&r2=394852&view=diff
==============================================================================
--- directory/trunks/mina/core/src/main/java/org/apache/mina/transport/socket/nio/support/SocketSessionConfigImpl.java (original)
+++ directory/trunks/mina/core/src/main/java/org/apache/mina/transport/socket/nio/support/SocketSessionConfigImpl.java Mon Apr 17 23:00:01 2006
@@ -24,7 +24,6 @@
 
 import org.apache.mina.common.ExceptionMonitor;
 import org.apache.mina.common.IoConnectorConfig;
-import org.apache.mina.common.RuntimeIOException;
 import org.apache.mina.common.support.BaseIoSessionConfig;
 import org.apache.mina.transport.socket.nio.SocketConnector;
 import org.apache.mina.transport.socket.nio.SocketSessionConfig;
@@ -37,53 +36,122 @@
  */
 public class SocketSessionConfigImpl extends BaseIoSessionConfig implements SocketSessionConfig
 {
-    private boolean reuseAddress;
-    private int receiveBufferSize;
-    private int sendBufferSize;
-    private int trafficClass;
-    private boolean keepAlive;
-    private boolean oobInline;
-    private int soLinger;
-    private boolean tcpNoDelay;
+	private static boolean SET_RECEIVE_BUFFER_SIZE_AVAILABLE = false;
+	private static boolean SET_SEND_BUFFER_SIZE_AVAILABLE = false;
+	private static boolean GET_TRAFFIC_CLASS_AVAILABLE = false;
+	private static boolean SET_TRAFFIC_CLASS_AVAILABLE = false;
+
+	private static boolean DEFAULT_REUSE_ADDRESS;
+    private static int DEFAULT_RECEIVE_BUFFER_SIZE;
+    private static int DEFAULT_SEND_BUFFER_SIZE;
+    private static int DEFAULT_TRAFFIC_CLASS;
+    private static boolean DEFAULT_KEEP_ALIVE;
+    private static boolean DEFAULT_OOB_INLINE;
+    private static int DEFAULT_SO_LINGER;
+    private static boolean DEFAULT_TCP_NO_DELAY;
+    
+    static
+    {
+    	initialize();
+    }
+    
+    private static void initialize()
+    {
+    	Socket socket = null;
+    	
+		socket = new Socket();
+
+		try
+		{
+			DEFAULT_REUSE_ADDRESS = socket.getReuseAddress();
+			DEFAULT_RECEIVE_BUFFER_SIZE = socket.getReceiveBufferSize();
+			DEFAULT_SEND_BUFFER_SIZE = socket.getSendBufferSize();
+			DEFAULT_KEEP_ALIVE = socket.getKeepAlive();
+			DEFAULT_OOB_INLINE = socket.getOOBInline();
+			DEFAULT_SO_LINGER = socket.getSoLinger();
+			DEFAULT_TCP_NO_DELAY = socket.getTcpNoDelay();
+			
+			// Check if setReceiveBufferSize is supported.
+			try
+			{
+				socket.setReceiveBufferSize(DEFAULT_RECEIVE_BUFFER_SIZE);
+				SET_RECEIVE_BUFFER_SIZE_AVAILABLE = true;
+			}
+			catch( SocketException e )
+			{
+				SET_RECEIVE_BUFFER_SIZE_AVAILABLE = false;
+			}
+			
+			// Check if setSendBufferSize is supported.
+			try
+			{
+				socket.setSendBufferSize(DEFAULT_SEND_BUFFER_SIZE);
+				SET_SEND_BUFFER_SIZE_AVAILABLE = true;
+			}
+			catch( SocketException e )
+			{
+				SET_SEND_BUFFER_SIZE_AVAILABLE = false;
+			}
+			
+			// Check if getTrafficClass is supported.
+			try
+			{
+				DEFAULT_TRAFFIC_CLASS = socket.getTrafficClass();
+				GET_TRAFFIC_CLASS_AVAILABLE = true;
+			}
+			catch( SocketException e )
+			{
+				GET_TRAFFIC_CLASS_AVAILABLE = false;
+				DEFAULT_TRAFFIC_CLASS = 0;
+			}
+		} catch (SocketException e) {
+			throw new ExceptionInInitializerError(e);
+		}
+		finally {
+			if( socket != null )
+			{
+				try
+				{
+					socket.close();
+				}
+				catch( IOException e )
+				{
+					ExceptionMonitor.getInstance().exceptionCaught(e);
+				}
+			}
+		}
+    }
+    
+    public static boolean isSetReceiveBufferSizeAvailable() {
+    	return SET_RECEIVE_BUFFER_SIZE_AVAILABLE;
+    }
+	
+    public static boolean isSetSendBufferSizeAvailable() {
+    	return SET_SEND_BUFFER_SIZE_AVAILABLE;
+    }
+	
+    public static boolean isGetTrafficClassAvailable() {
+    	return GET_TRAFFIC_CLASS_AVAILABLE;
+    }
+	
+    public static boolean isSetTrafficClassAvailable() {
+    	return SET_TRAFFIC_CLASS_AVAILABLE;
+    }
+	
+    private boolean reuseAddress = DEFAULT_REUSE_ADDRESS;
+    private int receiveBufferSize = DEFAULT_RECEIVE_BUFFER_SIZE;
+    private int sendBufferSize = DEFAULT_SEND_BUFFER_SIZE;
+    private int trafficClass = DEFAULT_TRAFFIC_CLASS;
+    private boolean keepAlive = DEFAULT_KEEP_ALIVE;
+    private boolean oobInline = DEFAULT_OOB_INLINE;
+    private int soLinger = DEFAULT_SO_LINGER;
+    private boolean tcpNoDelay = DEFAULT_TCP_NO_DELAY;
 
     /**
      * Creates a new instance.
-     * 
-     * @throws RuntimeIOException if failed to get the default configuration
      */
     public SocketSessionConfigImpl()
     {
-        Socket s = null;
-        try
-        {
-            s = new Socket();
-            reuseAddress = s.getReuseAddress();
-            receiveBufferSize = s.getReceiveBufferSize();
-            sendBufferSize = s.getSendBufferSize();
-            trafficClass = s.getTrafficClass();
-            keepAlive = s.getKeepAlive();
-            oobInline = s.getOOBInline();
-            soLinger = s.getSoLinger();
-            tcpNoDelay = s.getTcpNoDelay();
-        }
-        catch( SocketException e )
-        {
-            throw new RuntimeIOException( "Failed to get the default configuration.", e );
-        }
-        finally
-        {
-            if( s != null )
-            {
-                try
-                {
-                    s.close();
-                }
-                catch( IOException e )
-                {
-                    ExceptionMonitor.getInstance().exceptionCaught( e );
-                }
-            }
-        }
     }
 
     public boolean isReuseAddress()

Modified: directory/trunks/mina/core/src/main/java/org/apache/mina/transport/socket/nio/support/SocketSessionImpl.java
URL: http://svn.apache.org/viewcvs/directory/trunks/mina/core/src/main/java/org/apache/mina/transport/socket/nio/support/SocketSessionImpl.java?rev=394852&r1=394851&r2=394852&view=diff
==============================================================================
--- directory/trunks/mina/core/src/main/java/org/apache/mina/transport/socket/nio/support/SocketSessionImpl.java (original)
+++ directory/trunks/mina/core/src/main/java/org/apache/mina/transport/socket/nio/support/SocketSessionImpl.java Mon Apr 17 23:00:01 2006
@@ -46,7 +46,7 @@
 class SocketSessionImpl extends BaseIoSession
 {
     private final IoService manager;
-    private final SocketSessionConfig config = new SocketSessionConfigImpl();
+    private final SocketSessionConfig config = new SessionConfigImpl();
     private final SocketIoProcessor ioProcessor;
     private final SocketFilterChain filterChain;
     private final SocketChannel ch;
@@ -205,7 +205,7 @@
         return readBufferSize;
     }
 
-    private class SocketSessionConfigImpl extends BaseIoSessionConfig implements SocketSessionConfig
+    private class SessionConfigImpl extends BaseIoSessionConfig implements SocketSessionConfig
     {
         public boolean isKeepAlive()
         {
@@ -336,26 +336,36 @@
     
         public int getTrafficClass()
         {
-            try
-            {
-                return ch.socket().getTrafficClass();
-            }
-            catch( SocketException e )
-            {
-                throw new RuntimeIOException( e );
-            }
+        	if( SocketSessionConfigImpl.isGetTrafficClassAvailable() )
+        	{
+	            try
+	            {
+	                return ch.socket().getTrafficClass();
+	            }
+	            catch( SocketException e )
+	            {
+	                throw new RuntimeIOException( e );
+	            }
+        	}
+        	else
+        	{
+        		return 0;
+        	}
         }
     
         public void setTrafficClass( int tc )
         {
-            try
-            {
-                ch.socket().setTrafficClass( tc );
-            }
-            catch( SocketException e )
-            {
-                throw new RuntimeIOException( e );
-            }
+        	if( SocketSessionConfigImpl.isSetTrafficClassAvailable() )
+        	{
+	            try
+	            {
+	                ch.socket().setTrafficClass( tc );
+	            }
+	            catch( SocketException e )
+	            {
+	                throw new RuntimeIOException( e );
+	            }
+        	}
         }
     
         public int getSendBufferSize()
@@ -372,14 +382,17 @@
     
         public void setSendBufferSize( int size )
         {
-            try
-            {
-                ch.socket().setSendBufferSize( size );
-            }
-            catch( SocketException e )
-            {
-                throw new RuntimeIOException( e );
-            }
+        	if( SocketSessionConfigImpl.isSetSendBufferSizeAvailable() )
+        	{
+	            try
+	            {
+	                ch.socket().setSendBufferSize( size );
+	            }
+	            catch( SocketException e )
+	            {
+	                throw new RuntimeIOException( e );
+	            }
+        	}
         }
     
         public int getReceiveBufferSize()
@@ -396,15 +409,18 @@
     
         public void setReceiveBufferSize( int size )
         {
-            try
-            {
-                ch.socket().setReceiveBufferSize( size );
-                SocketSessionImpl.this.readBufferSize = size;
-            }
-            catch( SocketException e )
-            {
-                throw new RuntimeIOException( e );
-            }
+        	if( SocketSessionConfigImpl.isSetReceiveBufferSizeAvailable() )
+        	{
+	            try
+	            {
+	                ch.socket().setReceiveBufferSize( size );
+	                SocketSessionImpl.this.readBufferSize = size;
+	            }
+	            catch( SocketException e )
+	            {
+	                throw new RuntimeIOException( e );
+	            }
+        	}
         }
     }
 }