001 package railo.runtime.cache.eh.remote; 002 003 import java.io.IOException; 004 import java.net.URL; 005 import java.util.List; 006 007 import javax.xml.rpc.ServiceException; 008 009 import railo.commons.io.cache.CacheEntry; 010 import railo.loader.engine.CFMLEngineFactory; 011 import railo.runtime.cache.CacheSupport; 012 import railo.runtime.cache.eh.remote.rest.RESTClient; 013 import railo.runtime.cache.eh.remote.rest.sax.CacheConfiguration; 014 import railo.runtime.cache.eh.remote.soap.Element; 015 import railo.runtime.cache.eh.remote.soap.SoapClient; 016 import railo.runtime.config.ConfigWeb; 017 import railo.runtime.exp.PageException; 018 import railo.runtime.type.KeyImpl; 019 import railo.runtime.type.Struct; 020 import railo.runtime.util.Cast; 021 022 public class EHCacheRemote extends CacheSupport { 023 024 private URL url; 025 private String name; 026 private RESTClient rest; 027 private SoapClient soap; 028 029 030 public EHCacheRemote() { 031 } 032 033 public static void init(ConfigWeb config,String[] cacheNames,Struct[] arguments) throws IOException { 034 035 } 036 037 public void init(String name, Struct arguments) throws IOException { 038 Cast caster = CFMLEngineFactory.getInstance().getCastUtil(); 039 String strUrl=null; 040 041 try { 042 strUrl=caster.toString(arguments.get("url")); 043 this.name=caster.toString(arguments.get("remoteCacheName")); 044 045 } catch (PageException e) { 046 throw new IOException(e.getMessage()); 047 } 048 if(!strUrl.endsWith("/")){ 049 strUrl=strUrl+"/"; 050 } 051 this.url=new URL(strUrl); 052 053 054 055 056 this.rest=new RESTClient(new URL(url.toExternalForm()+"rest/")); 057 this.soap=new SoapClient(new URL(url.toExternalForm()+"soap/EhcacheWebServiceEndpoint?wsdl")); 058 } 059 060 /** 061 * @see railo.commons.io.cache.Cache#contains(java.lang.String) 062 */ 063 public boolean contains(String key) { 064 try { 065 return rest.contains(name, key); 066 } catch (IOException e) { 067 return false; 068 } 069 } 070 071 /** 072 * @see railo.commons.io.cache.Cache#keys() 073 */ 074 public List keys() { 075 try { 076 return soap.getKeysWithExpiryCheck(name); 077 } 078 catch (Throwable t) { 079 throw new RuntimeException(t); 080 } 081 } 082 083 public CacheEntry getQuiet(String key) throws IOException { 084 try { 085 return soap.getQuiet(name, key); 086 } 087 catch (ServiceException e) { 088 throw new IOException(e.getMessage()); 089 } 090 } 091 092 093 public CacheEntry getQuiet(String key,CacheEntry defaultValue) { 094 try { 095 return soap.getQuiet(name, key); 096 } 097 catch (Throwable t) { 098 return defaultValue; 099 } 100 } 101 102 public CacheEntry getCacheEntry(String key) throws IOException { 103 try { 104 return soap.get(name, key); 105 } 106 catch (ServiceException e) { 107 throw new IOException(e.getMessage()); 108 } 109 } 110 111 public CacheEntry getCacheEntry(String key,CacheEntry defaultValue) { 112 try { 113 return soap.get(name, key); 114 } 115 catch (Throwable t) { 116 return defaultValue; 117 } 118 } 119 120 121 122 public Struct getCustomInfo() { 123 Struct info=super.getCustomInfo(); 124 try { 125 CacheConfiguration conf = rest.getMeta(name).getCacheConfiguration(); 126 127 info.setEL("disk_expiry_thread_interval", new Double(conf.getDiskExpiryThreadIntervalSeconds())); 128 info.setEL("disk_spool_buffer_size", new Double(conf.getDiskSpoolBufferSize())); 129 info.setEL("max_elements_in_memory", new Double(conf.getMaxElementsInMemory())); 130 info.setEL("max_elements_on_disk", new Double(conf.getMaxElementsOnDisk())); 131 info.setEL("time_to_idle", new Double(conf.getTimeToIdleSeconds())); 132 info.setEL("time_to_live", new Double(conf.getTimeToLiveSeconds())); 133 info.setEL(KeyImpl.NAME, conf.getName()); 134 } 135 catch(Throwable t){ 136 //print.printST(t); 137 } 138 139 return info; 140 } 141 142 143 public long hitCount() { 144 // TODO Auto-generated method stub 145 return 0; 146 } 147 148 149 public long missCount() { 150 // TODO Auto-generated method stub 151 return 0; 152 } 153 154 public void put(String key, Object value, Long idleTime, Long liveTime) { 155 Boolean eternal = idleTime==null && liveTime==null?Boolean.TRUE:Boolean.FALSE; 156 Integer idle = idleTime==null?null:new Integer((int)idleTime.longValue()/1000); 157 Integer live = liveTime==null?null:new Integer((int)liveTime.longValue()/1000); 158 try { 159 Element el = new Element(); 160 el.setKey(key); 161 // TODO make text/plain for string 162 el.setMimeType("application/x-java-serialized-object"); 163 el.setValue(Converter.toBytes(value)); 164 el.setEternal(eternal); 165 el.setTimeToIdleSeconds(idle); 166 el.setTimeToLiveSeconds(live); 167 168 soap.put(name,el); 169 } catch (Throwable t) { 170 throw new RuntimeException(t); 171 } 172 173 } 174 175 /** 176 * @see railo.commons.io.cache.Cache#remove(java.lang.String) 177 */ 178 public boolean remove(String key) { 179 try { 180 return soap.remove(name, key); 181 } 182 catch (Exception e) { 183 return false; 184 } 185 } 186 187 188 }