01: /* Copyright 2003 The JA-SIG Collaborative. All rights reserved.
02: * See license distributed with this file and
03: * available online at http://www.uportal.org/license.html
04: */
05:
06: package org.jasig.portal.car;
07:
08: import java.util.Collections;
09: import java.util.HashMap;
10: import java.util.Map;
11:
12: import javax.servlet.ServletContext;
13:
14: /**
15: * MimeTypeCache.
16: * @author Mark Boyd {@link <a href="mailto:mark.boyd@engineer.com">mark.boyd@engineer.com</a>}
17: * @version $Revision: 36690 $
18: */
19: public class MimeTypeCache {
20: public final static String RCS_ID = "@(#) $Header$";
21:
22: private static Map cContextToCache = new HashMap();
23:
24: public static String getMimeType(ServletContext context,
25: String resourceName) {
26: // get the mime-type from the servlet context
27: // we cache and synchronize the mime type retrieval
28: // because too many calls to the container method
29: // crashes the IWS 6.0sp5 web server
30:
31: resourceName = resourceName.toLowerCase();
32:
33: String mimeType = null;
34:
35: int index = resourceName.lastIndexOf(".");
36:
37: if (index >= 0) {
38: String extension = resourceName.substring(index);
39:
40: Map mimeTypeCache = null;
41: synchronized (cContextToCache) {
42: mimeTypeCache = (Map) cContextToCache.get(context);
43: if (null == mimeTypeCache) {
44: mimeTypeCache = Collections
45: .synchronizedMap(new HashMap());
46: cContextToCache.put(context, mimeTypeCache);
47: }
48: }
49:
50: if (!mimeTypeCache.containsKey(extension)) {
51: synchronized (context) {
52: mimeType = context.getMimeType(resourceName);
53: }
54:
55: mimeTypeCache.put(extension, mimeType);
56: } else {
57: mimeType = (String) mimeTypeCache.get(extension);
58: }
59: } else {
60: mimeType = "application/octet-stream";
61: }
62:
63: return mimeType;
64: }
65: }
|