1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaf_classes/javax/activation/FileTypeMap.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,132 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package javax.activation; 1.30 + 1.31 +import java.io.File; 1.32 +import java.util.Map; 1.33 +import java.util.WeakHashMap; 1.34 + 1.35 +/** 1.36 + * The FileTypeMap is an abstract class that provides a data typing 1.37 + * interface for files. Implementations of this class will 1.38 + * implement the getContentType methods which will derive a content 1.39 + * type from a file name or a File object. FileTypeMaps could use any 1.40 + * scheme to determine the data type, from examining the file extension 1.41 + * of a file (like the MimetypesFileTypeMap) to opening the file and 1.42 + * trying to derive its type from the contents of the file. The 1.43 + * FileDataSource class uses the default FileTypeMap (a MimetypesFileTypeMap 1.44 + * unless changed) to determine the content type of files. 1.45 + * 1.46 + * @see javax.activation.FileTypeMap 1.47 + * @see javax.activation.FileDataSource 1.48 + * @see javax.activation.MimetypesFileTypeMap 1.49 + * 1.50 + * @since 1.6 1.51 + */ 1.52 + 1.53 +public abstract class FileTypeMap { 1.54 + 1.55 + private static FileTypeMap defaultMap = null; 1.56 + private static Map<ClassLoader,FileTypeMap> map = 1.57 + new WeakHashMap<ClassLoader,FileTypeMap>(); 1.58 + 1.59 + /** 1.60 + * The default constructor. 1.61 + */ 1.62 + public FileTypeMap() { 1.63 + super(); 1.64 + } 1.65 + 1.66 + /** 1.67 + * Return the type of the file object. This method should 1.68 + * always return a valid MIME type. 1.69 + * 1.70 + * @param file A file to be typed. 1.71 + * @return The content type. 1.72 + */ 1.73 + abstract public String getContentType(File file); 1.74 + 1.75 + /** 1.76 + * Return the type of the file passed in. This method should 1.77 + * always return a valid MIME type. 1.78 + * 1.79 + * @param filename the pathname of the file. 1.80 + * @return The content type. 1.81 + */ 1.82 + abstract public String getContentType(String filename); 1.83 + 1.84 + /** 1.85 + * Sets the default FileTypeMap for the system. This instance 1.86 + * will be returned to callers of getDefaultFileTypeMap. 1.87 + * 1.88 + * @param fileTypeMap The FileTypeMap. 1.89 + * @exception SecurityException if the caller doesn't have permission 1.90 + * to change the default 1.91 + */ 1.92 + public static synchronized void setDefaultFileTypeMap(FileTypeMap fileTypeMap) { 1.93 + SecurityManager security = System.getSecurityManager(); 1.94 + if (security != null) { 1.95 + try { 1.96 + // if it's ok with the SecurityManager, it's ok with me... 1.97 + security.checkSetFactory(); 1.98 + } catch (SecurityException ex) { 1.99 + // otherwise, we also allow it if this code and the 1.100 + // factory come from the same (non-system) class loader (e.g., 1.101 + // the JAF classes were loaded with the applet classes). 1.102 + if (FileTypeMap.class.getClassLoader() == null || 1.103 + FileTypeMap.class.getClassLoader() != 1.104 + fileTypeMap.getClass().getClassLoader()) 1.105 + throw ex; 1.106 + } 1.107 + } 1.108 + // remove any per-thread-context-class-loader FileTypeMap 1.109 + map.remove(SecuritySupport.getContextClassLoader()); 1.110 + defaultMap = fileTypeMap; 1.111 + } 1.112 + 1.113 + /** 1.114 + * Return the default FileTypeMap for the system. 1.115 + * If setDefaultFileTypeMap was called, return 1.116 + * that instance, otherwise return an instance of 1.117 + * <code>MimetypesFileTypeMap</code>. 1.118 + * 1.119 + * @return The default FileTypeMap 1.120 + * @see javax.activation.FileTypeMap#setDefaultFileTypeMap 1.121 + */ 1.122 + public static synchronized FileTypeMap getDefaultFileTypeMap() { 1.123 + if (defaultMap != null) 1.124 + return defaultMap; 1.125 + 1.126 + // fetch per-thread-context-class-loader default 1.127 + ClassLoader tccl = SecuritySupport.getContextClassLoader(); 1.128 + FileTypeMap def = map.get(tccl); 1.129 + if (def == null) { 1.130 + def = new MimetypesFileTypeMap(); 1.131 + map.put(tccl, def); 1.132 + } 1.133 + return def; 1.134 + } 1.135 +}