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

📄 a_cmselement.java

📁 内容管理
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
* File   : $Source: /usr/local/cvs/opencms/src/com/opencms/template/cache/A_CmsElement.java,v $
* Date   : $Date: 2003/02/26 10:30:37 $
* Version: $Revision: 1.36 $
*
* This library is part of OpenCms -
* the Open Source Content Mananagement System
*
* Copyright (C) 2001  The OpenCms Group
*
* 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 OpenCms, please see the
* OpenCms 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 com.opencms.template.cache;

import com.opencms.boot.I_CmsLogChannels;
import com.opencms.core.A_OpenCms;
import com.opencms.core.CmsException;
import com.opencms.file.CmsGroup;
import com.opencms.file.CmsObject;
import com.opencms.file.CmsResource;
import com.opencms.template.A_CmsCacheDirectives;
import com.opencms.template.CmsCacheDirectives;
import com.opencms.template.CmsTemplateClassManager;
import com.opencms.template.I_CmsTemplate;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

/**
 * An instance of A_CmsElement represents an requestable Element in the OpenCms
 * element cache area. It contains all informations to generate the content of this
 * element. It also stores the variants of once generated content to speed up
 * performance.
 *
 * It may point to other depending elements. Theses elements are called to generate
 * their content on generation-time.
 *
 * @author Andreas Schouten
 * @author Alexander Lucas
 */
public abstract class A_CmsElement implements com.opencms.boot.I_CmsLogChannels {

    /** The class-name of this element definition. */
    protected String m_className;

    /** The template-name of this element definition. */
    protected String m_templateName;

    /** Cache directives of this element. */
    protected A_CmsCacheDirectives m_cacheDirectives;

    /** The name of the group that can read this ressource. */
    protected String m_readAccessGroup;

    /** Last time this element was generated.(used for CacheDirectives timeout) */
    protected long m_timestamp = 0;

    /** All definitions declared in this element. */
    protected CmsElementDefinitionCollection m_elementDefinitions;

    /** LruCache for element variant cache */
    private CmsLruCache m_variants;

    /** indicates if this element may have a variant that has dependencies
     *  if such a element is deletet from elementcache the extern dependencies
     *  hashtable must be updated.
     */
    protected boolean m_hasDepVariants = false;

    /**
     * Initializer for an element with the given class and template name.
     */
    protected void init(String className, String templateName, String readAccessGroup, A_CmsCacheDirectives cd, int variantCachesize) {
        m_className = className;
        m_templateName = templateName;
        m_readAccessGroup = readAccessGroup;
        m_cacheDirectives = cd;
        m_elementDefinitions = new CmsElementDefinitionCollection();
        m_variants = new CmsLruCache(variantCachesize);
    }

    /**
     * Initializer for building an element with the given element definitions.
     * @param name the name of this element-definition.
     * @param className the classname of this element-definition.
     * @param readAccessGroup The group that may read the element.
     * @param cd Cache directives for this element
     * @param defs Vector with ElementDefinitions for this element.
     * @param variantCachesize The size of the variant cache.
     */
    protected void init(String className, String templateName, String readAccessGroup, A_CmsCacheDirectives cd, CmsElementDefinitionCollection defs, int variantCachesize) {
        m_className = className;
        m_templateName = templateName;
        m_readAccessGroup = readAccessGroup;
        m_cacheDirectives = cd;
        m_elementDefinitions = defs;
        m_variants = new CmsLruCache(variantCachesize);
    }

    /**
     * Adds a single definition to this element.
     * @param def - the ElementDefinition to add.
     */
    public void addDefinition(CmsElementDefinition def) {
        m_elementDefinitions.add(def);
    }

