1 /* 2 * Copyright (c) 2002, 2006, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * A simple name service based on an in-memory HashMap. 26 */ 27 import java.net.UnknownHostException; 28 import java.net.InetAddress; 29 import sun.net.spi.nameservice.*; 30 import java.util.*; 31 32 public final class SimpleNameService implements NameService { 33 34 private static LinkedHashMap hosts = new LinkedHashMap(); 35 36 private static String addrToString(byte addr[]) { 37 return Byte.toString(addr[0]) + "." + 38 Byte.toString(addr[1]) + "." + 39 Byte.toString(addr[2]) + "." + 40 Byte.toString(addr[3]); 41 } 42 43 // ------------ 44 45 public static void put(String host, String addr) { 46 hosts.put(host, addr); 47 } 48 49 public static void put(String host, byte addr[]) { 50 hosts.put(host, addrToString(addr)); 51 } 52 53 public static void remove(String host) { 54 hosts.remove(host); 55 } 56 57 public static int entries () { 58 return hosts.size(); 59 } 60 61 public static int lookupCalls() { 62 return lookupCalls; 63 } 64 65 static int lookupCalls = 0; 66 67 // ------------ 68 69 public SimpleNameService() throws Exception { 70 } 71 72 public InetAddress[] lookupAllHostAddr(String host) throws UnknownHostException { 73 74 lookupCalls ++; 75 76 String value = (String)hosts.get(host); 77 if (value == null) { 78 throw new UnknownHostException(host); 79 } 80 StringTokenizer st = new StringTokenizer(value, "."); 81 byte addr[] = new byte[4]; 82 for (int i=0; i<4; i++) { 83 addr[i] = (byte)Integer.parseInt(st.nextToken()); 84 } 85 InetAddress[] res = new InetAddress[1]; 86 res[0] = InetAddress.getByAddress(host, addr); 87 return res; 88 } 89 90 public String getHostByAddr(byte[] addr) throws UnknownHostException { 91 String addrString = addrToString(addr); 92 Iterator i = hosts.keySet().iterator(); 93 while (i.hasNext()) { 94 String host = (String)i.next(); 95 String value = (String)hosts.get(host); 96 if (value.equals(addrString)) { 97 return host; 98 } 99 } 100 throw new UnknownHostException(); 101 } 102 }