resolverimpl.java

来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 232 行

JAVA
232
字号
/*
 * $Id: ResolverImpl.java,v 1.3 2004/01/02 08:57:07 epr Exp $
 */
package org.jnode.net.ipv4.util;

import java.net.UnknownHostException;
import java.util.Hashtable;
import java.util.Iterator;

import org.jnode.driver.net.NetworkException;
import org.jnode.net.ProtocolAddress;
import org.jnode.net.Resolver;
import org.jnode.net.ipv4.IPv4Address;
import org.xbill.DNS.ExtendedResolver;
import org.xbill.DNS.Lookup;
import org.xbill.DNS.Record;
import org.xbill.DNS.SimpleResolver;
import org.xbill.DNS.TextParseException;

public class ResolverImpl implements Resolver {
	private static ExtendedResolver resolver;
	private static Hashtable resolvers;

	private static Hashtable hosts;

	private static Resolver res = null;

	static {
		hosts = new Hashtable();

		// this will have to come from hosts file
		final String localhost = "localhost";
		ProtocolAddress[] protocolAddresses = new ProtocolAddress[] { new IPv4Address("127.0.0.1")};

		hosts.put(localhost, protocolAddresses);
	}

	private ResolverImpl() {
	}

	/**
	 * Singleton
	 * 
	 * @return the singleton of the resolver
	 */

	public static Resolver getInstance() {
		if (res == null) {
			res = new ResolverImpl();
		}

		return res;
	}

	/**
	 * List all the dns servers
	 */

	public static void printDnsServers() {
		if (resolvers == null) {
			return;
		}

		Iterator iterator = resolvers.keySet().iterator();
		String dnsServer;

		while (iterator.hasNext()) {
			dnsServer = (String) iterator.next();

			System.out.println(dnsServer);
		}
	}

	/**
	 * Add a dns server
	 * 
	 * @param _dnsserver
	 * @throws NetworkException
	 */

	public static void addDnsServer(ProtocolAddress _dnsserver) throws NetworkException {
		System.out.println("System.getProperty(\"os.name\") " + System.getProperty("os.name"));
		System.setProperty("os.name", "JNode");

		if (resolver == null) {
			try {
				//        resolver = (ExtendedResolver) Lookup.getDefaultResolver();

				if (resolver == null) {
					resolver = new ExtendedResolver();
					Lookup.setDefaultResolver(resolver);
				}
			} catch (UnknownHostException e) {
				throw new NetworkException("Can't add DnsServer", e);
			}
		}

		try {
			String key = _dnsserver.toString();
			if (resolvers == null) {
				resolvers = new Hashtable();
			}

			if (!resolvers.containsKey(key)) {
				SimpleResolver simpleResolver = new SimpleResolver(key);

				resolver.addResolver(simpleResolver);
				resolvers.put(key, simpleResolver);
			}
		} catch (UnknownHostException e) {
			throw new NetworkException("Can't add DnsServer", e);
		}
	}

	/**
	 * removes a dns server
	 * 
	 * @param _dnsserver
	 */

	public static void removeDnsServer(ProtocolAddress _dnsserver) {

		if (resolver == null) {
			return;
		}

		String key = _dnsserver.toString();
		if (resolvers.containsKey(key)) {
			SimpleResolver simpleResolver = (SimpleResolver) resolvers.remove(key);
			resolver.deleteResolver(simpleResolver);
		}
	}

	/**
	 * Get from hosts file.
	 * 
	 * @param _hostname
	 * @return
	 */

	private ProtocolAddress[] getFromHostsFile(String _hostname) {
		// this should check for changes of the host file

		return (ProtocolAddress[]) hosts.get(_hostname);
	}

	/**
	 * Gets the address(es) of the given hostname.
	 * 
	 * @param hostname
	 * @return All addresses of the given hostname. The returned array is at least 1 address long.
	 * @throws java.net.UnknownHostException
	 */

	public ProtocolAddress[] getByName(String hostname) throws UnknownHostException {
		ProtocolAddress[] protocolAddresses = null;

		System.out.println("hh0");

		protocolAddresses = getFromHostsFile(hostname);

		if (protocolAddresses != null) {
			return protocolAddresses;
		}

		System.out.println("hh1");

		Lookup lookup = null;
		Lookup.setDefaultResolver(resolver);

		try {
			lookup = new Lookup(hostname);
		} catch (TextParseException e) {
			throw new UnknownHostException(hostname);
		}
		System.out.println("hh3");

		lookup.run();
		System.out.println("hh4");

		if (lookup.getResult() == Lookup.SUCCESSFUL) {
			Record[] records = lookup.getAnswers();

			System.out.println("hh5");

			protocolAddresses = new ProtocolAddress[records.length];

			for (int i = 0; i < records.length; i++) {
				Record record = records[i];
				protocolAddresses[i] = new IPv4Address(record.rdataToString());

				System.out.println(record.getName() + " " + record.rdataToString());
			}
		} else {
			System.out.println("lookup.getErrorString() " + lookup.getErrorString());
		}

		System.out.println("hh6");
		return protocolAddresses;
	}

	/**
	 * Gets the hostname of the given address.
	 * 
	 * @param address
	 * @return All hostnames of the given hostname. The returned array is at least 1 hostname long.
	 * @throws java.net.UnknownHostException
	 */

	public String[] getByAddress(ProtocolAddress address) throws UnknownHostException {
		return new String[0]; //To change body of implemented methods use Options | File Templates.
	}

	public static void main(String[] args) {
		ProtocolAddress protocolAddress = new IPv4Address("192.168.1.32");
		try {
			ResolverImpl.addDnsServer(protocolAddress);
		} catch (NetworkException e) {

		}
		try {
			ProtocolAddress[] re = ResolverImpl.getInstance().getByName("www.jnode.ddd");
			if (re.length > 0) {
				System.out.println(re[0]);
			}
		} catch (UnknownHostException e) {

		}

	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?