KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > beans > runtime > Cache


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2005 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.beans.runtime;
14
15 import info.magnolia.cms.core.Path;
16
17 import java.util.Hashtable JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import javax.servlet.http.HttpServletRequest JavaDoc;
21
22 import org.apache.commons.lang.StringUtils;
23 import org.apache.log4j.Logger;
24
25
26 /**
27  * @author Sameer Charles
28  * @version $Revision $ ($Author $)
29  */

30 public final class Cache {
31
32     /**
33      * Cached items: the key is the URI of the cached request and the entry is a Cache instance
34      */

35     private static Map JavaDoc cachedURIList = new Hashtable JavaDoc();
36
37     /**
38      * Logger.
39      */

40     private static Logger log = Logger.getLogger(Cache.class);
41
42     /**
43      * holds all URI's which are being cached by cache process this list is updated by CacheHandler on start and end of
44      * cache process
45      */

46     private static Map JavaDoc inProcessURIList = new Hashtable JavaDoc();
47
48     /**
49      * Time in milliseconds.
50      */

51     private long time;
52
53     /**
54      * Original size.
55      */

56     private int size;
57
58     /**
59      * Compressed size.
60      */

61     private int compressedSize;
62
63     /**
64      * Utility class, don't instantiate.
65      */

66     private Cache() {
67         // unused
68
}
69
70     /**
71      * @param request HttpServletRequest
72      * @return true is the request is cached
73      */

74     public static boolean isCached(HttpServletRequest JavaDoc request) {
75         return Cache.cachedURIList.get(Path.getURI(request)) != null;
76     }
77
78     /**
79      * @return true is the request URI is being cached
80      * @param request HttpServletRequest
81      */

82     public static boolean isInCacheProcess(HttpServletRequest JavaDoc request) {
83         return Cache.inProcessURIList.get(Path.getURI(request)) != null;
84     }
85
86     /**
87      * @param uri request URI
88      */

89     public static void addToInProcessURIList(String JavaDoc uri) {
90         Cache.inProcessURIList.put(uri, StringUtils.EMPTY);
91     }
92
93     /**
94      * @param uri request URI
95      */

96     public static void removeFromInProcessURIList(String JavaDoc uri) {
97         Cache.inProcessURIList.remove(uri);
98     }
99
100     /**
101      *
102      */

103     public static void clearInProcessURIList() {
104         Cache.inProcessURIList.clear();
105     }
106
107     /**
108      * @param uri request URI
109      * @param lastModified last modification time (ms from 1970)
110      * @param size original size
111      * @param compressedSize compressed size
112      * @param lastModified
113      */

114     public static void addToCachedURIList(String JavaDoc uri, long lastModified, int size, int compressedSize) {
115         Cache cacheMap = new Cache();
116         cacheMap.time = lastModified;
117         cacheMap.size = size;
118         cacheMap.compressedSize = compressedSize;
119         if (log.isDebugEnabled()) {
120             log.debug("Caching URI [" + uri + "]"); //$NON-NLS-1$ //$NON-NLS-2$
121
}
122         Cache.cachedURIList.put(uri, cacheMap);
123     }
124
125     /**
126      * @param uri request URI
127      */

128     public static void removeFromCachedURIList(String JavaDoc uri) {
129         Cache.cachedURIList.remove(uri);
130     }
131
132     /**
133      *
134      */

135     public static void clearCachedURIList() {
136         Cache.cachedURIList.clear();
137     }
138
139     /**
140      * @param request HttpServletRequest
141      * @return creation time in milliseconds
142      */

143     public static long getCreationTime(HttpServletRequest JavaDoc request) {
144         Cache cacheMap = (Cache) cachedURIList.get(Path.getURI(request));
145         if (cacheMap == null) {
146             return -1;
147         }
148         return cacheMap.time;
149     }
150
151     /**
152      * @param request HttpServletRequest
153      * @return size as on disk
154      */

155     public static int getSize(HttpServletRequest JavaDoc request) {
156         Cache cacheMap = (Cache) cachedURIList.get(Path.getURI(request));
157         return cacheMap.size;
158     }
159
160     /**
161      * @param request HttpServletRequest
162      * @return size as on disk
163      */

164     public static int getCompressedSize(HttpServletRequest JavaDoc request) {
165         Cache cacheMap = (Cache) cachedURIList.get(Path.getURI(request));
166         return cacheMap.compressedSize;
167     }
168
169     /**
170      * @param uri request URI
171      * @return creation time in miliseconds
172      * @deprecated use getCreationTime(HttpServletRequest). Cache could decide to handle requests internally using not
173      * only the request URI
174      */

175     public static long getCreationTime(String JavaDoc uri) {
176         Cache cacheMap = (Cache) cachedURIList.get(uri);
177         if (cacheMap == null) {
178             return -1;
179         }
180         return cacheMap.time;
181     }
182
183     /**
184      * @param uri request URI
185      * @return size as on disk
186      * @deprecated use getSize(HttpServletRequest). Cache could decide to handle requests internally using not only the
187      * request URI
188      */

189     public static int getSize(String JavaDoc uri) {
190         Cache cacheMap = (Cache) cachedURIList.get(uri);
191         return cacheMap.size;
192     }
193
194     /**
195      * @param uri request URI
196      * @return size as on disk
197      * @deprecated use getCompressedSize(HttpServletRequest). Cache could decide to handle requests internally using not
198      * only the request URI
199      */

200     public static int getCompressedSize(String JavaDoc uri) {
201         Cache cacheMap = (Cache) cachedURIList.get(uri);
202         return cacheMap.compressedSize;
203     }
204
205     /**
206      * @param uri request URI
207      * @return true is the request URI is cached
208      * @deprecated use isCached(HttpServletRequest). Cache could decide to handle requests internally using not only the
209      * request URI
210      */

211     public static boolean isCached(String JavaDoc uri) {
212         return Cache.cachedURIList.get(uri) != null;
213     }
214
215     /**
216      * @param uri request URI
217      * @return true is the request URI is being cached
218      * @deprecated use isInCacheProcess(HttpServletRequest). Cache could decide to handle requests internally using not
219      * only the request URI
220      */

221     public static boolean isInCacheProcess(String JavaDoc uri) {
222         return Cache.inProcessURIList.get(uri) != null;
223     }
224
225 }
226
Popular Tags