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
39 /**
40 * In-memory DNS resolver implementation.
41 *
42 * @since 4.2
43 */
44 public class InMemoryDnsResolver implements DnsResolver {
45
46 /** Logger associated to this class. */
47 private final Log log = LogFactory.getLog(InMemoryDnsResolver.class);
48
49 /**
50 * In-memory collection that will hold the associations between a host name
51 * and an array of InetAddress instances.
52 */
53 private Map<String, InetAddress[]> dnsMap;
54
55 /**
56 * Builds a DNS resolver that will resolve the host names against a
57 * collection held in-memory.
58 */
59 public InMemoryDnsResolver() {
60 dnsMap = new ConcurrentHashMap<String, InetAddress[]>();
61 }
62
63 /**
64 * Associates the given array of IP addresses to the given host in this DNS overrider.
65 * The IP addresses are assumed to be already resolved.
66 *
67 * @param host
68 * The host name to be associated with the given IP.
69 * @param ips
70 * array of IP addresses to be resolved by this DNS overrider to the given
71 * host name.
72 */
73 public void add(final String host, final InetAddress... ips) {
74 if (host == null) {
75 throw new IllegalArgumentException("Host name may not be null");
76 }
77 if (ips == null) {
78 throw new IllegalArgumentException("Array of IP addresses may not be null");
79 }
80 dnsMap.put(host, ips);
81 }
82
83 /**
84 * {@inheritDoc}
85 */
86 public InetAddress[] resolve(String host) throws UnknownHostException {
87 InetAddress[] resolvedAddresses = dnsMap.get(host);
88 if (log.isInfoEnabled()) {
89 log.info("Resolving " + host + " to " + Arrays.deepToString(resolvedAddresses));
90 }
91 if(resolvedAddresses == null){
92 throw new UnknownHostException(host + " cannot be resolved");
93 }
94 return resolvedAddresses;
95 }
96
97 }