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.InetAddress;
20  import java.net.InetSocketAddress;
21  
22  import org.apache.logging.log4j.core.config.Node;
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.ValidHost;
27  import org.apache.logging.log4j.core.config.plugins.validation.constraints.ValidPort;
28  
29  /**
30   * Plugin to hold a hostname and port (socket address).
31   *
32   * @since 2.8
33   */
34  @Plugin(name = "SocketAddress", category = Node.CATEGORY, printObject = true)
35  public class SocketAddress {
36  
37      /**
38       * Creates a SocketAddress corresponding to {@code localhost:0}.
39       *
40       * @return a SocketAddress for {@code localhost:0}
41       */
42      public static SocketAddress getLoopback() {
43          return new SocketAddress(InetAddress.getLoopbackAddress(), 0);
44      }
45  
46      // never null
47      private final InetSocketAddress socketAddress;
48  
49      private SocketAddress(final InetAddress host, final int port) {
50          this.socketAddress = new InetSocketAddress(host, port);
51      }
52  
53      public InetSocketAddress getSocketAddress() {
54          return socketAddress;
55      }
56  
57      public int getPort() {
58          return socketAddress.getPort();
59      }
60  
61      public InetAddress getAddress() {
62          return socketAddress.getAddress();
63      }
64  
65      public String getHostName() {
66          return socketAddress.getHostName();
67      }
68  
69      @PluginBuilderFactory
70      public static Builder newBuilder() {
71          return new Builder();
72      }
73  
74      public static class Builder implements org.apache.logging.log4j.core.util.Builder<SocketAddress> {
75  
76          @PluginBuilderAttribute
77          @ValidHost
78          private InetAddress host;
79  
80          @PluginBuilderAttribute
81          @ValidPort
82          private int port;
83  
84          public Builder setHost(final InetAddress host) {
85              this.host = host;
86              return this;
87          }
88  
89          public Builder setPort(final int port) {
90              this.port = port;
91              return this;
92          }
93  
94          @Override
95          public SocketAddress build() {
96              return new SocketAddress(host, port);
97          }
98      }
99  
100     @Override
101     public String toString() {
102         return socketAddress.toString();
103     }
104 
105 }