View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.http.impl.conn;
28  
29  import java.net.InetAddress;
30  import java.net.UnknownHostException;
31  import java.util.Arrays;
32  import java.util.Map;
33  import java.util.concurrent.ConcurrentHashMap;
34  
35  import org.apache.commons.logging.Log;
36  import org.apache.commons.logging.LogFactory;
37  import org.apache.http.conn.DnsResolver;
38  import org.apache.http.util.Args;
39  
40  /**
41   * In-memory {@link DnsResolver} implementation.
42   *
43   * @since 4.2
44   */
45  public class InMemoryDnsResolver implements DnsResolver {
46  
47      /** Logger associated to this class. */
48      private final Log log = LogFactory.getLog(InMemoryDnsResolver.class);
49  
50      /**
51       * In-memory collection that will hold the associations between a host name
52       * and an array of InetAddress instances.
53       */
54      private final Map<String, InetAddress[]> dnsMap;
55  
56      /**
57       * Builds a DNS resolver that will resolve the host names against a
58       * collection held in-memory.
59       */
60      public InMemoryDnsResolver() {
61          dnsMap = new ConcurrentHashMap<String, InetAddress[]>();
62      }
63  
64      /**
65       * Associates the given array of IP addresses to the given host in this DNS overrider.
66       * The IP addresses are assumed to be already resolved.
67       *
68       * @param host
69       *            The host name to be associated with the given IP.
70       * @param ips
71       *            array of IP addresses to be resolved by this DNS overrider to the given
72       *            host name.
73       */
74      public void add(final String host, final InetAddress... ips) {
75          Args.notNull(host, "Host name");
76          Args.notNull(ips, "Array of IP addresses");
77          dnsMap.put(host, ips);
78      }
79  
80      /**
81       * {@inheritDoc}
82       */
83      @Override
84      public InetAddress[] resolve(final String host) throws UnknownHostException {
85          final InetAddress[] resolvedAddresses = dnsMap.get(host);
86          if (log.isInfoEnabled()) {
87              log.info("Resolving " + host + " to " + Arrays.deepToString(resolvedAddresses));
88          }
89          if(resolvedAddresses == null){
90              throw new UnknownHostException(host + " cannot be resolved");
91          }
92          return resolvedAddresses;
93      }
94  
95  }