001 package railo.runtime.type.scope.storage.clean; 002 003 import java.io.IOException; 004 import java.util.Iterator; 005 import java.util.List; 006 import java.util.Map; 007 import java.util.Map.Entry; 008 009 import railo.commons.io.cache.Cache; 010 import railo.commons.io.cache.CacheEntry; 011 import railo.commons.io.cache.CacheKeyFilter; 012 import railo.runtime.cache.CacheConnection; 013 import railo.runtime.config.ConfigWebImpl; 014 import railo.runtime.type.scope.storage.StorageScopeCache; 015 import railo.runtime.type.scope.storage.StorageScopeEngine; 016 import railo.runtime.type.scope.storage.StorageScopeListener; 017 018 public class CacheStorageScopeCleaner extends StorageScopeCleanerSupport { 019 020 private Filter filter; 021 022 public CacheStorageScopeCleaner(int type,StorageScopeListener listener) { 023 super(type,listener,INTERVALL_MINUTE); 024 //this.strType=VariableInterpreter.scopeInt2String(type); 025 filter=new Filter(strType); 026 } 027 028 public void init(StorageScopeEngine engine) { 029 super.init(engine); 030 031 } 032 033 protected void _clean() { 034 ConfigWebImpl config = (ConfigWebImpl) engine.getFactory().getConfig(); 035 Map connections = config.getCacheConnections(); 036 CacheConnection cc; 037 038 if(connections!=null) { 039 Map.Entry<String, CacheConnection> entry; 040 Iterator<Entry<String, CacheConnection>> it = connections.entrySet().iterator(); 041 while(it.hasNext()){ 042 entry=it.next(); 043 cc=entry.getValue(); 044 if(cc.isStorage()){ 045 try { 046 clean(cc,config); 047 } catch (IOException e) { 048 error(e); 049 } 050 } 051 } 052 } 053 054 } 055 056 private void clean(CacheConnection cc, ConfigWebImpl config) throws IOException { 057 Cache cache = cc.getInstance(config); 058 int len=filter.length(),index; 059 List entries = cache.entries(filter); 060 CacheEntry ce; 061 long expires; 062 063 String key,appname,cfid; 064 if(entries.size()>0){ 065 Iterator<CacheEntry> it = entries.iterator(); 066 while(it.hasNext()){ 067 ce=it.next(); 068 expires=ce.lastModified().getTime()+ce.idleTimeSpan()-StorageScopeCache.SAVE_EXPIRES_OFFSET; 069 if(expires<=System.currentTimeMillis()) { 070 key=ce.getKey().substring(len); 071 index=key.indexOf(':'); 072 cfid=key.substring(0,index); 073 appname=key.substring(index+1); 074 075 if(listener!=null)listener.doEnd(engine, this,appname, cfid); 076 info("remove "+strType+"/"+appname+"/"+cfid+" from cache "+cc.getName()); 077 engine.remove(type,appname,cfid); 078 cache.remove(ce.getKey()); 079 } 080 } 081 } 082 083 //engine.remove(type,appName,cfid); 084 085 086 //return (Struct) cache.getValue(key,null); 087 } 088 089 public static class Filter implements CacheKeyFilter { 090 private String startsWith; 091 092 public Filter(String type){ 093 startsWith="railo-storage:"+type+":"; 094 } 095 096 public String toPattern() { 097 // TODO Auto-generated method stub 098 return startsWith+"*"; 099 } 100 101 public boolean accept(String key) { 102 return key.startsWith(startsWith); 103 } 104 105 public int length() { 106 return startsWith.length(); 107 } 108 109 } 110 }