6856856: NPE in HTTP protocol handler logging
authorjccollet
Mon, 06 Jul 2009 15:13:48 +0200
changeset 3217 07b65d4b6227
parent 3216 ee2bae5e28cf
child 3218 855a6257b729
child 3465 96252ea98a6a
6856856: NPE in HTTP protocol handler logging Summary: Fixed the NPE and Moved the java.util.logging dependency to a single class and used reflection to make it a soft one. Reviewed-by: chegar
jdk/src/share/classes/sun/net/www/http/HttpCapture.java
jdk/src/share/classes/sun/net/www/http/HttpClient.java
jdk/src/share/classes/sun/net/www/protocol/http/HttpLogFormatter.java
jdk/src/share/classes/sun/net/www/protocol/http/HttpURLConnection.java
--- a/jdk/src/share/classes/sun/net/www/http/HttpCapture.java	Fri Jul 03 16:26:43 2009 -0700
+++ b/jdk/src/share/classes/sun/net/www/http/HttpCapture.java	Mon Jul 06 15:13:48 2009 +0200
@@ -25,6 +25,8 @@
 
 package sun.net.www.http;
 import java.io.*;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
 import java.util.ArrayList;
 import java.util.logging.Level;
 import java.util.logging.Logger;
@@ -60,6 +62,76 @@
     private static boolean initialized = false;
     private static volatile ArrayList<Pattern> patterns = null;
     private static volatile ArrayList<String> capFiles = null;
