1 28 29 package com.opencms.template.cache; 30 31 import org.opencms.file.CmsObject; 32 import org.opencms.file.CmsResource; 33 import org.opencms.file.CmsUser; 34 import org.opencms.main.CmsException; 35 import org.opencms.main.CmsLog; 36 import org.opencms.main.OpenCms; 37 import org.opencms.security.CmsSecurityException; 38 39 import com.opencms.legacy.CmsLegacyException; 40 import com.opencms.legacy.CmsXmlTemplateLoader; 41 import com.opencms.template.A_CmsCacheDirectives; 42 import com.opencms.template.CmsCacheDirectives; 43 import com.opencms.template.CmsTemplateClassManager; 44 import com.opencms.template.I_CmsTemplate; 45 46 import java.io.ByteArrayOutputStream ; 47 import java.io.IOException ; 48 import java.util.Enumeration ; 49 import java.util.Hashtable ; 50 import java.util.Vector ; 51 52 66 public abstract class A_CmsElement { 67 68 69 protected String m_className; 70 71 72 protected String m_templateName; 73 74 75 protected A_CmsCacheDirectives m_cacheDirectives; 76 77 78 protected long m_timestamp = 0; 79 80 81 protected CmsElementDefinitionCollection m_elementDefinitions; 82 83 84 private CmsLruCache m_variants; 85 86 90 protected boolean m_hasDepVariants = false; 91 92 95 protected void init(String className, String templateName, A_CmsCacheDirectives cd, 96 int variantCachesize) { 97 98 m_className = className; 99 m_templateName = templateName; 100 m_cacheDirectives = cd; 101 m_elementDefinitions = new CmsElementDefinitionCollection(); 102 m_variants = new CmsLruCache(variantCachesize); 103 } 104 105 113 protected void init(String className, String templateName, A_CmsCacheDirectives cd, 114 CmsElementDefinitionCollection defs, int variantCachesize) { 115 116 m_className = className; 117 m_templateName = templateName; 118 m_cacheDirectives = cd; 119 m_elementDefinitions = defs; 120 m_variants = new CmsLruCache(variantCachesize); 121 } 122 123 127 public void addDefinition(CmsElementDefinition def) { 128 129 m_elementDefinitions.add(def); 130 } 131 132 137 public Vector addVariant(Object key, CmsElementVariant variant) { 138 139 if (CmsLog.getLog(this).isInfoEnabled()) { 140 CmsLog.getLog(this).info("Adding variant \"" + key + "\" to xml template cache"); 141 } 142 if (key != null) { 143 CmsElementVariant old = (CmsElementVariant)m_variants.get(key); 144 if ((old != null) && (old.size() == 0)) { 145 variant.addDependencies(old.getDependencies()); 146 variant.mergeNextTimeout(old.getNextTimeout()); 147 } 148 return m_variants.put(key, variant); 149 } 150 return null; 151 } 152 153 public void removeVariant(Object key) { 154 155 if (CmsLog.getLog(this).isInfoEnabled()) { 156 CmsLog.getLog(this).info(toString() + " removing variant \"" + key 157 + "\" from xml template cache"); 158 } 159 if (key != null) { 160 m_variants.remove(key); 161 } 162 } 163 164 169 public void checkReadAccess(CmsObject cms) throws CmsException { 170 171 return; 172 } 173 174 177 public void clearVariantCache() { 178 179 m_variants.clearCache(); 180 m_timestamp = System.currentTimeMillis(); 181 } 182 183 186 public Vector getAllVariantKeys() { 187 188 return m_variants.getAllKeys(); 189 } 190 191 196 public CmsElementVariant getVariant(Object key) { 197 198 if (key == null) { 199 return null; 200 } 201 CmsElementVariant result = (CmsElementVariant)m_variants.get(key); 202 if (result != null && result.size() == 0) { 203 result = null; 204 } 205 if (CmsLog.getLog(this).isInfoEnabled()) { 206 if (result != null) { 207 CmsLog.getLog(this).info(toString() + " getting variant \"" + key 208 + "\" from cache. "); 209 } else { 210 CmsLog.getLog(this).info(toString() + " Variant \"" + key 211 + "\" is not in element cache. "); 212 } 213 } 214 return result; 215 } 216 217 221 public boolean hasDependenciesVariants() { 222 223 return m_hasDepVariants; 224 } 225 226 229 public void thisElementHasDepVariants() { 230 231 m_hasDepVariants = true; 232 } 233 234 238 public CmsElementDefinitionCollection getAllDefinitions() { 239 240 return m_elementDefinitions; 241 } 242 243 248 public CmsElementDefinition getElementDefinition(String name) { 249 250 return m_elementDefinitions.get(name); 251 } 252 253 256 public A_CmsCacheDirectives getCacheDirectives() { 257 258 return m_cacheDirectives; 259 } 260 261 268 public void checkProxySettings(CmsObject cms, CmsCacheDirectives proxySettings, 269 Hashtable parameters) throws CmsException { 270 271 if (!(m_cacheDirectives.userSetProxyPrivate() && m_cacheDirectives.userSetProxyPublic())) { 273 boolean proxyPublic = false; 275 boolean proxyPrivate = false; 276 boolean export = false; 277 if (m_templateName == null) { 278 proxyPublic = true; 280 proxyPrivate = true; 281 export = true; 282 } else { 283 try { 284 if (m_cacheDirectives.isInternalCacheable() 285 && (!m_cacheDirectives.isUserPartOfKey())) { 286 CmsResource templ = cms.readResource(m_templateName); 287 int accessflags = templ.getFlags(); 288 if (!((accessflags & CmsResource.FLAG_INTERNAL) > 0)) { 289 proxyPrivate = true; 291 if ((!m_cacheDirectives.isParameterPartOfKey()) 292 && (!m_cacheDirectives.isTimeCritical())) { 293 export = true; 294 } 295 } 296 } 297 298 } catch (Exception e) { 299 if (CmsLog.getLog(this).isWarnEnabled()) { 301 CmsLog.getLog(this).warn(toString() 302 + " could not find out if the element is proxy cacheable", e); 303 } 304 } 305 } 306 if (!m_cacheDirectives.userSetProxyPrivate()) { 307 ((CmsCacheDirectives)m_cacheDirectives).setProxyPrivateCacheable(proxyPrivate); 308 } 309 if (!m_cacheDirectives.userSetProxyPublic()) { 310 ((CmsCacheDirectives)m_cacheDirectives).setProxyPublicCacheable(proxyPublic); 311 } 312 if (!m_cacheDirectives.userSetExport()) { 313 ((CmsCacheDirectives)m_cacheDirectives).setExport(export); 314 } 315 } 316 proxySettings.merge(m_cacheDirectives); 317 Enumeration elementNames = m_elementDefinitions.getAllElementNames(); 319 while (elementNames.hasMoreElements()) { 320 String name = (String )elementNames.nextElement(); 321 CmsElementDefinition currentDef = m_elementDefinitions.get(name); 322 A_CmsElement currentEle = CmsXmlTemplateLoader.getElementCache().getElementLocator().get(cms, new CmsElementDescriptor( 323 currentDef.getClassName(), currentDef.getTemplateName()), parameters); 324 currentEle.checkProxySettings(cms, proxySettings, parameters); 325 } 326 } 327 328 341 public abstract byte[] getContent(CmsElementCache elementCache, CmsObject cms, 342 CmsElementDefinitionCollection efDefs, String elementName, Hashtable parameters, 343 String methodParameter) throws CmsException; 344 345 351 protected I_CmsTemplate getTemplateClass(CmsObject cms, String classname) throws CmsException { 352 353 Object o = CmsTemplateClassManager.getClassInstance(classname); 354 if (o instanceof I_CmsTemplate) { 356 return (I_CmsTemplate)o; 357 } else { 358 throw new CmsLegacyException(classname + " is no OpenCms template class.", 359 CmsLegacyException.C_XML_NO_TEMPLATE_CLASS); 360 } 361 } 362 363 374 public byte[] resolveVariant(CmsObject cms, CmsElementVariant variant, 375 CmsElementCache elementCache, CmsElementDefinitionCollection elDefs, Hashtable parameters) 376 throws CmsException { 377 378 boolean resolveDebug = false; 379 if (resolveDebug) 380 System.err.println("= Start resolving variant " + variant); 381 int len = variant.size(); 382 try { 384 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 385 for (int i = 0; i < len; i++) { 386 if (resolveDebug) 388 System.err.print("= Part " + i + " is a "); 389 Object o = variant.get(i); 390 if (o instanceof String ) { 394 if (resolveDebug) 395 System.err.println("String"); 396 baos.write(((String )o).getBytes(cms.getRequestContext().getEncoding())); 397 } else if (o instanceof byte[]) { 398 if (resolveDebug) 399 System.err.println("byte array"); 400 baos.write((byte[])o); 401 } else if (o instanceof CmsElementLink) { 402 if (resolveDebug) 403 System.err.println("Element Link"); 404 405 String lookupName = ((CmsElementLink)o).getElementName(); 408 if (resolveDebug) 409 System.err.println("= Trying to resolve link \"" + lookupName + "\"."); 410 CmsElementDefinition elDef = elDefs.get(lookupName); 411 if (elDef != null) { 412 elDef.joinParameters(parameters); 415 parameters.put("_ELEMENT_", elDef.getName()); 417 if (elDef.getTemplateName() != null) { 418 parameters.put(elDef.getName() + "._TEMPLATE_", elDef.getTemplateName()); 419 } 420 parameters.put(elDef.getName() + "._CLASS_", elDef.getClassName()); 421 if (elDef.getTemplateSelector() != null) { 422 parameters.put(elDef.getName() + "._TEMPLATESELECTOR_", elDef.getTemplateSelector()); 423 } else { 424 parameters.put(elDef.getName() + "._TEMPLATESELECTOR_", "default"); 425 } 426 A_CmsElement subEl = elementCache.getElementLocator().get(cms, elDef.getDescriptor(), parameters); 428 if (resolveDebug) 429 System.err.println("= Element defintion for \"" + lookupName 430 + "\" says: "); 431 if (resolveDebug) 432 System.err.println("= -> Class : " + elDef.getClassName()); 433 if (resolveDebug) 434 System.err.println("= -> Template : " + elDef.getTemplateName()); 435 String errorMessage = ""; 436 if (subEl != null) { 437 if (resolveDebug) 440 System.err.println("= Element object found for \"" + lookupName 441 + "\". Calling getContent on this object. "); 442 byte[] buffer = null; 443 try { 444 buffer = subEl.getContent(elementCache, cms, elDefs, lookupName, parameters, null); 445 } catch (Exception e) { 446 if (CmsUser.USER_TYPE_SYSTEMUSER == cms.getRequestContext().currentUser().getType() 449 && !OpenCms.getDefaultUsers().getUserGuest().equals(cms.getRequestContext().currentUser().getName())) { 450 errorMessage = e.toString(); 452 } 453 subEl = null; 454 buffer = null; 455 if (e instanceof CmsException) { 456 CmsException ce = (CmsException)e; 457 if (ce instanceof CmsSecurityException) { 458 if (CmsLog.getLog(this).isErrorEnabled()) { 459 CmsLog.getLog(this).error("Access denied in element " 460 + lookupName, ce); 461 } 462 throw ce; 463 } else { 464 if (CmsLog.getLog(this).isErrorEnabled()) { 466 CmsLog.getLog(this).error("Error in element " 467 + lookupName, e); 468 } 469 } 470 } else { 471 if (CmsLog.getLog(this).isErrorEnabled()) { 473 CmsLog.getLog(this).error("Non-CmsException in element " 474 + lookupName, e); 475 } 476 } 477 } 478 if (buffer != null) { 480 baos.write(buffer); 481 } 482 } else { 483 if (resolveDebug) 486 System.err.println("= Cannot find Element object for \"" 487 + lookupName + "\". Ignoring this link. "); 488 if (CmsLog.getLog(this).isWarnEnabled()) { 489 CmsLog.getLog(this).warn("Cannot find Element object for \"" 490 + lookupName + "\", ignoring this link"); 491 } 492 } 493 494 if (subEl == null) { 497 baos.write(("[" + lookupName + "] ??? ").getBytes()); 498 baos.write(errorMessage.getBytes()); 499 } 500 } else { 501 baos.write(("[" + lookupName + "] Element not defined.").getBytes()); 504 if (CmsLog.getLog(this).isWarnEnabled()) { 505 CmsLog.getLog(this).warn("No element definition found for \"" 506 + lookupName + "\", ignoring this link"); 507 } 508 if (resolveDebug) { 509 System.err.println("= No element definition found for \"" + lookupName 510 + "\". Ignoring this link. "); 511 System.err.println(elDefs.toString()); 512 } 513 } 514 } else if (o instanceof CmsMethodLink) { 515 if (resolveDebug) 516 System.err.println("Method Link"); 517 String methodName = ((CmsMethodLink)o).getMethodeName(); 519 String methodParameter = ((CmsMethodLink)o).getMethodParameter(); 520 A_CmsElement metEle = elementCache.getElementLocator().get(cms, new CmsElementDescriptor( 521 m_className + "." + methodName, "METHOD"), parameters); 522 byte[] buffer = null; 523 if (metEle != null) { 524 try { 525 buffer = metEle.getContent(elementCache, cms, elDefs, null, parameters, methodParameter); 526 } catch (Exception e) { 527 if (e instanceof CmsException) { 528 if (CmsLog.getLog(this).isErrorEnabled()) { 529 CmsLog.getLog(this).error("Error in method " + methodName, e); 530 } 531 } else { 532 if (CmsLog.getLog(this).isErrorEnabled()) { 534 CmsLog.getLog(this).error("Non-CmsException in method " 535 + methodName, e); 536 } 537 } 538 } 539 } else { 540 if (resolveDebug) 543 System.err.println("= Cannot find methodElemtn object for \"" 544 + methodName + "\". Ignoring this link. "); 545 if (CmsLog.getLog(this).isWarnEnabled()) { 546 CmsLog.getLog(this).warn("Cannot find method Element object for \"" 547 + methodName + "\", ignoring this link"); 548 } 549 } 550 if (buffer != null) { 552 baos.write(buffer); 553 } 554 } 555 } 556 return baos.toByteArray(); 557 } catch (IOException e) { 558 if (CmsLog.getLog(this).isErrorEnabled()) { 560 CmsLog.getLog(this).error("IOException while writing to XMl template OutputStream", e); 561 } 562 throw new CmsLegacyException(CmsLegacyException.C_UNKNOWN_EXCEPTION, e); 563 } 564 } 565 566 570 public String toString() { 571 572 String part1 = getClass().getName(); 573 part1 = part1.substring(part1.lastIndexOf(".") + 1); 574 575 String part2 = m_className.substring(m_className.lastIndexOf(".") + 1); 576 String part3 = ""; 577 if (m_templateName != null) { 578 part3 = m_templateName.substring(m_templateName.lastIndexOf("/") + 1); 579 } 580 581 return "[" + part1 + " (" + part2 + "/" + part3 + ")]"; 582 } 583 }
| Popular Tags
|