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            @Override
018            public List<String> keys(CacheKeyFilter filter) throws IOException {
019                    List<String> keys = keys();
020                    List<String> list=new ArrayList<String>();
021                    Iterator<String> it = keys.iterator();
022                    String key;
023                    while(it.hasNext()){
024                            key= it.next();
025                            if(filter.accept(key))list.add(key);
026                    }
027                    return list;
028            }
029            
030            @Override
031            public List<CacheEntry> keys(CacheEntryFilter filter) throws IOException {
032                    List<String> keys = keys();
033                    List<CacheEntry> list=new ArrayList<CacheEntry>();
034                    Iterator<String> it = keys.iterator();
035                    String key;
036                    CacheEntry entry;
037                    while(it.hasNext()){
038                            key=it.next();
039                            entry=getQuiet(key,null);
040                            if(filter.accept(entry))list.add(entry);
041                    }
042                    return list;
043            }
044            
045            @Override
046            public List<CacheEntry> entries() throws IOException {
047                    List<String> keys = keys();
048                    List<CacheEntry> list=new ArrayList<CacheEntry>();
049                    Iterator<String> it = keys.iterator();
050                    while(it.hasNext()){
051                            list.add(getQuiet(it.next(),null));
052                    }
053                    return list;
054            }
055            
056            @Override
057            public List<CacheEntry> entries(CacheKeyFilter filter) throws IOException {
058                    List<String> keys = keys();
059                    List<CacheEntry> list=new ArrayList<CacheEntry>();
060                    Iterator<String> it = keys.iterator();
061                    String key;
062                    while(it.hasNext()){
063                            key=it.next();
064                            if(filter.accept(key))list.add(getQuiet(key,null));
065                    }
066                    return list;
067            }
068            
069            @Override
070            public List<CacheEntry> entries(CacheEntryFilter filter) throws IOException {
071                    List<String> keys = keys();
072                    List<CacheEntry> list=new ArrayList<CacheEntry>();
073                    Iterator<String> it = keys.iterator();
074                    CacheEntry entry;
075                    while(it.hasNext()){
076                            entry=getQuiet(it.next(),null);
077                            if(filter.accept(entry))list.add(entry);
078                    }
079                    return list;
080            }
081    
082            // there was the wrong generic type defined in the older interface, because of that we do not define a generic type at all here, just to be sure
083            @Override
084            public List values() throws IOException {
085                    List<String> keys = keys();
086                    List<Object> list=new ArrayList<Object>();
087                    Iterator<String> it = keys.iterator();
088                    String key;
089                    while(it.hasNext()){
090                            key=it.next();
091                            list.add(getQuiet(key,null).getValue());
092                    }
093                    return list;
094            }
095    
096            // there was the wrong generic type defined in the older interface, because of that we do not define a generic type at all here, just to be sure
097            @Override
098            public List values(CacheEntryFilter filter) throws IOException {
099                    List<String> keys = keys();
100                    List<Object> list=new ArrayList<Object>();
101                    Iterator<String> it = keys.iterator();
102                    String key;
103                    CacheEntry entry;
104                    while(it.hasNext()){
105                            key=it.next();
106                            entry=getQuiet(key,null);
107                            if(filter.accept(entry))list.add(entry.getValue());
108                    }
109                    return list;
110            }
111    
112            // there was the wrong generic type defined in the older interface, because of that we do not define a generic type at all here, just to be sure
113            @Override
114            public List values(CacheKeyFilter filter) throws IOException {
115                    List<String> keys = keys();
116                    List<Object> list=new ArrayList<Object>();
117                    Iterator<String> it = keys.iterator();
118                    String key;
119                    while(it.hasNext()){
120                            key=it.next();
121                            if(filter.accept(key))list.add(getQuiet(key,null).getValue());
122                    }
123                    return list;
124            }
125            
126            @Override
127            public int remove(CacheEntryFilter filter) throws IOException {
128                    List<String> keys = keys();
129                    int count=0;
130                    Iterator<String> it = keys.iterator();
131                    String key;
132                    CacheEntry entry;
133                    while(it.hasNext()){
134                            key=it.next();
135                            entry=getQuiet(key,null);
136                            if(filter==null || filter.accept(entry)){
137                                    remove(key);
138                                    count++;
139                            }
140                    }
141                    return count;
142            }
143            
144    
145            @Override
146            public int remove(CacheKeyFilter filter) throws IOException {
147                    List<String> keys = keys();
148                    int count=0;
149                    Iterator<String> it = keys.iterator();
150                    String key;
151                    while(it.hasNext()){
152                            key=it.next();
153                            if(filter==null || filter.accept(key)){
154                                    remove(key);
155                                    count++;
156                            }
157                    }
158                    return count;
159            }
160            
161            public Struct getCustomInfo() {
162                    return CacheUtil.getInfo(this);
163            }
164            
165    
166            @Override
167            public Object getValue(String key) throws IOException {
168                    return getCacheEntry(key).getValue();
169            }
170    
171            @Override
172            public Object getValue(String key, Object defaultValue) {
173                    CacheEntry entry = getCacheEntry(key,null);
174                    if(entry==null) return defaultValue;
175                    return entry.getValue();
176            } 
177            
178            protected static boolean valid(CacheEntry entry) {
179                    long now = System.currentTimeMillis();
180                    if(entry.liveTimeSpan()>0 && entry.liveTimeSpan()+entry.lastModified().getTime()<now){
181                            return false;
182                    }
183                    if(entry.idleTimeSpan()>0 && entry.idleTimeSpan()+entry.lastHit().getTime()<now){
184                            return false;
185                    }
186                    return true;
187            }
188            
189            @Override
190            public CacheEntry getCacheEntry(String key) throws IOException {
191                    CacheEntry entry = getCacheEntry(key, null);
192                    if(entry==null) throw new CacheException("there is no valid cache entry with key ["+key+"]");
193                    return entry;
194            }
195            
196            public CacheEntry getQuiet(String key) throws IOException {
197                    CacheEntry entry = getQuiet(key, null);
198                    if(entry==null) throw new CacheException("there is no valid cache entry with key ["+key+"]");
199                    return entry;
200            }
201    
202            public abstract CacheEntry getQuiet(String key, CacheEntry defaultValue);
203            
204    
205    }