changeset 17374:44bb7c7997ca

8146215: (fs) java/nio/file/Files/probeContentType/Basic.java failed frequently on Solaris-sparc with Unexpected type: text/plain Summary: Append a FileTypeDetector using java.net.FileNameMap as a fallback on all platforms Reviewed-by: alanb, rriggs, naoto
author bpb
date Mon, 08 Aug 2016 13:57:51 -0700
parents 67e8b431911d
children eebdcae9af85
files src/java.base/share/classes/java/net/URLConnection.java src/java.base/share/classes/sun/nio/fs/AbstractFileTypeDetector.java test/java/nio/file/Files/probeContentType/Basic.java
diffstat 3 files changed, 55 insertions(+), 52 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/java/net/URLConnection.java	Mon Aug 08 11:04:57 2016 -0700
+++ b/src/java.base/share/classes/java/net/URLConnection.java	Mon Aug 08 13:57:51 2016 -0700
@@ -291,12 +291,7 @@
    /**
     * @since   1.1
     */
-    private static FileNameMap fileNameMap;
-
-    /**
-     * @since 1.2.2
-     */
-    private static boolean fileNameMapLoaded = false;
+    private static volatile FileNameMap fileNameMap;
 
     /**
      * Loads filename map (a mimetable) from a data file. It will
@@ -308,18 +303,21 @@
      * @since 1.2
      * @see #setFileNameMap(java.net.FileNameMap)
      */
-    public static synchronized FileNameMap getFileNameMap() {
-        if ((fileNameMap == null) && !fileNameMapLoaded) {
-            fileNameMap = sun.net.www.MimeTable.loadTable();
-            fileNameMapLoaded = true;
+    public static FileNameMap getFileNameMap() {
+        FileNameMap map = fileNameMap;
+
+        if (map == null) {
+            fileNameMap = map = new FileNameMap() {
+                private FileNameMap internalMap =
+                    sun.net.www.MimeTable.loadTable();
+
+                public String getContentTypeFor(String fileName) {
+                    return internalMap.getContentTypeFor(fileName);
+                }
+            };
         }
 
-        return new FileNameMap() {
-            private FileNameMap map = fileNameMap;
-            public String getContentTypeFor(String fileName) {
-                return map.getContentTypeFor(fileName);
-            }
-        };
+        return map;
     }
 
     /**
--- a/src/java.base/share/classes/sun/nio/fs/AbstractFileTypeDetector.java	Mon Aug 08 11:04:57 2016 -0700
+++ b/src/java.base/share/classes/sun/nio/fs/AbstractFileTypeDetector.java	Mon Aug 08 13:57:51 2016 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2008, 2016, 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
@@ -25,6 +25,8 @@
 
 package sun.nio.fs;
 
+import java.net.FileNameMap;
+import java.net.URLConnection;
 import java.nio.file.Path;
 import java.nio.file.spi.FileTypeDetector;
 import java.util.Locale;
@@ -71,6 +73,16 @@
         if (file == null)
             throw new NullPointerException("'file' is null");
         String result = implProbeContentType(file);
+
+        // Fall back to content types property.
+        if (result == null) {
+            Path fileName = file.getFileName();
+            if (fileName != null) {
+                FileNameMap fileNameMap = URLConnection.getFileNameMap();
+                result = fileNameMap.getContentTypeFor(fileName.toString());
+            }
+        }
+
         return (result == null) ? null : parse(result);
     }
 
--- a/test/java/nio/file/Files/probeContentType/Basic.java	Mon Aug 08 11:04:57 2016 -0700
+++ b/test/java/nio/file/Files/probeContentType/Basic.java	Mon Aug 08 13:57:51 2016 -0700
@@ -22,7 +22,7 @@
  */
 
 /* @test
- * @bug 4313887 8129632 8129633 8162624
+ * @bug 4313887 8129632 8129633 8162624 8146215
  * @summary Unit test for probeContentType method
  * @library ../..
  * @build Basic SimpleFileTypeDetector
@@ -38,7 +38,6 @@
  * set of file extension to content type mappings.
  */
 public class Basic {
-
     private static final boolean IS_UNIX =
         ! System.getProperty("os.name").startsWith("Windows");
 
@@ -55,6 +54,23 @@
         return Files.createTempFile("red", ".grape");
     }
 
+    private static void checkMimeTypesFile(Path mimeTypes) {
+        if (!Files.exists(mimeTypes)) {
+            System.out.println(mimeTypes + " does not exist");
+        } else if (!Files.isReadable(mimeTypes)) {
+            System.out.println(mimeTypes + " is not readable");
+        } else {
+            System.out.println(mimeTypes + " contents:");
+            try (Stream<String> lines = Files.lines(mimeTypes)) {
+                lines.forEach(System.out::println);
+                System.out.println("");
+            } catch (IOException ioe) {
+                System.err.printf("Problem reading %s: %s%n",
+                                  mimeTypes, ioe.getMessage());
+            }
+        }
+    }
+
     private static int checkContentTypes(String expected, String actual) {
         assert expected != null;
         assert actual != null;
@@ -63,36 +79,10 @@
             if (IS_UNIX) {
                 Path userMimeTypes =
                     Paths.get(System.getProperty("user.home"), ".mime.types");
-                if (!Files.exists(userMimeTypes)) {
-                    System.out.println(userMimeTypes + " does not exist");
-                } else if (!Files.isReadable(userMimeTypes)) {
-                    System.out.println(userMimeTypes + " is not readable");
-                } else {
-                    System.out.println(userMimeTypes + " contents:");
-                    try (Stream<String> lines = Files.lines(userMimeTypes)) {
-                        lines.forEach(System.out::println);
-                        System.out.println("");
-                    } catch (IOException ioe) {
-                        System.err.println("Problem reading "
-                                           + userMimeTypes);
-                    }
-                }
+                checkMimeTypesFile(userMimeTypes);
 
                 Path etcMimeTypes = Paths.get("/etc/mime.types");
-                if (!Files.exists(etcMimeTypes)) {
-                    System.out.println(etcMimeTypes + " does not exist");
-                } else if (!Files.isReadable(etcMimeTypes)) {
-                    System.out.println(etcMimeTypes + " is not readable");
-                } else {
-                    System.out.println(etcMimeTypes + " contents:");
-                    try (Stream<String> lines = Files.lines(etcMimeTypes)) {
-                        lines.forEach(System.out::println);
-                        System.out.println("");
-                    } catch (IOException ioe) {
-                        System.err.println("Problem reading "
-                                           + etcMimeTypes);
-                    }
-                }
+                checkMimeTypesFile(etcMimeTypes);
             }
 
             System.err.println("Expected \"" + expected
@@ -108,8 +98,8 @@
     static int checkOSXContentTypes(String[] extensions, String[] expectedTypes)
         throws IOException {
         if (extensions.length != expectedTypes.length) {
-            throw new IllegalArgumentException
-                ("Parameter array lengths differ");
+            System.err.println("Parameter array lengths differ");
+            return 1;
         }
 
         int failures = 0;
@@ -157,9 +147,12 @@
         file = createGrapeFile();
         try {
             String type = Files.probeContentType(file);
-            if (type == null)
-                throw new RuntimeException("Custom file type detector not installed?");
-            failures += checkContentTypes("grape/unknown", type);
+            if (type == null) {
+                System.err.println("Custom file type detector not installed?");
+                failures++;
+            } else {
+                failures += checkContentTypes("grape/unknown", type);
+            }
         } finally {
             Files.delete(file);
         }