KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opencms > template > cache > CmsElementXml


1 /*
2 * File : $Source: /usr/local/cvs/opencms/src-modules/com/opencms/template/cache/CmsElementXml.java,v $
3 * Date : $Date: 2005/05/31 15:51:19 $
4 * Version: $Revision: 1.3 $
5 *
6 * This library is part of OpenCms -
7 * the Open Source Content Mananagement System
8 *
9 * Copyright (C) 2001 The OpenCms Group
10 *
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * For further information about OpenCms, please see the
22 * OpenCms Website: http://www.opencms.org
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 */

28
29 package com.opencms.template.cache;
30
31 import org.opencms.file.CmsObject;
32 import org.opencms.main.CmsException;
33 import org.opencms.main.CmsLog;
34 import org.opencms.security.CmsSecurityException;
35
36 import com.opencms.legacy.CmsLegacyException;
37 import com.opencms.legacy.CmsXmlTemplateLoader;
38 import com.opencms.template.A_CmsCacheDirectives;
39 import com.opencms.template.CmsCacheDirectives;
40 import com.opencms.template.I_CmsTemplate;
41
42 import java.util.Hashtable JavaDoc;
43
44 /**
45  * An instance of CmsElementXML represents an requestable Element in the OpenCms
46  * element cache area. It contains all informations to generate the content of this
47  * element. It also stores the variants of once generated content to speed up
48  * performance.
49  *
50  * It points to other depending elements. Theses elements are called to generate
51  * their content on generation-time.
52  *
53  * @author Alexander Lucas
54  *
55  * @deprecated Will not be supported past the OpenCms 6 release.
56  */

