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.List;
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.getConfig(),cacheName,ConfigImpl.CACHE_DEFAULT_OBJECT);
039                    } catch (IOException e) {
040                            throw Caster.toPageException(e);
041                    }
042                    StringBuffer sb=null;
043                    while(it.hasNext()){
044                            id= Util.key(Caster.toString(it.next()));
045                            if(!cache.remove(id) && throwOnError){
046                                    if(sb==null)sb=new StringBuffer();
047                                    else sb.append(',');
048                                    sb.append(id);
049                            }
050                                    
051                    }
052                    if(throwOnError && sb!=null)
053                            throw new ApplicationException("can not remove the elements with the following id(s) ["+sb+"]");
054                    return null;
055            }
056            private static Array toArray(Object oIds) throws PageException {
057                    if(Decision.isArray(oIds)){
058                            return Caster.toArray(oIds);
059                    }
060                    return List.listToArray(Caster.toString(oIds), ',');
061            }
062            
063    }