⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cmsflexcacheentry.java

📁 cms是开源的框架
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * File   : $Source: /usr/local/cvs/opencms/src/org/opencms/flex/CmsFlexCacheEntry.java,v $
 * Date   : $Date: 2006/03/27 14:52:35 $
 * Version: $Revision: 1.30 $
 *
 * This library is part of OpenCms -
 * the Open Source Content Mananagement System
 *
 * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * For further information about Alkacon Software GmbH, please see the
 * company website: http://www.alkacon.com
 *
 * For further information about OpenCms, please see the
 * project website: http://www.opencms.org
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package org.opencms.flex;

import org.opencms.cache.I_CmsLruCacheObject;
import org.opencms.file.CmsResource;
import org.opencms.i18n.CmsMessageContainer;
import org.opencms.main.CmsLog;
import org.opencms.monitor.CmsMemoryMonitor;
import org.opencms.monitor.I_CmsMemoryMonitorable;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;

import org.apache.commons.logging.Log;

/**
 * Contains the contents of a cached resource.<p>
 * 
 * It is basically a list of pre-generated output,
 * include() calls to other resources (with request parameters) and http headers that this 
 * resource requires to be set.<p>
 *
 * A CmsFlexCacheEntry might also describe a redirect-call, but in this case
 * nothing else will be cached.<p>
 *
 * The pre-generated output is saved in <code>byte[]</code> arrays.
 * The include() calls are saved as Strings of the included resource name, 
 * the parameters for the calls are saved in a HashMap.
 * The headers are saved in a HashMap.
 * In case of a redirect, the redircet target is cached in a String.<p>
 *
 * The CmsFlexCacheEntry can also have an expire date value, which indicates the time 
 * that his entry will become invalid and should thus be cleared from the cache.<p>
 *
 * @author  Alexander Kandzior 
 * @author Thomas Weckert  
 * 
 * @version $Revision: 1.30 $ 
 * 
 * @since 6.0.0 
 * 
 * @see org.opencms.cache.I_CmsLruCacheObject
 */
public class CmsFlexCacheEntry extends Object implements I_CmsLruCacheObject, I_CmsMemoryMonitorable {

    /** Initial size for lists. */
    public static final int INITIAL_CAPACITY_LISTS = 10;

    /** The log object for this class. */
    private static final Log LOG = CmsLog.getLog(CmsFlexCacheEntry.class);

    /** The CacheEntry's size in bytes. */
    private int m_byteSize;

    /** Indicates if this cache entry is completed. */
    private boolean m_completed;

    /** The "expires" date for this Flex cache entry. */
    private long m_dateExpires;

    /** The "last modified" date for this Flex cache entry. */
    private long m_dateLastModified;

    /** The list of items for this resource. */
    private List m_elements;

    /** A Map of cached headers for this resource. */
    private Map m_headers;

    /** Pointer to the next cache entry in the LRU cache. */
    private I_CmsLruCacheObject m_next;

    /** Pointer to the previous cache entry in the LRU cache. */
    private I_CmsLruCacheObject m_previous;

    /** A redirection target (if redirection is set). */
    private String m_redirectTarget;

    /** The key under which this cache entry is stored in the variation map. */
    private String m_variationKey;

    /** The variation map where this cache entry is stored. */
    private Map m_variationMap;

    /** 
     * Constructor for class CmsFlexCacheEntry.<p>
     * 
     * The way to use this class is to first use this empty constructor 
     * and later add data with the various add methods.
     */
    public CmsFlexCacheEntry() {

        m_elements = new ArrayList(INITIAL_CAPACITY_LISTS);
        m_dateExpires = CmsResource.DATE_EXPIRED_DEFAULT;
        m_dateLastModified = -1;
        // base memory footprint of this object with all referenced objects
        m_byteSize = 1024;

        setNextLruObject(null);
        setPreviousLruObject(null);
    }

    /** 
     * Adds an array of bytes to this cache entry,
     * this will usually be the result of some kind of output - stream.<p>
     *
     * @param bytes the output to save in the cache
     */
    public void add(byte[] bytes) {

        if (m_completed) {
            return;
        }
        if (m_redirectTarget == null) {
            // Add only if not already redirected
            m_elements.add(bytes);
            m_byteSize += CmsMemoryMonitor.getMemorySize(bytes);
        }
        bytes = null;
    }

    /** 
     * Add an include - call target resource to this cache entry.<p>
     *
     * @param resource a name of a resource in the OpenCms VFS
     * @param parameters a map of parameters specific to this include call
     */
    public void add(String resource, Map parameters) {

        if (m_completed) {
            return;
        }
        if (m_redirectTarget == null) {
            // Add only if not already redirected
            m_elements.add(resource);
            if (parameters == null) {
                parameters = Collections.EMPTY_MAP;
            }
            m_elements.add(parameters);
            m_byteSize += CmsMemoryMonitor.getMemorySize(resource);
        }
    }

    /** 
     * Add a map of headers to this cache entry,
     * which are usually collected in the class CmsFlexResponse first.<p>
     *
     * @param headers the map of headers to add to the entry 
     */
    public void addHeaders(Map headers) {

        if (m_completed) {
            return;
        }
        m_headers = headers;

        Iterator allHeaders = m_headers.keySet().iterator();
        while (allHeaders.hasNext()) {
            m_byteSize += CmsMemoryMonitor.getMemorySize(allHeaders.next());
        }
    }

    /**
     * @see org.opencms.cache.I_CmsLruCacheObject#addToLruCache()
     */
    public void addToLruCache() {

        // do nothing here...
        if (LOG.isDebugEnabled()) {
            LOG.debug(Messages.get().getBundle().key(Messages.LOG_FLEXCACHEENTRY_ADDED_ENTRY_1, this));
        }
    }

    /**
     * Completes this cache entry.<p>
     * 
     * A completed cache entry is made "unmodifiable",
     * so that no further data can be added and existing data can not be changed.
     * This is to prevend the (unlikley) case that some user-written class 
     * tries to make changes to a cache entry.<p>
     */
    public void complete() {

        m_completed = true;
        // Prevent changing of the cached lists
        if (m_headers != null) {
            m_headers = Collections.unmodifiableMap(m_headers);
        }
        if (m_elements != null) {
            m_elements = Collections.unmodifiableList(m_elements);
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug(Messages.get().getBundle().key(Messages.LOG_FLEXCACHEENTRY_ENTRY_COMPLETED_1, toString()));
        }
    }

    /**
     * Returns the list of data entries of this cache entry.<p>
     * 
     * Data entries are byte arrays representing some kind of ouput
     * or Strings representing include calls to other resources.
     *
     * @return the list of data elements of this cache entry
     */
    public List elements() {

        return m_elements;
    }

    /** 
     * Returns the expiration date of this cache entry,
     * this is set to the time when the entry becomes invalid.<p>
     * 
     * @return the expiration date value for this resource
     */
    public long getDateExpires() {

        return m_dateExpires;
    }

    /**
     * Returns the "last modified" date for this Flex cache entry.<p>
     * 
     * @return the "last modified" date for this Flex cache entry
     */
    public long getDateLastModified() {

        return m_dateLastModified;
    }

    /**
     * @see org.opencms.cache.I_CmsLruCacheObject#getLruCacheCosts()
     */
    public int getLruCacheCosts() {

        return m_byteSize;
    }

    /**
     * @see org.opencms.monitor.I_CmsMemoryMonitorable#getMemorySize()
     */
    public int getMemorySize() {

        return getLruCacheCosts();
    }

    /**
     * @see org.opencms.cache.I_CmsLruCacheObject#getNextLruObject()

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -