MultiThreadSocketSessionConfigImpl.java
001 /*
002  *  Licensed to the Apache Software Foundation (ASF) under one
003  *  or more contributor license agreements.  See the NOTICE file
004  *  distributed with this work for additional information
005  *  regarding copyright ownership.  The ASF licenses this file
006  *  to you under the Apache License, Version 2.0 (the
007  *  "License"); you may not use this file except in compliance
008  *  with the License.  You may obtain a copy of the License at
009  *
010  *    http://www.apache.org/licenses/LICENSE-2.0
011  *
012  *  Unless required by applicable law or agreed to in writing,
013  *  software distributed under the License is distributed on an
014  *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015  *  KIND, either express or implied.  See the License for the
016  *  specific language governing permissions and limitations
017  *  under the License.
018  *
019  */
020 package org.apache.mina.transport.socket.nio;
021 
022 import org.apache.mina.common.ExceptionMonitor;
023 import org.apache.mina.common.IoConnectorConfig;
024 import org.apache.mina.common.support.BaseIoSessionConfig;
025 
026 import java.io.IOException;
027 import java.net.Socket;
028 import java.net.SocketException;
029 
030 /**
031  * An {@link IoConnectorConfig} for {@link SocketConnector}.
032  *
033  @author The Apache Directory Project (mina-dev@directory.apache.org)
034  @version $Rev: 619823 $, $Date: 2008-02-08 10:09:37 +0000 (Fri, 08 Feb 2008) $
035  */
036 public class MultiThreadSocketSessionConfigImpl extends org.apache.mina.transport.socket.nio.SocketSessionConfigImpl
037 {
038     private static boolean SET_RECEIVE_BUFFER_SIZE_AVAILABLE = false;
039     private static boolean SET_SEND_BUFFER_SIZE_AVAILABLE = false;
040     private static boolean GET_TRAFFIC_CLASS_AVAILABLE = false;
041     private static boolean SET_TRAFFIC_CLASS_AVAILABLE = false;
042 
043     private static boolean DEFAULT_REUSE_ADDRESS;
044     private static int DEFAULT_RECEIVE_BUFFER_SIZE;
045     private static int DEFAULT_SEND_BUFFER_SIZE;
046     private static int DEFAULT_TRAFFIC_CLASS;
047     private static boolean DEFAULT_KEEP_ALIVE;
048     private static boolean DEFAULT_OOB_INLINE;
049     private static int DEFAULT_SO_LINGER;
050     private static boolean DEFAULT_TCP_NO_DELAY;
051 
052     static
053     {
054         initialize();
055     }
056 
057     private static void initialize()
058     {
059         Socket socket = null;
060 
061         socket = new Socket();
062 
063         try
064         {
065             DEFAULT_REUSE_ADDRESS = socket.getReuseAddress();
066             DEFAULT_RECEIVE_BUFFER_SIZE = socket.getReceiveBufferSize();
067             DEFAULT_SEND_BUFFER_SIZE = socket.getSendBufferSize();
068             DEFAULT_KEEP_ALIVE = socket.getKeepAlive();
069             DEFAULT_OOB_INLINE = socket.getOOBInline();
070             DEFAULT_SO_LINGER = socket.getSoLinger();
071             DEFAULT_TCP_NO_DELAY = socket.getTcpNoDelay();
072 
073             // Check if setReceiveBufferSize is supported.
074             try
075             {
076                 socket.setReceiveBufferSize(DEFAULT_RECEIVE_BUFFER_SIZE);
077                 SET_RECEIVE_BUFFER_SIZE_AVAILABLE = true;
078             }
079             catchSocketException e )
080             {
081                 SET_RECEIVE_BUFFER_SIZE_AVAILABLE = false;
082             }
083 
084             // Check if setSendBufferSize is supported.
085             try
086             {
087                 socket.setSendBufferSize(DEFAULT_SEND_BUFFER_SIZE);
088                 SET_SEND_BUFFER_SIZE_AVAILABLE = true;
089             }
090             catchSocketException e )
091             {
092                 SET_SEND_BUFFER_SIZE_AVAILABLE = false;
093             }
094 
095             // Check if getTrafficClass is supported.
096             try
097             {
098                 DEFAULT_TRAFFIC_CLASS = socket.getTrafficClass();
099                 GET_TRAFFIC_CLASS_AVAILABLE = true;
100             }
101             catchSocketException e )
102             {
103                 GET_TRAFFIC_CLASS_AVAILABLE = false;
104                 DEFAULT_TRAFFIC_CLASS = 0;
105             }
106         }
107         catchSocketException e )
108         {
109             throw new ExceptionInInitializerError(e);
110         }
111         finally
112         {
113             ifsocket != null )
114             {
115                 try
116                 {
117                     socket.close();
118                 }
119                 catchIOException e )
120                 {
121                     ExceptionMonitor.getInstance().exceptionCaught(e);
122                 }
123             }
124         }
125     }
126 
127     public static boolean isSetReceiveBufferSizeAvailable() {
128         return SET_RECEIVE_BUFFER_SIZE_AVAILABLE;
129     }
130 
131     public static boolean isSetSendBufferSizeAvailable() {
132         return SET_SEND_BUFFER_SIZE_AVAILABLE;
133     }
134 
135     public static boolean isGetTrafficClassAvailable() {
136         return GET_TRAFFIC_CLASS_AVAILABLE;
137     }
138 
139     public static boolean isSetTrafficClassAvailable() {
140         return SET_TRAFFIC_CLASS_AVAILABLE;
141     }
142 
143     private boolean reuseAddress = DEFAULT_REUSE_ADDRESS;
144     private int receiveBufferSize = DEFAULT_RECEIVE_BUFFER_SIZE;
145     private int sendBufferSize = DEFAULT_SEND_BUFFER_SIZE;
146     private int trafficClass = DEFAULT_TRAFFIC_CLASS;
147     private boolean keepAlive = DEFAULT_KEEP_ALIVE;
148     private boolean oobInline = DEFAULT_OOB_INLINE;
149     private int soLinger = DEFAULT_SO_LINGER;
150     private boolean tcpNoDelay = DEFAULT_TCP_NO_DELAY;
151 
152     /**
153      * Creates a new instance.
154      */
155     MultiThreadSocketSessionConfigImpl()
156     {
157     }
158 
159     public boolean isReuseAddress()
160     {
161         return reuseAddress;
162     }
163 
164     public void setReuseAddressboolean reuseAddress )
165     {
166         this.reuseAddress = reuseAddress;
167     }
168 
169     public int getReceiveBufferSize()
170     {
171         return receiveBufferSize;
172     }
173 
174     public void setReceiveBufferSizeint receiveBufferSize )
175     {
176         this.receiveBufferSize = receiveBufferSize;
177     }
178 
179     public int getSendBufferSize()
180     {
181         return sendBufferSize;
182     }
183 
184     public void setSendBufferSizeint sendBufferSize )
185     {
186         this.sendBufferSize = sendBufferSize;
187     }
188 
189     public int getTrafficClass()
190     {
191         return trafficClass;
192     }
193 
194     public void setTrafficClassint trafficClass )
195     {
196         this.trafficClass = trafficClass;
197     }
198 
199     public boolean isKeepAlive()
200     {
201         return keepAlive;
202     }
203 
204     public void setKeepAliveboolean keepAlive )
205     {
206         this.keepAlive = keepAlive;
207     }
208 
209     public boolean isOobInline()
210     {
211         return oobInline;
212     }
213 
214     public void setOobInlineboolean oobInline )
215     {
216         this.oobInline = oobInline;
217     }
218 
219     public int getSoLinger()
220     {
221         return soLinger;
222     }
223 
224     public void setSoLingerint soLinger )
225     {
226         this.soLinger = soLinger;
227     }
228 
229     public boolean isTcpNoDelay()
230     {
231         return tcpNoDelay;
232     }
233 
234     public void setTcpNoDelayboolean tcpNoDelay )
235     {
236         this.tcpNoDelay = tcpNoDelay;
237     }
238 
239 
240 }