6882384: Update http protocol handler to use PlatformLogger
authorchegar
Tue, 22 Sep 2009 14:49:06 +0100
changeset 3866 34cd368bd2dd
parent 3865 cc2ca3d07bbb
child 3868 d02ee9795d57
6882384: Update http protocol handler to use PlatformLogger Reviewed-by: jccollet, alanb
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
jdk/src/share/classes/sun/net/www/protocol/http/NTLMAuthenticationProxy.java
jdk/src/share/classes/sun/net/www/protocol/http/NegotiateAuthentication.java
jdk/src/share/classes/sun/util/logging/PlatformLogger.java
--- a/jdk/src/share/classes/sun/net/www/http/HttpCapture.java	Tue Sep 22 14:42:07 2009 +0100
+++ b/jdk/src/share/classes/sun/net/www/http/HttpCapture.java	Tue Sep 22 14:49:06 2009 +0100
@@ -24,14 +24,12 @@
  */
 
 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;
+import java.util.regex.*;
 import sun.net.NetProperties;
-import java.util.regex.*;
+import sun.util.logging.PlatformLogger;
 
 /**
  * Main class of the HTTP traffic capture tool.
@@ -62,76 +60,6 @@
     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;
@@ -187,7 +115,7 @@
             out = new BufferedWriter(new FileWriter(file, true));
             out.write("URL: " + url + "\n");
         } catch (IOException ex) {
-            Logger.getLogger(HttpCapture.class.getName()).log(Level.SEVERE, null, ex);
+            PlatformLogger.getLogger(HttpCapture.class.getName()).severe(null, ex);
         }
     }
 
--- a/jdk/src/share/classes/sun/net/www/http/HttpClient.java	Tue Sep 22 14:42:07 2009 +0100
+++ b/jdk/src/share/classes/sun/net/www/http/HttpClient.java	Tue Sep 22 14:49:06 2009 +0100
@@ -35,6 +35,7 @@
 import sun.net.www.MeteredStream;
 import sun.net.www.ParseUtil;
 import sun.net.www.protocol.http.HttpURLConnection;
+import sun.util.logging.PlatformLogger;
 
 /**
  * @author Herb Jellinek
@@ -804,8 +805,9 @@
 
             if (isKeepingAlive())   {
                 // Wrap KeepAliveStream if keep alive is enabled.
-                if (HttpCapture.isLoggable("FINEST")) {
-                    HttpCapture.finest("KeepAlive stream used: " + url);
+                PlatformLogger logger = HttpURLConnection.getHttpLogger();
+                if (logger.isLoggable(PlatformLogger.FINEST)) {
+                    logger.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	Tue Sep 22 14:42:07 2009 +0100
+++ b/jdk/src/share/classes/sun/net/www/protocol/http/HttpLogFormatter.java	Tue Sep 22 14:49:06 2009 +0100
@@ -49,8 +49,10 @@
 
     @Override
     public String format(LogRecord record) {
-        if (!"sun.net.www.http.HttpCapture".equalsIgnoreCase(record.getSourceClassName())) {
-            // Don't change format for stuff that doesn't concern us
+        String sourceClassName = record.getSourceClassName();
+        if (sourceClassName == null ||
+            !(sourceClassName.startsWith("sun.net.www.protocol.http") ||
+              sourceClassName.startsWith("sun.net.www.http"))) {
             return super.format(record);
         }
         String src = record.getMessage();
--- a/jdk/src/share/classes/sun/net/www/protocol/http/HttpURLConnection.java	Tue Sep 22 14:42:07 2009 +0100
+++ b/jdk/src/share/classes/sun/net/www/protocol/http/HttpURLConnection.java	Tue Sep 22 14:49:06 2009 +0100
@@ -57,7 +57,7 @@
 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 sun.util.logging.PlatformLogger;
 import java.text.SimpleDateFormat;
 import java.util.TimeZone;
 import java.net.MalformedURLException;
@@ -292,6 +292,10 @@
     private int connectTimeout = -1;
     private int readTimeout = -1;
 
+    /* Logging support */
+    private static final PlatformLogger logger =
+            PlatformLogger.getLogger("sun.net.www.protocol.http.HttpURLConnection");
+
     /*
      * privileged request password authentication
      *
@@ -309,20 +313,25 @@
         return java.security.AccessController.doPrivileged(
             new java.security.PrivilegedAction<PasswordAuthentication>() {
                 public PasswordAuthentication run() {
-                    if (HttpCapture.isLoggable("FINEST")) {
-                        HttpCapture.finest("Requesting Authentication: host =" + host + " url = " + url);
+                    if (logger.isLoggable(PlatformLogger.FINEST)) {
+                        logger.finest("Requesting Authentication: host =" + host + " url = " + url);
                     }
                     PasswordAuthentication pass = Authenticator.requestPasswordAuthentication(
                         host, addr, port, protocol,
                         prompt, scheme, url, authType);
-                    if (HttpCapture.isLoggable("FINEST")) {
-                        HttpCapture.finest("Authentication returned: " + (pass != null ? pass.toString() : "null"));
+                    if (logger.isLoggable(PlatformLogger.FINEST)) {
+                        logger.finest("Authentication returned: " + (pass != null ? pass.toString() : "null"));
                     }
                     return pass;
                 }
             });
     }
 
+    /* Logging support */
+    public static PlatformLogger getHttpLogger() {
+        return logger;
+    }
+
     /*
      * checks the validity of http message header and throws
      * IllegalArgumentException if invalid.
@@ -471,8 +480,8 @@
 
             setRequests=true;
         }
-        if (HttpCapture.isLoggable("FINE")) {
-            HttpCapture.fine(requests.toString());
+        if (logger.isLoggable(PlatformLogger.FINE)) {
+            logger.fine(requests.toString());
         }
         http.writeRequests(requests, poster);
         if (ps.checkError()) {
@@ -736,9 +745,9 @@
                         && !(cachedResponse instanceof SecureCacheResponse)) {
                         cachedResponse = null;
                     }
-                    if (HttpCapture.isLoggable("FINEST")) {
-                        HttpCapture.finest("Cache Request for " + uri + " / " + getRequestMethod());
-                        HttpCapture.finest("From cache: " + (cachedResponse != null ? cachedResponse.toString() : "null"));
+                    if (logger.isLoggable(PlatformLogger.FINEST)) {
+                        logger.finest("Cache Request for " + uri + " / " + getRequestMethod());
+                        logger.finest("From cache: " + (cachedResponse != null ? cachedResponse.toString() : "null"));
                     }
                     if (cachedResponse != null) {
                         cachedHeaders = mapToMessageHeader(cachedResponse.getHeaders());
@@ -777,8 +786,8 @@
                              });
                 if (sel != null) {
                     URI uri = sun.net.www.ParseUtil.toURI(url);
-                    if (HttpCapture.isLoggable("FINEST")) {
-                        HttpCapture.finest("ProxySelector Request for " + uri);
+                    if (logger.isLoggable(PlatformLogger.FINEST)) {
+                        logger.finest("ProxySelector Request for " + uri);
                     }
                     Iterator<Proxy> it = sel.select(uri).iterator();
                     Proxy p;
@@ -794,9 +803,9 @@
                                 http = getNewHttpClient(url, p, connectTimeout, false);
                                 http.setReadTimeout(readTimeout);
                             }
-                            if (HttpCapture.isLoggable("FINEST")) {
+                            if (logger.isLoggable(PlatformLogger.FINEST)) {
                                 if (p != null) {
-                                    HttpCapture.finest("Proxy used: " + p.toString());
+                                    logger.finest("Proxy used: " + p.toString());
                                 }
                             }
                             break;
@@ -1026,15 +1035,15 @@
 
             URI uri = ParseUtil.toURI(url);
             if (uri != null) {
-                if (HttpCapture.isLoggable("FINEST")) {
-                    HttpCapture.finest("CookieHandler request for " + uri);
+                if (logger.isLoggable(PlatformLogger.FINEST)) {
+                    logger.finest("CookieHandler request for " + uri);
                 }
                 Map<String, List<String>> cookies
                     = cookieHandler.get(
                         uri, requests.getHeaders(EXCLUDE_HEADERS));
                 if (!cookies.isEmpty()) {
-                    if (HttpCapture.isLoggable("FINEST")) {
-                        HttpCapture.finest("Cookies retrieved: " + cookies.toString());
+                    if (logger.isLoggable(PlatformLogger.FINEST)) {
+                        logger.finest("Cookies retrieved: " + cookies.toString());
                     }
                     for (Map.Entry<String, List<String>> entry :
                              cookies.entrySet()) {
@@ -1165,8 +1174,8 @@
                     writeRequests();
                 }
                 http.parseHTTP(responses, pi, this);
-                if (HttpCapture.isLoggable("FINE")) {
-                    HttpCapture.fine(responses.toString());
+                if (logger.isLoggable(PlatformLogger.FINE)) {
+                    logger.fine(responses.toString());
                 }
                 inputStream = http.getInputStream();
 
@@ -1610,8 +1619,8 @@
                 http.parseHTTP(responses, null, this);
 
                 /* Log the response to the CONNECT */
