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.InetAddress;
020import java.net.InetSocketAddress;
021
022import org.apache.logging.log4j.core.config.Node;
023import org.apache.logging.log4j.core.config.plugins.Plugin;
024import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
025import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
026import org.apache.logging.log4j.core.config.plugins.validation.constraints.ValidHost;
027import org.apache.logging.log4j.core.config.plugins.validation.constraints.ValidPort;
028
029/**
030 * Plugin to hold a hostname and port (socket address).
031 *
032 * @since 2.8
033 */
034@Plugin(name = "SocketAddress", category = Node.CATEGORY, printObject = true)
035public class SocketAddress {
036
037    /**
038     * Creates a SocketAddress corresponding to {@code localhost:0}.
039     *
040     * @return a SocketAddress for {@code localhost:0}
041     */
042    public static SocketAddress getLoopback() {
043        return new SocketAddress(InetAddress.getLoopbackAddress(), 0);
044    }
045
046    // never null
047    private final InetSocketAddress socketAddress;
048
049    private SocketAddress(final InetAddress host, final int port) {
050        this.socketAddress = new InetSocketAddress(host, port);
051    }
052
053    public InetSocketAddress getSocketAddress() {
054        return socketAddress;
055    }
056
057    public int getPort() {
058        return socketAddress.getPort();
059    }
060
061    public InetAddress getAddress() {
062        return socketAddress.getAddress();
063    }
064
065    public String getHostName() {
066        return socketAddress.getHostName();
067    }
068
069    @PluginBuilderFactory
070    public static Builder newBuilder() {
071        return new Builder();
072    }
073
074    public static class Builder implements org.apache.logging.log4j.core.util.Builder<SocketAddress> {
075
076        @PluginBuilderAttribute
077        @ValidHost
078        private InetAddress host;
079
080        @PluginBuilderAttribute
081        @ValidPort
082        private int port;
083
084        public Builder setHost(final InetAddress host) {
085            this.host = host;
086            return this;
087        }
088
089        public Builder setPort(final int port) {
090            this.port = port;
091            return this;
092        }
093
094        @Override
095        public SocketAddress build() {
096            return new SocketAddress(host, port);
097        }
098    }
099
100    @Override
101    public String toString() {
102        return socketAddress.toString();
103    }
104
105}