001 package railo.runtime.functions.cache; 002 003 import java.io.IOException; 004 import java.util.Iterator; 005 006 import railo.commons.io.cache.Cache; 007 import railo.runtime.PageContext; 008 import railo.runtime.config.ConfigImpl; 009 import railo.runtime.exp.ApplicationException; 010 import railo.runtime.exp.PageException; 011 import railo.runtime.ext.function.Function; 012 import railo.runtime.op.Caster; 013 import railo.runtime.op.Decision; 014 import railo.runtime.type.Array; 015 import railo.runtime.type.util.ListUtil; 016 017 /** 018 * 019 */ 020 public final class CacheRemove implements Function { 021 022 private static final long serialVersionUID = -5823359978885018762L; 023 024 public static String call(PageContext pc, Object ids) throws PageException { 025 return call(pc, ids, false,null); 026 } 027 public static String call(PageContext pc, Object ids, boolean throwOnError) throws PageException { 028 return call(pc, ids, throwOnError, null); 029 } 030 031 032 public static String call(PageContext pc, Object ids, boolean throwOnError, String cacheName) throws PageException { 033 Array arr = toArray(ids);// 034 Iterator it = arr.valueIterator(); 035 String id; 036 Cache cache; 037 try { 038 cache = Util.getCache(pc,cacheName,ConfigImpl.CACHE_DEFAULT_OBJECT); 039 } catch (IOException e) { 040 throw Caster.toPageException(e); 041 } 042 StringBuffer sb=null; 043 try{ 044 while(it.hasNext()){ 045 id= Util.key(Caster.toString(it.next())); 046 if(!cache.remove(id) && throwOnError){ 047 if(sb==null)sb=new StringBuffer(); 048 else sb.append(','); 049 sb.append(id); 050 } 051 } 052 } 053 catch (IOException e) {} 054 if(throwOnError && sb!=null) 055 throw new ApplicationException("can not remove the elements with the following id(s) ["+sb+"]"); 056 return null; 057 } 058 private static Array toArray(Object oIds) throws PageException { 059 if(Decision.isArray(oIds)){ 060 return Caster.toArray(oIds); 061 } 062 return ListUtil.listToArray(Caster.toString(oIds), ','); 063 } 064 065 }