001/* 002 * ==================================================================== 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, 014 * software distributed under the License is distributed on an 015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 016 * KIND, either express or implied. See the License for the 017 * specific language governing permissions and limitations 018 * under the License. 019 * ==================================================================== 020 * 021 * This software consists of voluntary contributions made by many 022 * individuals on behalf of the Apache Software Foundation. For more 023 * information on the Apache Software Foundation, please see 024 * <http://www.apache.org/>. 025 * 026 */ 027package org.apache.http.impl.conn; 028 029import java.net.InetAddress; 030import java.net.UnknownHostException; 031import java.util.Arrays; 032import java.util.Map; 033import java.util.concurrent.ConcurrentHashMap; 034 035import org.apache.commons.logging.Log; 036import org.apache.commons.logging.LogFactory; 037import org.apache.http.conn.DnsResolver; 038import org.apache.http.util.Args; 039 040/** 041 * In-memory {@link DnsResolver} implementation. 042 * 043 * @since 4.2 044 */ 045public class InMemoryDnsResolver implements DnsResolver { 046 047 /** Logger associated to this class. */ 048 private final Log log = LogFactory.getLog(InMemoryDnsResolver.class); 049 050 /** 051 * In-memory collection that will hold the associations between a host name 052 * and an array of InetAddress instances. 053 */ 054 private final Map<String, InetAddress[]> dnsMap; 055 056 /** 057 * Builds a DNS resolver that will resolve the host names against a 058 * collection held in-memory. 059 */ 060 public InMemoryDnsResolver() { 061 dnsMap = new ConcurrentHashMap<String, InetAddress[]>(); 062 } 063 064 /** 065 * Associates the given array of IP addresses to the given host in this DNS overrider. 066 * The IP addresses are assumed to be already resolved. 067 * 068 * @param host 069 * The host name to be associated with the given IP. 070 * @param ips 071 * array of IP addresses to be resolved by this DNS overrider to the given 072 * host name. 073 */ 074 public void add(final String host, final InetAddress... ips) { 075 Args.notNull(host, "Host name"); 076 Args.notNull(ips, "Array of IP addresses"); 077 dnsMap.put(host, ips); 078 } 079 080 /** 081 * {@inheritDoc} 082 */ 083 @Override 084 public InetAddress[] resolve(final String host) throws UnknownHostException { 085 final InetAddress[] resolvedAddresses = dnsMap.get(host); 086 if (log.isInfoEnabled()) { 087 log.info("Resolving " + host + " to " + Arrays.deepToString(resolvedAddresses)); 088 } 089 if(resolvedAddresses == null){ 090 throw new UnknownHostException(host + " cannot be resolved"); 091 } 092 return resolvedAddresses; 093 } 094 095}