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