KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > equinox > http > servlet > internal > ProxyContext


1 /*******************************************************************************
2  * Copyright (c) 2005-2007 Cognos Incorporated, IBM Corporation and others
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * Cognos Incorporated - initial API and implementation
10  * IBM Corporation - bug fixes and enhancements
11  *******************************************************************************/

12 package org.eclipse.equinox.http.servlet.internal;
13
14 import java.io.File JavaDoc;
15 import java.util.*;
16 import javax.servlet.ServletContext JavaDoc;
17 import javax.servlet.http.HttpServletRequest JavaDoc;
18 import org.osgi.service.http.HttpContext;
19
20 /**
21  * The ProxyContext provides something similar to a ServletContext for all servlets and resources under a particular ProxyServlet.
22  * In particular it holds and represent the concept of "context path" through the Proxy Servlets servlet path.
23  * The Http Service also requires a ServletContext namespaced by each individual HttpContext. The ProxyContext provides support for the
24  * attribute map of a ServletContext again namespaced by HttpContext as specified in the Http Service specification. The ContextAttributes
25  * are reference counted so that when the HttpContext is no longer referenced the associated context attributes can be
26  * garbage collected and the context temp dir deleteted.
27  */

28 public class ProxyContext {
29     private static final String JavaDoc JAVAX_SERVLET_CONTEXT_TEMPDIR = "javax.servlet.context.tempdir"; //$NON-NLS-1$
30

31     private String JavaDoc servletPath;
32     private HashMap attributesMap = new HashMap();
33     File JavaDoc proxyContextTempDir;
34
35     public ProxyContext(ServletContext JavaDoc servletContext) {
36         File JavaDoc tempDir = (File JavaDoc) servletContext.getAttribute(JAVAX_SERVLET_CONTEXT_TEMPDIR);
37         if (tempDir != null) {
38             proxyContextTempDir = new File JavaDoc(tempDir, "proxytemp"); //$NON-NLS-1$
39
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 JavaDoc req) {
50         if (servletPath == null)
51             servletPath = HttpServletRequestAdaptor.getDispatchServletPath(req);
52     }
53
54     synchronized String JavaDoc 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     /**
81      * deleteDirectory is a convenience method to recursively delete a directory
82      * @param directory - the directory to delete.
83      * @return was the delete succesful
84      */

85     protected static boolean deleteDirectory(File JavaDoc directory) {
86         if (directory.exists() && directory.isDirectory()) {
87             File JavaDoc[] 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 JavaDoc contextTempDir = new File JavaDoc(proxyContextTempDir, "hc_" + httpContext.hashCode()); //$NON-NLS-1$
106
contextTempDir.mkdirs();
107                 put(JAVAX_SERVLET_CONTEXT_TEMPDIR, contextTempDir);
108             }
109         }
110         
111         public void destroy() {
112             File JavaDoc contextTempDir = (File JavaDoc) 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