57 public class CmsElementXml extends A_CmsElement {
58
59     /**
60      * Constructor for an element with the given class and template name.
61      */

62     public CmsElementXml(String JavaDoc className, String JavaDoc templateName, CmsCacheDirectives cd, int variantCachesize) {
63         init(className, templateName, cd, variantCachesize);
64     }
65
66     /**
67      * A construcor which creates an element with the given element
68      * definitions.
69      * @param name the name of this element-definition.
70      * @param className the classname of this element-definition.
71      * @param readAccessGroup The group that may read the element.
72      * @param cd Cache directives for this element
73      * @param defs CmsElementDefinitionCollection for this element.
74      */

75     public CmsElementXml(String JavaDoc className, String JavaDoc templateName, CmsCacheDirectives cd, CmsElementDefinitionCollection defs, int variantCachesize) {
76         init(className, templateName, cd, defs, variantCachesize);
77     }
78
79     /**
80      * Get the content of this element.
81      * @param elementCache Entry point for the element cache
82      * @param cms CmsObject for accessing system resources
83      * @param elDefs Definitions of this element's subelements
84      * @param parameters All parameters of this request
85      * @param methodParameter not used here, only methodelemets need it.
86      * @return Byte array with the processed content of this element.
87      * @throws CmsException
88      */

89     public byte[] getContent(CmsElementCache elementCache, CmsObject cms, CmsElementDefinitionCollection elDefs, String JavaDoc elementName, Hashtable JavaDoc parameters, String JavaDoc methodParameter) throws CmsException {
90         byte[] result = null;
91
92         // Merge own element definitions with our parent's definitions
93
CmsElementDefinitionCollection mergedElDefs = new CmsElementDefinitionCollection(elDefs, m_elementDefinitions);
94
95         // Get out own cache directives
96
A_CmsCacheDirectives cd = getCacheDirectives();
97
98         CmsElementVariant variant = null;
99
100         // Now check, if there is a variant of this element in the cache.
101
if(cd.isInternalCacheable()) {
102
103             checkReadAccess(cms);
104             // check if this Element has a date of expiry
105
if (cd.isTimeCritical() && (m_timestamp < cd.getTimeout().getLastChange())){
106                 // this element is too old, delete the cache
107
if(this.hasDependenciesVariants()){
108                     // remove all the variants from the extern dep table
109
CmsXmlTemplateLoader.getOnlineElementCache().getElementLocator().removeElementFromDependencies(
110                                 mergedElDefs.get(elementName).getDescriptor(), this);
111                 }
112                 clearVariantCache();
113             }else{
114                 variant = getVariant(cd.getCacheKey(cms, parameters));
115                 if((variant != null) && variant.isTimeCritical()
116                                      && variant.getNextTimeout() < System.currentTimeMillis()){
117                     // the variant is not longer valid, remove it from the extern dependencies
118
CmsXmlTemplateLoader.getOnlineElementCache().getElementLocator().removeVariantFromDependencies(
119                                         m_className +"|" + m_templateName +"|" + cd.getCacheKey(cms, parameters), variant);
120                     variant = null;
121                 }
122             }
123             if(variant != null) {
124                 result = resolveVariant(cms, variant, elementCache, mergedElDefs, parameters);
125             }
126         }
127         if(variant == null) {
128             // This element was not found in the variant cache.
129
// We have to generate it by calling the "classic" getContent() method on the template
130
// class.
131

132             // get template class
133
I_CmsTemplate templateClass = null;
134             try {
135                 templateClass = getTemplateClass(cms, m_className);
136             } catch(Throwable JavaDoc e) {
137                 if(CmsLog.getLog(this).isErrorEnabled()) {
138                     CmsLog.getLog(this).error("Could not load my template class \"" + m_className + "\"", e);
139                     return e.toString().getBytes();
140                 }
141             }
142
143             try {
144                 /*if(cd.isInternalCacheable()) {
145                     System.err.println(toString() + " ### Variant not in cache. Must be generated.");
146                 } else {
147                     System.err.println(toString() + " ### Element not cacheable. Generating variant temporarily.");
148                 }*/

149                 // startProcessing() later will be responsible for generating our new variant.
150
// since the method resolveVariant (THIS method) will be called recursively
151
// by startProcessing(), we have to pass the current element definitions.
152
// Unfortunately, there is no other way than putting them into our parameter
153
// hashtable. For compatibility reasons we are not allowed to change
154
// the interface of getContent() or startProcessing()
155
parameters.put("_ELDEFS_", mergedElDefs);
156                 String JavaDoc templateSelector = null;
157                 try{
158                     templateSelector = mergedElDefs.get(elementName).getTemplateSelector();
159                 }catch(Exception JavaDoc e){
160                 }
161                 try {
162                     String JavaDoc theTemplate = m_templateName;
163                     if(theTemplate == null){
164                         try{
165                             theTemplate = mergedElDefs.get("body").getTemplateName();
166                         }catch(Exception JavaDoc exc){
167                             if(CmsLog.getLog(this).isErrorEnabled()) {
168                                 CmsLog.getLog(this).error("Could not find the body element to get the default template file for " + this.toString(), exc);
169                             }
170                         }
171                     }
172                     result = templateClass.getContent(cms, theTemplate, elementName, parameters, templateSelector);
173                 } catch(Exception JavaDoc e) {
174                     if(e instanceof CmsException) {
175                         CmsException ce = (CmsException)e;
176                         if(ce instanceof CmsSecurityException) {
177                             // This was an access denied exception.
178
// This is not very critical at the moment.
179
if(CmsLog.getLog(this).isDebugEnabled()) {
180                                 CmsLog.getLog(this).debug("Access denied in getContent for template class " + m_className);
181                             }
182                         } else {
183                             // Any other CmsException.
184
// This could be more critical.
185
if(CmsLog.getLog(this).isWarnEnabled()) {
186                                 CmsLog.getLog(this).warn("Error in getContent() for template class " + m_className, e);
187                             }
188                         }
189                         throw ce;
190                     } else {
191                         // No CmsException. This is really, really bad!
192
if(CmsLog.getLog(this).isErrorEnabled()) {
193                             CmsLog.getLog(this).error("Non OpenCms error occured in getContent for template class " + m_className, e);
194                         }
195                         throw new CmsLegacyException(CmsLegacyException.C_UNKNOWN_EXCEPTION, e);
196                     }
197                 }
198             }
199             catch(CmsException e) {
200                 // Clear cache and do logging here
201
throw e;
202             }
203         }
204         return result;
205     }
206 }
Popular Tags