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    *      https://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   * <p>
39   * Public access is through {@link StringLookupFactory}.
40   * </p>
41   *
42   * @see StringLookupFactory
43   * @since 1.3
44   */
45  final class InetAddressStringLookup extends AbstractStringLookup {
46  
47      /**
48       * Defines the LOCAL_HOST constant.
49       */
50      static final InetAddressStringLookup LOCAL_HOST = new InetAddressStringLookup(InetAddress::getLocalHost);
51  
52      /**
53       * Defines the LOCAL_HOST constant.
54       */
55      static final InetAddressStringLookup LOOPACK_ADDRESS = new InetAddressStringLookup(InetAddress::getLoopbackAddress);
56  
57      /**
58       * Supplies the {@link InetAddress}.
59       */
60      private final FailableSupplier<InetAddress, UnknownHostException> inetAddressSupplier;
61  
62      /**
63       * No need to build instances for now.
64       */
65      private InetAddressStringLookup(final FailableSupplier<InetAddress, UnknownHostException> inetAddressSupplier) {
66          this.inetAddressSupplier = Objects.requireNonNull(inetAddressSupplier, "inetAddressSupplier");
67      }
68  
69      private InetAddress getInetAddress() throws UnknownHostException {
70          // Don't cache result, methods, like InetAddress::getLocalHost do their own cacheing.
71          return inetAddressSupplier.get();
72      }
73  
74      /**
75       * Looks up the value of a local host key.
76       *
77       * @param key the key to be looked up, may be null.
78       * @return The value of the environment variable.
79       */
80      @Override
81      public String lookup(final String key) {
82          if (key == null) {
83              return null;
84          }
85          try {
86              switch (key) {
87              case InetAddressKeys.KEY_NAME:
88                  return getInetAddress().getHostName();
89              case InetAddressKeys.KEY_CANONICAL_NAME:
90                  return getInetAddress().getCanonicalHostName();
91              case InetAddressKeys.KEY_ADDRESS:
92                  return getInetAddress().getHostAddress();
93              default:
94                  throw new IllegalArgumentException(key);
95              }
96          } catch (final UnknownHostException e) {
97              return null;
98          }
99      }
100 }