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

Sun, 15 Dec 2013 23:35:45 +0100

author
mkos
date
Sun, 15 Dec 2013 23:35:45 +0100
changeset 494
2fcd3ddb57a6
parent 286
f50545b5e2f1
child 637
9c07ef4934dd
permissions
-rw-r--r--

8025152: Enhance activation set up
8028388: 9 jaxws tests failed in nightly build with java.lang.ClassCastException
Summary: fix also reviewed by Bill Shannon, Alexander Fomin
Reviewed-by: dfuchs, hawtin, mgrebac
Contributed-by: bill.shannon@oracle.com

ohair@286 1 /*
mkos@494 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
ohair@286 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286 4 *
ohair@286 5 * This code is free software; you can redistribute it and/or modify it
ohair@286 6 * under the terms of the GNU General Public License version 2 only, as
ohair@286 7 * published by the Free Software Foundation. Oracle designates this
ohair@286 8 * particular file as subject to the "Classpath" exception as provided
ohair@286 9 * by Oracle in the LICENSE file that accompanied this code.
ohair@286 10 *
ohair@286 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286 14 * version 2 for more details (a copy is included in the LICENSE file that
ohair@286 15 * accompanied this code).
ohair@286 16 *
ohair@286 17 * You should have received a copy of the GNU General Public License version
ohair@286 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286 20 *
ohair@286 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286 22 * or visit www.oracle.com if you need additional information or have any
ohair@286 23 * questions.
ohair@286 24 */
ohair@286 25
ohair@286 26 package javax.activation;
ohair@286 27
ohair@286 28 import java.io.*;
ohair@286 29 import java.net.*;
ohair@286 30 import java.util.*;
ohair@286 31 import com.sun.activation.registries.MimeTypeFile;
ohair@286 32 import com.sun.activation.registries.LogSupport;
ohair@286 33
ohair@286 34 /**
ohair@286 35 * This class extends FileTypeMap and provides data typing of files
ohair@286 36 * via their file extension. It uses the <code>.mime.types</code> format. <p>
ohair@286 37 *
ohair@286 38 * <b>MIME types file search order:</b><p>
ohair@286 39 * The MimetypesFileTypeMap looks in various places in the user's
ohair@286 40 * system for MIME types file entries. When requests are made
ohair@286 41 * to search for MIME types in the MimetypesFileTypeMap, it searches
ohair@286 42 * MIME types files in the following order:
ohair@286 43 * <p>
ohair@286 44 * <ol>
ohair@286 45 * <li> Programmatically added entries to the MimetypesFileTypeMap instance.
ohair@286 46 * <li> The file <code>.mime.types</code> in the user's home directory.
ohair@286 47 * <li> The file &lt;<i>java.home</i>&gt;<code>/lib/mime.types</code>.
ohair@286 48 * <li> The file or resources named <code>META-INF/mime.types</code>.
ohair@286 49 * <li> The file or resource named <code>META-INF/mimetypes.default</code>
ohair@286 50 * (usually found only in the <code>activation.jar</code> file).
ohair@286 51 * </ol>
ohair@286 52 * <p>
ohair@286 53 * <b>MIME types file format:</b><p>
ohair@286 54 *
ohair@286 55 * <code>
ohair@286 56 * # comments begin with a '#'<br>
ohair@286 57 * # the format is &lt;mime type> &lt;space separated file extensions><br>
ohair@286 58 * # for example:<br>
ohair@286 59 * text/plain txt text TXT<br>
ohair@286 60 * # this would map file.txt, file.text, and file.TXT to<br>
ohair@286 61 * # the mime type "text/plain"<br>
ohair@286 62 * </code>
ohair@286 63 *
ohair@286 64 * @author Bart Calder
ohair@286 65 * @author Bill Shannon
ohair@286 66 *
ohair@286 67 * @since 1.6
ohair@286 68 */
ohair@286 69 public class MimetypesFileTypeMap extends FileTypeMap {
ohair@286 70 /*
ohair@286 71 * We manage a collection of databases, searched in order.
ohair@286 72 */
ohair@286 73 private MimeTypeFile[] DB;
ohair@286 74 private static final int PROG = 0; // programmatically added entries
ohair@286 75
ohair@286 76 private static String defaultType = "application/octet-stream";
ohair@286 77
ohair@286 78 /**
ohair@286 79 * The default constructor.
ohair@286 80 */
ohair@286 81 public MimetypesFileTypeMap() {
ohair@286 82 Vector dbv = new Vector(5); // usually 5 or less databases
ohair@286 83 MimeTypeFile mf = null;
ohair@286 84 dbv.addElement(null); // place holder for PROG entry
ohair@286 85
ohair@286 86 LogSupport.log("MimetypesFileTypeMap: load HOME");
ohair@286 87 try {
ohair@286 88 String user_home = System.getProperty("user.home");
ohair@286 89
ohair@286 90 if (user_home != null) {
ohair@286 91 String path = user_home + File.separator + ".mime.types";
ohair@286 92 mf = loadFile(path);
ohair@286 93 if (mf != null)
ohair@286 94 dbv.addElement(mf);
ohair@286 95 }
ohair@286 96 } catch (SecurityException ex) {}
ohair@286 97
ohair@286 98 LogSupport.log("MimetypesFileTypeMap: load SYS");
ohair@286 99 try {
ohair@286 100 // check system's home
ohair@286 101 String system_mimetypes = System.getProperty("java.home") +
ohair@286 102 File.separator + "lib" + File.separator + "mime.types";
ohair@286 103 mf = loadFile(system_mimetypes);
ohair@286 104 if (mf != null)
ohair@286 105 dbv.addElement(mf);
ohair@286 106 } catch (SecurityException ex) {}
ohair@286 107
ohair@286 108 LogSupport.log("MimetypesFileTypeMap: load JAR");
ohair@286 109 // load from the app's jar file
ohair@286 110 loadAllResources(dbv, "META-INF/mime.types");
ohair@286 111
ohair@286 112 LogSupport.log("MimetypesFileTypeMap: load DEF");
mkos@494 113 mf = loadResource("/META-INF/mimetypes.default");
ohair@286 114
mkos@494 115 if (mf != null)
mkos@494 116 dbv.addElement(mf);
ohair@286 117
ohair@286 118 DB = new MimeTypeFile[dbv.size()];
ohair@286 119 dbv.copyInto(DB);
ohair@286 120 }
ohair@286 121
ohair@286 122 /**
ohair@286 123 * Load from the named resource.
ohair@286 124 */
ohair@286 125 private MimeTypeFile loadResource(String name) {
ohair@286 126 InputStream clis = null;
ohair@286 127 try {
ohair@286 128 clis = SecuritySupport.getResourceAsStream(this.getClass(), name);
ohair@286 129 if (clis != null) {
ohair@286 130 MimeTypeFile mf = new MimeTypeFile(clis);
ohair@286 131 if (LogSupport.isLoggable())
ohair@286 132 LogSupport.log("MimetypesFileTypeMap: successfully " +
ohair@286 133 "loaded mime types file: " + name);
ohair@286 134 return mf;
ohair@286 135 } else {
ohair@286 136 if (LogSupport.isLoggable())
ohair@286 137 LogSupport.log("MimetypesFileTypeMap: not loading " +
ohair@286 138 "mime types file: " + name);
ohair@286 139 }
ohair@286 140 } catch (IOException e) {
ohair@286 141 if (LogSupport.isLoggable())
ohair@286 142 LogSupport.log("MimetypesFileTypeMap: can't load " + name, e);
ohair@286 143 } catch (SecurityException sex) {
ohair@286 144 if (LogSupport.isLoggable())
ohair@286 145 LogSupport.log("MimetypesFileTypeMap: can't load " + name, sex);
ohair@286 146 } finally {
ohair@286 147 try {
ohair@286 148 if (clis != null)
ohair@286 149 clis.close();
ohair@286 150 } catch (IOException ex) { } // ignore it
ohair@286 151 }
ohair@286 152 return null;
ohair@286 153 }
ohair@286 154
ohair@286 155 /**
ohair@286 156 * Load all of the named resource.
ohair@286 157 */
ohair@286 158 private void loadAllResources(Vector v, String name) {
ohair@286 159 boolean anyLoaded = false;
ohair@286 160 try {
ohair@286 161 URL[] urls;
ohair@286 162 ClassLoader cld = null;
ohair@286 163 // First try the "application's" class loader.
ohair@286 164 cld = SecuritySupport.getContextClassLoader();
ohair@286 165 if (cld == null)
ohair@286 166 cld = this.getClass().getClassLoader();
ohair@286 167 if (cld != null)
ohair@286 168 urls = SecuritySupport.getResources(cld, name);
ohair@286 169 else
ohair@286 170 urls = SecuritySupport.getSystemResources(name);
ohair@286 171 if (urls != null) {
ohair@286 172 if (LogSupport.isLoggable())
ohair@286 173 LogSupport.log("MimetypesFileTypeMap: getResources");
ohair@286 174 for (int i = 0; i < urls.length; i++) {
ohair@286 175 URL url = urls[i];
ohair@286 176 InputStream clis = null;
ohair@286 177 if (LogSupport.isLoggable())
ohair@286 178 LogSupport.log("MimetypesFileTypeMap: URL " + url);
ohair@286 179 try {
ohair@286 180 clis = SecuritySupport.openStream(url);
ohair@286 181 if (clis != null) {
ohair@286 182 v.addElement(new MimeTypeFile(clis));
ohair@286 183 anyLoaded = true;
ohair@286 184 if (LogSupport.isLoggable())
ohair@286 185 LogSupport.log("MimetypesFileTypeMap: " +
ohair@286 186 "successfully loaded " +
ohair@286 187 "mime types from URL: " + url);
ohair@286 188 } else {
ohair@286 189 if (LogSupport.isLoggable())
ohair@286 190 LogSupport.log("MimetypesFileTypeMap: " +
ohair@286 191 "not loading " +
ohair@286 192 "mime types from URL: " + url);
ohair@286 193 }
ohair@286 194 } catch (IOException ioex) {
ohair@286 195 if (LogSupport.isLoggable())
ohair@286 196 LogSupport.log("MimetypesFileTypeMap: can't load " +
ohair@286 197 url, ioex);
ohair@286 198 } catch (SecurityException sex) {
ohair@286 199 if (LogSupport.isLoggable())
ohair@286 200 LogSupport.log("MimetypesFileTypeMap: can't load " +
ohair@286 201 url, sex);
ohair@286 202 } finally {
ohair@286 203 try {
ohair@286 204 if (clis != null)
ohair@286 205 clis.close();
ohair@286 206 } catch (IOException cex) { }
ohair@286 207 }
ohair@286 208 }
ohair@286 209 }
ohair@286 210 } catch (Exception ex) {
ohair@286 211 if (LogSupport.isLoggable())
ohair@286 212 LogSupport.log("MimetypesFileTypeMap: can't load " + name, ex);
ohair@286 213 }
ohair@286 214
ohair@286 215 // if failed to load anything, fall back to old technique, just in case
ohair@286 216 if (!anyLoaded) {
ohair@286 217 LogSupport.log("MimetypesFileTypeMap: !anyLoaded");
ohair@286 218 MimeTypeFile mf = loadResource("/" + name);
ohair@286 219 if (mf != null)
ohair@286 220 v.addElement(mf);
ohair@286 221 }
ohair@286 222 }
ohair@286 223
ohair@286 224 /**
ohair@286 225 * Load the named file.
ohair@286 226 */
ohair@286 227 private MimeTypeFile loadFile(String name) {
ohair@286 228 MimeTypeFile mtf = null;
ohair@286 229
ohair@286 230 try {
ohair@286 231 mtf = new MimeTypeFile(name);
ohair@286 232 } catch (IOException e) {
ohair@286 233 // e.printStackTrace();
ohair@286 234 }
ohair@286 235 return mtf;
ohair@286 236 }
ohair@286 237
ohair@286 238 /**
ohair@286 239 * Construct a MimetypesFileTypeMap with programmatic entries
ohair@286 240 * added from the named file.
ohair@286 241 *
ohair@286 242 * @param mimeTypeFileName the file name
ohair@286 243 */
ohair@286 244 public MimetypesFileTypeMap(String mimeTypeFileName) throws IOException {
ohair@286 245 this();
ohair@286 246 DB[PROG] = new MimeTypeFile(mimeTypeFileName);
ohair@286 247 }
ohair@286 248
ohair@286 249 /**
ohair@286 250 * Construct a MimetypesFileTypeMap with programmatic entries
ohair@286 251 * added from the InputStream.
ohair@286 252 *
ohair@286 253 * @param is the input stream to read from
ohair@286 254 */
ohair@286 255 public MimetypesFileTypeMap(InputStream is) {
ohair@286 256 this();
ohair@286 257 try {
ohair@286 258 DB[PROG] = new MimeTypeFile(is);
ohair@286 259 } catch (IOException ex) {
ohair@286 260 // XXX - really should throw it
ohair@286 261 }
ohair@286 262 }
ohair@286 263
ohair@286 264 /**
ohair@286 265 * Prepend the MIME type values to the registry.
ohair@286 266 *
ohair@286 267 * @param mime_types A .mime.types formatted string of entries.
ohair@286 268 */
ohair@286 269 public synchronized void addMimeTypes(String mime_types) {
ohair@286 270 // check to see if we have created the registry
ohair@286 271 if (DB[PROG] == null)
ohair@286 272 DB[PROG] = new MimeTypeFile(); // make one
ohair@286 273
ohair@286 274 DB[PROG].appendToRegistry(mime_types);
ohair@286 275 }
ohair@286 276
ohair@286 277 /**
ohair@286 278 * Return the MIME type of the file object.
ohair@286 279 * The implementation in this class calls
ohair@286 280 * <code>getContentType(f.getName())</code>.
ohair@286 281 *
ohair@286 282 * @param f the file
ohair@286 283 * @return the file's MIME type
ohair@286 284 */
ohair@286 285 public String getContentType(File f) {
ohair@286 286 return this.getContentType(f.getName());
ohair@286 287 }
ohair@286 288
ohair@286 289 /**
ohair@286 290 * Return the MIME type based on the specified file name.
ohair@286 291 * The MIME type entries are searched as described above under
ohair@286 292 * <i>MIME types file search order</i>.
ohair@286 293 * If no entry is found, the type "application/octet-stream" is returned.
ohair@286 294 *
ohair@286 295 * @param filename the file name
ohair@286 296 * @return the file's MIME type
ohair@286 297 */
ohair@286 298 public synchronized String getContentType(String filename) {
ohair@286 299 int dot_pos = filename.lastIndexOf("."); // period index
ohair@286 300
ohair@286 301 if (dot_pos < 0)
ohair@286 302 return defaultType;
ohair@286 303
ohair@286 304 String file_ext = filename.substring(dot_pos + 1);
ohair@286 305 if (file_ext.length() == 0)
ohair@286 306 return defaultType;
ohair@286 307
ohair@286 308 for (int i = 0; i < DB.length; i++) {
ohair@286 309 if (DB[i] == null)
ohair@286 310 continue;
ohair@286 311 String result = DB[i].getMIMETypeString(file_ext);
ohair@286 312 if (result != null)
ohair@286 313 return result;
ohair@286 314 }
ohair@286 315 return defaultType;
ohair@286 316 }
ohair@286 317
ohair@286 318 /**
ohair@286 319 * for debugging...
ohair@286 320 *
ohair@286 321 public static void main(String[] argv) throws Exception {
ohair@286 322 MimetypesFileTypeMap map = new MimetypesFileTypeMap();
ohair@286 323 System.out.println("File " + argv[0] + " has MIME type " +
ohair@286 324 map.getContentType(argv[0]));
ohair@286 325 System.exit(0);
ohair@286 326 }
ohair@286 327 */
ohair@286 328 }

mercurial