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