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.op.Caster; 018 019 public class Util { 020 021 public static Cache getDefault(PageContext pc, int type) throws IOException { 022 return getDefault(pc.getConfig(), type); 023 } 024 025 public static CacheConnection getDefaultCacheConnection(Config config, int type) throws IOException { 026 CacheConnection cc= ((ConfigImpl)config).getCacheDefaultConnection(type); 027 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"); 028 return cc; 029 } 030 public static Cache getDefault(Config config, int type) throws IOException { 031 CacheConnection cc= ((ConfigImpl)config).getCacheDefaultConnection(type); 032 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"); 033 return cc.getInstance(config); 034 } 035 036 public static Cache getDefault(Config config, int type,Cache defaultValue) { 037 CacheConnection cc= ((ConfigImpl)config).getCacheDefaultConnection(type); 038 039 if(cc==null) return defaultValue; 040 try { 041 return cc.getInstance(config); 042 } catch (IOException e) { 043 return defaultValue; 044 } 045 } 046 047 048 public static CacheConnection getCacheConnection(Config config,String cacheName, int type) throws IOException { 049 if(StringUtil.isEmpty(cacheName)){ 050 return getDefaultCacheConnection(config, type); 051 } 052 return getCacheConnection(config, cacheName); 053 } 054 055 public static Cache getCache(Config config,String cacheName, int type) throws IOException { 056 if(StringUtil.isEmpty(cacheName)){ 057 return getDefault(config, type); 058 } 059 return getCache(config, cacheName); 060 } 061 062 public static Cache getCache(Config config,String cacheName, int type, Cache defaultValue) { 063 if(StringUtil.isEmpty(cacheName)){ 064 return getDefault(config, type,defaultValue); 065 } 066 return getCache(config, cacheName,defaultValue); 067 } 068 069 /** 070 * @param pc 071 * @param cacheName 072 * @param type 073 * @return 074 * @throws IOException 075 * @deprecated use <code>getCache(Config config,String cacheName, int type)</code> instead 076 */ 077 public static Cache getCache(PageContext pc,String cacheName, int type) throws IOException { 078 return getCache(pc.getConfig(), cacheName); 079 } 080 /** 081 * @param pc 082 * @param cacheName 083 * @return 084 * @throws IOException 085 * @deprecated use <code>getCache(Config config,String cacheName)</code> instead 086 */ 087 public static Cache getCache(PageContext pc,String cacheName) throws IOException { 088 return getCache(pc.getConfig(), cacheName); 089 } 090 public static Cache getCache(Config config,String cacheName) throws IOException { 091 CacheConnection cc= (CacheConnection) ((ConfigImpl)config).getCacheConnections().get(cacheName.toLowerCase().trim()); 092 if(cc==null) throw noCache(config,cacheName); 093 return cc.getInstance(config); 094 } 095 public static Cache getCache(Config config,String cacheName, Cache defaultValue) { 096 CacheConnection cc= (CacheConnection) ((ConfigImpl)config).getCacheConnections().get(cacheName.toLowerCase().trim()); 097 if(cc==null) return defaultValue; 098 try { 099 return cc.getInstance(config); 100 } catch (IOException e) { 101 return defaultValue; 102 } 103 } 104 public static CacheConnection getCacheConnection(Config config,String cacheName) throws IOException { 105 CacheConnection cc= (CacheConnection) ((ConfigImpl)config).getCacheConnections().get(cacheName.toLowerCase().trim()); 106 if(cc==null) throw noCache(config,cacheName); 107 return cc; 108 } 109 private static CacheException noCache(Config config, String cacheName) { 110 StringBuilder sb=new StringBuilder("there is no cache defined with name [").append(cacheName).append("], available caches are ["); 111 Iterator it = ((ConfigImpl)config).getCacheConnections().keySet().iterator(); 112 if(it.hasNext()){ 113 sb.append(Caster.toString(it.next(),"")); 114 } 115 while(it.hasNext()){ 116 sb.append(", ").append(Caster.toString(it.next(),"")); 117 } 118 sb.append("]"); 119 120 return new CacheException(sb.toString()); 121 } 122 123 public static CacheConnection getCacheConnection(Config config,String cacheName, CacheConnection defaultValue) { 124 CacheConnection cc= (CacheConnection) ((ConfigImpl)config).getCacheConnections().get(cacheName.toLowerCase().trim()); 125 if(cc==null) return defaultValue; 126 return cc; 127 } 128 129 private static String toStringType(int type, String defaultValue) { 130 if(type==ConfigImpl.CACHE_DEFAULT_OBJECT) return "object"; 131 if(type==ConfigImpl.CACHE_DEFAULT_TEMPLATE) return "template"; 132 if(type==ConfigImpl.CACHE_DEFAULT_QUERY) return "query"; 133 if(type==ConfigImpl.CACHE_DEFAULT_RESOURCE) return "resource"; 134 return defaultValue; 135 } 136 137 public static String key(String key) { 138 return key.toUpperCase().trim(); 139 } 140 141 142 public static boolean removeEL(ConfigWeb config, CacheConnection cc) { 143 try { 144 remove(config,cc); 145 return true; 146 } catch (Throwable e) { 147 return false; 148 } 149 } 150 public static void remove(ConfigWeb config, CacheConnection cc) throws Throwable { 151 Cache c = cc.getInstance(config); 152 // FUTURE no reflection needed 153 Method remove=null; 154 try{ 155 remove = c.getClass().getMethod("remove", new Class[0]); 156 157 } 158 catch(Exception ioe){ 159 c.remove((CacheEntryFilter)null); 160 return; 161 } 162 163 try { 164 remove.invoke(c, new Object[0]); 165 } 166 catch (InvocationTargetException e) { 167 throw e.getTargetException(); 168 } 169 } 170 171 public static int toType(String type, int defaultValue) { 172 type=type.trim().toLowerCase(); 173 if("object".equals(type)) return ConfigImpl.CACHE_DEFAULT_OBJECT; 174 if("query".equals(type)) return ConfigImpl.CACHE_DEFAULT_QUERY; 175 if("resource".equals(type)) return ConfigImpl.CACHE_DEFAULT_RESOURCE; 176 if("template".equals(type)) return ConfigImpl.CACHE_DEFAULT_TEMPLATE; 177 return defaultValue; 178 } 179 180 public static String toType(int type, String defaultValue) { 181 if(ConfigImpl.CACHE_DEFAULT_OBJECT==type) return "object"; 182 if(ConfigImpl.CACHE_DEFAULT_QUERY==type) return "query"; 183 if(ConfigImpl.CACHE_DEFAULT_RESOURCE==type) return "resource"; 184 if(ConfigImpl.CACHE_DEFAULT_TEMPLATE==type) return "template"; 185 return defaultValue; 186 } 187 }