1 50 51 package org.openlaszlo.iv.flash.cache; 52 53 import org.openlaszlo.iv.flash.api.*; 54 import org.openlaszlo.iv.flash.util.*; 55 import org.openlaszlo.iv.flash.url.*; 56 import java.util.*; 57 import java.io.*; 58 59 import org.w3c.dom.Document ; 60 61 64 public class XMLCache extends GenericCache { 65 66 private static XMLCache instance = new XMLCache(); 67 68 private XMLCache() { 69 } 70 71 public static XMLCache getInstance() { 72 return instance; 73 } 74 75 public static CacheSettings getSettings() { 76 return instance.getMySettings(); 77 } 78 79 84 public static Document getXMLDocument( IVUrl url ) { 85 return getXMLDocument( url.getName() ); 86 } 87 88 93 public static Document getXMLDocument( String key ) { 94 CacheItem item = instance.getItem( key ); 95 if( item == null ) return null; 96 return (Document ) item.getObject(); 97 } 98 99 105 public static void addXMLDocument( IVUrl url, Document doc ) { 106 long lifespan = getSettings().getDefaultExpire(); 108 if( !getSettings().isForce() ) { 109 String gxcStr = url.getParameter( "gxc" ); 110 if( gxcStr != null ) { 111 if( !Util.toBool(gxcStr, false) ) return; 112 String v = url.getParameter( "gxe" ); 113 if( v != null ) { 114 lifespan = Util.toLong( v, -1L )*1000L; 115 } 116 } else { 117 return; 118 } 119 } 120 long now = System.currentTimeMillis(); 121 if( lifespan <= 0 ) { 122 lifespan = Long.MAX_VALUE-now; 123 } 124 long expire = now + lifespan; 125 CacheItem item = new XMLCacheItem( url, doc, now, expire, getSettings().isCheckModifiedSince() ); 126 instance.addItem( item ); 127 } 128 129 protected boolean isModified( CacheItem item ) { 130 if( !getSettings().isCheckModifiedSince() ) return super.isModified( item ); 131 XMLCacheItem it = (XMLCacheItem) item; 132 it.getUrl().refresh(); 133 return it.lastModified() != it.getUrl().lastModified(); 134 } 135 136 public static class XMLCacheItem extends CacheItem { 137 private long lastModified = 0; 138 private IVUrl url; 139 140 public XMLCacheItem( IVUrl url, Document doc, long cacheTime, long expireAfter, boolean isCheckModifiedSince ) { 141 super( url.getName(), doc, 1, cacheTime, expireAfter ); 142 this.url = url; 143 if( isCheckModifiedSince ) { 144 this.lastModified = url.lastModified(); 145 } 146 } 147 148 public IVUrl getUrl() { return url; } 149 public long lastModified() { return lastModified; } 150 } 151 152 } 153 | Popular Tags |