001    package railo.runtime.functions.cache;
002    
003    import java.io.IOException;
004    
005    import railo.commons.io.cache.Cache;
006    import railo.commons.lang.StringUtil;
007    import railo.runtime.PageContext;
008    import railo.runtime.config.ConfigImpl;
009    import railo.runtime.exp.FunctionException;
010    import railo.runtime.exp.PageException;
011    import railo.runtime.ext.function.Function;
012    import railo.runtime.op.Caster;
013    
014    /**
015     * 
016     */
017    public final class CacheGet implements Function {
018    
019            private static final long serialVersionUID = -7164470356423036571L;
020    
021            public static Object call(PageContext pc, String key) throws PageException {
022                    try {
023                            return _call(pc, key, false, Util.getDefault(pc, ConfigImpl.CACHE_DEFAULT_OBJECT));
024                    } 
025                    catch (IOException e) {
026                            throw Caster.toPageException(e);
027                    }
028            }
029    
030            public static Object call(PageContext pc, String key, Object objThrowWhenNotExist) throws PageException {
031                    // default behavior, second parameter is a boolean
032                    Boolean throwWhenNotExist=Caster.toBoolean(objThrowWhenNotExist,null);
033                    if(throwWhenNotExist!=null) {
034                            try {
035                                    return _call(pc, key, throwWhenNotExist.booleanValue(), Util.getDefault(pc, ConfigImpl.CACHE_DEFAULT_OBJECT));
036                            } 
037                            catch (IOException e) {
038                                    throw Caster.toPageException(e);
039                            }
040                    }
041                    
042                    // compatibility behavior, second parameter is a cacheName 
043                    if(objThrowWhenNotExist instanceof String) {
044                            String cacheName=(String)objThrowWhenNotExist;
045                            if(!StringUtil.isEmpty(cacheName)) {
046                                    try {
047                                            Cache cache = Util.getCache(pc.getConfig(),cacheName,null);
048                                            
049                                            if(cache!=null) 
050                                                    return _call(pc, key, false, cache);
051                                    } 
052                                    catch (IOException e) {
053                                            throw Caster.toPageException(e);
054                                    }
055                            }
056                    }
057                    
058                    // not a boolean or cacheName
059                    throw new FunctionException(pc, "cacheGet", 2, "ThrowWhenNotExist", "arguments needs to be a boolean value, but also a valid cacheName is supported for compatibility reasons to other engines");
060            }
061            
062            public static Object call(PageContext pc, String key, Object objThrowWhenNotExist,String cacheName) throws PageException {
063                    
064                    
065                    Boolean throwWhenNotExist=Caster.toBoolean(objThrowWhenNotExist,null);
066                    if(throwWhenNotExist==null)throw new FunctionException(pc, "cacheGet", 2, "ThrowWhenNotExist", "arguments needs to be a boolean value");
067                    
068                    try {
069                            Cache cache = Util.getCache(pc,cacheName,ConfigImpl.CACHE_DEFAULT_OBJECT);
070                            return _call(pc, key, throwWhenNotExist.booleanValue(), cache);
071                    } catch (IOException e) {
072                            throw Caster.toPageException(e);
073                    }
074            }
075    
076            private static Object _call(PageContext pc, String key, boolean throwWhenNotExist,Cache cache) throws IOException {
077                    return throwWhenNotExist?cache.getValue(Util.key(key)):cache.getValue(Util.key(key),null);
078            }
079    }