1 /* 2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package javax.activation; 27 28 import java.io.File; 29 import java.util.Map; 30 import java.util.WeakHashMap; 31 32 /** 33 * The FileTypeMap is an abstract class that provides a data typing 34 * interface for files. Implementations of this class will 35 * implement the getContentType methods which will derive a content 36 * type from a file name or a File object. FileTypeMaps could use any 37 * scheme to determine the data type, from examining the file extension 38 * of a file (like the MimetypesFileTypeMap) to opening the file and 39 * trying to derive its type from the contents of the file. The 40 * FileDataSource class uses the default FileTypeMap (a MimetypesFileTypeMap 41 * unless changed) to determine the content type of files. 42 * 43 * @see javax.activation.FileTypeMap 44 * @see javax.activation.FileDataSource 45 * @see javax.activation.MimetypesFileTypeMap 46 * 47 * @since 1.6 48 */ 49 50 public abstract class FileTypeMap { 51 52 private static FileTypeMap defaultMap = null; 53 private static Map<ClassLoader,FileTypeMap> map = 54 new WeakHashMap<ClassLoader,FileTypeMap>(); 55 56 /** 57 * The default constructor. 58 */ 59 public FileTypeMap() { 60 super(); 61 } 62 63 /** 64 * Return the type of the file object. This method should 65 * always return a valid MIME type. 66 * 67 * @param file A file to be typed. 68 * @return The content type. 69 */ 70 abstract public String getContentType(File file); 71 72 /** 73 * Return the type of the file passed in. This method should 74 * always return a valid MIME type. 75 * 76 * @param filename the pathname of the file. 77 * @return The content type. 78 */ 79 abstract public String getContentType(String filename); 80 81 /** 82 * Sets the default FileTypeMap for the system. This instance 83 * will be returned to callers of getDefaultFileTypeMap. 84 * 85 * @param fileTypeMap The FileTypeMap. 86 * @exception SecurityException if the caller doesn't have permission 87 * to change the default 88 */ 89 public static synchronized void setDefaultFileTypeMap(FileTypeMap fileTypeMap) { 90 SecurityManager security = System.getSecurityManager(); 91 if (security != null) { 92 try { 93 // if it's ok with the SecurityManager, it's ok with me... 94 security.checkSetFactory(); 95 } catch (SecurityException ex) { 96 // otherwise, we also allow it if this code and the 97 // factory come from the same (non-system) class loader (e.g., 98 // the JAF classes were loaded with the applet classes). 99 ClassLoader cl = FileTypeMap.class.getClassLoader(); 100 if (cl == null || cl.getParent() == null || 101 cl != fileTypeMap.getClass().getClassLoader()) 102 throw ex; 103 } 104 } 105 // remove any per-thread-context-class-loader FileTypeMap 106 map.remove(SecuritySupport.getContextClassLoader()); 107 defaultMap = fileTypeMap; 108 } 109 110 /** 111 * Return the default FileTypeMap for the system. 112 * If setDefaultFileTypeMap was called, return 113 * that instance, otherwise return an instance of 114 * <code>MimetypesFileTypeMap</code>. 115 * 116 * @return The default FileTypeMap 117 * @see javax.activation.FileTypeMap#setDefaultFileTypeMap 118 */ 119 public static synchronized FileTypeMap getDefaultFileTypeMap() { 120 if (defaultMap != null) 121 return defaultMap; 122 123 // fetch per-thread-context-class-loader default 124 ClassLoader tccl = SecuritySupport.getContextClassLoader(); 125 FileTypeMap def = map.get(tccl); 126 if (def == null) { 127 def = new MimetypesFileTypeMap(); 128 map.put(tccl, def); 129 } 130 return def; 131 } 132 }