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.commons.text.lookup;
18  
19  import java.net.InetAddress;
20  import java.net.UnknownHostException;
21  import java.util.Objects;
22  
23  import org.apache.commons.lang3.function.FailableSupplier;
24  
25  /**
26   * Looks up keys related to an {@link InetAddresse}.
27   * <ul>
28   *   <li>local host: host name, canonical host name, host address.</li>
29   * </ul>
30   * <p>
31   * The lookup keys are:
32   * </p>
33   * <ul>
34   * <li><strong>name</strong>: for the local host name, for example {@code EXAMPLE}.</li>
35   * <li><strong>canonical-name</strong>: for the local canonical host name, for example {@code EXAMPLE.apache.org}.</li>
36   * <li><strong>address</strong>: for the local host address, for example {@code 192.168.56.1}.</li>
37   * </ul>
38   *
39   * @since 1.3
40   */
41  final class InetAddressStringLookup extends AbstractStringLookup {
42  
43      /**
44       * Defines the LOCAL_HOST constant.
45       */
46      static final InetAddressStringLookup LOCAL_HOST = new InetAddressStringLookup(InetAddress::getLocalHost);
47  
48      /**
49       * Defines the LOCAL_HOST constant.
50       */
51      static final InetAddressStringLookup LOOPACK_ADDRESS = new InetAddressStringLookup(InetAddress::getLoopbackAddress);
52  
53      /**
54       * Supplies the InetAddress.
55       */
56      private final FailableSupplier<InetAddress, UnknownHostException> inetAddressSupplier;
57  
58      /**
59       * No need to build instances for now.
60       */
61      private InetAddressStringLookup(final FailableSupplier<InetAddress, UnknownHostException> inetAddressSupplier) {
62          this.inetAddressSupplier = Objects.requireNonNull(inetAddressSupplier, "inetAddressSupplier");
63      }
64  
65      private InetAddress getInetAddress() throws UnknownHostException {
66          // Don't cache result, methods, like InetAddress::getLocalHost do their own cacheing.
67          return inetAddressSupplier.get();
68      }
69  
70      /**
71       * Looks up the value of a local host key.
72       *
73       * @param key the key to be looked up, may be null.
74       * @return The value of the environment variable.
75       */
76      @Override
77      public String lookup(final String key) {
78          if (key == null) {
79              return null;
80          }
81          try {
82              switch (key) {
83              case InetAddressKeys.KEY_NAME:
84                  return getInetAddress().getHostName();
85              case InetAddressKeys.KEY_CANONICAL_NAME:
86                  return getInetAddress().getCanonicalHostName();
87              case InetAddressKeys.KEY_ADDRESS:
88                  return getInetAddress().getHostAddress();
89              default:
90                  throw new IllegalArgumentException(key);
91              }
92          } catch (final UnknownHostException e) {
93              return null;
94          }
95      }
96  }