📄 cmselementlocator.java
字号:
/*
* File : $Source: /usr/local/cvs/opencms/src/com/opencms/template/cache/CmsElementLocator.java,v $
* Date : $Date: 2003/01/20 23:59:22 $
* Version: $Revision: 1.24 $
*
* 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.CmsObject;
import com.opencms.template.CmsMethodCacheDirectives;
import com.opencms.template.I_CmsTemplate;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.StringTokenizer;
import java.util.Vector;
/**
* The ElementLocator is used to receive CmsElement-Objects. It is the Cache for
* these CmsElement-Objects. The CmsElement-Objects are stored in memory or -
* if they are notc used a long time - written to an external database. The
* locator manages all the reading, writing and management of the CmsElement's.
*
* @author Andreas Schouten
* @author Alexander Lucas
*/
public class CmsElementLocator implements com.opencms.boot.I_CmsLogChannels {
/**
* A hashtable to store the elements.
*/
private CmsLruCache m_elements;
/**
* link to the extern dependencies vector
*/
private Hashtable m_dependenciesExtern = null;
/**
* The default constructor for this locator.
*/
CmsElementLocator(int cacheSize) {
if(cacheSize < 2){
cacheSize = 50000;
}
m_elements = new CmsLruCache(cacheSize);
}
/**
* Adds a new Element to this locator.
* This method is kept private and must not be used from outside.
* New elements automatically are generated and stored by the Locator,
* so no one really needs to use this method.
* @param descriptor - the descriptor for this element.
* @param element - the Element to put in this locator.
*/
private void put(CmsElementDescriptor desc, A_CmsElement element) {
Vector removedElement = m_elements.put(desc, element);
if(removedElement != null && m_dependenciesExtern != null){
// look if the element is critical and if clear the m_dependenciesExtern table
removeElementFromDependencies((CmsElementDescriptor)removedElement.firstElement(),
(A_CmsElement)removedElement.lastElement());
}
}
/**
* Deletes all variantdependenciesEntries of an Element from the extern dependencies table.
*
* @param the descriptor of the element.
* @param the element itself.
*/
public void removeElementFromDependencies(CmsElementDescriptor desc, A_CmsElement element){
if(element.hasDependenciesVariants()){
Vector variantKeys = element.getAllVariantKeys();
String cacheStart = desc.getClassName() +"|"+ desc.getTemplateName() +"|";
for(int i=0; i<variantKeys.size(); i++){
String key = (String)variantKeys.elementAt(i);
removeVariantFromDependencies(cacheStart + key, element.getVariant(key));
}
}
}
/**
* Deletes all variantdependenciesEntries of an Variant from the extern dependencies table.
*
* @param key the compleate entry in the table like "classname|template|variantcachekey"
* @param the variant
*/
public void removeVariantFromDependencies(String key, CmsElementVariant variant){
if(variant != null){
Vector variantDeps = variant.getDependencies();
if(variantDeps != null){
for(int j=0; j<variantDeps.size(); j++){
Vector externEntrys = (Vector)m_dependenciesExtern.get(variantDeps.elementAt(j));
if(externEntrys != null){
externEntrys.removeElement(key);
}
}
}
}
}
/**
* Gets a Elements from this locator.
* @param desc - the descriptor to locate the element.
* @return the element that was found.
*/
public A_CmsElement get(CmsObject cms, CmsElementDescriptor desc, Hashtable parameters) throws CmsException{
A_CmsElement result;
result = (A_CmsElement)m_elements.get(desc);
if(result == null) {
// the element was not found in the element cache
// we have to generate it
I_CmsTemplate cmsTemplate = null;
// look if it is an methode element
if("METHOD".equals(desc.getTemplateName())){
String orgClassName = desc.getClassName();
String className = orgClassName.substring(0,orgClassName.lastIndexOf("."));
String methodName = orgClassName.substring(orgClassName.lastIndexOf(".")+1);
try {
cmsTemplate = (I_CmsTemplate)com.opencms.template.CmsTemplateClassManager.getClassInstance(cms, className);
CmsMethodCacheDirectives mcd = (CmsMethodCacheDirectives)cmsTemplate.getClass().getMethod(
"getMethodCacheDirectives", new Class[] {
CmsObject.class, String.class}).invoke(cmsTemplate,
new Object[] {cms, methodName});;
result = new CmsMethodElement(className, methodName, mcd,
cms.getRequestContext().getElementCache().getVariantCachesize());
put(desc, result);
} catch(Throwable e) {
if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging()) {
A_OpenCms.log(C_OPENCMS_CRITICAL, toString() + " Could not initialize method element for class \"" + className + "\". ");
A_OpenCms.log(C_OPENCMS_CRITICAL, e.toString());
return null;
}
}
}else{
try {
cmsTemplate = (I_CmsTemplate)com.opencms.template.CmsTemplateClassManager.getClassInstance(cms, desc.getClassName());
result = cmsTemplate.createElement(cms, desc.getTemplateName(), parameters);
put(desc, result);
} catch(Throwable e) {
if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging()) {
A_OpenCms.log(C_OPENCMS_CRITICAL, toString() + " Could not initialize (sub-)element for class \"" + desc.getClassName() + "\". ");
A_OpenCms.log(C_OPENCMS_CRITICAL, e.toString());
throw new CmsException("Could not initialize (sub-)element for class \"" +
desc.getClassName() + "\". " +e.toString() , CmsException.C_XML_WRONG_TEMPLATE_CLASS);
}
}
}
}
return result;
}
/**
* Gets the Information of max size and size for the cache.
*
* @return a Vector whith informations about the size of the cache.
*/
public Vector getCacheInfo(){
return m_elements.getCacheInfo();
}
/**
* for debbuging only. Prints information about the cache system.
*
* @param selector Selects which info is printed.
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -