001    package railo.runtime.cache.legacy;
002    
003    import java.io.IOException;
004    import java.io.OutputStream;
005    
006    import javax.servlet.http.HttpServletRequest;
007    
008    import org.apache.oro.text.regex.MalformedPatternException;
009    
010    import railo.commons.io.cache.Cache;
011    import railo.commons.io.res.Resource;
012    import railo.commons.lang.StringUtil;
013    import railo.runtime.PageContext;
014    import railo.runtime.config.ConfigImpl;
015    import railo.runtime.functions.cache.Util;
016    import railo.runtime.type.dt.TimeSpan;
017    
018     
019    public abstract class CacheItem {
020    
021            protected final String fileName;
022            public static CacheItem getInstance(PageContext pc, String id, String key, boolean useId, Resource dir, String cacheName, TimeSpan timespan) throws IOException{
023                    HttpServletRequest req = pc. getHttpServletRequest();
024            Cache cache = Util.getCache(pc.getConfig(),cacheName,ConfigImpl.CACHE_DEFAULT_TEMPLATE,null);   
025                    if(cache!=null) 
026                            return new CacheItemCache(pc, req, id, key, useId, cache,timespan);
027                    return new CacheItemFS(pc, req, id, key, useId, dir);
028            }
029            
030            public CacheItem(PageContext pc, HttpServletRequest req, String id, String key, boolean useId) throws IOException {
031                    
032                    //raw
033                    String filename=req.getServletPath();
034            if(!StringUtil.isEmpty(req.getQueryString())) {
035                    filename+="?"+req.getQueryString();
036                    if(useId)filename+="&cfcache_id="+id;
037            }
038            else {
039                    if(useId)filename+="?cfcache_id="+id;
040            }
041            if(useId && !StringUtil.isEmpty(key)) filename=key;
042            if(!StringUtil.isEmpty(req.getContextPath())) filename=req.getContextPath()+filename;
043            fileName=filename;
044            
045            
046            
047                    
048            }
049            
050            
051            
052            public abstract boolean isValid();
053            
054            public abstract boolean isValid(TimeSpan timespan);
055            
056            public abstract void writeTo(OutputStream os, String charset) throws IOException;
057            
058            public abstract String getValue() throws IOException;
059            
060            public abstract void store(String result) throws IOException;
061            
062            public abstract void store(byte[] barr,boolean append) throws IOException;
063    
064            //protected abstract void _flushAll(PageContext pc, Resource dir) throws IOException;
065    
066            //protected abstract void _flush(PageContext pc, Resource dir, String expireurl) throws IOException;
067            
068            public static void flushAll(PageContext pc, Resource dir, String cacheName) throws IOException {
069                    Cache cache = Util.getCache(pc.getConfig(),cacheName,ConfigImpl.CACHE_DEFAULT_TEMPLATE,null);   
070                    if(cache!=null) CacheItemCache._flushAll(pc, cache);
071                    else CacheItemFS._flushAll(pc, dir);
072            }
073    
074            public static void flush(PageContext pc, Resource dir, String cacheName,String expireurl) throws IOException, MalformedPatternException {
075                    Cache cache = Util.getCache(pc.getConfig(),cacheName,ConfigImpl.CACHE_DEFAULT_TEMPLATE,null);   
076                    if(cache!=null) CacheItemCache._flush(pc, cache,expireurl);
077                    else CacheItemFS._flush(pc, dir, expireurl);
078            }
079    }