001 package railo.runtime.functions.cache; 002 003 import java.io.IOException; 004 import java.lang.reflect.InvocationTargetException; 005 import java.lang.reflect.Method; 006 import java.util.Iterator; 007 008 import railo.commons.io.cache.Cache; 009 import railo.commons.io.cache.CacheEntryFilter; 010 import railo.commons.io.cache.exp.CacheException; 011 import railo.commons.lang.StringUtil; 012 import railo.runtime.PageContext; 013 import railo.runtime.cache.CacheConnection; 014 import railo.runtime.config.Config; 015 import railo.runtime.config.ConfigImpl; 016 import railo.runtime.config.ConfigWeb; 017 import railo.runtime.engine.ThreadLocalPageContext; 018 019 public class Util { 020 021 /** 022 * get the default cache for a certain type, also check definitions in application context (application.cfc/cfapplication) 023 * @param pc current PageContext 024 * @param type default type -> Config.CACHE_DEFAULT_... 025 * @param defaultValue value returned when there is no default cache for this type 026 * @return matching cache 027 */ 028 public static Cache getDefault(PageContext pc, int type,Cache defaultValue) { 029 // get default from application conetx 030 String name=pc!=null?pc.getApplicationContext().getDefaultCacheName(type):null; 031 if(!StringUtil.isEmpty(name)){ 032 Cache cc = getCache(pc.getConfig(), name, null); 033 if(cc!=null) return cc; 034 } 035 036 // get default from config 037 Config config=ThreadLocalPageContext.getConfig(pc); 038 CacheConnection cc= ((ConfigImpl)config).getCacheDefaultConnection(type); 039 if(cc==null) return defaultValue; 040 try { 041 return cc.getInstance(config); 042 } catch (Throwable t) { 043 return defaultValue; 044 } 045 046 047 } 048 049 /** 050 * get the default cache for a certain type, also check definitions in application context (application.cfc/cfapplication) 051 * @param pc current PageContext 052 * @param type default type -> Config.CACHE_DEFAULT_... 053 * @return matching cache 054 * @throws IOException 055 */ 056 public static Cache getDefault(PageContext pc, int type) throws IOException { 057 // get default from application conetx 058 String name=pc!=null?pc.getApplicationContext().getDefaultCacheName(type):null; 059 if(!StringUtil.isEmpty(name)){ 060 Cache cc = getCache(pc.getConfig(), name, null); 061 if(cc!=null) return cc; 062 } 063 064 // get default from config 065 Config config = ThreadLocalPageContext.getConfig(pc); 066 CacheConnection cc= ((ConfigImpl)config).getCacheDefaultConnection(type); 067 if(cc==null) throw new CacheException("there is no default "+toStringType(type,"")+" cache defined, you need to define this default cache in the Railo Administrator"); 068 return cc.getInstance(config); 069 070 071 } 072 073 public static Cache getCache(PageContext pc,String cacheName, int type) throws IOException { 074 if(StringUtil.isEmpty(cacheName)){ 075 return getDefault(pc, type); 076 } 077 return getCache(ThreadLocalPageContext.getConfig(pc), cacheName); 078 } 079 080 public static Cache getCache(PageContext pc,String cacheName, int type, Cache defaultValue) { 081 if(StringUtil.isEmpty(cacheName)){ 082 return getDefault(pc, type,defaultValue); 083 } 084 return getCache(ThreadLocalPageContext.getConfig(pc), cacheName,defaultValue); 085 } 086 087 088 public static Cache getCache(Config config,String cacheName) throws IOException { 089 CacheConnection cc= config.getCacheConnections().get(cacheName.toLowerCase().trim()); 090 if(cc==null) throw noCache(config,cacheName); 091 return cc.getInstance(config); 092 } 093 094 public static Cache getCache(Config config,String cacheName, Cache defaultValue) { 095 CacheConnection cc= config.getCacheConnections().get(cacheName.toLowerCase().trim()); 096 if(cc==null) return defaultValue; 097 try { 098 return cc.getInstance(config); 099 } catch (Throwable t) { 100 return defaultValue; 101 } 102 } 103 public static CacheConnection getCacheConnection(Config config,String cacheName) throws IOException { 104 CacheConnection cc= config.getCacheConnections().get(cacheName.toLowerCase().trim()); 105 if(cc==null) throw noCache(config,cacheName); 106 return cc; 107 } 108 109 public static CacheConnection getCacheConnection(Config config,String cacheName, CacheConnection defaultValue) { 110 CacheConnection cc= config.getCacheConnections().get(cacheName.toLowerCase().trim()); 111 if(cc==null) return defaultValue; 112 return cc; 113 } 114 115 116 117 118 119 120 private static CacheException noCache(Config config, String cacheName) { 121 StringBuilder sb=new StringBuilder("there is no cache defined with name [").append(cacheName).append("], available caches are ["); 122 Iterator<String> it = ((ConfigImpl)config).getCacheConnections().keySet().iterator(); 123 if(it.hasNext()){ 124 sb.append(it.next()); 125 } 126 while(it.hasNext()){ 127 sb.append(", ").append(it.next()); 128 } 129 sb.append("]"); 130 131 return new CacheException(sb.toString()); 132 } 133 134 private static String toStringType(int type, String defaultValue) { 135 if(type==ConfigImpl.CACHE_DEFAULT_OBJECT) return "object"; 136 if(type==ConfigImpl.CACHE_DEFAULT_TEMPLATE) return "template"; 137 if(type==ConfigImpl.CACHE_DEFAULT_QUERY) return "query"; 138 if(type==ConfigImpl.CACHE_DEFAULT_RESOURCE) return "resource"; 139 if(type==ConfigImpl.CACHE_DEFAULT_FUNCTION) return "function"; 140 return defaultValue; 141 } 142 143 public static String key(String key) { 144 return key.toUpperCase().trim(); 145 } 146 147 148 public static boolean removeEL(ConfigWeb config, CacheConnection cc) { 149 try { 150 remove(config,cc); 151 return true; 152 } catch (Throwable e) { 153 return false; 154 } 155 } 156 public static void remove(ConfigWeb config, CacheConnection cc) throws Throwable { 157 Cache c = cc.getInstance(config); 158 // FUTURE no reflection needed 159 160 161 Method remove=null; 162 try{ 163 remove = c.getClass().getMethod("remove", new Class[0]); 164 165 } 166 catch(Exception ioe){ 167 c.remove((CacheEntryFilter)null); 168 return; 169 } 170 171 try { 172 remove.invoke(c, new Object[0]); 173 } 174 catch (InvocationTargetException e) { 175 throw e.getTargetException(); 176 } 177 } 178 179 public static int toType(String type, int defaultValue) { 180 type=type.trim().toLowerCase(); 181 if("object".equals(type)) return ConfigImpl.CACHE_DEFAULT_OBJECT; 182 if("query".equals(type)) return ConfigImpl.CACHE_DEFAULT_QUERY; 183 if("resource".equals(type)) return ConfigImpl.CACHE_DEFAULT_RESOURCE; 184 if("template".equals(type)) return ConfigImpl.CACHE_DEFAULT_TEMPLATE; 185 if("function".equals(type)) return ConfigImpl.CACHE_DEFAULT_FUNCTION; 186 return defaultValue; 187 } 188 189 public static String toType(int type, String defaultValue) { 190 if(ConfigImpl.CACHE_DEFAULT_OBJECT==type) return "object"; 191 if(ConfigImpl.CACHE_DEFAULT_QUERY==type) return "query"; 192 if(ConfigImpl.CACHE_DEFAULT_RESOURCE==type) return "resource"; 193 if(ConfigImpl.CACHE_DEFAULT_TEMPLATE==type) return "template"; 194 if(ConfigImpl.CACHE_DEFAULT_FUNCTION==type) return "function"; 195 return defaultValue; 196 } 197 }