001    package railo.runtime.cache;
002    
003    import java.io.IOException;
004    import java.util.ArrayList;
005    import java.util.Iterator;
006    import java.util.List;
007    
008    import railo.commons.io.cache.Cache;
009    import railo.commons.io.cache.CacheEntry;
010    import railo.commons.io.cache.CacheEntryFilter;
011    import railo.commons.io.cache.CacheKeyFilter;
012    import railo.commons.io.cache.exp.CacheException;
013    import railo.runtime.type.Struct;
014    
015    public abstract class CacheSupport implements Cache {
016    
017            /**
018             * @see railo.commons.io.cache.Cache#keys(railo.commons.io.cache.CacheKeyFilter)
019             */
020            public List keys(CacheKeyFilter filter) {
021                    List keys = keys();
022                    List list=new ArrayList();
023                    Iterator it = keys.iterator();
024                    String key;
025                    while(it.hasNext()){
026                            key=(String) it.next();
027                            if(filter.accept(key))list.add(key);
028                    }
029                    return list;
030            }
031            
032            /**
033             * @see railo.commons.io.cache.Cache#keys(railo.commons.io.cache.CacheEntryFilter)
034             */
035            public List keys(CacheEntryFilter filter) {
036                    List keys = keys();
037                    List list=new ArrayList();
038                    Iterator it = keys.iterator();
039                    String key;
040                    CacheEntry entry;
041                    while(it.hasNext()){
042                            key=(String) it.next();
043                            entry=getQuiet(key,null);
044                            if(filter.accept(entry))list.add(key);
045                    }
046                    return list;
047            }
048            
049            /**
050             * @see railo.commons.io.cache.Cache#entries()
051             */
052            public List entries() {
053                    List keys = keys();
054                    List list=new ArrayList();
055                    Iterator it = keys.iterator();
056                    String key;
057                    while(it.hasNext()){
058                            key=(String) it.next();
059                            list.add(getQuiet(key,null));
060                    }
061                    return list;
062            }
063            
064            /**
065             * @see railo.commons.io.cache.Cache#entries(railo.commons.io.cache.CacheKeyFilter)
066             */
067            public List entries(CacheKeyFilter filter) {
068                    List keys = keys();
069                    List list=new ArrayList();
070                    Iterator it = keys.iterator();
071                    String key;
072                    while(it.hasNext()){
073                            key=(String) it.next();
074                            if(filter.accept(key))list.add(getQuiet(key,null));
075                    }
076                    return list;
077            }
078            
079            /**
080             * @see railo.commons.io.cache.Cache#entries(railo.commons.io.cache.CacheEntryFilter)
081             */
082            public List entries(CacheEntryFilter filter) {
083                    List keys = keys();
084                    List list=new ArrayList();
085                    Iterator it = keys.iterator();
086                    String key;
087                    CacheEntry entry;
088                    while(it.hasNext()){
089                            key=(String) it.next();
090                            entry=getQuiet(key,null);
091                            if(filter.accept(entry))list.add(entry);
092                    }
093                    return list;
094            }
095            
096            /**
097             * @see railo.commons.io.cache.Cache#values()
098             */
099            public List values() {
100                    List keys = keys();
101                    List list=new ArrayList();
102                    Iterator it = keys.iterator();
103                    String key;
104                    while(it.hasNext()){
105                            key=(String) it.next();
106                            list.add(getQuiet(key,null).getValue());
107                    }
108                    return list;
109            }
110            
111    
112            /**
113             * @see railo.commons.io.cache.Cache#values(railo.commons.io.cache.CacheEntryFilter)
114             */
115            public List values(CacheEntryFilter filter) {
116                    List keys = keys();
117                    List list=new ArrayList();
118                    Iterator it = keys.iterator();
119                    String key;
120                    CacheEntry entry;
121                    while(it.hasNext()){
122                            key=(String) it.next();
123                            entry=getQuiet(key,null);
124                            if(filter.accept(entry))list.add(entry.getValue());
125                    }
126                    return list;
127            }
128            
129            /**
130             * @see railo.commons.io.cache.Cache#values(railo.commons.io.cache.CacheKeyFilter)
131             */
132            public List values(CacheKeyFilter filter) {
133                    List keys = keys();
134                    List list=new ArrayList();
135                    Iterator it = keys.iterator();
136                    String key;
137                    while(it.hasNext()){
138                            key=(String) it.next();
139                            if(filter.accept(key))list.add(getQuiet(key,null).getValue());
140                    }
141                    return list;
142            }
143            
144            /**
145             * @see railo.commons.io.cache.Cache#remove(railo.commons.io.cache.CacheEntryFilter)
146             */
147            public int remove(CacheEntryFilter filter) {
148                    List keys = keys();
149                    int count=0;
150                    Iterator it = keys.iterator();
151                    String key;
152                    CacheEntry entry;
153                    while(it.hasNext()){
154                            key=(String) it.next();
155                            entry=getQuiet(key,null);
156                            if(filter==null || filter.accept(entry)){
157                                    remove(key);
158                                    count++;
159                            }
160                    }
161                    return count;
162            }
163            
164    
165            /**
166             * @see railo.commons.io.cache.Cache#remove(railo.commons.io.cache.CacheKeyFilter)
167             */
168            public int remove(CacheKeyFilter filter) {
169                    List keys = keys();
170                    int count=0;
171                    Iterator it = keys.iterator();
172                    String key;
173                    while(it.hasNext()){
174                            key=(String) it.next();
175                            if(filter==null || filter.accept(key)){
176                                    remove(key);
177                                    count++;
178                            }
179                    }
180                    return count;
181            }
182            
183            public Struct getCustomInfo() {
184                    return CacheUtil.getInfo(this);
185            }
186            
187    
188            /**
189             * @see railo.commons.io.cache.Cache#getValue(java.lang.String)
190             */
191            public Object getValue(String key) throws IOException {
192                    return getCacheEntry(key).getValue();
193            }
194    
195            /**
196             * @see railo.commons.io.cache.Cache#getValue(java.lang.String, java.lang.Object)
197             */
198            public Object getValue(String key, Object defaultValue) {
199                    CacheEntry entry = getCacheEntry(key,null);
200                    if(entry==null) return defaultValue;
201                    return entry.getValue();
202            } 
203            
204            protected static boolean valid(CacheEntry entry) {
205                    long now = System.currentTimeMillis();
206                    if(entry.liveTimeSpan()>0 && entry.liveTimeSpan()+entry.created().getTime()<now){
207                            return false;
208                    }
209                    if(entry.idleTimeSpan()>0 && entry.idleTimeSpan()+entry.lastHit().getTime()<now){
210                            return false;
211                    }
212                    return true;
213            }
214            
215            /**
216             * @see railo.commons.io.cache.Cache#getCacheEntry(java.lang.String)
217             */
218            public CacheEntry getCacheEntry(String key) throws IOException {
219                    CacheEntry entry = getCacheEntry(key, null);
220                    if(entry==null) throw new CacheException("there is no valid cache entry with key ["+key+"]");
221                    return entry;
222            }
223            
224            public CacheEntry getQuiet(String key) throws IOException {
225                    CacheEntry entry = getQuiet(key, null);
226                    if(entry==null) throw new CacheException("there is no valid cache entry with key ["+key+"]");
227                    return entry;
228            }
229    
230            public abstract CacheEntry getQuiet(String key, CacheEntry defaultValue);
231            
232    
233    }