001: // PushCacheManager.java
002: // $Id: PushCacheManager.java,v 1.2 2007/02/09 22:21:53 ylafon Exp $
003: // (c) COPYRIGHT MIT, INRIA and Keio, 2001.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.www.protocol.http.cache.push;
007:
008: import org.w3c.util.ArrayDictionary;
009: import org.w3c.www.protocol.http.Request;
010: import org.w3c.www.protocol.http.HttpManager;
011:
012: import org.w3c.www.protocol.http.cache.CachedResource;
013: import org.w3c.www.protocol.http.cache.CacheGeneration;
014:
015: import java.net.URL;
016:
017: import java.util.Enumeration;
018:
019: /**
020: * Singleton class to manage push cache.
021: * @see PushCacheHandler
022: *
023: * @author Paul Henshaw, The Fantastic Corporation, Paul.Henshaw@fantastic.com
024: * @version $Revision: 1.2 $
025: * $Id: PushCacheManager.java,v 1.2 2007/02/09 22:21:53 ylafon Exp $
026: */
027: public class PushCacheManager {
028: /**
029: * Additional header field and value to identify push resources
030: */
031: public static final String HEADER_FIELD = "tfc_from_push_stream";
032: public static final String HEADER_VALUE = "yes";
033: private static PushCacheManager _instance = null;
034:
035: private PushCacheFilter _filter = null;
036:
037: /**
038: * Access to manager
039: */
040: public static PushCacheManager instance() {
041: if (_instance == null) {
042: _instance = new PushCacheManager();
043: }
044: return (_instance);
045: }
046:
047: /**
048: * <bold>true</bold> iff res contains extra header identifying it as
049: * having been inserted directly into the cache from a PUSH source
050: */
051: public boolean isPushResource(CachedResource res) {
052: if (res != null) {
053: ArrayDictionary dict = res.getExtraHeaders();
054: String v = (String) dict.get(HEADER_FIELD);
055: if (v != null && v.equalsIgnoreCase(HEADER_VALUE)) {
056: return (true);
057: }
058: }
059: return (false);
060: }
061:
062: /**
063: * Store a push reply in the cache
064: */
065: public void storeReply(PushReply reply) {
066: try {
067: String url = reply.getUrl();
068:
069: //
070: // forge request for this reply
071: //
072: Request request = HttpManager.getManager().createRequest();
073: request.setMethod("GET");
074: request.setURL(new URL(url));
075:
076: //
077: // remove old page if present
078: //
079: removeURL(url);
080:
081: //
082: // Store new resource
083: //
084: CachedResource res = new PushEntityCachedResource(_filter,
085: request, reply);
086: getStore().storeCachedResource(res, res.getContentLength());
087: _filter.sync();
088: } catch (Exception e) {
089: e.printStackTrace();
090: }
091: }
092:
093: /**
094: * Remove a reply from the cache
095: */
096: public void removeURL(String url) {
097: try {
098: deleteRes(getStore().getCachedResourceReference(url));
099: } catch (Exception e) {
100: e.printStackTrace();
101: }
102: }
103:
104: /**
105: * True iff url is present in cache
106: */
107: public boolean isPresent(String url) {
108: try {
109: CachedResource res = getStore().getCachedResourceReference(
110: url);
111: if (res == null) {
112: // never been present in cache
113: return (false);
114: }
115:
116: if (res.getFile() == null || !res.getFile().exists()) {
117: // We have seen this before, but has since been deleted
118: return (false);
119: }
120: return (true);
121: } catch (Exception e) {
122: e.printStackTrace();
123: }
124: return (false);
125: }
126:
127: /**
128: * Remove resource from cache
129: */
130: protected void deleteRes(CachedResource res) {
131: if (res != null) {
132: res.delete();
133: getStore().getState().notifyResourceDeleted(res);
134: _filter.sync();
135: }
136: }
137:
138: /**
139: * Remove all entries from the cache
140: */
141: public void cleanCache() {
142: CacheGeneration gen = getStore().getMRUGeneration();
143: while (gen != null) {
144: Enumeration e = gen.getCachedResources();
145: while (e.hasMoreElements()) {
146: deleteRes((CachedResource) e.nextElement());
147: }
148: gen = getStore().getNextGeneration(gen);
149: }
150: }
151:
152: /**
153: * Called by PushCacheFilter on initialization
154: */
155: protected void registerFilter(PushCacheFilter cf) {
156: _filter = cf;
157: }
158:
159: /**
160: * Singleton; no public constructor
161: * @see #instance
162: */
163: protected PushCacheManager() {
164: // NULL
165: }
166:
167: /**
168: * Access to filter store
169: */
170: protected PushCacheStore getStore() {
171: return (_filter.getPushCacheStore());
172: }
173: }
|