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 org.apache.logging.log4j.core.Core;
21  import org.apache.logging.log4j.core.config.plugins.Plugin;
22  import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
23  import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
24  import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required;
25  import org.apache.logging.log4j.core.util.Builder;
26  
27  /**
28   * Holds all socket options settable via {@link Socket#setPerformancePreferences(int, int, int)}.
29   * <p>
30   * The {@link Socket#setPerformancePreferences(int, int, int)} API may not be implemented by a JRE.
31   * </p>
32   */
33  @Plugin(name = "SocketPerformancePreferences", category = Core.CATEGORY_NAME, printObject = true)
34  public class SocketPerformancePreferences implements Builder<SocketPerformancePreferences>, Cloneable {
35  
36      @PluginBuilderFactory
37      public static SocketPerformancePreferences newBuilder() {
38          return new SocketPerformancePreferences();
39      }
40  
41      @PluginBuilderAttribute
42      @Required
43      private int bandwidth;
44  
45      @PluginBuilderAttribute
46      @Required
47      private int connectionTime;
48  
49      @PluginBuilderAttribute
50      @Required
51      private int latency;
52  
53      public void apply(final Socket socket) {
54          socket.setPerformancePreferences(connectionTime, latency, bandwidth);
55      }
56  
57      @Override
58      public SocketPerformancePreferences build() {
59          try {
60              return (SocketPerformancePreferences) clone();
61          } catch (final CloneNotSupportedException e) {
62              throw new IllegalStateException(e);
63          }
64      }
65  
66      public int getBandwidth() {
67          return bandwidth;
68      }
69  
70      public int getConnectionTime() {
71          return connectionTime;
72      }
73  
74      public int getLatency() {
75          return latency;
76      }
77  
78      public void setBandwidth(final int bandwidth) {
79          this.bandwidth = bandwidth;
80      }
81  
82      public void setConnectionTime(final int connectionTime) {
83          this.connectionTime = connectionTime;
84      }
85  
86      public void setLatency(final int latency) {
87          this.latency = latency;
88      }
89  
90      @Override
91      public String toString() {
92          return "SocketPerformancePreferences [bandwidth=" + bandwidth + ", connectionTime=" + connectionTime
93                  + ", latency=" + latency + "]";
94      }
95  
96  }