Package de.innovationgate.wgpublisher.cache

Source Code of de.innovationgate.wgpublisher.cache.WebTMLCache$CacheKey

/*******************************************************************************
* Copyright 2009, 2010 Innovation Gate GmbH. All Rights Reserved.
*
* This file is part of the OpenWGA server platform.
*
* OpenWGA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In addition, a special exception is granted by the copyright holders
* of OpenWGA called "OpenWGA plugin exception". You should have received
* a copy of this exception along with OpenWGA in file COPYING.
* If not, see <http://www.openwga.com/gpl-plugin-exception>.
*
* OpenWGA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenWGA in file COPYING.
* If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

package de.innovationgate.wgpublisher.cache;

import java.io.IOException;
import java.io.Serializable;
import java.io.Writer;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;

import de.innovationgate.utils.WGUtils;
import de.innovationgate.utils.cache.Cache;
import de.innovationgate.utils.cache.CacheException;
import de.innovationgate.utils.cache.CacheFactory;
import de.innovationgate.webgate.api.WGException;

/**
* A wrapper around a generic cache object to use it as WebTML cache
*/
public class WebTMLCache {
   
    private int _capacity;

    public static class CacheEntry implements Serializable {

        private String _code;

        private Date _date;

        public CacheEntry(String code, Date date) {
            super();
            _code = code;
            _date = date;
        }

        public String getCode() {
            return _code;
        }

        public Date getDate() {
            return _date;
        }

    }

    public static class CacheKey {
       
        private String _dbKey;
        private String _tagId;
        private String _expressionKey;

        public CacheKey(String dbKey, String tag, String expressionKey) {
            super();
            _dbKey = dbKey;
            _tagId = tag;
            _expressionKey = expressionKey;
        }
       
        public CacheKey(String stringKey) {
            super();
            List<String> elements = WGUtils.deserializeCollection(stringKey, "//");
            _dbKey = elements.get(0);
            _tagId = elements.get(1);
            _expressionKey = elements.get(2);
        }

        public String getExpressionKey() {
            return _expressionKey;
        }

        public String getTagId() {
            return _tagId;
        }

        public String toString() {
            return _dbKey + "//" +_tagId + "//" + _expressionKey;
        }

        public String getDbKey() {
            return _dbKey;
        }

        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + ((_dbKey == null) ? 0 : _dbKey.hashCode());
            result = prime * result + ((_expressionKey == null) ? 0 : _expressionKey.hashCode());
            result = prime * result + ((_tagId == null) ? 0 : _tagId.hashCode());
            return result;
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            CacheKey other = (CacheKey) obj;
            if (_dbKey == null) {
                if (other._dbKey != null)
                    return false;
            }
            else if (!_dbKey.equals(other._dbKey))
                return false;
            if (_expressionKey == null) {
                if (other._expressionKey != null)
                    return false;
            }
            else if (!_expressionKey.equals(other._expressionKey))
                return false;
            if (_tagId == null) {
                if (other._tagId != null)
                    return false;
            }
            else if (!_tagId.equals(other._tagId))
                return false;
            return true;
        }
    }

    private Cache _cache;

    private int _cacheIndex;

    public void clear() throws CacheException {
        init();
    }
   
    public void clearForDatabase(String dbKey) throws CacheException {
       _cache.flushGroup(dbKey);
    }

    public void close() throws CacheException {
        _cache.destroy();
    }

    public void dump(Writer out) throws IOException {
        out.write("DBKEY;TAGID;CACHEKEY;TIME;SIZE\n");
        DateFormat dateFormat = new SimpleDateFormat();
        try {
            Iterator keys = _cache.getEntryKeys().iterator();
            while (keys.hasNext()) {
                String keyString = (String) keys.next();
                CacheKey key = new CacheKey(keyString);
                try {
                    CacheEntry entry = (CacheEntry) _cache.readEntry(keyString);
                    out.write(key.getDbKey());
                    out.write(";");
                    out.write(key.getTagId());
                    out.write(";");
                    out.write(key.getExpressionKey());
                    out.write(";");
                    out.write(dateFormat.format(entry.getDate()));
                    out.write(";");
                    out.write(String.valueOf(((String) entry.getCode()).length()));
                    out.write("\n");
                }
                catch (CacheException e) {
                    out.write("Exception reading cache entry " + key.toString() + ": " + e.getClass().getName() + " - " + e.getMessage() + "\n");
                }
            }
        }
        catch (CacheException e) {
            throw new IOException("Cache Exception: " + e.getMessage());
        }
    }

    public String getCache(String dbKey, String tagid, String key, Date dblastmod) throws CacheException {

        CacheKey cacheKey = new CacheKey(dbKey, tagid, key);

        CacheEntry entry = (CacheEntry) _cache.readEntry(cacheKey.toString());
        if (entry == null) {
            return null;
        }

        if (entry.getDate().getTime() < dblastmod.getTime()) {
            return null;
        }

        return (String) entry.getCode();
       
    }
   
    public void removeCache(String dbKey, String tagid, String key) throws CacheException {
        CacheKey cacheKey = new CacheKey(dbKey, tagid, key);
        _cache.flushEntry(cacheKey.toString());
    }

    public long getEntriesCount() {
        try {
            return _cache.getSize();
        }
        catch (CacheException e) {
            return 0;
        }
    }

    public WebTMLCache(int capacity) throws CacheException {

        setCapacity(capacity);
        init();

    }

    private synchronized void init() throws CacheException {
       
        Cache oldCache = _cache;
        _cache = CacheFactory.createCache("WebTMLCache-" + increaseCacheIndex(), _capacity, null);
        if (oldCache != null) {
            oldCache.destroy();
        }
    }

    private int increaseCacheIndex() {
        _cacheIndex++;
        return _cacheIndex;
    }

    public void putCache(String dbKey, String tagid, String key, String content, Date cacheDate, int latency) throws CacheException {

        CacheKey cacheKey = new CacheKey(dbKey, tagid, key);
        CacheEntry cacheEntry = new CacheEntry(content, cacheDate);
        _cache.writeEntry(cacheKey.toString(), cacheEntry, dbKey, latency);
       
    }

    public int getCapacity() {
        return _capacity;
    }

    public void setCapacity(int capacity) {
        _capacity = capacity;
    }

    public Cache getCache() {
        return _cache;
    }

}
TOP

Related Classes of de.innovationgate.wgpublisher.cache.WebTMLCache$CacheKey

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.