6969683: Generify ResolverConfiguration codes
authorweijun
Mon, 19 Jul 2010 10:02:50 +0800
changeset 6112 b9d1b10c662c
parent 6111 48902dd9b4d7
child 6113 6b79a63f1d4b
6969683: Generify ResolverConfiguration codes Reviewed-by: alanb, chegar
jdk/src/share/classes/com/sun/jndi/dns/DnsContextFactory.java
jdk/src/share/classes/sun/net/dns/ResolverConfiguration.java
jdk/src/share/classes/sun/net/spi/nameservice/dns/DNSNameService.java
jdk/src/solaris/classes/sun/net/dns/ResolverConfigurationImpl.java
jdk/src/windows/classes/sun/net/dns/ResolverConfigurationImpl.java
--- a/jdk/src/share/classes/com/sun/jndi/dns/DnsContextFactory.java	Fri Jul 16 16:45:52 2010 -0400
+++ b/jdk/src/share/classes/com/sun/jndi/dns/DnsContextFactory.java	Mon Jul 19 10:02:50 2010 +0800
@@ -1,5 +1,5 @@
 /*
- * 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.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -132,7 +132,7 @@
             throw new ConfigurationException("DNS pseudo-URL required");
         }
 
-        List servers = new ArrayList();
+        List<String> servers = new ArrayList<>();
 
         for (int i = 0; i < urls.length; i++) {
             String server = urls[i].getHost();
@@ -142,7 +142,7 @@
                 // No server or port given, so look to underlying platform.
                 // ResolverConfiguration does some limited caching, so the
                 // following is reasonably efficient even if called rapid-fire.
-                List platformServers =
+                List<String> platformServers =
                     ResolverConfiguration.open().nameservers();
                 if (!platformServers.isEmpty()) {
                     servers.addAll(platformServers);
@@ -157,8 +157,7 @@
                         ? server
                         : server + ":" + port);
         }
-        return (String[]) servers.toArray(
-                                        new String[servers.size()]);
+        return servers.toArray(new String[servers.size()]);
     }
 
     /*
--- a/jdk/src/share/classes/sun/net/dns/ResolverConfiguration.java	Fri Jul 16 16:45:52 2010 -0400
+++ b/jdk/src/share/classes/sun/net/dns/ResolverConfiguration.java	Mon Jul 19 10:02:50 2010 +0800
@@ -1,5 +1,5 @@
 /*
- * 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.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -26,7 +26,6 @@
 package sun.net.dns;
 
 import java.util.List;
-import java.io.IOException;
 
 /**
  * The configuration of the client resolver.
@@ -68,7 +67,7 @@
      *
      * @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.
@@ -78,7 +77,7 @@
      *
      * @return list of the name servers
      */
-    public abstract List nameservers();
+    public abstract List<String> nameservers();
 
 
     /**
--- a/jdk/src/share/classes/sun/net/spi/nameservice/dns/DNSNameService.java	Fri Jul 16 16:45:52 2010 -0400
+++ b/jdk/src/share/classes/sun/net/spi/nameservice/dns/DNSNameService.java	Mon Jul 19 10:02:50 2010 +0800
@@ -1,5 +1,5 @@
 /*
- * 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.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -45,20 +45,21 @@
 public final class DNSNameService implements NameService {
 
     // List of domains specified by property
-    private LinkedList domainList = null;
+    private LinkedList<String> domainList = null;
 
     // JNDI-DNS URL for name servers specified via property
     private String nameProviderUrl = null;
 
     // 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
     private static class ThreadContext {
         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.nsList = nsList;
         }
@@ -67,16 +68,16 @@
             return dirCtxt;
         }
 
-        public List nameservers() {
+        public List<String> nameservers() {
             return nsList;
         }
     }
 
     // Returns a per-thread DirContext
     private DirContext getTemporaryContext() throws NamingException {
-        SoftReference ref = (SoftReference)contextRef.get();
+        SoftReference<ThreadContext> ref = contextRef.get();
         ThreadContext thrCtxt = null;
-        List nsList = null;
+        List<String> nsList = null;
 
         // if no property specified we need to obtain the list of servers
         //
@@ -87,7 +88,7 @@
         // specified then we need to check if the DNS configuration
         // has changed.
         //
-        if ((ref != null) && ((thrCtxt = (ThreadContext)ref.get()) != null)) {
+        if ((ref != null) && ((thrCtxt = ref.get()) != null)) {
             if (nameProviderUrl == null) {
                 if (!thrCtxt.nameservers().equals(nsList)) {
                     // DNS configuration has changed
@@ -98,7 +99,7 @@
 
         // new thread context needs to be created
         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",
                     "com.sun.jndi.dns.DnsContextFactory");
 
@@ -119,10 +120,9 @@
             //
             DirContext dirCtxt;
             try {
-                dirCtxt = (DirContext)
-                    java.security.AccessController.doPrivileged(
-                        new java.security.PrivilegedExceptionAction() {
-                            public Object run() throws NamingException {
+                dirCtxt = java.security.AccessController.doPrivileged(
+                        new java.security.PrivilegedExceptionAction<DirContext>() {
+                            public DirContext run() throws NamingException {
                                 // Create the DNS context using NamingManager rather than using
                                 // the initial context constructor. This avoids having the initial
                                 // context constructor call itself.
@@ -130,7 +130,7 @@
                                 if (!(ctx instanceof DirContext)) {
                                     return null; // cannot create a DNS context
                                 }
-                                return ctx;
+                                return (DirContext)ctx;
                             }
                     });
             } catch (java.security.PrivilegedActionException pae) {
@@ -161,18 +161,18 @@
      *
      * @throws  UnknownHostException if lookup fails or other error.
      */
