1 12 package org.eclipse.equinox.http.servlet.internal; 13 14 import java.io.File ; 15 import java.util.*; 16 import javax.servlet.ServletContext ; 17 import javax.servlet.http.HttpServletRequest ; 18 import org.osgi.service.http.HttpContext; 19 20 28 public class ProxyContext { 29 private static final String JAVAX_SERVLET_CONTEXT_TEMPDIR = "javax.servlet.context.tempdir"; 31 private String servletPath; 32 private HashMap attributesMap = new HashMap(); 33 File proxyContextTempDir; 34 35 public ProxyContext(ServletContext servletContext) { 36 File tempDir = (File ) servletContext.getAttribute(JAVAX_SERVLET_CONTEXT_TEMPDIR); 37 if (tempDir != null) { 38 proxyContextTempDir = new File (tempDir, "proxytemp"); deleteDirectory(proxyContextTempDir); 40 proxyContextTempDir.mkdirs(); 41 } 42 } 43 44 public void destroy() { 45 if (proxyContextTempDir != null) 46 deleteDirectory(proxyContextTempDir); 47 } 48 49 synchronized void initializeServletPath(HttpServletRequest req) { 50 if (servletPath == null) 51 servletPath = HttpServletRequestAdaptor.getDispatchServletPath(req); 52 } 53 54 synchronized String getServletPath() { 55 return servletPath; 56 } 57 58 synchronized void createContextAttributes(HttpContext httpContext) { 59 ContextAttributes attributes = (ContextAttributes) attributesMap.get(httpContext); 60 if (attributes == null) { 61 attributes = new ContextAttributes(httpContext); 62 attributesMap.put(httpContext, attributes); 63 } 64 attributes.addReference(); 65 } 66 67 synchronized void destroyContextAttributes(HttpContext httpContext) { 68 ContextAttributes attributes = (ContextAttributes) attributesMap.get(httpContext); 69 attributes.removeReference(); 70 if (attributes.referenceCount() == 0) { 71 attributesMap.remove(httpContext); 72 attributes.destroy(); 73 } 74 } 75 76 synchronized Dictionary getContextAttributes(HttpContext httpContext) { 77 return (Dictionary) attributesMap.get(httpContext); 78 } 79 80 85 protected static boolean deleteDirectory(File directory) { 86 if (directory.exists() && directory.isDirectory()) { 87 File [] files = directory.listFiles(); 88 for (int i = 0; i < files.length; i++) { 89 if (files[i].isDirectory()) { 90 deleteDirectory(files[i]); 91 } else { 92 files[i].delete(); 93 } 94 } 95 } 96 return directory.delete(); 97 } 98 99 public class ContextAttributes extends Hashtable { 100 private static final long serialVersionUID = 1916670423277243587L; 101 private int referenceCount; 102 103 public ContextAttributes(HttpContext httpContext) { 104 if (proxyContextTempDir != null) { 105 File contextTempDir = new File (proxyContextTempDir, "hc_" + httpContext.hashCode()); contextTempDir.mkdirs(); 107 put(JAVAX_SERVLET_CONTEXT_TEMPDIR, contextTempDir); 108 } 109 } 110 111 public void destroy() { 112 File contextTempDir = (File ) get(JAVAX_SERVLET_CONTEXT_TEMPDIR); 113 if (contextTempDir != null) 114 deleteDirectory(contextTempDir); 115 } 116 117 public void addReference() { 118 referenceCount++; 119 } 120 121 public void removeReference() { 122 referenceCount--; 123 } 124 125 public int referenceCount() { 126 return referenceCount; 127 } 128 } 129 } 130 | Popular Tags |