001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.core.net;
018
019import java.net.Socket;
020import org.apache.logging.log4j.core.Core;
021import org.apache.logging.log4j.core.config.plugins.Plugin;
022import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
023import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
024import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required;
025import org.apache.logging.log4j.core.util.Builder;
026
027/**
028 * Holds all socket options settable via {@link Socket#setPerformancePreferences(int, int, int)}.
029 * <p>
030 * The {@link Socket#setPerformancePreferences(int, int, int)} API may not be implemented by a JRE.
031 * </p>
032 */
033@Plugin(name = "SocketPerformancePreferences", category = Core.CATEGORY_NAME, printObject = true)
034public class SocketPerformancePreferences implements Builder<SocketPerformancePreferences>, Cloneable {
035
036    @PluginBuilderFactory
037    public static SocketPerformancePreferences newBuilder() {
038        return new SocketPerformancePreferences();
039    }
040
041    @PluginBuilderAttribute
042    @Required
043    private int bandwidth;
044
045    @PluginBuilderAttribute
046    @Required
047    private int connectionTime;
048
049    @PluginBuilderAttribute
050    @Required
051    private int latency;
052
053    public void apply(final Socket socket) {
054        socket.setPerformancePreferences(connectionTime, latency, bandwidth);
055    }
056
057    @Override
058    public SocketPerformancePreferences build() {
059        try {
060            return (SocketPerformancePreferences) clone();
061        } catch (final CloneNotSupportedException e) {
062            throw new IllegalStateException(e);
063        }
064    }
065
066    public int getBandwidth() {
067        return bandwidth;
068    }
069
070    public int getConnectionTime() {
071        return connectionTime;
072    }
073
074    public int getLatency() {
075        return latency;
076    }
077
078    public void setBandwidth(final int bandwidth) {
079        this.bandwidth = bandwidth;
080    }
081
082    public void setConnectionTime(final int connectionTime) {
083        this.connectionTime = connectionTime;
084    }
085
086    public void setLatency(final int latency) {
087        this.latency = latency;
088    }
089
090    @Override
091    public String toString() {
092        return "SocketPerformancePreferences [bandwidth=" + bandwidth + ", connectionTime=" + connectionTime
093                + ", latency=" + latency + "]";
094    }
095
096}