001    package railo.runtime.cache;
002    
003    import java.io.IOException;
004    import java.util.HashMap;
005    import java.util.Iterator;
006    import java.util.List;
007    import java.util.Map;
008    
009    import org.apache.oro.text.regex.MalformedPatternException;
010    
011    import railo.commons.io.cache.Cache;
012    import railo.commons.io.cache.CacheEntry;
013    import railo.commons.io.cache.exp.CacheException;
014    import railo.loader.util.Util;
015    import railo.runtime.cache.util.CacheKeyFilterAll;
016    import railo.runtime.cache.util.WildCardFilter;
017    import railo.runtime.op.Caster;
018    import railo.runtime.type.Array;
019    import railo.runtime.type.ArrayImpl;
020    import railo.runtime.type.Struct;
021    import railo.runtime.type.StructImpl;
022    import railo.runtime.type.dt.TimeSpan;
023    // MUST this must be come from configuration
024    public class CacheEngine {
025    
026    
027            private static Map caches=new HashMap();
028            private Cache cache;
029            
030            public CacheEngine(Cache cache) {
031                    this.cache=cache;
032            }
033    
034    
035            public void delete(String key,boolean throwWhenNotExists) throws IOException {
036                    if(!cache.remove(key) && throwWhenNotExists)
037                            throw new CacheException("there is no entry in cache with key ["+key+"]");
038            }
039    
040            public boolean exists(String key) {
041                    return cache.contains(key);
042            }
043    
044            public int flush(String key, String filter) throws MalformedPatternException, IOException {
045                    if(!Util.isEmpty(key)) return cache.remove(key)?1:0;
046                    if(!Util.isEmpty(filter)) return cache.remove(new WildCardFilter(filter,false));
047                    return cache.remove(CacheKeyFilterAll.getInstance());
048            }
049    
050            public Object get(String key, Object defaultValue) {
051                    return cache.getValue(key, defaultValue);
052            }
053    
054            public Object get(String key) throws IOException {
055                    return cache.getValue(key);
056            }
057    
058            public Array keys(String filter) {
059                    try {
060                            List keys;
061                            if(Util.isEmpty(filter)) keys=cache.keys();
062                            else keys=cache.keys(new WildCardFilter(filter,false));
063                            return Caster.toArray(keys);
064                    } 
065                    catch (Exception e) {}
066                    return new ArrayImpl();
067            }
068    
069            public Struct list(String filter) {
070                    
071                    Struct sct=new StructImpl();
072                    try {
073                            List entries;
074                            if(Util.isEmpty(filter)) entries=cache.entries();
075                            else entries=cache.entries(new WildCardFilter(filter,false));
076                            
077                            Iterator it = entries.iterator();
078                            CacheEntry entry;
079                            while(it.hasNext()){
080                                    entry=(CacheEntry) it.next();
081                                    sct.setEL(entry.getKey(), entry.getValue());
082                            }
083                    } 
084                    catch (Exception e) {e.printStackTrace();}
085                    return sct;
086            }
087    
088            public void set(String key, Object value, TimeSpan timespan) {
089                    Long until=timespan==null?null:Long.valueOf(timespan.getMillis());
090                    cache.put(key, value, null, until); 
091            }
092    
093            public Struct info() {
094                    return cache.getCustomInfo();
095            }
096            
097            public Struct info(String key) throws IOException {
098                    if(key==null) return info();
099                    CacheEntry entry = cache.getCacheEntry(key);
100                    return entry.getCustomInfo();
101            }
102    
103            public Cache getCache() {
104                    return cache;
105            }
106    
107    
108    
109            
110    
111            /*public static void updateConnection(Config config, String name, String className, Struct connection, boolean _default) throws IOException {
112                    Document doc = getDocument(config);
113                    
114                    Element parent= getChildByName(doc.getDocumentElement(),"connections");
115            Element[] connections=getChildren(parent,"connection");
116            Element conn;
117            String str;
118            
119            // update
120            boolean updated=false;
121            for(int i=0;i<connections.length;i++){
122                    conn = connections[i];
123                    str=conn.getAttribute("name");
124                    if(_default)conn.setAttribute("default", "false");
125                    if(!Util.isEmpty(str) && str.trim().equalsIgnoreCase(name.trim())){
126                            updateConnection(conn,name,className,connection,_default);
127                            updated=true;
128                    }
129            }
130            
131            // insert
132            if(!updated){
133                    conn=doc.createElement("connection");
134                    updateConnection(conn,name,className,connection,_default);
135                    parent.appendChild(conn);
136            }
137            store(config,doc);
138            }
139    
140            private static void updateConnection(Element conn, String name, String className, Struct custom, boolean _default) {
141                    conn.setAttribute("name", name);
142                    conn.setAttribute("class", className);
143                    conn.setAttribute("custom", toString(custom));
144                    conn.setAttribute("default", _default?"true":"false");
145            }
146    
147            public static void deleteConnection(Config config, String name) throws IOException {
148                    Document doc = getDocument(config);
149                    
150                    Element parent= getChildByName(doc.getDocumentElement(),"connections");
151            Element[] connections=getChildren(parent,"connection");
152            Element conn;
153            String str;
154            for(int i=0;i<connections.length;i++){
155                    conn = connections[i];
156                    str=conn.getAttribute("name");
157                    if(Util.isEmpty(str) || !str.trim().equalsIgnoreCase(name.trim()))
158                            continue;
159                    parent.removeChild(conn);
160            }
161            store(config,doc);
162            }*/
163    
164            
165    
166            
167            
168        
169    
170            
171    }