001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.runtime.functions.cache;
020
021import java.util.ArrayList;
022
023import lucee.commons.io.cache.exp.CacheException;
024import lucee.commons.lang.StringUtil;
025import lucee.runtime.PageContext;
026import lucee.runtime.cache.CacheConnection;
027import lucee.runtime.config.ConfigImpl;
028import lucee.runtime.exp.PageException;
029import lucee.runtime.exp.SecurityException;
030import lucee.runtime.op.Caster;
031import lucee.runtime.type.Collection.Key;
032import lucee.runtime.type.KeyImpl;
033import lucee.runtime.type.Struct;
034import lucee.runtime.type.util.ListUtil;
035
036public class CacheSetProperties {
037
038        private static final Key OBJECT_TYPE = KeyImpl.intern("objecttype");
039
040        public static Object call(PageContext pc,Struct properties) throws PageException {
041                try {
042                        Object obj=properties.removeEL(OBJECT_TYPE); 
043                        String objectType=Caster.toString(obj);
044                        
045                        CacheConnection[] conns=getCaches(pc,objectType);
046                        for(int i=0;i<conns.length;i++){
047                                setProperties(conns[i],properties);
048                        }
049                } catch (CacheException e) {
050                        throw Caster.toPageException(e);
051                }
052                
053                
054                return call(pc, null);
055        }
056
057        private static void setProperties(CacheConnection cc, Struct properties) throws SecurityException {
058                throw new SecurityException("it is not allowed to change cache connection setting this way, please use the tag cfadmin or the lucee administrator frontend instead ");
059        }
060
061        private static CacheConnection[] getCaches(PageContext pc,String cacheName) throws CacheException {
062                ConfigImpl config=(ConfigImpl) pc.getConfig();
063                if(StringUtil.isEmpty(cacheName)){
064                        
065                        return new CacheConnection[]{
066                                        config.getCacheDefaultConnection(ConfigImpl.CACHE_DEFAULT_OBJECT),
067                                        config.getCacheDefaultConnection(ConfigImpl.CACHE_DEFAULT_TEMPLATE)
068                        }
069                        ;
070                        // MUST which one is first
071                }
072                
073                ArrayList<CacheConnection> list=new ArrayList<CacheConnection>();
074                String name;
075                String[] names=ListUtil.listToStringArray(cacheName, ',');
076                for(int i=0;i<names.length;i++){
077                        name=names[i].trim().toLowerCase();
078                        if(name.equalsIgnoreCase("template"))
079                                list.add(config.getCacheDefaultConnection(ConfigImpl.CACHE_DEFAULT_TEMPLATE));
080                        else if(name.equalsIgnoreCase("object"))
081                                list.add(config.getCacheDefaultConnection(ConfigImpl.CACHE_DEFAULT_OBJECT));
082                        else{
083                                CacheConnection cc= config.getCacheConnections().get(name);
084                                if(cc==null) throw new CacheException("there is no cache defined with name ["+name+"]");
085                                list.add(cc);
086                        }
087                }
088                return list.toArray(new CacheConnection[list.size()]);
089        }
090}