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.legacy;
020
021import java.io.ByteArrayInputStream;
022import java.io.IOException;
023import java.io.OutputStream;
024
025import javax.servlet.http.HttpServletRequest;
026
027import lucee.commons.io.IOUtil;
028import lucee.commons.io.cache.Cache;
029import lucee.commons.lang.ExceptionUtil;
030import lucee.commons.lang.StringUtil;
031import lucee.runtime.PageContext;
032import lucee.runtime.cache.util.CacheKeyFilterAll;
033import lucee.runtime.cache.util.WildCardFilter;
034import lucee.runtime.exp.PageException;
035import lucee.runtime.op.Caster;
036import lucee.runtime.type.dt.TimeSpan;
037
038import org.apache.oro.text.regex.MalformedPatternException;
039
040public class CacheItemCache extends CacheItem {
041
042        private Cache cache;
043        private TimeSpan timespan;
044        private String lcFileName;
045
046        public CacheItemCache(PageContext pc, HttpServletRequest req, String id, String key, boolean useId, Cache cache, TimeSpan timespan)  {
047                super(pc, req, id, key, useId);
048                this.cache=cache;
049                this.timespan=timespan;
050                lcFileName=fileName;
051        }
052
053        @Override
054        public boolean isValid() {
055                try {
056                        return cache.getValue(lcFileName)!=null;
057                } catch (IOException e) {
058                        return false;
059                }
060        }
061
062        @Override
063        public boolean isValid(TimeSpan timespan) {
064                return isValid();
065        }
066
067        @Override
068        public void writeTo(OutputStream os,String charset) throws IOException {
069                byte[] barr = getValue().getBytes(StringUtil.isEmpty(charset,true)?"UTF-8":charset);
070                IOUtil.copy(new ByteArrayInputStream(barr),os,true,false);
071        }
072
073        public String getValue() throws IOException {
074                try {
075                        return Caster.toString(cache.getValue(lcFileName));
076                } catch (PageException e) {
077                        throw ExceptionUtil.toIOException(e);
078                }
079        }
080
081        @Override
082        public void store(String value) throws IOException {
083                cache.put(lcFileName, value, null,valueOf(timespan));
084                
085        }
086
087        @Override
088        public void store(byte[] barr, boolean append) throws IOException {
089                String value=(append)?getValue():"";
090                value+=IOUtil.toString(barr, "UTF-8");
091                store(value);
092        }
093
094        public static void _flushAll(PageContext pc, Cache cache) throws IOException {
095                cache.remove(CacheKeyFilterAll.getInstance());
096        }
097
098        public static void _flush(PageContext pc, Cache cache, String expireurl) throws MalformedPatternException, IOException {
099                cache.remove(new WildCardFilter(expireurl,true));
100        }
101        
102        private static Long valueOf(TimeSpan timeSpan) {
103                if(timeSpan==null) return null;
104                return Long.valueOf(timeSpan.getMillis());
105        }
106
107}