提交 41702346 编写于 作者: W weijun

6969683: Generify ResolverConfiguration codes

Reviewed-by: alanb, chegar
上级 e80093ab
/* /*
* Copyright (c) 2000, 2004, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -132,7 +132,7 @@ public class DnsContextFactory implements InitialContextFactory { ...@@ -132,7 +132,7 @@ public class DnsContextFactory implements InitialContextFactory {
throw new ConfigurationException("DNS pseudo-URL required"); throw new ConfigurationException("DNS pseudo-URL required");
} }
List servers = new ArrayList(); List<String> servers = new ArrayList<>();
for (int i = 0; i < urls.length; i++) { for (int i = 0; i < urls.length; i++) {
String server = urls[i].getHost(); String server = urls[i].getHost();
...@@ -142,7 +142,7 @@ public class DnsContextFactory implements InitialContextFactory { ...@@ -142,7 +142,7 @@ public class DnsContextFactory implements InitialContextFactory {
// No server or port given, so look to underlying platform. // No server or port given, so look to underlying platform.
// ResolverConfiguration does some limited caching, so the // ResolverConfiguration does some limited caching, so the
// following is reasonably efficient even if called rapid-fire. // following is reasonably efficient even if called rapid-fire.
List platformServers = List<String> platformServers =
ResolverConfiguration.open().nameservers(); ResolverConfiguration.open().nameservers();
if (!platformServers.isEmpty()) { if (!platformServers.isEmpty()) {
servers.addAll(platformServers); servers.addAll(platformServers);
...@@ -157,8 +157,7 @@ public class DnsContextFactory implements InitialContextFactory { ...@@ -157,8 +157,7 @@ public class DnsContextFactory implements InitialContextFactory {
? server ? server
: server + ":" + port); : server + ":" + port);
} }
return (String[]) servers.toArray( return servers.toArray(new String[servers.size()]);
new String[servers.size()]);
} }
/* /*
......
/* /*
* Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -26,7 +26,6 @@ ...@@ -26,7 +26,6 @@
package sun.net.dns; package sun.net.dns;
import java.util.List; import java.util.List;
import java.io.IOException;
/** /**
* The configuration of the client resolver. * The configuration of the client resolver.
...@@ -68,7 +67,7 @@ public abstract class ResolverConfiguration { ...@@ -68,7 +67,7 @@ public abstract class ResolverConfiguration {
* *
* @return list of domain names * @return list of domain names
*/ */
public abstract List searchlist(); public abstract List<String> searchlist();
/** /**
* Returns a list of name servers used for host name lookup. * Returns a list of name servers used for host name lookup.
...@@ -78,7 +77,7 @@ public abstract class ResolverConfiguration { ...@@ -78,7 +77,7 @@ public abstract class ResolverConfiguration {
* *
* @return list of the name servers * @return list of the name servers
*/ */
public abstract List nameservers(); public abstract List<String> nameservers();
/** /**
......
/* /*
* Copyright (c) 2000, 2005, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -45,20 +45,21 @@ import sun.security.action.*; ...@@ -45,20 +45,21 @@ import sun.security.action.*;
public final class DNSNameService implements NameService { public final class DNSNameService implements NameService {
// List of domains specified by property // List of domains specified by property
private LinkedList domainList = null; private LinkedList<String> domainList = null;
// JNDI-DNS URL for name servers specified via property // JNDI-DNS URL for name servers specified via property
private String nameProviderUrl = null; private String nameProviderUrl = null;
// Per-thread soft cache of the last temporary context // Per-thread soft cache of the last temporary context
private static ThreadLocal contextRef = new ThreadLocal(); private static ThreadLocal<SoftReference<ThreadContext>> contextRef =
new ThreadLocal<>();
// Simple class to encapsulate the temporary context // Simple class to encapsulate the temporary context
private static class ThreadContext { private static class ThreadContext {
private DirContext dirCtxt; private DirContext dirCtxt;
private List nsList; private List<String> nsList;
public ThreadContext(DirContext dirCtxt, List nsList) { public ThreadContext(DirContext dirCtxt, List<String> nsList) {
this.dirCtxt = dirCtxt; this.dirCtxt = dirCtxt;
this.nsList = nsList; this.nsList = nsList;
} }
...@@ -67,16 +68,16 @@ public final class DNSNameService implements NameService { ...@@ -67,16 +68,16 @@ public final class DNSNameService implements NameService {
return dirCtxt; return dirCtxt;
} }
public List nameservers() { public List<String> nameservers() {
return nsList; return nsList;
} }
} }
// Returns a per-thread DirContext // Returns a per-thread DirContext
private DirContext getTemporaryContext() throws NamingException { private DirContext getTemporaryContext() throws NamingException {
SoftReference ref = (SoftReference)contextRef.get(); SoftReference<ThreadContext> ref = contextRef.get();
ThreadContext thrCtxt = null; ThreadContext thrCtxt = null;
List nsList = null; List<String> nsList = null;
// if no property specified we need to obtain the list of servers // if no property specified we need to obtain the list of servers
// //
...@@ -87,7 +88,7 @@ public final class DNSNameService implements NameService { ...@@ -87,7 +88,7 @@ public final class DNSNameService implements NameService {
// specified then we need to check if the DNS configuration // specified then we need to check if the DNS configuration
// has changed. // has changed.
// //
if ((ref != null) && ((thrCtxt = (ThreadContext)ref.get()) != null)) { if ((ref != null) && ((thrCtxt = ref.get()) != null)) {
if (nameProviderUrl == null) { if (nameProviderUrl == null) {
if (!thrCtxt.nameservers().equals(nsList)) { if (!thrCtxt.nameservers().equals(nsList)) {
// DNS configuration has changed // DNS configuration has changed
...@@ -98,7 +99,7 @@ public final class DNSNameService implements NameService { ...@@ -98,7 +99,7 @@ public final class DNSNameService implements NameService {
// new thread context needs to be created // new thread context needs to be created
if (thrCtxt == null) { if (thrCtxt == null) {
final Hashtable<String,Object> env = new Hashtable<String,Object>(); final Hashtable<String,Object> env = new Hashtable<>();
env.put("java.naming.factory.initial", env.put("java.naming.factory.initial",
"com.sun.jndi.dns.DnsContextFactory"); "com.sun.jndi.dns.DnsContextFactory");
...@@ -119,10 +120,9 @@ public final class DNSNameService implements NameService { ...@@ -119,10 +120,9 @@ public final class DNSNameService implements NameService {
// //
DirContext dirCtxt; DirContext dirCtxt;
try { try {
dirCtxt = (DirContext) dirCtxt = java.security.AccessController.doPrivileged(
java.security.AccessController.doPrivileged( new java.security.PrivilegedExceptionAction<DirContext>() {
new java.security.PrivilegedExceptionAction() { public DirContext run() throws NamingException {
public Object run() throws NamingException {
// Create the DNS context using NamingManager rather than using // Create the DNS context using NamingManager rather than using
// the initial context constructor. This avoids having the initial // the initial context constructor. This avoids having the initial
// context constructor call itself. // context constructor call itself.
...@@ -130,7 +130,7 @@ public final class DNSNameService implements NameService { ...@@ -130,7 +130,7 @@ public final class DNSNameService implements NameService {
if (!(ctx instanceof DirContext)) { if (!(ctx instanceof DirContext)) {
return null; // cannot create a DNS context return null; // cannot create a DNS context
} }
return ctx; return (DirContext)ctx;
} }
}); });
} catch (java.security.PrivilegedActionException pae) { } catch (java.security.PrivilegedActionException pae) {
...@@ -161,18 +161,18 @@ public final class DNSNameService implements NameService { ...@@ -161,18 +161,18 @@ public final class DNSNameService implements NameService {
* *
* @throws UnknownHostException if lookup fails or other error. * @throws UnknownHostException if lookup fails or other error.
*/ */
private ArrayList resolve(final DirContext ctx, final String name, final String[] ids, private ArrayList<String> resolve(final DirContext ctx, final String name,
int depth) throws UnknownHostException final String[] ids, int depth)
throws UnknownHostException
{ {
ArrayList results = new ArrayList(); ArrayList<String> results = new ArrayList<>();
Attributes attrs; Attributes attrs;
// do the query // do the query
try { try {
attrs = (Attributes) attrs = java.security.AccessController.doPrivileged(
java.security.AccessController.doPrivileged( new java.security.PrivilegedExceptionAction<Attributes>() {
new java.security.PrivilegedExceptionAction() { public Attributes run() throws NamingException {
public Object run() throws NamingException {
return ctx.getAttributes(name, ids); return ctx.getAttributes(name, ids);
} }
}); });
...@@ -181,7 +181,7 @@ public final class DNSNameService implements NameService { ...@@ -181,7 +181,7 @@ public final class DNSNameService implements NameService {
} }
// non-requested type returned so enumeration is empty // non-requested type returned so enumeration is empty
NamingEnumeration ne = attrs.getAll(); NamingEnumeration<? extends Attribute> ne = attrs.getAll();
if (!ne.hasMoreElements()) { if (!ne.hasMoreElements()) {
throw new UnknownHostException("DNS record not found"); throw new UnknownHostException("DNS record not found");
} }
...@@ -190,7 +190,7 @@ public final class DNSNameService implements NameService { ...@@ -190,7 +190,7 @@ public final class DNSNameService implements NameService {
UnknownHostException uhe = null; UnknownHostException uhe = null;
try { try {
while (ne.hasMoreElements()) { while (ne.hasMoreElements()) {
Attribute attr = (Attribute)ne.next(); Attribute attr = ne.next();
String attrID = attr.getID(); String attrID = attr.getID();
for (NamingEnumeration e = attr.getAll(); e.hasMoreElements();) { for (NamingEnumeration e = attr.getAll(); e.hasMoreElements();) {
...@@ -251,13 +251,12 @@ public final class DNSNameService implements NameService { ...@@ -251,13 +251,12 @@ public final class DNSNameService implements NameService {
// no property specified so check host DNS resolver configured // no property specified so check host DNS resolver configured
// with at least one nameserver in dotted notation. // with at least one nameserver in dotted notation.
// //
List nsList = ResolverConfiguration.open().nameservers(); List<String> nsList = ResolverConfiguration.open().nameservers();
if (nsList.size() == 0) if (nsList.isEmpty()) {
throw new RuntimeException("no nameservers provided"); throw new RuntimeException("no nameservers provided");
}
boolean found = false; boolean found = false;
Iterator i = nsList.iterator(); for (String addr: nsList) {
while (i.hasNext()) {
String addr = (String)i.next();
if (IPAddressUtil.isIPv4LiteralAddress(addr) || if (IPAddressUtil.isIPv4LiteralAddress(addr) ||
IPAddressUtil.isIPv6LiteralAddress(addr)) { IPAddressUtil.isIPv6LiteralAddress(addr)) {
found = true; found = true;
...@@ -308,8 +307,8 @@ public final class DNSNameService implements NameService { ...@@ -308,8 +307,8 @@ public final class DNSNameService implements NameService {
// suffix if the list has one entry. // suffix if the list has one entry.
if (results == null) { if (results == null) {
List searchList = null; List<String> searchList = null;
Iterator i; Iterator<String> i;
boolean usingSearchList = false; boolean usingSearchList = false;
if (domainList != null) { if (domainList != null) {
...@@ -324,7 +323,7 @@ public final class DNSNameService implements NameService { ...@@ -324,7 +323,7 @@ public final class DNSNameService implements NameService {
// iterator through each domain suffix // iterator through each domain suffix
while (i.hasNext()) { while (i.hasNext()) {
String parentDomain = (String)i.next(); String parentDomain = i.next();
int start = 0; int start = 0;
while ((start = parentDomain.indexOf(".")) != -1 while ((start = parentDomain.indexOf(".")) != -1
&& start < parentDomain.length() -1) { && start < parentDomain.length() -1) {
...@@ -407,7 +406,7 @@ public final class DNSNameService implements NameService { ...@@ -407,7 +406,7 @@ public final class DNSNameService implements NameService {
String literalip = ""; String literalip = "";
String[] ids = { "PTR" }; String[] ids = { "PTR" };
DirContext ctx; DirContext ctx;
ArrayList results = null; ArrayList<String> results = null;
try { try {
ctx = getTemporaryContext(); ctx = getTemporaryContext();
} catch (NamingException nx) { } catch (NamingException nx) {
...@@ -420,7 +419,7 @@ public final class DNSNameService implements NameService { ...@@ -420,7 +419,7 @@ public final class DNSNameService implements NameService {
literalip += "IN-ADDR.ARPA."; literalip += "IN-ADDR.ARPA.";
results = resolve(ctx, literalip, ids, 0); results = resolve(ctx, literalip, ids, 0);
host = (String)results.get(0); host = results.get(0);
} else if (addr.length == 16) { // IPv6 Address } else if (addr.length == 16) { // IPv6 Address
/** /**
* Because RFC 3152 changed the root domain name for reverse * Because RFC 3152 changed the root domain name for reverse
...@@ -437,7 +436,7 @@ public final class DNSNameService implements NameService { ...@@ -437,7 +436,7 @@ public final class DNSNameService implements NameService {
try { try {
results = resolve(ctx, ip6lit, ids, 0); results = resolve(ctx, ip6lit, ids, 0);
host = (String)results.get(0); host = results.get(0);
} catch (UnknownHostException e) { } catch (UnknownHostException e) {
host = null; host = null;
} }
...@@ -445,7 +444,7 @@ public final class DNSNameService implements NameService { ...@@ -445,7 +444,7 @@ public final class DNSNameService implements NameService {
// IP6.ARPA lookup failed, let's try the older IP6.INT // IP6.ARPA lookup failed, let's try the older IP6.INT
ip6lit = literalip + "IP6.INT."; ip6lit = literalip + "IP6.INT.";
results = resolve(ctx, ip6lit, ids, 0); results = resolve(ctx, ip6lit, ids, 0);
host = (String)results.get(0); host = results.get(0);
} }
} }
} catch (Exception e) { } catch (Exception e) {
...@@ -478,11 +477,10 @@ public final class DNSNameService implements NameService { ...@@ -478,11 +477,10 @@ public final class DNSNameService implements NameService {
* @return String containing the JNDI-DNS provider URL * @return String containing the JNDI-DNS provider URL
* corresponding to the supplied List of nameservers. * corresponding to the supplied List of nameservers.
*/ */
private static String createProviderURL(List nsList) { private static String createProviderURL(List<String> nsList) {
Iterator i = nsList.iterator();
StringBuffer sb = new StringBuffer(); StringBuffer sb = new StringBuffer();
while (i.hasNext()) { for (String s: nsList) {
appendIfLiteralAddress((String)i.next(), sb); appendIfLiteralAddress(s, sb);
} }
return sb.toString(); return sb.toString();
} }
......
/* /*
* Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -56,8 +56,11 @@ public class ResolverConfigurationImpl ...@@ -56,8 +56,11 @@ public class ResolverConfigurationImpl
// Parse /etc/resolv.conf to get the values for a particular // Parse /etc/resolv.conf to get the values for a particular
// keyword. // keyword.
// //
private LinkedList resolvconf(String keyword, int maxperkeyword, int maxkeywords) { private LinkedList<String> resolvconf(String keyword,
LinkedList ll = new LinkedList(); int maxperkeyword,
int maxkeywords)
{
LinkedList<String> ll = new LinkedList<>();
try { try {
BufferedReader in = BufferedReader in =
...@@ -99,8 +102,8 @@ public class ResolverConfigurationImpl ...@@ -99,8 +102,8 @@ public class ResolverConfigurationImpl
return ll; return ll;
} }
private LinkedList searchlist; private LinkedList<String> searchlist;
private LinkedList nameservers; private LinkedList<String> nameservers;
// Load DNS configuration from OS // Load DNS configuration from OS
...@@ -118,9 +121,9 @@ public class ResolverConfigurationImpl ...@@ -118,9 +121,9 @@ public class ResolverConfigurationImpl
// get the name servers from /etc/resolv.conf // get the name servers from /etc/resolv.conf
nameservers = nameservers =
(LinkedList)java.security.AccessController.doPrivileged( java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction() { new java.security.PrivilegedAction<LinkedList<String>>() {
public Object run() { public LinkedList<String> run() {
// typically MAXNS is 3 but we've picked 5 here // typically MAXNS is 3 but we've picked 5 here
// to allow for additional servers if required. // to allow for additional servers if required.
return resolvconf("nameserver", 1, 5); return resolvconf("nameserver", 1, 5);
...@@ -137,15 +140,15 @@ public class ResolverConfigurationImpl ...@@ -137,15 +140,15 @@ public class ResolverConfigurationImpl
// obtain search list or local domain // obtain search list or local domain
private LinkedList getSearchList() { private LinkedList<String> getSearchList() {
LinkedList sl; LinkedList<String> sl;
// first try the search keyword in /etc/resolv.conf // first try the search keyword in /etc/resolv.conf
sl = (LinkedList)java.security.AccessController.doPrivileged( sl = java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction() { new java.security.PrivilegedAction<LinkedList<String>>() {
public Object run() { public LinkedList<String> run() {
LinkedList ll; LinkedList ll;
// first try search keyword (max 6 domains) // first try search keyword (max 6 domains)
...@@ -177,10 +180,10 @@ public class ResolverConfigurationImpl ...@@ -177,10 +180,10 @@ public class ResolverConfigurationImpl
// try domain keyword in /etc/resolv.conf // try domain keyword in /etc/resolv.conf
sl = (LinkedList)java.security.AccessController.doPrivileged( sl = java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction() { new java.security.PrivilegedAction<LinkedList<String>>() {
public Object run() { public LinkedList<String> run() {
LinkedList ll; LinkedList<String> ll;
ll = resolvconf("domain", 1, 1); ll = resolvconf("domain", 1, 1);
if (ll.size() > 0) { if (ll.size() > 0) {
...@@ -197,7 +200,7 @@ public class ResolverConfigurationImpl ...@@ -197,7 +200,7 @@ public class ResolverConfigurationImpl
// no local domain so try fallback (RPC) domain or // no local domain so try fallback (RPC) domain or
// hostname // hostname
sl = new LinkedList(); sl = new LinkedList<>();
String domain = fallbackDomain0(); String domain = fallbackDomain0();
if (domain != null && domain.length() > 0) { if (domain != null && domain.length() > 0) {
sl.add(domain); sl.add(domain);
...@@ -213,7 +216,7 @@ public class ResolverConfigurationImpl ...@@ -213,7 +216,7 @@ public class ResolverConfigurationImpl
opts = new OptionsImpl(); opts = new OptionsImpl();
} }
public List searchlist() { public List<String> searchlist() {
synchronized (lock) { synchronized (lock) {
loadConfig(); loadConfig();
...@@ -222,7 +225,7 @@ public class ResolverConfigurationImpl ...@@ -222,7 +225,7 @@ public class ResolverConfigurationImpl
} }
} }
public List nameservers() { public List<String> nameservers() {
synchronized (lock) { synchronized (lock) {
loadConfig(); loadConfig();
......
/* /*
* Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -28,7 +28,6 @@ package sun.net.dns; ...@@ -28,7 +28,6 @@ package sun.net.dns;
import java.util.List; import java.util.List;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import java.io.IOException;
/* /*
* An implementation of sun.net.ResolverConfiguration for Windows. * An implementation of sun.net.ResolverConfiguration for Windows.
...@@ -63,8 +62,8 @@ public class ResolverConfigurationImpl ...@@ -63,8 +62,8 @@ public class ResolverConfigurationImpl
// Parse string that consists of token delimited by space or commas // Parse string that consists of token delimited by space or commas
// and return LinkedHashMap // and return LinkedHashMap
private LinkedList stringToList(String str) { private LinkedList<String> stringToList(String str) {
LinkedList ll = new LinkedList(); LinkedList<String> ll = new LinkedList<>();
// comma and space are valid delimites // comma and space are valid delimites
StringTokenizer st = new StringTokenizer(str, ", "); StringTokenizer st = new StringTokenizer(str, ", ");
...@@ -112,7 +111,7 @@ public class ResolverConfigurationImpl ...@@ -112,7 +111,7 @@ public class ResolverConfigurationImpl
opts = new OptionsImpl(); opts = new OptionsImpl();
} }
public List searchlist() { public List<String> searchlist() {
synchronized (lock) { synchronized (lock) {
loadConfig(); loadConfig();
...@@ -121,7 +120,7 @@ public class ResolverConfigurationImpl ...@@ -121,7 +120,7 @@ public class ResolverConfigurationImpl
} }
} }
public List nameservers() { public List<String> nameservers() {
synchronized (lock) { synchronized (lock) {
loadConfig(); loadConfig();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册