    /**
     * Adds a single variant to this element.
     * @param def - the ElementVariant to add.
     * @return a CmsElementVariant of dependencies that must be deleted from extern store for this element
     */
    public Vector addVariant(Object key, CmsElementVariant variant) {
        if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging()) {
            A_OpenCms.log(C_OPENCMS_ELEMENTCACHE, toString() + " adding variant \"" + key + "\" to cache. ");
        }
        if(key != null){
            CmsElementVariant old = (CmsElementVariant)m_variants.get(key);
            if ((old != null) && (old.size() == 0)){
                variant.addDependencies(old.getDependencies());
                variant.mergeNextTimeout(old.getNextTimeout());
            }
            return m_variants.put(key, variant);
        }
        return null;
    }

    /**
     *
     */
    public void removeVariant(Object key){
        if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging()) {
            A_OpenCms.log(C_OPENCMS_ELEMENTCACHE, toString() + " removing variant \"" + key + "\" from cache. ");
        }
        if(key != null){
            m_variants.remove(key);
        }
    }

    /**
     * checks the read access.
     * @param cms The cms Object for reading groups.
     * @throws CmsException if no read access.
     */
    public void checkReadAccess(CmsObject cms) throws CmsException{
        if (m_readAccessGroup == null || "".equals(m_readAccessGroup )){
            // everyone can read this
            return;
        }
        CmsGroup currentGroup = cms.getRequestContext().currentGroup();
        if (m_readAccessGroup.equals(currentGroup.getName())){
            // easy: same group; access granted
            return;
        }
        // maybe it is an Admin
        if(currentGroup.getName().equals(CmsObject.C_GROUP_ADMIN)){
            // ok Admins can read everything
            return;
        }
        // limited access and not the same group, but maybe parentgroup?
        CmsGroup group1 = currentGroup;
        CmsGroup group2 = cms.readGroup(m_readAccessGroup);
        do{
            group1 = cms.getParent(group1.getName());
            if(group1 != null && group1.getId() == group2.getId()){
                // is parent; access granted
                return;
            }
        }while(group1 != null);

        // ok. last chance. It could be the owner of the file
        boolean readError = false;
        try{
            //if(m_templateName == null){then the readAccessGroup should be null, so we dont have to care here
            cms.readFileHeader(m_templateName);
        }catch(CmsException e){
            readError = true;
        }
        if ( !readError){
            return;
        }

        // no way to read this sorry
        if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging()) {
            A_OpenCms.log(C_OPENCMS_ELEMENTCACHE, toString() + " no read access. ");
        }
        throw new CmsException(currentGroup.getName()+" has no read access to "+m_templateName+". ",
                                CmsException.C_ACCESS_DENIED);
    }

    /**
     * Clears all variants. Used for TimeCritical elements.
     */
    public void clearVariantCache(){
        m_variants.clearCache();
        m_timestamp = System.currentTimeMillis();
    }

    /**
     *
     */
    public Vector getAllVariantKeys(){
        return m_variants.getAllKeys();
    }

    /**
     * Get a variant from the vatiant cache
     * @param key Key of the ElementVariant.
     * @return Cached CmsElementVariant object
     */
    public CmsElementVariant getVariant(Object key) {
        if (key == null){
            return null;
        }
        CmsElementVariant result = (CmsElementVariant)m_variants.get(key);
        if(result != null && result.size() == 0){
            result = null;
        }
        if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging()) {
            if(result != null) {
                A_OpenCms.log(C_OPENCMS_ELEMENTCACHE, toString() + " getting variant \"" + key + "\" from cache. ");
            } else {
                A_OpenCms.log(C_OPENCMS_ELEMENTCACHE, toString() + " Variant \"" + key + "\" is not in element cache. ");
            }
        }
        return result;
    }

    /**
     * says if the extern dependenciescache has to be updated when this element
     * is deleted.
     */
    public boolean hasDependenciesVariants(){
        return m_hasDepVariants;
    }

    /**
     * indicates this element critical for delete.
     */
    public void thisElementHasDepVariants(){
        m_hasDepVariants = true;
    }

    /**
     * Returns a Vector with all ElementDefinitions
     * @return a Vector with all ElementDefinitions.
     */
    public CmsElementDefinitionCollection getAllDefinitions() {
        return m_elementDefinitions;
    }

    /**
     * Get the element definition for the sub-element with the given name
     * @param name Name of the sub-element that should be looked up
     * @return Element definition of <em>name</em>
     */
    public CmsElementDefinition getElementDefinition(String name) {
        return (CmsElementDefinition)m_elementDefinitions.get(name);
    }

    /** Get cache directives for this element.
     *  @return cache directives.
     */
    public A_CmsCacheDirectives getCacheDirectives() {
        return m_cacheDirectives;
    }

    /**
     * checks the proxy public and the proxy private cache settings
     *  of this element and all subelements.
     *  @param cms the cms object.
     *  @param proxySettings The CacheDirectives to merge the own CacheDriectives with.
     *  @param parameters A Hashtable with the parameters.
     */
    public void checkProxySettings(CmsObject cms, CmsCacheDirectives proxySettings, Hashtable parameters) throws CmsException{
        // first our own cachedirectives are they set or not?
        if (!(m_cacheDirectives.userSetProxyPrivate() && m_cacheDirectives.userSetProxyPublic())){
            // we have to find out manually
            boolean proxyPublic = false;
            boolean proxyPrivate = false;
            boolean export = false;
            if(m_templateName == null){
                // no template given set everything to true
                proxyPublic = true;

⌨️ 快捷键说明

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