-    private ArrayList resolve(final DirContext ctx, final String name, final String[] ids,
-                              int depth) throws UnknownHostException
+    private ArrayList<String> resolve(final DirContext ctx, final String name,
+                                      final String[] ids, int depth)
+            throws UnknownHostException
     {
-        ArrayList results = new ArrayList();
+        ArrayList<String> results = new ArrayList<>();
         Attributes attrs;
 
         // do the query
         try {
-            attrs = (Attributes)
-                java.security.AccessController.doPrivileged(
-                    new java.security.PrivilegedExceptionAction() {
-                        public Object run() throws NamingException {
+            attrs = java.security.AccessController.doPrivileged(
+                    new java.security.PrivilegedExceptionAction<Attributes>() {
+                        public Attributes run() throws NamingException {
                             return ctx.getAttributes(name, ids);
                         }
                 });
@@ -181,7 +181,7 @@
         }
 
         // non-requested type returned so enumeration is empty
-        NamingEnumeration ne = attrs.getAll();
+        NamingEnumeration<? extends Attribute> ne = attrs.getAll();
         if (!ne.hasMoreElements()) {
             throw new UnknownHostException("DNS record not found");
         }
@@ -190,7 +190,7 @@
         UnknownHostException uhe = null;
         try {
             while (ne.hasMoreElements()) {
-                Attribute attr = (Attribute)ne.next();
+                Attribute attr = ne.next();
                 String attrID = attr.getID();
 
                 for (NamingEnumeration e = attr.getAll(); e.hasMoreElements();) {
@@ -251,13 +251,12 @@
             // no property specified so check host DNS resolver configured
             // with at least one nameserver in dotted notation.
             //
-            List nsList = ResolverConfiguration.open().nameservers();
-            if (nsList.size() == 0)
+            List<String> nsList = ResolverConfiguration.open().nameservers();
+            if (nsList.isEmpty()) {
                 throw new RuntimeException("no nameservers provided");
+            }
             boolean found = false;
-            Iterator i = nsList.iterator();
-            while (i.hasNext()) {
-                String addr = (String)i.next();
+            for (String addr: nsList) {
                 if (IPAddressUtil.isIPv4LiteralAddress(addr) ||
                     IPAddressUtil.isIPv6LiteralAddress(addr)) {
                     found = true;
@@ -308,8 +307,8 @@
         // suffix if the list has one entry.
 
         if (results == null) {
-            List searchList = null;
-            Iterator i;
+            List<String> searchList = null;
+            Iterator<String> i;
             boolean usingSearchList = false;
 
             if (domainList != null) {
@@ -324,7 +323,7 @@
 
             // iterator through each domain suffix
             while (i.hasNext()) {
-                String parentDomain = (String)i.next();
+                String parentDomain = i.next();
                 int start = 0;
                 while ((start = parentDomain.indexOf(".")) != -1
                        && start < parentDomain.length() -1) {
@@ -407,7 +406,7 @@
             String literalip = "";
             String[] ids = { "PTR" };
             DirContext ctx;
-            ArrayList results = null;
+            ArrayList<String> results = null;
             try {
                 ctx = getTemporaryContext();
             } catch (NamingException nx) {
@@ -420,7 +419,7 @@
                 literalip += "IN-ADDR.ARPA.";
 
                 results = resolve(ctx, literalip, ids, 0);
-                host = (String)results.get(0);
+                host = results.get(0);
             } else if (addr.length == 16) { // IPv6 Address
                 /**
                  * Because RFC 3152 changed the root domain name for reverse
@@ -437,7 +436,7 @@
 
                 try {
                     results = resolve(ctx, ip6lit, ids, 0);
-                    host = (String)results.get(0);
+                    host = results.get(0);
                 } catch (UnknownHostException e) {
                     host = null;
                 }
@@ -445,7 +444,7 @@
                     // IP6.ARPA lookup failed, let's try the older IP6.INT
                     ip6lit = literalip + "IP6.INT.";
                     results = resolve(ctx, ip6lit, ids, 0);
-                    host = (String)results.get(0);
+                    host = results.get(0);
                 }
             }
         } catch (Exception e) {
@@ -478,11 +477,10 @@
      * @return String containing the JNDI-DNS provider URL
      *         corresponding to the supplied List of nameservers.
      */
-    private static String createProviderURL(List nsList) {
-        Iterator i = nsList.iterator();
+    private static String createProviderURL(List<String> nsList) {
         StringBuffer sb = new StringBuffer();
-        while (i.hasNext()) {
-            appendIfLiteralAddress((String)i.next(), sb);
+        for (String s: nsList) {
+            appendIfLiteralAddress(s, sb);
         }
         return sb.toString();
     }
--- a/jdk/src/solaris/classes/sun/net/dns/ResolverConfigurationImpl.java	Fri Jul 16 16:45:52 2010 -0400
+++ b/jdk/src/solaris/classes/sun/net/dns/ResolverConfigurationImpl.java	Mon Jul 19 10:02:50 2010 +0800
@@ -1,5 +1,5 @@
 /*
- * 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.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -56,8 +56,11 @@
     // Parse /etc/resolv.conf to get the values for a particular
     // keyword.
     //
-    private LinkedList resolvconf(String keyword, int maxperkeyword, int maxkeywords) {
-        LinkedList ll = new LinkedList();
+    private LinkedList<String> resolvconf(String keyword,
+                                          int maxperkeyword,
+                                          int maxkeywords)
+    {
+        LinkedList<String> ll = new LinkedList<>();
 
         try {
             BufferedReader in =
@@ -99,8 +102,8 @@
         return ll;
     }
 
-    private LinkedList searchlist;
-    private LinkedList nameservers;
+    private LinkedList<String> searchlist;
+    private LinkedList<String> nameservers;
 
 
     // Load DNS configuration from OS
@@ -118,9 +121,9 @@
 
         // get the name servers from /etc/resolv.conf
         nameservers =
-            (LinkedList)java.security.AccessController.doPrivileged(
-                new java.security.PrivilegedAction() {
-                    public Object run() {
+            java.security.AccessController.doPrivileged(
+                new java.security.PrivilegedAction<LinkedList<String>>() {
+                    public LinkedList<String> run() {
                         // typically MAXNS is 3 but we've picked 5 here
                         // to allow for additional servers if required.
                         return resolvconf("nameserver", 1, 5);
@@ -137,15 +140,15 @@
 
     // 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
 
-        sl = (LinkedList)java.security.AccessController.doPrivileged(
-                 new java.security.PrivilegedAction() {
-                    public Object run() {
+        sl = java.security.AccessController.doPrivileged(
+                 new java.security.PrivilegedAction<LinkedList<String>>() {
+                    public LinkedList<String> run() {
                         LinkedList ll;
 
                         // first try search keyword (max 6 domains)
@@ -177,10 +180,10 @@
 
         // try domain keyword in /etc/resolv.conf
 
-        sl = (LinkedList)java.security.AccessController.doPrivileged(
-                 new java.security.PrivilegedAction() {
-                    public Object run() {
-                        LinkedList ll;
+        sl = java.security.AccessController.doPrivileged(
+                 new java.security.PrivilegedAction<LinkedList<String>>() {
+                    public LinkedList<String> run() {
+                        LinkedList<String> ll;
 
                         ll = resolvconf("domain", 1, 1);
                         if (ll.size() > 0) {
@@ -197,7 +200,7 @@
         // no local domain so try fallback (RPC) domain or
         // hostname
 
-        sl = new LinkedList();
+        sl = new LinkedList<>();
         String domain = fallbackDomain0();
         if (domain != null && domain.length() > 0) {
             sl.add(domain);
@@ -213,7 +216,7 @@
         opts = new OptionsImpl();
     }
 
-    public List searchlist() {
+    public List<String> searchlist() {
         synchronized (lock) {
             loadConfig();
 
@@ -222,7 +225,7 @@
         }
     }
 
-    public List nameservers() {
+    public List<String> nameservers() {
         synchronized (lock) {
             loadConfig();
 
--- a/jdk/src/windows/classes/sun/net/dns/ResolverConfigurationImpl.java	Fri Jul 16 16:45:52 2010 -0400
+++ b/jdk/src/windows/classes/sun/net/dns/ResolverConfigurationImpl.java	Mon Jul 19 10:02:50 2010 +0800
@@ -1,5 +1,5 @@
 /*
- * 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.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -28,7 +28,6 @@
 import java.util.List;
 import java.util.LinkedList;
 import java.util.StringTokenizer;
-import java.io.IOException;
 
 /*
  * An implementation of sun.net.ResolverConfiguration for Windows.
@@ -63,8 +62,8 @@
 
     // Parse string that consists of token delimited by space or commas
     // and return LinkedHashMap
-    private LinkedList stringToList(String str) {
-        LinkedList ll = new LinkedList();
+    private LinkedList<String> stringToList(String str) {
+        LinkedList<String> ll = new LinkedList<>();
 
         // comma and space are valid delimites
         StringTokenizer st = new StringTokenizer(str, ", ");
@@ -112,7 +111,7 @@
         opts = new OptionsImpl();
     }
 
-    public List searchlist() {
+    public List<String> searchlist() {
         synchronized (lock) {
             loadConfig();
 
@@ -121,7 +120,7 @@
         }
     }
 
-    public List nameservers() {
+    public List<String> nameservers() {
         synchronized (lock) {
             loadConfig();