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 org.apache.http.annotation.Immutable;
31  import org.apache.http.util.Args;
32  
33  /**
34   * Socket configuration.
35   *
36   * @since 4.3
37   */
38  @Immutable
39  public class SocketConfig implements Cloneable {
40  
41      public static final SocketConfig DEFAULT = new Builder().build();
42  
43      private final int soTimeout;
44      private final boolean soReuseAddress;
45      private final int soLinger;
46      private final boolean soKeepAlive;
47      private final boolean tcpNoDelay;
48  
49      SocketConfig(
50              final int soTimeout,
51              final boolean soReuseAddress,
52              final int soLinger,
53              final boolean soKeepAlive,
54              final boolean tcpNoDelay) {
55          super();
56          this.soTimeout = soTimeout;
57          this.soReuseAddress = soReuseAddress;
58          this.soLinger = soLinger;
59          this.soKeepAlive = soKeepAlive;
60          this.tcpNoDelay = tcpNoDelay;
61      }
62  
63      /**
64       * Determines the default socket timeout value for non-blocking I/O operations.
65       * <p/>
66       * Default: <code>0</code> (no timeout)
67       *
68       * @see java.net.SocketOptions#SO_TIMEOUT
69       */
70      public int getSoTimeout() {
71          return soTimeout;
72      }
73  
74      /**
75       * Determines the default value of the {@link java.net.SocketOptions#SO_REUSEADDR} parameter
76       * for newly created sockets.
77       * <p/>
78       * Default: <code>false</code>
79       *
80       * @see java.net.SocketOptions#SO_REUSEADDR
81       */
82      public boolean isSoReuseAddress() {
83          return soReuseAddress;
84      }
85  
86      /**
87       * Determines the default value of the {@link java.net.SocketOptions#SO_LINGER} parameter
88       * for newly created sockets.
89       * <p/>
90       * Default: <code>-1</code>
91       *
92       * @see java.net.SocketOptions#SO_LINGER
93       */
94      public int getSoLinger() {
95          return soLinger;
96      }
97  
98      /**
99       * Determines the default value of the {@link java.net.SocketOptions#SO_KEEPALIVE} parameter
100      * for newly created sockets.
101      * <p/>
102      * Default: <code>-1</code>
103      *
104      * @see java.net.SocketOptions#SO_KEEPALIVE
105      */
106     public boolean isSoKeepAlive() {
107         return this.soKeepAlive;
108     }
109 
110     /**
111      * Determines the default value of the {@link java.net.SocketOptions#TCP_NODELAY} parameter
112      * for newly created sockets.
113      * <p/>
114      * Default: <code>false</code>
115      *
116      * @see java.net.SocketOptions#TCP_NODELAY
117      */
118     public boolean isTcpNoDelay() {
119         return tcpNoDelay;
120     }
121 
122     @Override
123     protected SocketConfig clone() throws CloneNotSupportedException {
124         return (SocketConfig) super.clone();
125     }
126 
127     @Override
128     public String toString() {
129         final StringBuilder builder = new StringBuilder();
130         builder.append("[soTimeout=").append(this.soTimeout)
131                 .append(", soReuseAddress=").append(this.soReuseAddress)
132                 .append(", soLinger=").append(this.soLinger)
133                 .append(", soKeepAlive=").append(this.soKeepAlive)
134                 .append(", tcpNoDelay=").append(this.tcpNoDelay)
135                 .append("]");
136         return builder.toString();
137     }
138 
139     public static SocketConfig.Builder custom() {
140         return new Builder();
141     }
142 
143     public static SocketConfig.Builder copy(final SocketConfig config) {
144         Args.notNull(config, "Socket config");
145         return new Builder()
146             .setSoTimeout(config.getSoTimeout())
147             .setSoReuseAddress(config.isSoReuseAddress())
148             .setSoLinger(config.getSoLinger())
149             .setSoKeepAlive(config.isSoKeepAlive())
150             .setTcpNoDelay(config.isTcpNoDelay());
151     }
152 
153     public static class Builder {
154 
155         private int soTimeout;
156         private boolean soReuseAddress;
157         private int soLinger;
158         private boolean soKeepAlive;
159         private boolean tcpNoDelay;
160 
161         Builder() {
162             this.soLinger = -1;
163             this.tcpNoDelay = true;
164         }
165 
166         public Builder setSoTimeout(final int soTimeout) {
167             this.soTimeout = soTimeout;
168             return this;
169         }
170 
171         public Builder setSoReuseAddress(final boolean soReuseAddress) {
172             this.soReuseAddress = soReuseAddress;
173             return this;
174         }
175 
176         public Builder setSoLinger(final int soLinger) {
177             this.soLinger = soLinger;
178             return this;
179         }
180 
181         public Builder setSoKeepAlive(final boolean soKeepAlive) {
182             this.soKeepAlive = soKeepAlive;
183             return this;
184         }
185 
186         public Builder setTcpNoDelay(final boolean tcpNoDelay) {
187             this.tcpNoDelay = tcpNoDelay;
188             return this;
189         }
190 
191         public SocketConfig build() {
192             return new SocketConfig(soTimeout, soReuseAddress, soLinger, soKeepAlive, tcpNoDelay);
193         }
194 
195     }
196 
197 }