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.soap;
020
021import java.io.ByteArrayInputStream;
022import java.io.ObjectInputStream;
023import java.util.Date;
024
025import lucee.commons.io.cache.CacheEntry;
026import lucee.commons.lang.ExceptionUtil;
027import lucee.loader.util.Util;
028import lucee.runtime.cache.CacheUtil;
029import lucee.runtime.type.Struct;
030
031public class SoapCacheEntry implements CacheEntry {
032
033        private Element element;
034
035        public SoapCacheEntry(Element element) {
036                this.element=element;
037                
038        }
039
040
041        @Override
042        public Date created() {
043                return new Date(element.getExpirationDate().longValue()-element.getTimeToLiveSeconds().longValue());
044        }
045
046        @Override
047        public Date lastHit() {
048                return new Date(0);
049                // TODO return new Date(element.getLastAccessTime());
050        }
051
052        @Override
053        public Date lastModified() {
054                return new Date(0);
055                // TODO long value = element.getLastUpdateTime();
056                // TODO if(value==0)return created();
057                // TODO return new Date(value); 
058        }
059
060        @Override
061        public int hitCount() {
062                return 0;
063                // TODO return (int)element.getHitCount();
064        }
065
066        @Override
067        public long idleTimeSpan() {
068                return element.getTimeToIdleSeconds().intValue()*1000;
069        }
070
071        @Override
072        public long liveTimeSpan() { 
073                return element.getTimeToLiveSeconds().intValue()*1000;
074        }
075
076        @Override
077        public long size() {
078                return element.getValue().length;
079        }
080
081        @Override
082        public String getKey() {
083                return (String) element.getKey();
084        }
085
086        @Override
087        public Object getValue() {
088                try{
089                ByteArrayInputStream bais = new ByteArrayInputStream(element.getValue());
090                if("application/x-java-serialized-object".equals(element.getMimeType())){
091                        ObjectInputStream ois=new ObjectInputStream(bais);
092                return ois.readObject();
093            }
094            // other
095            return Util.toString(bais);
096                }
097                catch(Throwable t){
098                ExceptionUtil.rethrowIfNecessary(t);
099                        return null;
100                }
101        }
102
103        public void setElement(Element element) {
104                this.element=element;
105        }
106        
107
108        @Override
109        public String toString() {
110                return CacheUtil.toString(this);
111        }
112
113        @Override
114        public Struct getCustomInfo() {
115                Struct info=CacheUtil.getInfo(this);
116                // TODO info.setEL("version", new Double(element.getVersion()));
117                return info;
118        }
119
120}