View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.config;
29  
30  import java.net.SocketOptions;
31  
32  import org.apache.http.annotation.Immutable;
33  import org.apache.http.util.Args;
34  
35  /**
36   * Socket configuration.
37   *
38   * @since 4.3
39   */
40  @Immutable
41  public class SocketConfig implements Cloneable {
42  
43      public static final SocketConfig DEFAULT = new Builder().build();
44  
45      private final int soTimeout;
46      private final boolean soReuseAddress;
47      private final int soLinger;
48      private final boolean soKeepAlive;
49      private final boolean tcpNoDelay;
50  
51      SocketConfig(
52              final int soTimeout,
53              final boolean soReuseAddress,
54              final int soLinger,
55              final boolean soKeepAlive,
56              final boolean tcpNoDelay) {
57          super();
58          this.soTimeout = soTimeout;
59          this.soReuseAddress = soReuseAddress;
60          this.soLinger = soLinger;
61          this.soKeepAlive = soKeepAlive;
62          this.tcpNoDelay = tcpNoDelay;
63      }
64  
65      /**
66       * Determines the default socket timeout value for non-blocking I/O operations.
67       * <p/>
68       * Default: <code>0</code> (no timeout)
69       *
70       * @see SocketOptions#SO_TIMEOUT
71       */
72      public int getSoTimeout() {
73          return soTimeout;
74      }
75  
76      /**
77       * Determines the default value of the {@link SocketOptions#SO_REUSEADDR} parameter
78       * for newly created sockets.
79       * <p/>
80       * Default: <code>false</code>
81       *
82       * @see SocketOptions#SO_REUSEADDR
83       */
84      public boolean isSoReuseAddress() {
85          return soReuseAddress;
86      }
87  
88      /**
89       * Determines the default value of the {@link SocketOptions#SO_LINGER} parameter
90       * for newly created sockets.
91       * <p/>
92       * Default: <code>-1</code>
93       *
94       * @see SocketOptions#SO_LINGER
95       */
96      public int getSoLinger() {
97          return soLinger;
98      }
99  
100     /**
101      * Determines the default value of the {@link SocketOptions#SO_KEEPALIVE} parameter
102      * for newly created sockets.
103      * <p/>
104      * Default: <code>-1</code>
105      *
106      * @see SocketOptions#SO_KEEPALIVE
107      */
108     public boolean isSoKeepAlive() {
109         return this.soKeepAlive;
110     }
111 
112     /**
113      * Determines the default value of the {@link SocketOptions#TCP_NODELAY} parameter
114      * for newly created sockets.
115      * <p/>
116      * Default: <code>false</code>
117      *
118      * @see SocketOptions#TCP_NODELAY
119      */
120     public boolean isTcpNoDelay() {
121         return tcpNoDelay;
122     }
123 
124     @Override
125     protected SocketConfig clone() throws CloneNotSupportedException {
126         return (SocketConfig) super.clone();
127     }
128 
129     @Override
130     public String toString() {
131         final StringBuilder builder = new StringBuilder();
132         builder.append("[soTimeout=").append(this.soTimeout)
133                 .append(", soReuseAddress=").append(this.soReuseAddress)
134                 .append(", soLinger=").append(this.soLinger)
135                 .append(", soKeepAlive=").append(this.soKeepAlive)
136                 .append(", tcpNoDelay=").append(this.tcpNoDelay)
137                 .append("]");
138         return builder.toString();
139     }
140 
141     public static SocketConfig.Builder custom() {
142         return new Builder();
143     }
144 
145     public static SocketConfig.Builder copy(final SocketConfig config) {
146         Args.notNull(config, "Socket config");
147         return new Builder()
148             .setSoTimeout(config.getSoTimeout())
149             .setSoReuseAddress(config.isSoReuseAddress())
150             .setSoLinger(config.getSoLinger())
151             .setSoKeepAlive(config.isSoKeepAlive())
152             .setTcpNoDelay(config.isTcpNoDelay());
153     }
154 
155     public static class Builder {
156 
157         private int soTimeout;
158         private boolean soReuseAddress;
159         private int soLinger;
160         private boolean soKeepAlive;
161         private boolean tcpNoDelay;
162 
163         Builder() {
164             this.soLinger = -1;
165             this.tcpNoDelay = true;
166         }
167 
168         public Builder setSoTimeout(final int soTimeout) {
169             this.soTimeout = soTimeout;
170             return this;
171         }
172 
173         public Builder setSoReuseAddress(final boolean soReuseAddress) {
174             this.soReuseAddress = soReuseAddress;
175             return this;
176         }
177 
178         public Builder setSoLinger(final int soLinger) {
179             this.soLinger = soLinger;
180             return this;
181         }
182 
183         public Builder setSoKeepAlive(final boolean soKeepAlive) {
184             this.soKeepAlive = soKeepAlive;
185             return this;
186         }
187 
188         public Builder setTcpNoDelay(final boolean tcpNoDelay) {
189             this.tcpNoDelay = tcpNoDelay;
190             return this;
191         }
192 
193         public SocketConfig build() {
194             return new SocketConfig(soTimeout, soReuseAddress, soLinger, soKeepAlive, tcpNoDelay);
195         }
196 
197     }
198 
199 }