View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
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.validation.constraints.Required;
27  import org.apache.logging.log4j.core.util.Builder;
28  
29  /**
30   * Holds all socket options settable via {@link Socket#setPerformancePreferences(int, int, int)}.
31   * <p>
32   * The {@link Socket#setPerformancePreferences(int, int, int)} API may not be implemented by a JRE.
33   * </p>
34   */
35  @Plugin(name = "SocketPerformancePreferences", category = Core.CATEGORY_NAME, printObject = true)
36  public class SocketPerformancePreferences implements Builder<SocketPerformancePreferences>, Cloneable {
37  
38      @PluginBuilderFactory
39      public static SocketPerformancePreferences newBuilder() {
40          return new SocketPerformancePreferences();
41      }
42  
43      @PluginBuilderAttribute
44      @Required
45      private int bandwidth;
46  
47      @PluginBuilderAttribute
48      @Required
49      private int connectionTime;
50  
51      @PluginBuilderAttribute
52      @Required
53      private int latency;
54  
55      public void apply(final Socket socket) {
56          socket.setPerformancePreferences(connectionTime, latency, bandwidth);
57      }
58  
59      @Override
60      public SocketPerformancePreferences build() {
61          try {
62              return (SocketPerformancePreferences) clone();
63          } catch (final CloneNotSupportedException e) {
64              throw new IllegalStateException(e);
65          }
66      }
67  
68      public int getBandwidth() {
69          return bandwidth;
70      }
71  
72      public int getConnectionTime() {
73          return connectionTime;
74      }
75  
76      public int getLatency() {
77          return latency;
78      }
79  
80      public void setBandwidth(final int bandwidth) {
81          this.bandwidth = bandwidth;
82      }
83  
84      public void setConnectionTime(final int connectionTime) {
85          this.connectionTime = connectionTime;
86      }
87  
88      public void setLatency(final int latency) {
89          this.latency = latency;
90      }
91  
92      @Override
93      public String toString() {
94          return "SocketPerformancePreferences [bandwidth=" + bandwidth + ", connectionTime=" + connectionTime
95                  + ", latency=" + latency + "]";
96      }
97  
98  }