blob: 36d445a8cb7e9d4f69fcd2cfd82e981dafd9baa3 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2002-2006 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/*
25 * A simple name service based on an in-memory HashMap.
26 */
27import java.net.UnknownHostException;
28import java.net.InetAddress;
29import sun.net.spi.nameservice.*;
30import java.util.*;
31
32public 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}