1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.core.net;
18
19 import java.net.Socket;
20 import java.net.SocketException;
21
22 import org.apache.logging.log4j.core.Core;
23 import org.apache.logging.log4j.core.config.plugins.Plugin;
24 import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
25 import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
26 import org.apache.logging.log4j.core.config.plugins.PluginElement;
27 import org.apache.logging.log4j.core.util.Builder;
28
29
30
31
32 @Plugin(name = "SocketOptions", category = Core.CATEGORY_NAME, printObject = true)
33 public class SocketOptions implements Builder<SocketOptions>, Cloneable {
34
35 @PluginBuilderFactory
36 public static SocketOptions newBuilder() {
37 return new SocketOptions();
38 }
39
40 @PluginBuilderAttribute
41 private Boolean keepAlive;
42
43 @PluginBuilderAttribute
44 private Boolean oobInline;
45
46 @PluginElement("PerformancePreferences")
47 private SocketPerformancePreferences performancePreferences;
48
49 @PluginBuilderAttribute
50 private Integer receiveBufferSize;
51
52 @PluginBuilderAttribute
53 private Boolean reuseAddress;
54
55 @PluginBuilderAttribute
56 private Rfc1349TrafficClass rfc1349TrafficClass;
57
58 @PluginBuilderAttribute
59 private Integer sendBufferSize;
60
61 @PluginBuilderAttribute
62 private Integer soLinger;
63
64 @PluginBuilderAttribute
65 private Integer soTimeout;
66
67 @PluginBuilderAttribute
68 private Boolean tcpNoDelay;
69
70 @PluginBuilderAttribute
71 private Integer trafficClass;
72
73 public void apply(final Socket socket) throws SocketException {
74 if (keepAlive != null) {
75 socket.setKeepAlive(keepAlive.booleanValue());
76 }
77 if (oobInline != null) {
78 socket.setOOBInline(oobInline.booleanValue());
79 }
80 if (reuseAddress != null) {
81 socket.setReuseAddress(reuseAddress.booleanValue());
82 }
83 if (performancePreferences != null) {
84 performancePreferences.apply(socket);
85 }
86 if (receiveBufferSize != null) {
87 socket.setReceiveBufferSize(receiveBufferSize.intValue());
88 }
89 if (soLinger != null) {
90 socket.setSoLinger(true, soLinger.intValue());
91 }
92 if (soTimeout != null) {
93 socket.setSoTimeout(soTimeout.intValue());
94 }
95 if (tcpNoDelay != null) {
96 socket.setTcpNoDelay(tcpNoDelay.booleanValue());
97 }
98 final Integer actualTrafficClass = getActualTrafficClass();
99 if (actualTrafficClass != null) {
100 socket.setTrafficClass(actualTrafficClass);
101 }
102 }
103
104 @Override
105 public SocketOptions build() {
106 try {
107 return (SocketOptions) clone();
108 } catch (final CloneNotSupportedException e) {
109 throw new IllegalStateException(e);
110 }
111 }
112
113 public Integer getActualTrafficClass() {
114 if (trafficClass != null && rfc1349TrafficClass != null) {
115 throw new IllegalStateException("You MUST not set both customTrafficClass and trafficClass.");
116 }
117 if (trafficClass != null) {
118 return trafficClass;
119 }
120 if (rfc1349TrafficClass != null) {
121 return Integer.valueOf(rfc1349TrafficClass.value());
122 }
123 return null;
124 }
125
126 public SocketPerformancePreferences getPerformancePreferences() {
127 return performancePreferences;
128 }
129
130 public Integer getReceiveBufferSize() {
131 return receiveBufferSize;
132 }
133
134 public Rfc1349TrafficClass getRfc1349TrafficClass() {
135 return rfc1349TrafficClass;
136 }
137
138 public Integer getSendBufferSize() {
139 return sendBufferSize;
140 }
141
142 public Integer getSoLinger() {
143 return soLinger;
144 }
145
146 public Integer getSoTimeout() {
147 return soTimeout;
148 }
149
150 public Integer getTrafficClass() {
151 return trafficClass;
152 }
153
154 public Boolean isKeepAlive() {
155 return keepAlive;
156 }
157
158 public Boolean isOobInline() {
159 return oobInline;
160 }
161
162 public Boolean isReuseAddress() {
163 return reuseAddress;
164 }
165
166 public Boolean isTcpNoDelay() {
167 return tcpNoDelay;
168 }
169
170 public SocketOptions setKeepAlive(final boolean keepAlive) {
171 this.keepAlive = Boolean.valueOf(keepAlive);
172 return this;
173 }
174
175 public SocketOptions setOobInline(final boolean oobInline) {
176 this.oobInline = Boolean.valueOf(oobInline);
177 return this;
178 }
179
180 public SocketOptions setPerformancePreferences(final SocketPerformancePreferences performancePreferences) {
181 this.performancePreferences = performancePreferences;
182 return this;
183 }
184
185 public SocketOptions setReceiveBufferSize(final int receiveBufferSize) {
186 this.receiveBufferSize = receiveBufferSize;
187 return this;
188 }
189
190 public SocketOptions setReuseAddress(final boolean reuseAddress) {
191 this.reuseAddress = Boolean.valueOf(reuseAddress);
192 return this;
193 }
194
195 public SocketOptions setRfc1349TrafficClass(final Rfc1349TrafficClass trafficClass) {
196 this.rfc1349TrafficClass = trafficClass;
197 return this;
198 }
199
200 public SocketOptions setSendBufferSize(final int sendBufferSize) {
201 this.sendBufferSize = sendBufferSize;
202 return this;
203 }
204
205 public SocketOptions setSoLinger(final int soLinger) {
206 this.soLinger = soLinger;
207 return this;
208 }
209
210 public SocketOptions setSoTimeout(final int soTimeout) {
211 this.soTimeout = soTimeout;
212 return this;
213 }
214
215 public SocketOptions setTcpNoDelay(final boolean tcpNoDelay) {
216 this.tcpNoDelay = Boolean.valueOf(tcpNoDelay);
217 return this;
218 }
219
220 public SocketOptions setTrafficClass(final int trafficClass) {
221 this.trafficClass = trafficClass;
222 return this;
223 }
224
225 @Override
226 public String toString() {
227 return "SocketOptions [keepAlive=" + keepAlive + ", oobInline=" + oobInline + ", performancePreferences="
228 + performancePreferences + ", receiveBufferSize=" + receiveBufferSize + ", reuseAddress=" + reuseAddress
229 + ", rfc1349TrafficClass=" + rfc1349TrafficClass + ", sendBufferSize=" + sendBufferSize + ", soLinger="
230 + soLinger + ", soTimeout=" + soTimeout + ", tcpNoDelay=" + tcpNoDelay + ", trafficClass="
231 + trafficClass + "]";
232 }
233 }