View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.mina.transport.tcp;
20  
21  import java.net.Socket;
22  import java.net.SocketException;
23  
24  import javax.net.ssl.SSLContext;
25  
26  import org.apache.mina.api.ConfigurationException;
27  import org.apache.mina.api.IdleStatus;
28  import org.apache.mina.session.TrafficClassEnum;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  /**
33   * This class proxy the inner java.net.Socket configuration with the SocketSessionConfig of the session.
34   * 
35   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
36   */
37  public class ProxyTcpSessionConfig implements TcpSessionConfig {
38  
39      private static final Logger LOG = LoggerFactory.getLogger(ProxyTcpSessionConfig.class);
40  
41      private final Socket socket;
42  
43      public ProxyTcpSessionConfig(Socket socket) {
44          this.socket = socket;
45      }
46  
47      private long idleTimeRead = -1;
48  
49      private long idleTimeWrite = -1;
50  
51      /**
52       * {@inheritDoc}
53       */
54      @Override
55      public long getIdleTimeInMillis(IdleStatus status) {
56          switch (status) {
57          case READ_IDLE:
58              return idleTimeRead;
59          case WRITE_IDLE:
60              return idleTimeWrite;
61          default:
62              throw new IllegalStateException("unexpected excetion, unknown idle status : " + status);
63          }
64      }
65  
66      /**
67       * {@inheritDoc}
68       */
69      @Override
70      public void setIdleTimeInMillis(IdleStatus status, long ildeTimeInMilli) {
71          switch (status) {
72          case READ_IDLE:
73              this.idleTimeRead = ildeTimeInMilli;
74              break;
75          case WRITE_IDLE:
76              this.idleTimeWrite = ildeTimeInMilli;
77              break;
78          default:
79              throw new IllegalStateException("unexpected excetion, unknown idle status : " + status);
80          }
81      }
82  
83      /**
84       * {@inheritDoc}
85       */
86      @Override
87      public Boolean isTcpNoDelay() {
88          try {
89              return socket.getTcpNoDelay();
90          } catch (SocketException e) {
91              throw new ConfigurationException(e);
92          }
93      }
94  
95      /**
96       * {@inheritDoc}
97       */
98      @Override
99      public void setTcpNoDelay(boolean tcpNoDelay) {
100         LOG.debug("set TCP no delay '{}' for session '{}'", tcpNoDelay, this);
101         try {
102             socket.setTcpNoDelay(tcpNoDelay);
103         } catch (SocketException e) {
104             throw new ConfigurationException(e);
105         }
106     }
107 
108     /**
109      * {@inheritDoc}
110      */
111     @Override
112     public Boolean isReuseAddress() {
113         try {
114             return socket.getReuseAddress();
115         } catch (SocketException e) {
116             throw new ConfigurationException(e);
117         }
118     }
119 
120     /**
121      * {@inheritDoc}
122      */
123     @Override
124     public void setReuseAddress(boolean reuseAddress) {
125         LOG.debug("set reuse address '{}' for session '{}'", reuseAddress, this);
126         try {
127             socket.setReuseAddress(reuseAddress);
128         } catch (SocketException e) {
129             throw new ConfigurationException(e);
130         }
131     }
132 
133     /**
134      * {@inheritDoc}
135      */
136     @Override
137     public Integer getReadBufferSize() {
138         try {
139             return socket.getReceiveBufferSize();
140         } catch (SocketException e) {
141             throw new ConfigurationException(e);
142         }
143     }
144 
145     /**
146      * {@inheritDoc}
147      */
148     @Override
149     public void setReadBufferSize(int receiveBufferSize) {
150         LOG.debug("set receive buffer size '{}' for session '{}'", receiveBufferSize, this);
151         try {
152             socket.setReceiveBufferSize(receiveBufferSize);
153         } catch (SocketException e) {
154             throw new ConfigurationException(e);
155         }
156     }
157 
158     /**
159      * {@inheritDoc}
160      */
161     @Override
162     public Integer getSendBufferSize() {
163         try {
164             return socket.getSendBufferSize();
165         } catch (SocketException e) {
166             throw new ConfigurationException(e);
167         }
168     }
169 
170     /**
171      * {@inheritDoc}
172      */
173     @Override
174     public void setSendBufferSize(int sendBufferSize) {
175         LOG.debug("set send buffer size '{}' for session '{}'", sendBufferSize, this);
176         try {
177             socket.setSendBufferSize(sendBufferSize);
178         } catch (SocketException e) {
179             throw new ConfigurationException(e);
180         }
181     }
182 
183     /**
184      * {@inheritDoc}
185      */
186     @Override
187     public int getTrafficClass() {
188         try {
189             return socket.getTrafficClass();
190         } catch (SocketException e) {
191             throw new ConfigurationException(e);
192         }
193     }
194 
195     /**
196      * {@inheritDoc}
197      */
198     @Override
199     public void setTrafficClass(int trafficClass) {
200         LOG.debug("set traffic class '{}' for session '{}'", trafficClass, this);
201         try {
202             socket.setTrafficClass(trafficClass);
203         } catch (SocketException e) {
204             throw new ConfigurationException(e);
205         }
206     }
207 
208     /**
209      * {@inheritDoc}
210      */
211     @Override
212     public void setTrafficClass(TrafficClassEnum trafficClass) {
213         LOG.debug("set traffic class '{}' for session '{}'", trafficClass, this);
214         try {
215             socket.setTrafficClass(trafficClass.getValue());
216         } catch (SocketException e) {
217             throw new ConfigurationException(e);
218         }
219     }
220 
221     /**
222      * {@inheritDoc}
223      */
224     @Override
225     public Boolean isKeepAlive() {
226         try {
227             return socket.getKeepAlive();
228         } catch (SocketException e) {
229             throw new ConfigurationException(e);
230         }
231     }
232 
233     /**
234      * {@inheritDoc}
235      */
236     @Override
237     public void setKeepAlive(boolean keepAlive) {
238         LOG.debug("set keep alive '{}' for session '{}'", keepAlive, this);
239         try {
240             socket.setKeepAlive(keepAlive);
241         } catch (SocketException e) {
242             throw new ConfigurationException(e);
243         }
244     }
245 
246     /**
247      * {@inheritDoc}
248      */
249     @Override
250     public Boolean isOobInline() {
251         try {
252             return socket.getOOBInline();
253         } catch (SocketException e) {
254             throw new ConfigurationException(e);
255         }
256     }
257 
258     /**
259      * {@inheritDoc}
260      */
261     @Override
262     public void setOobInline(boolean oobInline) {
263         LOG.debug("set oob inline '{}' for session '{}'", oobInline, this);
264         try {
265             socket.setOOBInline(oobInline);
266         } catch (SocketException e) {
267             throw new ConfigurationException(e);
268         }
269     }
270 
271     /**
272      * {@inheritDoc}
273      */
274     @Override
275     public Integer getSoLinger() {
276         try {
277             return socket.getSoLinger();
278         } catch (SocketException e) {
279             throw new ConfigurationException(e);
280         }
281     }
282 
283     /**
284      * {@inheritDoc}
285      */
286     @Override
287     public void setSoLinger(int soLinger) {
288         LOG.debug("set so linger '{}' for session '{}'", soLinger, this);
289         try {
290             socket.setSoLinger(soLinger > 0, soLinger);
291         } catch (SocketException e) {
292             throw new ConfigurationException(e);
293         }
294     }
295 
296     /**
297      * {@inheritDoc}
298      */
299     @Override
300     public boolean isSecured() {
301         return false;
302     }
303 
304     @Override
305     public SSLContext getSslContext() {
306         return null;
307     }
308 
309     @Override
310     public void setSslContext(SSLContext sslContext) {
311     }
312 
313     /**
314      * {@inheritDoc}
315      */
316     @Override
317     public Integer getTimeout() {
318         try {
319             return socket.getSoTimeout();
320         } catch (SocketException e) {
321             throw new ConfigurationException(e);
322         }
323     }
324 
325     /**
326      * {@inheritDoc}
327      */
328     @Override
329     public void setTimeout(int timeout) {
330         try {
331             socket.setSoTimeout(timeout);
332         } catch (SocketException e) {
333             throw new ConfigurationException(e);
334         }
335     }
336 }