-                if (HttpCapture.isLoggable("FINE")) {
-                    HttpCapture.fine(responses.toString());
+                if (logger.isLoggable(PlatformLogger.FINE)) {
+                    logger.fine(responses.toString());
                 }
 
                 statusLine = responses.getValue(0);
@@ -1738,8 +1747,8 @@
         setPreemptiveProxyAuthentication(requests);
 
          /* Log the CONNECT request */
-        if (HttpCapture.isLoggable("FINE")) {
-            HttpCapture.fine(requests.toString());
+        if (logger.isLoggable(PlatformLogger.FINE)) {
+            logger.fine(requests.toString());
         }
 
         http.writeRequests(requests, null);
@@ -1852,7 +1861,7 @@
                         }
                         a = null;
                         if (tryTransparentNTLMProxy) {
-                            HttpCapture.finest("Trying Transparent NTLM authentication");
+                            logger.finest("Trying Transparent NTLM authentication");
                         } else {
                             a = privilegedRequestPasswordAuthentication(
                                                 host, null, port, url.getProtocol(),
@@ -1880,7 +1889,7 @@
                     ret = new NegotiateAuthentication(new HttpCallerInfo(authhdr.getHttpCallerInfo(), "Kerberos"));
                     break;
                 case UNKNOWN:
-                    HttpCapture.finest("Unknown/Unsupported authentication scheme: " + scheme);
+                    logger.finest("Unknown/Unsupported authentication scheme: " + scheme);
                 default:
                     throw new AssertionError("should not reach here");
                 }
@@ -1906,8 +1915,8 @@
                 }
             }
         }
