1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
5 *
6 * The contents of this file are subject to the terms of either the GNU
7 * General Public License Version 2 only ("GPL") or the Common Development
8 * and Distribution License("CDDL") (collectively, the "License"). You
9 * may not use this file except in compliance with the License. You can obtain
10 * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
11 * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
12 * language governing permissions and limitations under the License.
13 *
14 * When distributing the software, include this License Header Notice in each
15 * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
16 * Sun designates this particular file as subject to the "Classpath" exception
17 * as provided by Sun in the GPL Version 2 section of the License file that
18 * accompanied this code. If applicable, add the following below the License
19 * Header, with the fields enclosed by brackets [] replaced by your own
20 * identifying information: "Portions Copyrighted [year]
21 * [name of copyright owner]"
22 *
23 * Contributor(s):
24 *
25 * If you wish your version of this file to be governed by only the CDDL or
26 * only the GPL Version 2, indicate your decision by adding "[Contributor]
27 * elects to include this software in this distribution under the [CDDL or GPL
28 * Version 2] license." If you don't indicate a single choice of license, a
29 * recipient has the option to distribute your version of this file under
30 * either the CDDL, the GPL Version 2 or to extend the choice of license to
31 * its licensees as provided above. However, if you add GPL Version 2 code
32 * and therefore, elected the GPL Version 2 license, then the option applies
33 * only if the new code is made subject to such option by the copyright
34 * holder.
35 */
36
37 /*
38 * @(#)FileTypeMap.java 1.9 07/05/14
39 */
40
41 package javax.activation;
42
43 import java.io.File;
44
45 /**
46 * The FileTypeMap is an abstract class that provides a data typing
47 * interface for files. Implementations of this class will
48 * implement the getContentType methods which will derive a content
49 * type from a file name or a File object. FileTypeMaps could use any
50 * scheme to determine the data type, from examining the file extension
51 * of a file (like the MimetypesFileTypeMap) to opening the file and
52 * trying to derive its type from the contents of the file. The
53 * FileDataSource class uses the default FileTypeMap (a MimetypesFileTypeMap
54 * unless changed) to determine the content type of files.
55 *
56 * @see javax.activation.FileTypeMap
57 * @see javax.activation.FileDataSource
58 * @see javax.activation.MimetypesFileTypeMap
59 */
60
61 public abstract class FileTypeMap {
62
63 private static FileTypeMap defaultMap = null;
64
65 /**
66 * The default constructor.
67 */
68 public FileTypeMap() {
69 super();
70 }
71
72 /**
73 * Return the type of the file object. This method should
74 * always return a valid MIME type.
75 *
76 * @param file A file to be typed.
77 * @return The content type.
78 */
79 abstract public String getContentType(File file);
80
81 /**
82 * Return the type of the file passed in. This method should
83 * always return a valid MIME type.
84 *
85 * @param filename the pathname of the file.
86 * @return The content type.
87 */
88 abstract public String getContentType(String filename);
89
90 /**
91 * Sets the default FileTypeMap for the system. This instance
92 * will be returned to callers of getDefaultFileTypeMap.
93 *
94 * @param map The FileTypeMap.
95 * @exception SecurityException if the caller doesn't have permission
96 * to change the default
97 */
98 public static void setDefaultFileTypeMap(FileTypeMap map) {
99 SecurityManager security = System.getSecurityManager();
100 if (security != null) {
101 try {
102 // if it's ok with the SecurityManager, it's ok with me...
103 security.checkSetFactory();
104 } catch (SecurityException ex) {
105 // otherwise, we also allow it if this code and the
106 // factory come from the same class loader (e.g.,
107 // the JAF classes were loaded with the applet classes).
108 if (FileTypeMap.class.getClassLoader() !=
109 map.getClass().getClassLoader())
110 throw ex;
111 }
112 }
113 defaultMap = map;
114 }
115
116 /**
117 * Return the default FileTypeMap for the system.
118 * If setDefaultFileTypeMap was called, return
119 * that instance, otherwise return an instance of
120 * <code>MimetypesFileTypeMap</code>.
121 *
122 * @return The default FileTypeMap
123 * @see javax.activation.FileTypeMap#setDefaultFileTypeMap
124 */
125 public static FileTypeMap getDefaultFileTypeMap() {
126 // XXX - probably should be synchronized
127 if (defaultMap == null)
128 defaultMap = new MimetypesFileTypeMap();
129 return defaultMap;
130 }
131 }