+    /* Logging is done in an ugly way so that it does not require the presence
+     * the java.util.logging package. If the Logger class is not available, then
+     * logging is turned off. This is for helping the modularization effort.
+     */
+    private static Object logger = null;
+    private static boolean logging = false;
+
+    static {
+        Class cl;
+        try {
+            cl = Class.forName("java.util.logging.Logger");
+        } catch (ClassNotFoundException ex) {
+            cl = null;
+        }
+        if (cl != null) {
+            try {
+                Method m = cl.getMethod("getLogger", String.class);
+                logger = m.invoke(null, "sun.net.www.protocol.http.HttpURLConnection");
+                logging = true;
+            } catch (NoSuchMethodException noSuchMethodException) {
+            } catch (SecurityException securityException) {
+            } catch (IllegalAccessException illegalAccessException) {
+            } catch (IllegalArgumentException illegalArgumentException) {
+            } catch (InvocationTargetException invocationTargetException) {
+            }
+        }
+    }
+
+    public static void fine(String s) {
+        if (logging) {
+            ((Logger)logger).fine(s);
+        }
+    }
+
+    public static void finer(String s) {
+        if (logging) {
+            ((Logger)logger).finer(s);
+        }
+    }
+
+    public static void finest(String s) {
+        if (logging) {
+            ((Logger)logger).finest(s);
+        }
+    }
+
+    public static void severe(String s) {
+        if (logging) {
+            ((Logger)logger).finest(s);
+        }
+    }
+
+    public static void info(String s) {
+        if (logging) {
+            ((Logger)logger).info(s);
+        }
+    }
+
+    public static void warning(String s) {
+        if (logging) {
+            ((Logger)logger).warning(s);
+        }
+    }
+
+    public static boolean isLoggable(String level) {
+        if (!logging) {
+            return false;
+        }
+        return ((Logger)logger).isLoggable(Level.parse(level));
+    }
 
     private static synchronized void init() {
         initialized = true;
--- a/jdk/src/share/classes/sun/net/www/http/HttpClient.java	Fri Jul 03 16:26:43 2009 -0700
+++ b/jdk/src/share/classes/sun/net/www/http/HttpClient.java	Mon Jul 06 15:13:48 2009 +0200
@@ -28,8 +28,6 @@
 import java.io.*;
 import java.net.*;
 import java.util.Locale;
-import java.util.logging.Level;
-import java.util.logging.Logger;
 import sun.net.NetworkClient;
 import sun.net.ProgressSource;
 import sun.net.www.MessageHeader;
@@ -66,10 +64,6 @@
     /** Default port number for http daemons. REMIND: make these private */
     static final int    httpPortNumber = 80;
 
-    // Use same logger as HttpURLConnection since we want to combine both event
-    // streams into one single HTTP log
-    private static Logger logger = Logger.getLogger("sun.net.www.protocol.http.HttpURLConnection");
-
     /** return default port number (subclasses may override) */
     protected int getDefaultPort () { return httpPortNumber; }
 
@@ -810,8 +804,8 @@
 
             if (isKeepingAlive())   {
                 // Wrap KeepAliveStream if keep alive is enabled.
-                if (logger.isLoggable(Level.FINEST)) {
-                    logger.finest("KeepAlive stream used: " + url);
+                if (HttpCapture.isLoggable("FINEST")) {
+                    HttpCapture.finest("KeepAlive stream used: " + url);
                 }
                 serverInput = new KeepAliveStream(serverInput, pi, cl, this);
                 failedOnce = false;
--- a/jdk/src/share/classes/sun/net/www/protocol/http/HttpLogFormatter.java	Fri Jul 03 16:26:43 2009 -0700
+++ b/jdk/src/share/classes/sun/net/www/protocol/http/HttpLogFormatter.java	Mon Jul 06 15:13:48 2009 +0200
@@ -49,8 +49,7 @@
 
     @Override
     public String format(LogRecord record) {
-        if (!"sun.net.www.protocol.http.HttpURLConnection".equalsIgnoreCase(record.getSourceClassName())
-                && !"sun.net.www.http.HttpClient".equalsIgnoreCase(record.getSourceClassName())) {
+        if (!"sun.net.www.http.HttpCapture".equalsIgnoreCase(record.getSourceClassName())) {
             // Don't change format for stuff that doesn't concern us
             return super.format(record);
         }
--- a/jdk/src/share/classes/sun/net/www/protocol/http/HttpURLConnection.java	Fri Jul 03 16:26:43 2009 -0700
+++ b/jdk/src/share/classes/sun/net/www/protocol/http/HttpURLConnection.java	Mon Jul 06 15:13:48 2009 +0200
@@ -51,14 +51,13 @@
 import java.util.Locale;
 import java.util.StringTokenizer;
 import java.util.Iterator;
-import java.util.logging.Level;
-import java.util.logging.Logger;
 import sun.net.*;
 import sun.net.www.*;
 import sun.net.www.http.HttpClient;
 import sun.net.www.http.PosterOutputStream;
 import sun.net.www.http.ChunkedInputStream;
 import sun.net.www.http.ChunkedOutputStream;
+import sun.net.www.http.HttpCapture;
 import java.text.SimpleDateFormat;
 import java.util.TimeZone;
 import java.net.MalformedURLException;
@@ -71,8 +70,6 @@
 
 public class HttpURLConnection extends java.net.HttpURLConnection {
 
-    private static Logger logger = Logger.getLogger("sun.net.www.protocol.http.HttpURLConnection");
-
     static String HTTP_CONNECT = "CONNECT";
 
     static final String version;
@@ -304,14 +301,14 @@
         return java.security.AccessController.doPrivileged(
             new java.security.PrivilegedAction<PasswordAuthentication>() {
                 public PasswordAuthentication run() {
-                    if (logger.isLoggable(Level.FINEST)) {
-                        logger.finest("Requesting Authentication: host =" + host + " url = " + url);
+                    if (HttpCapture.isLoggable("FINEST")) {
+                        HttpCapture.finest("Requesting Authentication: host =" + host + " url = " + url);
                     }
                     PasswordAuthentication pass = Authenticator.requestPasswordAuthentication(
                         host, addr, port, protocol,
                         prompt, scheme, url, authType);
-                    if (pass != null && logger.isLoggable(Level.FINEST)) {
-                        logger.finest("Authentication returned: " + pass.toString());
+                    if (HttpCapture.isLoggable("FINEST")) {
+                        HttpCapture.finest("Authentication returned: " + (pass != null ? pass.toString() : "null"));
                     }
                     return pass;
                 }
@@ -466,8 +463,8 @@
 
             setRequests=true;
         }
-        if (logger.isLoggable(Level.FINE)) {
-            logger.fine(requests.toString());
+        if (HttpCapture.isLoggable("FINE")) {
+            HttpCapture.fine(requests.toString());
         }
         http.writeRequests(requests, poster);
         if (ps.checkError()) {
@@ -723,11 +720,9 @@
                         && !(cachedResponse instanceof SecureCacheResponse)) {
                         cachedResponse = null;
                     }
-                    if (logger.isLoggable(Level.FINEST)) {
-                        logger.finest("Cache Request for " + uri + " / " + getRequestMethod());
-                        if (cachedResponse != null) {
-                            logger.finest("From cache: "+cachedResponse.toString());
-                        }
+                    if (HttpCapture.isLoggable("FINEST")) {
+                        HttpCapture.finest("Cache Request for " + uri + " / " + getRequestMethod());
+                        HttpCapture.finest("From cache: " + (cachedResponse != null ? cachedResponse.toString() : "null"));
                     }
                     if (cachedResponse != null) {
                         cachedHeaders = mapToMessageHeader(cachedResponse.getHeaders());
@@ -766,8 +761,8 @@
                              });
                 if (sel != null) {
                     URI uri = sun.net.www.ParseUtil.toURI(url);
-                    if (logger.isLoggable(Level.FINEST)) {
-                        logger.finest("ProxySelector Request for " + uri);
+                    if (HttpCapture.isLoggable("FINEST")) {
+                        HttpCapture.finest("ProxySelector Request for " + uri);
                     }
                     Iterator<Proxy> it = sel.select(uri).iterator();
                     Proxy p;
@@ -783,9 +778,9 @@
                                 http = getNewHttpClient(url, p, connectTimeout, false);
                                 http.setReadTimeout(readTimeout);
                             }
-                            if (logger.isLoggable(Level.FINEST)) {
+                            if (HttpCapture.isLoggable("FINEST")) {
                                 if (p != null) {
-                                    logger.finest("Proxy used: " + p.toString());
+                                    HttpCapture.finest("Proxy used: " + p.toString());
                                 }
                             }
                             break;
@@ -1015,15 +1010,15 @@
 
             URI uri = ParseUtil.toURI(url);
             if (uri != null) {
-                if (logger.isLoggable(Level.FINEST)) {
-                    logger.finest("CookieHandler request for " + uri);
+                if (HttpCapture.isLoggable("FINEST")) {
+                    HttpCapture.finest("CookieHandler request for " + uri);
                 }
                 Map<String, List<String>> cookies
                     = cookieHandler.get(
                         uri, requests.getHeaders(EXCLUDE_HEADERS));
                 if (!cookies.isEmpty()) {
-                    if (logger.isLoggable(Level.FINEST)) {
-                        logger.finest("Cookies retrieved: " + cookies.toString());
+                    if (HttpCapture.isLoggable("FINEST")) {
+                        HttpCapture.finest("Cookies retrieved: " + cookies.toString());
                     }
                     for (Map.Entry<String, List<String>> entry :
                              cookies.entrySet()) {
@@ -1154,8 +1149,8 @@
                     writeRequests();
                 }
                 http.parseHTTP(responses, pi, this);
-                if (logger.isLoggable(Level.FINE)) {
-                    logger.fine(responses.toString());
+                if (HttpCapture.isLoggable("FINE")) {
+                    HttpCapture.fine(responses.toString());
                 }
                 inputStream = http.getInputStream();
 
@@ -1599,8 +1594,8 @@
                 http.parseHTTP(responses, null, this);
 
                 /* Log the response to the CONNECT */
-                if (logger.isLoggable(Level.FINE)) {
-                    logger.fine(responses.toString());
+                if (HttpCapture.isLoggable("FINE")) {
+                    HttpCapture.fine(responses.toString());
                 }
 
                 statusLine = responses.getValue(0);
@@ -1727,8 +1722,8 @@
         setPreemptiveProxyAuthentication(requests);
 
          /* Log the CONNECT request */
-        if (logger.isLoggable(Level.FINE)) {
-            logger.fine(requests.toString());
+        if (HttpCapture.isLoggable("FINE")) {
+            HttpCapture.fine(requests.toString());
         }
 
         http.writeRequests(requests, null);
@@ -1872,8 +1867,8 @@
                 }
             }
         }
-        if (logger.isLoggable(Level.FINER)) {
-            logger.finer("Proxy Authentication for " + authhdr.toString() +" returned " + ret.toString());
+        if (HttpCapture.isLoggable("FINER")) {
+            HttpCapture.finer("Proxy Authentication for " + authhdr.toString() +" returned " + (ret != null ? ret.toString() : "null"));
         }
         return ret;
     }
@@ -2002,8 +1997,8 @@
                 }
             }
         }
-        if (logger.isLoggable(Level.FINER)) {
-            logger.finer("Server Authentication for " + authhdr.toString() +" returned " + ret.toString());
+        if (HttpCapture.isLoggable("FINER")) {
+            HttpCapture.finer("Server Authentication for " + authhdr.toString() +" returned " + (ret != null ? ret.toString() : "null"));
         }
         return ret;
     }
@@ -2078,8 +2073,8 @@
         if (streaming()) {
             throw new HttpRetryException (RETRY_MSG3, stat, loc);
         }
-        if (logger.isLoggable(Level.FINE)) {
-            logger.fine("Redirected from " + url + " to " + locUrl);
+        if (HttpCapture.isLoggable("FINE")) {
+            HttpCapture.fine("Redirected from " + url + " to " + locUrl);
         }
 
         // clear out old response headers!!!!