-        if (HttpCapture.isLoggable("FINER")) {
-            HttpCapture.finer("Proxy Authentication for " + authhdr.toString() +" returned " + (ret != null ? ret.toString() : "null"));
+        if (logger.isLoggable(PlatformLogger.FINER)) {
+            logger.finer("Proxy Authentication for " + authhdr.toString() +" returned " + (ret != null ? ret.toString() : "null"));
         }
         return ret;
     }
@@ -2004,7 +2013,7 @@
                         }
                         a = null;
                         if (tryTransparentNTLMServer) {
-                            HttpCapture.finest("Trying Transparent NTLM authentication");
+                            logger.finest("Trying Transparent NTLM authentication");
                         } else {
                             a = privilegedRequestPasswordAuthentication(
                                 url.getHost(), addr, port, url.getProtocol(),
@@ -2027,7 +2036,7 @@
                     }
                     break;
                 case UNKNOWN:
-                    HttpCapture.finest("Unknown/Unsupported authentication scheme: " + scheme);
+                    logger.finest("Unknown/Unsupported authentication scheme: " + scheme);
                 default:
                     throw new AssertionError("should not reach here");
                 }
@@ -2051,8 +2060,8 @@
                 }
             }
         }
-        if (HttpCapture.isLoggable("FINER")) {
-            HttpCapture.finer("Server Authentication for " + authhdr.toString() +" returned " + (ret != null ? ret.toString() : "null"));
+        if (logger.isLoggable(PlatformLogger.FINER)) {
+            logger.finer("Server Authentication for " + authhdr.toString() +" returned " + (ret != null ? ret.toString() : "null"));
         }
         return ret;
     }
@@ -2127,8 +2136,8 @@
         if (streaming()) {
             throw new HttpRetryException (RETRY_MSG3, stat, loc);
         }
