src/share/jaf_classes/javax/activation/FileTypeMap.java

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 494
2fcd3ddb57a6
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

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

mercurial