jaxws/src/jdk.xml.bind/share/classes/com/sun/istack/internal/tools/DefaultAuthenticator.java
changeset 43852 93a527059d8a
parent 25871 b80b84e87032
--- a/jaxws/src/jdk.xml.bind/share/classes/com/sun/istack/internal/tools/DefaultAuthenticator.java	Fri Feb 10 00:39:51 2017 -0800
+++ b/jaxws/src/jdk.xml.bind/share/classes/com/sun/istack/internal/tools/DefaultAuthenticator.java	Thu Feb 16 13:14:39 2017 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2017, 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
@@ -33,6 +33,7 @@
 import java.io.InputStreamReader;
 import java.io.UnsupportedEncodingException;
 import java.lang.reflect.Field;
+import java.lang.reflect.Method;
 import java.net.Authenticator;
 import java.net.Authenticator.RequestorType;
 import java.net.MalformedURLException;
@@ -42,6 +43,8 @@
 import java.net.URLEncoder;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.logging.Level;
@@ -56,11 +59,12 @@
  */
 public class DefaultAuthenticator extends Authenticator {
 
+    private static final Logger LOGGER = Logger.getLogger(DefaultAuthenticator.class.getName());
     private static DefaultAuthenticator instance;
     private static Authenticator systemAuthenticator = getCurrentAuthenticator();
     private String proxyUser;
     private String proxyPasswd;
-    private final List<AuthInfo> authInfo = new ArrayList<AuthInfo>();
+    private final List<AuthInfo> authInfo = new ArrayList<>();
     private static int counter = 0;
 
     DefaultAuthenticator() {
@@ -145,10 +149,7 @@
                 fi = new FileInputStream(f);
                 is = new InputStreamReader(fi, "UTF-8");
                 in = new BufferedReader(is);
-            } catch (UnsupportedEncodingException e) {
-                listener.onError(e, locator);
-                return;
-            } catch (FileNotFoundException e) {
+            } catch (UnsupportedEncodingException | FileNotFoundException e) {
                 listener.onError(e, locator);
                 return;
             }
@@ -170,7 +171,7 @@
                 }
             } catch (IOException e) {
                 listener.onError(e, locator);
-                Logger.getLogger(DefaultAuthenticator.class.getName()).log(Level.SEVERE, e.getMessage(), e);
+                LOGGER.log(Level.SEVERE, e.getMessage(), e);
             }
         } finally {
             try {
@@ -184,7 +185,7 @@
                     fi.close();
                 }
             } catch (IOException ex) {
-                Logger.getLogger(DefaultAuthenticator.class.getName()).log(Level.SEVERE, null, ex);
+                LOGGER.log(Level.SEVERE, null, ex);
             }
         }
     }
@@ -225,6 +226,29 @@
     }
 
     static Authenticator getCurrentAuthenticator() {
+        try {
+            return AccessController.doPrivileged(new PrivilegedExceptionAction<Authenticator>() {
+                @Override
+                public Authenticator run() throws Exception {
+                    Method method = Authenticator.class.getMethod("getDefault");
+                    return (Authenticator) method.invoke(null);
+                }
+
+            });
+        } catch (PrivilegedActionException pae) {
+            if (LOGGER.isLoggable(Level.FINE)) {
+                LOGGER.log(Level.FINE, null, pae);
+            }
+            Exception ex = pae.getException();
+            if (!(ex instanceof NoSuchMethodException)) {
+                // if Authenticator.getDefault has not been found,
+                // we likely didn't get through sec, so return null
+                // and don't care about JDK version we're on
+                return null;
+            }
+            // or we're on JDK <9, so let's continue the old way...
+        }
+
         final Field f = getTheAuthenticator();
         if (f == null) {
             return null;
@@ -239,7 +263,7 @@
                 }
             });
             return (Authenticator) f.get(null);
-        } catch (Exception ex) {
+        } catch (IllegalAccessException | IllegalArgumentException ex) {
             return null;
         } finally {
             AccessController.doPrivileged(new PrivilegedAction<Void>() {
@@ -255,7 +279,7 @@
     private static Field getTheAuthenticator() {
         try {
             return Authenticator.class.getDeclaredField("theAuthenticator");
-        } catch (Exception ex) {
+        } catch (NoSuchFieldException | SecurityException ex) {
             return null;
         }
     }
@@ -277,7 +301,7 @@
         @Override
         public void onError(Exception e, Locator loc) {
             System.err.println(getLocationString(loc) + ": " + e.getMessage());
-            Logger.getLogger(DefaultAuthenticator.class.getName()).log(Level.SEVERE, e.getMessage(), e);
+            LOGGER.log(Level.SEVERE, e.getMessage(), e);
         }
 
         private String getLocationString(Locator l) {