-        if (HttpCapture.isLoggable("FINE")) {
-            HttpCapture.fine("Redirected from " + url + " to " + locUrl);
+        if (logger.isLoggable(PlatformLogger.FINE)) {
+            logger.fine("Redirected from " + url + " to " + locUrl);
         }
 
         // clear out old response headers!!!!
--- a/jdk/src/share/classes/sun/net/www/protocol/http/NTLMAuthenticationProxy.java	Tue Sep 22 14:42:07 2009 +0100
+++ b/jdk/src/share/classes/sun/net/www/protocol/http/NTLMAuthenticationProxy.java	Tue Sep 22 14:49:06 2009 +0100
@@ -28,7 +28,7 @@
 import java.net.PasswordAuthentication;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Method;
-import sun.net.www.http.HttpCapture;
+import sun.util.logging.PlatformLogger;
 
 /**
  * Proxy class for loading NTLMAuthentication, so as to remove static
@@ -59,7 +59,7 @@
         try {
             return threeArgCtr.newInstance(isProxy, url, pw);
         } catch (ReflectiveOperationException roe) {
-            log(roe);
+            finest(roe);
         }
 
         return null;
@@ -72,7 +72,7 @@
         try {
             return fiveArgCtr.newInstance(isProxy, host, port, pw);
         } catch (ReflectiveOperationException roe) {
-            log(roe);
+            finest(roe);
         }
 
         return null;
@@ -86,7 +86,7 @@
         try {
             return (Boolean)method.invoke(null);
         } catch (ReflectiveOperationException roe) {
-            log(roe);
+            finest(roe);
         }
 
         return false;
@@ -116,7 +116,7 @@
                                                    fiveArg);
             }
         } catch (ClassNotFoundException cnfe) {
-            log(cnfe);
+            finest(cnfe);
         } catch (ReflectiveOperationException roe) {
             throw new AssertionError(roe);
         }
@@ -124,9 +124,8 @@
         return null;
     }
 
-    static void log(Exception e) {
-        if (HttpCapture.isLoggable("FINEST")) {
-            HttpCapture.finest("NTLMAuthenticationProxy: " + e);
-        }
+    static void finest(Exception e) {
+        PlatformLogger logger = HttpURLConnection.getHttpLogger();
+        logger.finest("NTLMAuthenticationProxy: " + e);
     }
 }
--- a/jdk/src/share/classes/sun/net/www/protocol/http/NegotiateAuthentication.java	Tue Sep 22 14:42:07 2009 +0100
+++ b/jdk/src/share/classes/sun/net/www/protocol/http/NegotiateAuthentication.java	Tue Sep 22 14:49:06 2009 +0100
@@ -30,12 +30,12 @@
 import sun.net.www.HeaderParser;
 import sun.misc.BASE64Decoder;
 import sun.misc.BASE64Encoder;
+import sun.util.logging.PlatformLogger;
 
 import java.net.URL;
 import java.io.IOException;
 import java.net.Authenticator.RequestorType;
 import java.lang.reflect.Constructor;
-import sun.net.www.http.HttpCapture;
 import static sun.net.www.protocol.http.AuthScheme.NEGOTIATE;
 import static sun.net.www.protocol.http.AuthScheme.KERBEROS;
 
@@ -258,7 +258,7 @@
             clazz = Class.forName("sun.net.www.protocol.http.NegotiatorImpl", true, null);
             c = clazz.getConstructor(HttpCallerInfo.class);
         } catch (ClassNotFoundException cnfe) {
-            log(cnfe);
+            finest(cnfe);
             throw cnfe;
         } catch (ReflectiveOperationException roe) {
             // if the class is there then something seriously wrong if
@@ -269,10 +269,10 @@
         try {
             return (Negotiator) (c.newInstance(hci));
         } catch (ReflectiveOperationException roe) {
-            log(roe);
+            finest(roe);
             Throwable t = roe.getCause();
             if (t != null && t instanceof Exception)
-                log((Exception)t);
+                finest((Exception)t);
             throw roe;
         }
     }
@@ -281,9 +281,8 @@
 
     abstract byte[] nextToken(byte[] in) throws IOException;
 
-    static void log(Exception e) {
-        if (HttpCapture.isLoggable("FINEST")) {
-            HttpCapture.finest("NegotiateAuthentication: " + e);
-        }
+    static void finest(Exception e) {
+        PlatformLogger logger = HttpURLConnection.getHttpLogger();
+        logger.finest("NegotiateAuthentication: " + e);
     }
 }
--- a/jdk/src/share/classes/sun/util/logging/PlatformLogger.java	Tue Sep 22 14:42:07 2009 +0100
+++ b/jdk/src/share/classes/sun/util/logging/PlatformLogger.java	Tue Sep 22 14:49:06 2009 +0100
@@ -29,15 +29,15 @@
 import java.lang.ref.WeakReference;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
-import java.io.File;
 import java.io.PrintStream;
 import java.io.PrintWriter;
 import java.io.StringWriter;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
 import java.text.MessageFormat;
-import java.util.logging.Logger;
-import java.util.*;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
 import sun.misc.JavaLangAccess;
 import sun.misc.SharedSecrets;
 
@@ -493,6 +493,7 @@
         private static final Method getLoggerMethod;
         private static final Method setLevelMethod;
         private static final Method getLevelMethod;
+        private static final Method isLoggableMethod;
         private static final Method logMethod;
         private static final Method logThrowMethod;
         private static final Method logParamsMethod;
@@ -505,6 +506,7 @@
             getLoggerMethod = getMethod(loggerClass, "getLogger", String.class);
             setLevelMethod = getMethod(loggerClass, "setLevel", levelClass);
             getLevelMethod = getMethod(loggerClass, "getLevel");
+            isLoggableMethod = getMethod(loggerClass, "isLoggable", levelClass);
             logMethod = getMethod(loggerClass, "log", levelClass, String.class);
             logThrowMethod = getMethod(loggerClass, "log", levelClass, String.class, Throwable.class);
             logParamsMethod = getMethod(loggerClass, "log", levelClass, String.class, Object[].class);
@@ -608,6 +610,10 @@
             levelValue = newLevel;
             invoke(setLevelMethod, javaLogger, levelObjects.get(newLevel));
         }
+
+        public boolean isLoggable(int level) {
+            return (Boolean) invoke(isLoggableMethod, javaLogger, levelObjects.get(level));
+        }
     }