1 31 32 package org.opencms.flex; 33 34 import org.opencms.cache.I_CmsLruCacheObject; 35 import org.opencms.file.CmsResource; 36 import org.opencms.i18n.CmsMessageContainer; 37 import org.opencms.main.CmsLog; 38 import org.opencms.monitor.CmsMemoryMonitor; 39 import org.opencms.monitor.I_CmsMemoryMonitorable; 40 41 import java.io.IOException ; 42 import java.util.ArrayList ; 43 import java.util.Collections ; 44 import java.util.Iterator ; 45 import java.util.List ; 46 import java.util.Map ; 47 48 import javax.servlet.ServletException ; 49 50 import org.apache.commons.logging.Log; 51 52 80 public class CmsFlexCacheEntry extends Object implements I_CmsLruCacheObject, I_CmsMemoryMonitorable { 81 82 83 public static final int INITIAL_CAPACITY_LISTS = 10; 84 85 86 private static final Log LOG = CmsLog.getLog(CmsFlexCacheEntry.class); 87 88 89 private int m_byteSize; 90 91 92 private boolean m_completed; 93 94 95 private long m_dateExpires; 96 97 98 private long m_dateLastModified; 99 100 101 private List m_elements; 102 103 104 private Map m_headers; 105 106 107 private I_CmsLruCacheObject m_next; 108 109 110 private I_CmsLruCacheObject m_previous; 111 112 113 private String m_redirectTarget; 114 115 116 private String m_variationKey; 117 118 119 private Map m_variationMap; 120 121 127 public CmsFlexCacheEntry() { 128 129 m_elements = new ArrayList (INITIAL_CAPACITY_LISTS); 130 m_dateExpires = CmsResource.DATE_EXPIRED_DEFAULT; 131 m_dateLastModified = -1; 132 m_byteSize = 1024; 134 135 setNextLruObject(null); 136 setPreviousLruObject(null); 137 } 138 139 145 public void add(byte[] bytes) { 146 147 if (m_completed) { 148 return; 149 } 150 if (m_redirectTarget == null) { 151 m_elements.add(bytes); 153 m_byteSize += CmsMemoryMonitor.getMemorySize(bytes); 154 } 155 bytes = null; 156 } 157 158 164 public void add(String resource, Map parameters) { 165 166 if (m_completed) { 167 return; 168 } 169 if (m_redirectTarget == null) { 170 m_elements.add(resource); 172 if (parameters == null) { 173 parameters = Collections.EMPTY_MAP; 174 } 175 m_elements.add(parameters); 176 m_byteSize += CmsMemoryMonitor.getMemorySize(resource); 177 } 178 } 179 180 186 public void addHeaders(Map headers) { 187 188 if (m_completed) { 189 return; 190 } 191 m_headers = headers; 192 193 Iterator allHeaders = m_headers.keySet().iterator(); 194 while (allHeaders.hasNext()) { 195 m_byteSize += CmsMemoryMonitor.getMemorySize(allHeaders.next()); 196 } 197 } 198 199 202 public void addToLruCache() { 203 204 if (LOG.isDebugEnabled()) { 206 LOG.debug(Messages.get().getBundle().key(Messages.LOG_FLEXCACHEENTRY_ADDED_ENTRY_1, this)); 207 } 208 } 209 210 218 public void complete() { 219 220 m_completed = true; 221 if (m_headers != null) { 223 m_headers = Collections.unmodifiableMap(m_headers); 224 } 225 if (m_elements != null) { 226 m_elements = Collections.unmodifiableList(m_elements); 227 } 228 if (LOG.isDebugEnabled()) { 229 LOG.debug(Messages.get().getBundle().key(Messages.LOG_FLEXCACHEENTRY_ENTRY_COMPLETED_1, toString())); 230 } 231 } 232 233 241 public List elements() { 242 243 return m_elements; 244 } 245 246 252 public long getDateExpires() { 253 254 return m_dateExpires; 255 } 256 257 262 public long getDateLastModified() { 263 264 return m_dateLastModified; 265 } 266 267 270 public int getLruCacheCosts() { 271 272 return m_byteSize; 273 } 274 275 278 public int getMemorySize() { 279 280 return getLruCacheCosts(); 281 } 282 283 286 public I_CmsLruCacheObject getNextLruObject() { 287 288 return m_next; 289 } 290 291 294 public I_CmsLruCacheObject getPreviousLruObject() { 295 296 return m_previous; 297 } 298 299 302 public Object getValue() { 303 304 return m_elements; 305 } 306 307 310 public void removeFromLruCache() { 311 312 if ((m_variationMap != null) && (m_variationKey != null)) { 313 m_variationMap.remove(m_variationKey); 314 } 315 if (LOG.isDebugEnabled()) { 316 LOG.debug(Messages.get().getBundle().key( 317 Messages.LOG_FLEXCACHEENTRY_REMOVED_ENTRY_FOR_VARIATION_1, 318 m_variationKey)); 319 } 320 clear(); 321 } 322 323 336 public void service(CmsFlexRequest req, CmsFlexResponse res) 337 throws CmsFlexCacheException, ServletException , IOException { 338 339 if (!m_completed) { 340 return; 341 } 342 343 if (m_redirectTarget != null) { 344 res.setOnlyBuffering(false); 345 res.sendRedirect(m_redirectTarget); 347 } else { 348 CmsFlexResponse.processHeaders(m_headers, res); 350 boolean hasNoSubElements = ((m_elements != null) && (m_elements.size() == 1)); 352 for (int i = 0; i < m_elements.size(); i++) { 354 Object o = m_elements.get(i); 355 if (o instanceof String ) { 356 i++; 358 Map map = (Map )m_elements.get(i); 359 Map oldMap = null; 360 if (map.size() > 0) { 361 oldMap = req.getParameterMap(); 362 req.addParameterMap(map); 363 } 364 req.getRequestDispatcher((String )o).include(req, res); 366 if (oldMap != null) { 368 req.setParameterMap(oldMap); 369 } 370 } else { 371 try { 372 res.writeToOutputStream((byte[])o, hasNoSubElements); 373 } catch (IOException e) { 374 375 CmsMessageContainer message = Messages.get().container( 376 Messages.LOG_FLEXCACHEKEY_NOT_FOUND_1, 377 getClass().getName()); 378 if (LOG.isDebugEnabled()) { 379 LOG.debug(message.key()); 380 } 381 382 throw new CmsFlexCacheException(message, e); 383 } 384 } 385 } 386 } 387 } 388 389 395 public synchronized void setDateExpires(long dateExpires) { 396 397 m_dateExpires = dateExpires; 398 if (LOG.isDebugEnabled()) { 399 long now = System.currentTimeMillis(); 400 LOG.debug(Messages.get().getBundle().key( 401 Messages.LOG_FLEXCACHEENTRY_SET_EXPIRATION_DATE_3, 402 new Long (m_dateExpires), 403 new Long (now), 404 new Long (m_dateExpires - now))); 405 } 406 } 407 408 420 public void setDateExpiresToNextTimeout(long timeout) { 421 422 if (timeout < 0 || !m_completed) { 423 return; 424 } 425 426 long now = System.currentTimeMillis(); 427 long daytime = now % 86400000; 428 long timeoutMinutes = timeout * 60000; 429 setDateExpires(now - (daytime % timeoutMinutes) + timeoutMinutes); 430 } 431 432 437 public void setDateLastModified(long dateLastModified) { 438 439 m_dateLastModified = dateLastModified; 440 } 441 442 450 public void setDateLastModifiedToPreviousTimeout(long timeout) { 451 452 long now = System.currentTimeMillis(); 453 long daytime = now % 86400000; 454 long timeoutMinutes = timeout * 60000; 455 setDateLastModified(now - (daytime % timeoutMinutes)); 456 } 457 458 461 public void setNextLruObject(I_CmsLruCacheObject theNextEntry) { 462 463 m_next = theNextEntry; 464 } 465 466 469 public void setPreviousLruObject(I_CmsLruCacheObject thePreviousEntry) { 470 471 m_previous = thePreviousEntry; 472 } 473 474 485 public void setRedirect(String target) { 486 487 if (m_completed) { 488 return; 489 } 490 m_redirectTarget = target; 491 m_byteSize = 512 + CmsMemoryMonitor.getMemorySize(target); 492 m_elements = null; 494 m_headers = null; 495 } 496 497 505 public void setVariationData(String theVariationKey, Map theVariationMap) { 506 507 m_variationKey = theVariationKey; 508 m_variationMap = theVariationMap; 509 } 510 511 516 public String toString() { 517 518 String str = null; 519 if (m_redirectTarget == null) { 520 str = "CmsFlexCacheEntry [" + m_elements.size() + " Elements/" + getLruCacheCosts() + " bytes]\n"; 521 Iterator i = m_elements.iterator(); 522 int count = 0; 523 while (i.hasNext()) { 524 count++; 525 Object o = i.next(); 526 if (o instanceof String ) { 527 str += "" + count + " - <cms:include target=" + o + ">\n"; 528 } else if (o instanceof byte[]) { 529 str += "" + count + " - <![CDATA[" + new String ((byte[])o) + "]]>\n"; 530 } else { 531 str += "<!--[" + o.toString() + "]-->"; 532 } 533 } 534 } else { 535 str = "CmsFlexCacheEntry [Redirect to target=" + m_redirectTarget + "]"; 536 } 537 return str; 538 } 539 540 545 protected void finalize() throws Throwable { 546 547 try { 548 clear(); 549 } catch (Throwable t) { 550 } 552 super.finalize(); 553 } 554 555 558 private void clear() { 559 560 m_elements = null; 562 m_headers = null; 563 m_variationKey = null; 565 m_variationMap = null; 566 } 567 } 568 | Popular Tags |