| 1 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 ; 44 import java.lang.reflect.InvocationTargetException ; 45 import java.util.Hashtable ; 46 47 58 59 public class CmsMethodElement extends A_CmsElement { 60 61 64 private String m_methodName; 65 66 69 public CmsMethodElement(String className, String methodName, CmsMethodCacheDirectives mcd, int variantCachesize) { 70 m_methodName = methodName; 71 init(className, methodName, mcd, variantCachesize); 72 } 73 74 81 public void checkProxySettings(CmsObject cms, CmsCacheDirectives proxySettings, Hashtable parameters){ 82 83 proxySettings.merge(m_cacheDirectives); 84 } 85 86 96 public byte[] getContent(CmsElementCache elementCache, CmsObject cms, CmsElementDefinitionCollection elDefs, String elementName, Hashtable parameters, String methodParameter) throws CmsException { 97 String result = null; 98 99 A_CmsCacheDirectives cd = getCacheDirectives(); 101 102 if (cd == null) { 103 cd = new CmsMethodCacheDirectives(false); 105 } 106 107 String 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 )variant.get(0); 123 } 124 } 125 if(variant == null){ 126 129 I_CmsTemplate templateClass = null; 131 try { 132 templateClass = getTemplateClass(cms, m_className); 133 } catch(Throwable 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 Object methodResult = null; 141 try{ 142 methodResult = templateClass.getClass().getMethod(m_methodName, new Class [] { 143 CmsObject.class, String .class, A_CmsXmlContent.class, 144 Object .class}).invoke(templateClass, 145 new Object [] {cms, methodParameter, null, parameters}); 146 }catch(NoSuchMethodException 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 targetEx) { 149 Throwable e = targetEx.getTargetException(); 152 if(!(e instanceof CmsException)) { 153 throwException("User method " + m_methodName + " throwed an exception. " + e, CmsLegacyException.C_UNKNOWN_EXCEPTION); 155 }else { 156 throw (CmsException)e; 159 } 160 }catch(Exception 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 ){ 165 result = (String )methodResult; 166 }else if(methodResult instanceof byte[]){ 167 try { 168 result = new String ((byte[])methodResult, cms.getRequestContext().getEncoding()); 169 } catch (UnsupportedEncodingException uee) { 170 throw new CmsLegacyException(CmsLegacyException.C_LOADER_GENERIC_ERROR, uee); 171 } 172 }else if(methodResult instanceof Integer ){ 173 result = ((Integer )methodResult).toString(); 174 }else if(methodResult instanceof CmsProcessedString){ 175 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 uee) { 193 throw new CmsLegacyException(CmsLegacyException.C_LOADER_GENERIC_ERROR, uee); 194 } 195 } 196 197 203 public void checkReadAccess(CmsObject cms) throws CmsException{ 204 } 205 206 214 protected void throwException(String 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 |