0N/A/*
3909N/A * Copyright (c) 2002, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * A simple name service based on an in-memory HashMap.
0N/A */
0N/Aimport java.net.UnknownHostException;
0N/Aimport java.net.InetAddress;
0N/Aimport sun.net.spi.nameservice.*;
0N/Aimport java.util.*;
0N/A
0N/Apublic final class SimpleNameService implements NameService {
0N/A
0N/A private static LinkedHashMap hosts = new LinkedHashMap();
0N/A
0N/A private static String addrToString(byte addr[]) {
0N/A return Byte.toString(addr[0]) + "." +
0N/A Byte.toString(addr[1]) + "." +
0N/A Byte.toString(addr[2]) + "." +
0N/A Byte.toString(addr[3]);
0N/A }
0N/A
0N/A // ------------
0N/A
0N/A public static void put(String host, String addr) {
0N/A hosts.put(host, addr);
0N/A }
0N/A
0N/A public static void put(String host, byte addr[]) {
0N/A hosts.put(host, addrToString(addr));
0N/A }
0N/A
0N/A public static void remove(String host) {
0N/A hosts.remove(host);
0N/A }
0N/A
0N/A public static int entries () {
0N/A return hosts.size();
0N/A }
0N/A
0N/A public static int lookupCalls() {
0N/A return lookupCalls;
0N/A }
0N/A
0N/A static int lookupCalls = 0;
0N/A
0N/A // ------------
0N/A
0N/A public SimpleNameService() throws Exception {
0N/A }
0N/A
0N/A public InetAddress[] lookupAllHostAddr(String host) throws UnknownHostException {
0N/A
0N/A lookupCalls ++;
0N/A
0N/A String value = (String)hosts.get(host);
0N/A if (value == null) {
0N/A throw new UnknownHostException(host);
0N/A }
0N/A StringTokenizer st = new StringTokenizer(value, ".");
0N/A byte addr[] = new byte[4];
0N/A for (int i=0; i<4; i++) {
0N/A addr[i] = (byte)Integer.parseInt(st.nextToken());
0N/A }
0N/A InetAddress[] res = new InetAddress[1];
0N/A res[0] = InetAddress.getByAddress(host, addr);
0N/A return res;
0N/A }
0N/A
0N/A public String getHostByAddr(byte[] addr) throws UnknownHostException {
0N/A String addrString = addrToString(addr);
0N/A Iterator i = hosts.keySet().iterator();
0N/A while (i.hasNext()) {
0N/A String host = (String)i.next();
0N/A String value = (String)hosts.get(host);
0N/A if (value.equals(addrString)) {
0N/A return host;
0N/A }
0N/A }
0N/A throw new UnknownHostException();
0N/A }
0N/A}