001    package railo.runtime.functions.cache;
002    
003    import java.util.ArrayList;
004    
005    import railo.commons.io.cache.exp.CacheException;
006    import railo.commons.lang.StringUtil;
007    import railo.runtime.PageContext;
008    import railo.runtime.cache.CacheConnection;
009    import railo.runtime.config.ConfigImpl;
010    import railo.runtime.exp.PageException;
011    import railo.runtime.exp.SecurityException;
012    import railo.runtime.op.Caster;
013    import railo.runtime.type.Collection.Key;
014    import railo.runtime.type.util.ListUtil;
015    import railo.runtime.type.KeyImpl;
016    import railo.runtime.type.Struct;
017    
018    public class CacheSetProperties {
019    
020            private static final Key OBJECT_TYPE = KeyImpl.intern("objecttype");
021    
022            public static Object call(PageContext pc,Struct properties) throws PageException {
023                    try {
024                            Object obj=properties.removeEL(OBJECT_TYPE); 
025                            String objectType=Caster.toString(obj);
026                            
027                            CacheConnection[] conns=getCaches(pc,objectType);
028                            for(int i=0;i<conns.length;i++){
029                                    setProperties(conns[i],properties);
030                            }
031                    } catch (CacheException e) {
032                            throw Caster.toPageException(e);
033                    }
034                    
035                    
036                    return call(pc, null);
037            }
038    
039            private static void setProperties(CacheConnection cc, Struct properties) throws SecurityException {
040                    throw new SecurityException("it is not allowed to change cache connection setting this way, please use the tag cfadmin or the railo administrator frontend instead ");
041            }
042    
043            private static CacheConnection[] getCaches(PageContext pc,String cacheName) throws CacheException {
044                    ConfigImpl config=(ConfigImpl) pc.getConfig();
045                    if(StringUtil.isEmpty(cacheName)){
046                            
047                            return new CacheConnection[]{
048                                            config.getCacheDefaultConnection(ConfigImpl.CACHE_DEFAULT_OBJECT),
049                                            config.getCacheDefaultConnection(ConfigImpl.CACHE_DEFAULT_TEMPLATE)
050                            }
051                            ;
052                            // MUST which one is first
053                    }
054                    
055                    ArrayList<CacheConnection> list=new ArrayList<CacheConnection>();
056                    String name;
057                    String[] names=ListUtil.listToStringArray(cacheName, ',');
058                    for(int i=0;i<names.length;i++){
059                            name=names[i].trim().toLowerCase();
060                            if(name.equalsIgnoreCase("template"))
061                                    list.add(config.getCacheDefaultConnection(ConfigImpl.CACHE_DEFAULT_TEMPLATE));
062                            else if(name.equalsIgnoreCase("object"))
063                                    list.add(config.getCacheDefaultConnection(ConfigImpl.CACHE_DEFAULT_OBJECT));
064                            else{
065                                    CacheConnection cc= config.getCacheConnections().get(name);
066                                    if(cc==null) throw new CacheException("there is no cache defined with name ["+name+"]");
067                                    list.add(cc);
068                            }
069                    }
070                    return list.toArray(new CacheConnection[list.size()]);
071            }
072    }