aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package javax.activation; aoqi@0: aoqi@0: import java.io.File; aoqi@0: import java.util.Map; aoqi@0: import java.util.WeakHashMap; aoqi@0: aoqi@0: /** aoqi@0: * The FileTypeMap is an abstract class that provides a data typing aoqi@0: * interface for files. Implementations of this class will aoqi@0: * implement the getContentType methods which will derive a content aoqi@0: * type from a file name or a File object. FileTypeMaps could use any aoqi@0: * scheme to determine the data type, from examining the file extension aoqi@0: * of a file (like the MimetypesFileTypeMap) to opening the file and aoqi@0: * trying to derive its type from the contents of the file. The aoqi@0: * FileDataSource class uses the default FileTypeMap (a MimetypesFileTypeMap aoqi@0: * unless changed) to determine the content type of files. aoqi@0: * aoqi@0: * @see javax.activation.FileTypeMap aoqi@0: * @see javax.activation.FileDataSource aoqi@0: * @see javax.activation.MimetypesFileTypeMap aoqi@0: * aoqi@0: * @since 1.6 aoqi@0: */ aoqi@0: aoqi@0: public abstract class FileTypeMap { aoqi@0: aoqi@0: private static FileTypeMap defaultMap = null; aoqi@0: private static Map map = aoqi@0: new WeakHashMap(); aoqi@0: aoqi@0: /** aoqi@0: * The default constructor. aoqi@0: */ aoqi@0: public FileTypeMap() { aoqi@0: super(); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Return the type of the file object. This method should aoqi@0: * always return a valid MIME type. aoqi@0: * aoqi@0: * @param file A file to be typed. aoqi@0: * @return The content type. aoqi@0: */ aoqi@0: abstract public String getContentType(File file); aoqi@0: aoqi@0: /** aoqi@0: * Return the type of the file passed in. This method should aoqi@0: * always return a valid MIME type. aoqi@0: * aoqi@0: * @param filename the pathname of the file. aoqi@0: * @return The content type. aoqi@0: */ aoqi@0: abstract public String getContentType(String filename); aoqi@0: aoqi@0: /** aoqi@0: * Sets the default FileTypeMap for the system. This instance aoqi@0: * will be returned to callers of getDefaultFileTypeMap. aoqi@0: * aoqi@0: * @param fileTypeMap The FileTypeMap. aoqi@0: * @exception SecurityException if the caller doesn't have permission aoqi@0: * to change the default aoqi@0: */ aoqi@0: public static synchronized void setDefaultFileTypeMap(FileTypeMap fileTypeMap) { aoqi@0: SecurityManager security = System.getSecurityManager(); aoqi@0: if (security != null) { aoqi@0: try { aoqi@0: // if it's ok with the SecurityManager, it's ok with me... aoqi@0: security.checkSetFactory(); aoqi@0: } catch (SecurityException ex) { aoqi@0: // otherwise, we also allow it if this code and the aoqi@0: // factory come from the same (non-system) class loader (e.g., aoqi@0: // the JAF classes were loaded with the applet classes). aoqi@0: if (FileTypeMap.class.getClassLoader() == null || aoqi@0: FileTypeMap.class.getClassLoader() != aoqi@0: fileTypeMap.getClass().getClassLoader()) aoqi@0: throw ex; aoqi@0: } aoqi@0: } aoqi@0: // remove any per-thread-context-class-loader FileTypeMap aoqi@0: map.remove(SecuritySupport.getContextClassLoader()); aoqi@0: defaultMap = fileTypeMap; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Return the default FileTypeMap for the system. aoqi@0: * If setDefaultFileTypeMap was called, return aoqi@0: * that instance, otherwise return an instance of aoqi@0: * MimetypesFileTypeMap. aoqi@0: * aoqi@0: * @return The default FileTypeMap aoqi@0: * @see javax.activation.FileTypeMap#setDefaultFileTypeMap aoqi@0: */ aoqi@0: public static synchronized FileTypeMap getDefaultFileTypeMap() { aoqi@0: if (defaultMap != null) aoqi@0: return defaultMap; aoqi@0: aoqi@0: // fetch per-thread-context-class-loader default aoqi@0: ClassLoader tccl = SecuritySupport.getContextClassLoader(); aoqi@0: FileTypeMap def = map.get(tccl); aoqi@0: if (def == null) { aoqi@0: def = new MimetypesFileTypeMap(); aoqi@0: map.put(tccl, def); aoqi@0: } aoqi@0: return def; aoqi@0: } aoqi@0: }