24 */ |
24 */ |
25 |
25 |
26 package javax.activation; |
26 package javax.activation; |
27 |
27 |
28 import java.io.File; |
28 import java.io.File; |
|
29 import java.util.Map; |
|
30 import java.util.WeakHashMap; |
29 |
31 |
30 /** |
32 /** |
31 * The FileTypeMap is an abstract class that provides a data typing |
33 * The FileTypeMap is an abstract class that provides a data typing |
32 * interface for files. Implementations of this class will |
34 * interface for files. Implementations of this class will |
33 * implement the getContentType methods which will derive a content |
35 * implement the getContentType methods which will derive a content |
46 */ |
48 */ |
47 |
49 |
48 public abstract class FileTypeMap { |
50 public abstract class FileTypeMap { |
49 |
51 |
50 private static FileTypeMap defaultMap = null; |
52 private static FileTypeMap defaultMap = null; |
|
53 private static Map<ClassLoader,FileTypeMap> map = |
|
54 new WeakHashMap<ClassLoader,FileTypeMap>(); |
51 |
55 |
52 /** |
56 /** |
53 * The default constructor. |
57 * The default constructor. |
54 */ |
58 */ |
55 public FileTypeMap() { |
59 public FileTypeMap() { |
76 |
80 |
77 /** |
81 /** |
78 * Sets the default FileTypeMap for the system. This instance |
82 * Sets the default FileTypeMap for the system. This instance |
79 * will be returned to callers of getDefaultFileTypeMap. |
83 * will be returned to callers of getDefaultFileTypeMap. |
80 * |
84 * |
81 * @param map The FileTypeMap. |
85 * @param fileTypeMap The FileTypeMap. |
82 * @exception SecurityException if the caller doesn't have permission |
86 * @exception SecurityException if the caller doesn't have permission |
83 * to change the default |
87 * to change the default |
84 */ |
88 */ |
85 public static void setDefaultFileTypeMap(FileTypeMap map) { |
89 public static synchronized void setDefaultFileTypeMap(FileTypeMap fileTypeMap) { |
86 SecurityManager security = System.getSecurityManager(); |
90 SecurityManager security = System.getSecurityManager(); |
87 if (security != null) { |
91 if (security != null) { |
88 try { |
92 try { |
89 // if it's ok with the SecurityManager, it's ok with me... |
93 // if it's ok with the SecurityManager, it's ok with me... |
90 security.checkSetFactory(); |
94 security.checkSetFactory(); |
91 } catch (SecurityException ex) { |
95 } catch (SecurityException ex) { |
92 // otherwise, we also allow it if this code and the |
96 // otherwise, we also allow it if this code and the |
93 // factory come from the same class loader (e.g., |
97 // factory come from the same (non-system) class loader (e.g., |
94 // the JAF classes were loaded with the applet classes). |
98 // the JAF classes were loaded with the applet classes). |
95 if (FileTypeMap.class.getClassLoader() != |
99 if (FileTypeMap.class.getClassLoader() == null || |
96 map.getClass().getClassLoader()) |
100 FileTypeMap.class.getClassLoader() != |
|
101 fileTypeMap.getClass().getClassLoader()) |
97 throw ex; |
102 throw ex; |
98 } |
103 } |
99 } |
104 } |
100 defaultMap = map; |
105 // remove any per-thread-context-class-loader FileTypeMap |
|
106 map.remove(SecuritySupport.getContextClassLoader()); |
|
107 defaultMap = fileTypeMap; |
101 } |
108 } |
102 |
109 |
103 /** |
110 /** |
104 * Return the default FileTypeMap for the system. |
111 * Return the default FileTypeMap for the system. |
105 * If setDefaultFileTypeMap was called, return |
112 * If setDefaultFileTypeMap was called, return |
107 * <code>MimetypesFileTypeMap</code>. |
114 * <code>MimetypesFileTypeMap</code>. |
108 * |
115 * |
109 * @return The default FileTypeMap |
116 * @return The default FileTypeMap |
110 * @see javax.activation.FileTypeMap#setDefaultFileTypeMap |
117 * @see javax.activation.FileTypeMap#setDefaultFileTypeMap |
111 */ |
118 */ |
112 public static FileTypeMap getDefaultFileTypeMap() { |
119 public static synchronized FileTypeMap getDefaultFileTypeMap() { |
113 // XXX - probably should be synchronized |
120 if (defaultMap != null) |
114 if (defaultMap == null) |
121 return defaultMap; |
115 defaultMap = new MimetypesFileTypeMap(); |
122 |
116 return defaultMap; |
123 // fetch per-thread-context-class-loader default |
|
124 ClassLoader tccl = SecuritySupport.getContextClassLoader(); |
|
125 FileTypeMap def = map.get(tccl); |
|
126 if (def == null) { |
|
127 def = new MimetypesFileTypeMap(); |
|
128 map.put(tccl, def); |
|
129 } |
|
130 return def; |
117 } |
131 } |
118 } |
132 } |