KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2 * File : $Source: /usr/local/cvs/opencms/src-modules/com/opencms/template/cache/CmsMethodElement.java,v $
3 * Date : $Date: 2005/06/27 23:22:30 $
4 * Version: $Revision: 1.6 $
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
35 import com.opencms.legacy.CmsLegacyException;
36 import com.opencms.template.A_CmsCacheDirectives;
37 import com.opencms.template.A_CmsXmlContent;
38 import com.opencms.template.CmsCacheDirectives;
39 import com.opencms.template.CmsMethodCacheDirectives;
40 import com.opencms.template.CmsProcessedString;
41 import com.opencms.template.I_CmsTemplate;
42
43 import java.io.UnsupportedEncodingException JavaDoc;
44 import java.lang.reflect.InvocationTargetException JavaDoc;
45 import java.util.Hashtable JavaDoc;
46
47 /**
48  * An instance of CmsMethodElement represents an special method Element in the OpenCms
49  * element cache area. It contains all informations to generate the output of this
50  * method. It also stores the variants of once generated content to speed up
51  * performance.
52  *
53  * @author Hanjo Riege
54  * @version 1.0
55  *
56  * @deprecated Will not be supported past the OpenCms 6 release.
57  */

58
59 public class CmsMethodElement extends A_CmsElement {
60
61     /**
62      * the name of the methode.
63      */

64     private String JavaDoc m_methodName;
65
66     /**
67      * Constructor for an element with the given class and template name.
68      */

69     public CmsMethodElement(String JavaDoc className, String JavaDoc methodName, CmsMethodCacheDirectives mcd, int variantCachesize) {
70         m_methodName = methodName;
71         init(className, methodName, mcd, variantCachesize);
72     }
73
74     /**
75      * checks the proxy public and the proxy private cache settings
76      * of this element and all subelements. This is a Methodelement so there are no subelements.
77      * @param cms the cms object.
78      * @param proxySettings The CacheDirectives to merge the own CacheDriectives with.
79      * @param parameters A Hashtable with the parameters.
80      */

81     public void checkProxySettings(CmsObject cms, CmsCacheDirectives proxySettings, Hashtable JavaDoc parameters){
82
83         proxySettings.merge(m_cacheDirectives);
84     }
85
86     /**
87      * Get the content of this element.
88      * @param elementCache Entry point for the element cache
89      * @param cms CmsObject for accessing system resources
90      * @param elDefs Definitions of this element's subelements
91      * @param parameters All parameters of this request
92      * @param methodParameter contains the parameter for the methode (the tagcontent in the xmlfile).
93      * @return Byte array with the processed content of this element.
94      * @throws CmsException
95      */

96     public byte[] getContent(CmsElementCache elementCache, CmsObject cms, CmsElementDefinitionCollection elDefs, String JavaDoc elementName, Hashtable JavaDoc parameters, String JavaDoc methodParameter) throws CmsException {
97         String JavaDoc result = null;
98
99         // get our own cache directives
100
A_CmsCacheDirectives cd = getCacheDirectives();
101         
102         if (cd == null) {
103             // the XmlTemplate implementation is faulty, let assume no caching
104
cd = new CmsMethodCacheDirectives(false);
105         }
106         
107         // cacheKey with the methodeParameter so we have variantes for each parameter
108
String JavaDoc cacheKey = cd.getCacheKey(cms, parameters);
109         if (cacheKey != null){
110             cacheKey += methodParameter;
111         }
112
113         CmsElementVariant variant = null;
114
115         if(cd.isInternalCacheable()){
116             if(cd.isTimeCritical() && (m_timestamp < cd.getTimeout().getLastChange())){
117                 clearVariantCache();
118             }else{
119                 variant = getVariant(cacheKey);
120             }
121             if(variant != null){
122                 result = (String JavaDoc)variant.get(0);
123             }
124         }
125         if(variant == null){
126             // this methode was not found in the variant cache
127
// we have to generate it by calling the methode in the template class
128

129             // Get template class.
130
I_CmsTemplate templateClass = null;
131             try {
132                 templateClass = getTemplateClass(cms, m_className);
133             } catch(Throwable JavaDoc e) {
134                 if(CmsLog.getLog(this).isErrorEnabled()) {
135                     CmsLog.getLog(this).error("Could not load my template class \"" + m_className + "\"", e);
136                     return e.toString().getBytes();
137                 }
138             }
139             // now call the method
140
Object JavaDoc methodResult = null;
141             try{
142                 methodResult = templateClass.getClass().getMethod(m_methodName, new Class JavaDoc[] {
143                                     CmsObject.class, String JavaDoc.class, A_CmsXmlContent.class,
144                                      Object JavaDoc.class}).invoke(templateClass,
145                                      new Object JavaDoc[] {cms, methodParameter, null, parameters});
146             }catch(NoSuchMethodException JavaDoc exc) {
147                 throwException("[CmsMethodElemtent] User method " + m_methodName + " was not found in class " + templateClass.getClass().getName() + ".", CmsLegacyException.C_XML_NO_USER_METHOD);
148             }catch(InvocationTargetException JavaDoc targetEx) {
149                 // the method could be invoked, but throwed a exception
150
// itself. Get this exception and throw it again.
151
Throwable JavaDoc e = targetEx.getTargetException();
152                 if(!(e instanceof CmsException)) {
153                     // Only print an error if this is NO CmsException
154
throwException("User method " + m_methodName + " throwed an exception. " + e, CmsLegacyException.C_UNKNOWN_EXCEPTION);
155                 }else {
156                     // This is a CmsException
157
// Error printing should be done previously.
158
throw (CmsException)e;
159                 }
160             }catch(Exception JavaDoc exc2) {
161                 throwException("User method " + m_methodName + " was found but could not be invoked. " + exc2, CmsLegacyException.C_XML_NO_USER_METHOD);
162             }
163             if(methodResult != null){
164                 if(methodResult instanceof String JavaDoc){
165                     result = (String JavaDoc)methodResult;
166                 }else if(methodResult instanceof byte[]){
167                     try {
168                         result = new String JavaDoc((byte[])methodResult, cms.getRequestContext().getEncoding());
169                     } catch (UnsupportedEncodingException JavaDoc uee) {
170                         throw new CmsLegacyException(CmsLegacyException.C_LOADER_GENERIC_ERROR, uee);
171                     }
172                 }else if(methodResult instanceof Integer JavaDoc){
173                     result = ((Integer JavaDoc)methodResult).toString();
174                 }else if(methodResult instanceof CmsProcessedString){
175                     // result stays null but we have to write to the variant cache
176
variant = new CmsElementVariant();
177                     variant.add(((CmsProcessedString)methodResult).toString());
178                     addVariant(cacheKey, variant);
179                 }else {
180                     throwException("User method " + m_methodName + " in class " + templateClass.getClass().getName() + " returned an unsupported Object: " + methodResult.getClass().getName(), CmsLegacyException.C_XML_PROCESS_ERROR);
181                 }
182             }
183             if((result != null)&&(cacheKey != null)&&(cd.isInternalCacheable())){
184                 variant = new CmsElementVariant();
185                 variant.add(result);
186                 addVariant(cacheKey, variant);
187             }
188         }
189         if (result == null) return null;
190         try {
191             return result.getBytes(cms.getRequestContext().getEncoding());
192         } catch (UnsupportedEncodingException JavaDoc uee) {
193             throw new CmsLegacyException(CmsLegacyException.C_LOADER_GENERIC_ERROR, uee);
194         }
195     }
196
197     /**
198      * checks the read access. The methode elements can be read by everyone. So we
199      * don't have to check something here.
200      * @param cms The cms Object for reading groups.
201      * @throws CmsException if no read access.
202      */

203     public void checkReadAccess(CmsObject cms) throws CmsException{
204     }
205
206     /**
207      * Help method that handles any occuring exception by writing
208      * an error message to the OpenCms logfile and throwing a
209      * CmsException of the given type.
210      * @param errorMessage String with the error message to be printed.
211      * @param type Type of the exception to be thrown.
212      * @throws CmsLegacyException
213      */

214     protected void throwException(String JavaDoc errorMessage, int type) throws CmsLegacyException {
215         if(CmsLog.getLog(this).isErrorEnabled() ) {
216             CmsLog.getLog(this).error(errorMessage);
217         }
218         throw new CmsLegacyException(errorMessage, type);
219     }
220
221 }
Popular Tags