InetAddressStringLookup.java

  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. import java.net.InetAddress;
  19. import java.net.UnknownHostException;
  20. import java.util.Objects;

  21. import org.apache.commons.lang3.function.FailableSupplier;

  22. /**
  23.  * Looks up keys related to an {@link InetAddresse}.
  24.  * <ul>
  25.  *   <li>local host: host name, canonical host name, host address.</li>
  26.  * </ul>
  27.  * <p>
  28.  * The lookup keys are:
  29.  * </p>
  30.  * <ul>
  31.  * <li><strong>name</strong>: for the local host name, for example {@code EXAMPLE}.</li>
  32.  * <li><strong>canonical-name</strong>: for the local canonical host name, for example {@code EXAMPLE.apache.org}.</li>
  33.  * <li><strong>address</strong>: for the local host address, for example {@code 192.168.56.1}.</li>
  34.  * </ul>
  35.  *
  36.  * @since 1.3
  37.  */
  38. final class InetAddressStringLookup extends AbstractStringLookup {

  39.     /**
  40.      * Defines the LOCAL_HOST constant.
  41.      */
  42.     static final InetAddressStringLookup LOCAL_HOST = new InetAddressStringLookup(InetAddress::getLocalHost);

  43.     /**
  44.      * Defines the LOCAL_HOST constant.
  45.      */
  46.     static final InetAddressStringLookup LOOPACK_ADDRESS = new InetAddressStringLookup(InetAddress::getLoopbackAddress);

  47.     /**
  48.      * Supplies the InetAddress.
  49.      */
  50.     private final FailableSupplier<InetAddress, UnknownHostException> inetAddressSupplier;

  51.     /**
  52.      * No need to build instances for now.
  53.      */
  54.     private InetAddressStringLookup(final FailableSupplier<InetAddress, UnknownHostException> inetAddressSupplier) {
  55.         this.inetAddressSupplier = Objects.requireNonNull(inetAddressSupplier, "inetAddressSupplier");
  56.     }

  57.     private InetAddress getInetAddress() throws UnknownHostException {
  58.         // Don't cache result, methods, like InetAddress::getLocalHost do their own cacheing.
  59.         return inetAddressSupplier.get();
  60.     }

  61.     /**
  62.      * Looks up the value of a local host key.
  63.      *
  64.      * @param key the key to be looked up, may be null.
  65.      * @return The value of the environment variable.
  66.      */
  67.     @Override
  68.     public String lookup(final String key) {
  69.         if (key == null) {
  70.             return null;
  71.         }
  72.         try {
  73.             switch (key) {
  74.             case InetAddressKeys.KEY_NAME:
  75.                 return getInetAddress().getHostName();
  76.             case InetAddressKeys.KEY_CANONICAL_NAME:
  77.                 return getInetAddress().getCanonicalHostName();
  78.             case InetAddressKeys.KEY_ADDRESS:
  79.                 return getInetAddress().getHostAddress();
  80.             default:
  81.                 throw new IllegalArgumentException(key);
  82.             }
  83.         } catch (final UnknownHostException e) {
  84.             return null;
  85.         }
  86.     }
  87. }