Tue, 06 Mar 2012 16:09:35 -0800
7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom
1 /*
2 * Copyright (c) 1997, 2005, 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 */
26 package javax.activation;
28 import java.io.*;
29 import java.net.*;
30 import java.util.*;
31 import com.sun.activation.registries.MimeTypeFile;
32 import com.sun.activation.registries.LogSupport;
34 /**
35 * This class extends FileTypeMap and provides data typing of files
36 * via their file extension. It uses the <code>.mime.types</code> format. <p>
37 *
38 * <b>MIME types file search order:</b><p>
39 * The MimetypesFileTypeMap looks in various places in the user's
40 * system for MIME types file entries. When requests are made
41 * to search for MIME types in the MimetypesFileTypeMap, it searches
42 * MIME types files in the following order:
43 * <p>
44 * <ol>
45 * <li> Programmatically added entries to the MimetypesFileTypeMap instance.
46 * <li> The file <code>.mime.types</code> in the user's home directory.
47 * <li> The file <<i>java.home</i>><code>/lib/mime.types</code>.
48 * <li> The file or resources named <code>META-INF/mime.types</code>.
49 * <li> The file or resource named <code>META-INF/mimetypes.default</code>
50 * (usually found only in the <code>activation.jar</code> file).
51 * </ol>
52 * <p>
53 * <b>MIME types file format:</b><p>
54 *
55 * <code>
56 * # comments begin with a '#'<br>
57 * # the format is <mime type> <space separated file extensions><br>
58 * # for example:<br>
59 * text/plain txt text TXT<br>
60 * # this would map file.txt, file.text, and file.TXT to<br>
61 * # the mime type "text/plain"<br>
62 * </code>
63 *
64 * @author Bart Calder
65 * @author Bill Shannon
66 *
67 * @since 1.6
68 */
69 public class MimetypesFileTypeMap extends FileTypeMap {
70 /*
71 * We manage a collection of databases, searched in order.
72 * The default database is shared between all instances
73 * of this class.
74 * XXX - Can we safely share more databases between instances?
75 */
76 private static MimeTypeFile defDB = null;
77 private MimeTypeFile[] DB;
78 private static final int PROG = 0; // programmatically added entries
80 private static String defaultType = "application/octet-stream";
82 /**
83 * The default constructor.
84 */
85 public MimetypesFileTypeMap() {
86 Vector dbv = new Vector(5); // usually 5 or less databases
87 MimeTypeFile mf = null;
88 dbv.addElement(null); // place holder for PROG entry
90 LogSupport.log("MimetypesFileTypeMap: load HOME");
91 try {
92 String user_home = System.getProperty("user.home");
94 if (user_home != null) {
95 String path = user_home + File.separator + ".mime.types";
96 mf = loadFile(path);
97 if (mf != null)
98 dbv.addElement(mf);
99 }
100 } catch (SecurityException ex) {}
102 LogSupport.log("MimetypesFileTypeMap: load SYS");
103 try {
104 // check system's home
105 String system_mimetypes = System.getProperty("java.home") +
106 File.separator + "lib" + File.separator + "mime.types";
107 mf = loadFile(system_mimetypes);
108 if (mf != null)
109 dbv.addElement(mf);
110 } catch (SecurityException ex) {}
112 LogSupport.log("MimetypesFileTypeMap: load JAR");
113 // load from the app's jar file
114 loadAllResources(dbv, "META-INF/mime.types");
116 LogSupport.log("MimetypesFileTypeMap: load DEF");
117 synchronized (MimetypesFileTypeMap.class) {
118 // see if another instance has created this yet.
119 if (defDB == null)
120 defDB = loadResource("/META-INF/mimetypes.default");
121 }
123 if (defDB != null)
124 dbv.addElement(defDB);
126 DB = new MimeTypeFile[dbv.size()];
127 dbv.copyInto(DB);
128 }
130 /**
131 * Load from the named resource.
132 */
133 private MimeTypeFile loadResource(String name) {
134 InputStream clis = null;
135 try {
136 clis = SecuritySupport.getResourceAsStream(this.getClass(), name);
137 if (clis != null) {
138 MimeTypeFile mf = new MimeTypeFile(clis);
139 if (LogSupport.isLoggable())
140 LogSupport.log("MimetypesFileTypeMap: successfully " +
141 "loaded mime types file: " + name);
142 return mf;
143 } else {
144 if (LogSupport.isLoggable())
145 LogSupport.log("MimetypesFileTypeMap: not loading " +
146 "mime types file: " + name);
147 }
148 } catch (IOException e) {
149 if (LogSupport.isLoggable())
150 LogSupport.log("MimetypesFileTypeMap: can't load " + name, e);
151 } catch (SecurityException sex) {
152 if (LogSupport.isLoggable())
153 LogSupport.log("MimetypesFileTypeMap: can't load " + name, sex);
154 } finally {
155 try {
156 if (clis != null)
157 clis.close();
158 } catch (IOException ex) { } // ignore it
159 }
160 return null;
161 }
163 /**
164 * Load all of the named resource.
165 */
166 private void loadAllResources(Vector v, String name) {
167 boolean anyLoaded = false;
168 try {
169 URL[] urls;
170 ClassLoader cld = null;
171 // First try the "application's" class loader.
172 cld = SecuritySupport.getContextClassLoader();
173 if (cld == null)
174 cld = this.getClass().getClassLoader();
175 if (cld != null)
176 urls = SecuritySupport.getResources(cld, name);
177 else
178 urls = SecuritySupport.getSystemResources(name);
179 if (urls != null) {
180 if (LogSupport.isLoggable())
181 LogSupport.log("MimetypesFileTypeMap: getResources");
182 for (int i = 0; i < urls.length; i++) {
183 URL url = urls[i];
184 InputStream clis = null;
185 if (LogSupport.isLoggable())
186 LogSupport.log("MimetypesFileTypeMap: URL " + url);
187 try {
188 clis = SecuritySupport.openStream(url);
189 if (clis != null) {
190 v.addElement(new MimeTypeFile(clis));
191 anyLoaded = true;
192 if (LogSupport.isLoggable())
193 LogSupport.log("MimetypesFileTypeMap: " +
194 "successfully loaded " +
195 "mime types from URL: " + url);
196 } else {
197 if (LogSupport.isLoggable())
198 LogSupport.log("MimetypesFileTypeMap: " +
199 "not loading " +
200 "mime types from URL: " + url);
201 }
202 } catch (IOException ioex) {
203 if (LogSupport.isLoggable())
204 LogSupport.log("MimetypesFileTypeMap: can't load " +
205 url, ioex);
206 } catch (SecurityException sex) {
207 if (LogSupport.isLoggable())
208 LogSupport.log("MimetypesFileTypeMap: can't load " +
209 url, sex);
210 } finally {
211 try {
212 if (clis != null)
213 clis.close();
214 } catch (IOException cex) { }
215 }
216 }
217 }
218 } catch (Exception ex) {
219 if (LogSupport.isLoggable())
220 LogSupport.log("MimetypesFileTypeMap: can't load " + name, ex);
221 }
223 // if failed to load anything, fall back to old technique, just in case
224 if (!anyLoaded) {
225 LogSupport.log("MimetypesFileTypeMap: !anyLoaded");
226 MimeTypeFile mf = loadResource("/" + name);
227 if (mf != null)
228 v.addElement(mf);
229 }
230 }
232 /**
233 * Load the named file.
234 */
235 private MimeTypeFile loadFile(String name) {
236 MimeTypeFile mtf = null;
238 try {
239 mtf = new MimeTypeFile(name);
240 } catch (IOException e) {
241 // e.printStackTrace();
242 }
243 return mtf;
244 }
246 /**
247 * Construct a MimetypesFileTypeMap with programmatic entries
248 * added from the named file.
249 *
250 * @param mimeTypeFileName the file name
251 */
252 public MimetypesFileTypeMap(String mimeTypeFileName) throws IOException {
253 this();
254 DB[PROG] = new MimeTypeFile(mimeTypeFileName);
255 }
257 /**
258 * Construct a MimetypesFileTypeMap with programmatic entries
259 * added from the InputStream.
260 *
261 * @param is the input stream to read from
262 */
263 public MimetypesFileTypeMap(InputStream is) {
264 this();
265 try {
266 DB[PROG] = new MimeTypeFile(is);
267 } catch (IOException ex) {
268 // XXX - really should throw it
269 }
270 }
272 /**
273 * Prepend the MIME type values to the registry.
274 *
275 * @param mime_types A .mime.types formatted string of entries.
276 */
277 public synchronized void addMimeTypes(String mime_types) {
278 // check to see if we have created the registry
279 if (DB[PROG] == null)
280 DB[PROG] = new MimeTypeFile(); // make one
282 DB[PROG].appendToRegistry(mime_types);
283 }
285 /**
286 * Return the MIME type of the file object.
287 * The implementation in this class calls
288 * <code>getContentType(f.getName())</code>.
289 *
290 * @param f the file
291 * @return the file's MIME type
292 */
293 public String getContentType(File f) {
294 return this.getContentType(f.getName());
295 }
297 /**
298 * Return the MIME type based on the specified file name.
299 * The MIME type entries are searched as described above under
300 * <i>MIME types file search order</i>.
301 * If no entry is found, the type "application/octet-stream" is returned.
302 *
303 * @param filename the file name
304 * @return the file's MIME type
305 */
306 public synchronized String getContentType(String filename) {
307 int dot_pos = filename.lastIndexOf("."); // period index
309 if (dot_pos < 0)
310 return defaultType;
312 String file_ext = filename.substring(dot_pos + 1);
313 if (file_ext.length() == 0)
314 return defaultType;
316 for (int i = 0; i < DB.length; i++) {
317 if (DB[i] == null)
318 continue;
319 String result = DB[i].getMIMETypeString(file_ext);
320 if (result != null)
321 return result;
322 }
323 return defaultType;
324 }
326 /**
327 * for debugging...
328 *
329 public static void main(String[] argv) throws Exception {
330 MimetypesFileTypeMap map = new MimetypesFileTypeMap();
331 System.out.println("File " + argv[0] + " has MIME type " +
332 map.getContentType(argv[0]));
333 System.exit(0);
334 }
335 */
336 }