1 16 package com.blandware.atleap.webapp.util.core; 17 18 import com.blandware.atleap.webapp.menu.MenuComponent; 19 import com.opensymphony.oscache.base.Cache; 20 import com.opensymphony.oscache.base.NeedsRefreshException; 21 import com.opensymphony.oscache.web.ServletCacheAdministrator; 22 import org.apache.commons.logging.Log; 23 import org.apache.commons.logging.LogFactory; 24 25 import javax.servlet.ServletContext ; 26 import javax.servlet.http.HttpServletRequest ; 27 import javax.servlet.jsp.PageContext ; 28 import java.io.Serializable ; 29 import java.util.Date ; 30 31 40 public class CacheUtil { 41 protected transient final Log log = LogFactory.getLog(CacheUtil.class); 42 43 public static final String PAGE_SUFFIX = "_page"; 44 public static final String LAYOUT_SUFFIX = "_layout"; 45 public static final String LOCALIZABLE_SUFFIX = "_localizable"; 46 public static final String RESOURCE_SUFFIX = "_resource"; 47 public static final String CONTENT_PAGE_SUFFIX = "_contentPage"; 48 public static final String MENU_SUFFIX = "_menu"; 49 public static final String FIELD_INDICES_SUFFIX = "_fieldIndices"; 50 51 public static final String FIELD_VALUE_GROUP = "fieldValueGroup"; 52 public static final String FIELD_INDEXES_GROUP = "fieldIndexesGroup"; 53 public static final String RESOURCE_GROUP = "resourceGroup"; 54 public static final String CONTENT_PAGE_GROUP = "contentPageGroup"; 55 public static final String MENU_GROUP = "menuGroup"; 56 57 public static final String INSTANCE_KEY = "com.blandware.atleap.util.CacheUtil.INSTANCE"; 58 59 protected static final String TIME_KEY = "cache.web.time"; 60 protected static final String RESOURCE_MAXSIZE_KEY = "cache.web.resource.maxsize"; 61 62 protected ServletCacheAdministrator admin = null; 63 protected HttpServletRequest request = null; 64 protected ServletContext servletContext = null; 65 protected int refreshPeriod = -1; 66 protected int resourceMaxSize = 50000; 67 68 74 public static CacheUtil getInstance(HttpServletRequest request) { 75 CacheUtil ourInstance = (CacheUtil) request.getAttribute(INSTANCE_KEY); 76 if ( ourInstance == null ) { 77 ourInstance = new CacheUtil(request); 78 request.setAttribute(INSTANCE_KEY, ourInstance); 79 } 80 return ourInstance; 81 } 82 83 88 protected CacheUtil(HttpServletRequest request) { 89 this.request = request; 90 this.servletContext = request.getSession().getServletContext(); 91 92 admin = ServletCacheAdministrator.getInstance(servletContext); 94 if ( admin == null ) { 95 String error = "Cannot get cache administrator"; 96 if ( log.isErrorEnabled() ) { 97 log.error(error); 98 } 99 throw new RuntimeException (error); 100 } 101 102 Integer rp = GlobalProperties.getInstance(servletContext).getInteger(TIME_KEY); 104 if ( rp != null ) { 105 refreshPeriod = rp.intValue(); 106 } else { 107 if ( log.isWarnEnabled() ) { 108 log.warn("Cache refresh period is not specified in key '" + TIME_KEY + "' using default " + String.valueOf(refreshPeriod)); 109 } 110 } 111 112 Integer ms = GlobalProperties.getInstance(servletContext).getInteger(RESOURCE_MAXSIZE_KEY); 114 if ( ms != null ) { 115 resourceMaxSize = ms.intValue(); 116 } else { 117 if ( log.isWarnEnabled() ) { 118 log.warn("Maxsize of resource in cache is not specified in key '" + RESOURCE_MAXSIZE_KEY + "' using default " + String.valueOf(resourceMaxSize)); 119 } 120 } 121 122 } 123 124 126 134 public CFVData getPageFieldValueFromCache(String pageUri, String identifier, String locale) { 135 return (CFVData) getFromCache(pageUri + "_" + identifier, locale, PAGE_SUFFIX, PageContext.APPLICATION_SCOPE); 136 } 137 138 146 public void putPageFieldValueInCache(CFVData data, String pageUri, String identifier, String locale) { 147 putInCache(data, pageUri + "_" + identifier, locale, PAGE_SUFFIX, new String []{pageUri, FIELD_VALUE_GROUP}, PageContext.APPLICATION_SCOPE); 148 } 149 150 157 public void flushPageFieldValueCache(String pageUri, String identifier, String locale) { 158 flushCache(pageUri + "_" + identifier, locale, PAGE_SUFFIX, PageContext.APPLICATION_SCOPE); 159 } 160 161 166 public void flushPageFieldValueCache(String pageUri) { 167 flushCacheGroup(pageUri, PageContext.APPLICATION_SCOPE); 168 } 169 170 171 173 185 public void putLayoutFieldValueInCache(CFVData data, String layoutDefinition, String pageUri, String identifier, String locale) { 186 putInCache(data, pageUri + "_" + identifier, locale, PAGE_SUFFIX, new String []{pageUri, layoutDefinition, FIELD_VALUE_GROUP}, PageContext.APPLICATION_SCOPE); 187 } 188 189 194 public void flushLayoutFieldValueCache(String layoutDefinition) { 195 flushCacheGroup(layoutDefinition, PageContext.APPLICATION_SCOPE); 196 } 197 198 200 208 public CFVData getLocalizableFieldValueFromCache(Long localizableId, String identifier, String locale) { 209 return (CFVData) getFromCache(localizableId + "_" + identifier, locale, LOCALIZABLE_SUFFIX, PageContext.APPLICATION_SCOPE); 210 } 211 212 220 public void putLocalizableFieldValueInCache(CFVData data, Long localizableId, String identifier, String locale) { 221 putInCache(data, localizableId + "_" + identifier, locale, LOCALIZABLE_SUFFIX, new String []{localizableId.toString(), FIELD_VALUE_GROUP}, PageContext.APPLICATION_SCOPE); 222 } 223 224 231 public void flushLocalizableFieldValueCache(Long localizableId, String identifier, String locale) { 232 flushCache(localizableId + "_" + identifier, locale, LOCALIZABLE_SUFFIX, PageContext.APPLICATION_SCOPE); 233 } 234 235 240 public void flushLocalizableFieldValueCache(Long localizableId) { 241 flushCacheGroup(localizableId.toString(), PageContext.APPLICATION_SCOPE); 242 } 243 244 246 252 public ResourceData getResourceFromCache(String uri) { 253 return (ResourceData) getFromCache(uri, null, RESOURCE_SUFFIX, PageContext.APPLICATION_SCOPE); 254 } 255 256 262 public void putResourceInCache(ResourceData resourceData, String uri) { 263 if ( resourceData.getData().length <= resourceMaxSize ) { 264 putInCache(resourceData, uri, null, RESOURCE_SUFFIX, new String []{RESOURCE_GROUP}, PageContext.APPLICATION_SCOPE); 265 } else { 266 resourceData.setData(null); 268 putInCache(resourceData, uri, null, RESOURCE_SUFFIX, new String []{RESOURCE_GROUP}, PageContext.APPLICATION_SCOPE); 269 } 270 } 271 272 277 public void updateResourceRolesInCache(String uri, String roles) { 278 ResourceData resourceData = getResourceFromCache(uri); 279 if (resourceData != null) { 280 resourceData.setRoles(roles); 281 putResourceInCache(resourceData, uri); 282 } 283 } 284 285 290 public void flushResourceCache(String uri) { 291 flushCache(uri, null, RESOURCE_SUFFIX, PageContext.APPLICATION_SCOPE); 292 } 293 294 297 public void flushResourceCache() { 298 flushCacheGroup(RESOURCE_GROUP, PageContext.APPLICATION_SCOPE); 299 } 300 301 303 313 public MenuComponent getMenuFromCache(String menuName, String locale, String layoutDefinition, String pageUri) { 314 return (MenuComponent) getFromCache(menuName + "_" + layoutDefinition + "_" + pageUri, locale, MENU_SUFFIX, PageContext.APPLICATION_SCOPE); 315 } 316 317 328 public void putMenuInCache(MenuComponent menuComponent, String menuName, String locale, String layoutDefinition, String pageUri) { 329 putInCache(menuComponent, menuName + "_" + layoutDefinition + "_" + pageUri, locale, MENU_SUFFIX, new String []{MENU_GROUP}, PageContext.APPLICATION_SCOPE); 330 } 331 332 335 public void flushMenuCache() { 336 flushCacheGroup(MENU_GROUP, PageContext.APPLICATION_SCOPE); 337 } 338 339 340 342 351 public String [] getFieldIndices(String fieldIdentifier, String locale, String layoutDefinition, String pageUri) { 352 return (String []) getFromCache(fieldIdentifier + "_" + layoutDefinition + "_" + pageUri, locale, FIELD_INDICES_SUFFIX, PageContext.APPLICATION_SCOPE); 353 } 354 355 364 public void putFieldIndicesInCache(String [] fieldIndices, String fieldIdentifier, String locale, String layoutDefinition, String pageUri) { 365 putInCache(fieldIndices, fieldIdentifier + "_" + layoutDefinition + "_" + pageUri, locale, FIELD_INDICES_SUFFIX, new String []{FIELD_INDEXES_GROUP}, PageContext.APPLICATION_SCOPE); 366 } 367 368 371 public void flushFieldIndices() { 372 flushCacheGroup(FIELD_INDEXES_GROUP, PageContext.APPLICATION_SCOPE); 373 } 374 375 377 383 public ContentPageData getContentPageFromCache(String uri) { 384 return (ContentPageData) getFromCache(uri, null, CONTENT_PAGE_SUFFIX, PageContext.APPLICATION_SCOPE); 385 } 386 387 393 public void putContentPageInCache(ContentPageData contentPageData, String uri) { 394 putInCache(contentPageData, uri, null, CONTENT_PAGE_SUFFIX, new String []{CONTENT_PAGE_GROUP}, PageContext.APPLICATION_SCOPE); 395 } 396 397 402 public void updateContentPageLastModifiedInCache(String uri) { 403 ContentPageData cpd = getContentPageFromCache(uri); 404 if ( cpd != null ) { 405 cpd.setLastModified(new Date ().getTime()); 406 putContentPageInCache(cpd, uri); 407 } 408 } 409 410 415 public void updateContentPageRolesInCache(String uri, String roles) { 416 ContentPageData cpd = getContentPageFromCache(uri); 417 if ( cpd != null ) { 418 cpd.setRoles(roles); 419 putContentPageInCache(cpd, uri); 420 } 421 } 422 423 428 public void flushContentPageCache(String uri) { 429 flushCache(uri, null, CONTENT_PAGE_SUFFIX, PageContext.APPLICATION_SCOPE); 430 } 431 432 435 public void flushContentPageCache() { 436 flushCacheGroup(CONTENT_PAGE_GROUP, PageContext.APPLICATION_SCOPE); 437 } 438 439 440 442 452 public Object getFromCache(String compositeKey, String locale, String suffix, int scope) { 453 if ( log.isDebugEnabled() ) { 454 log.debug("Get from cache compositeKey: " + compositeKey + ", locale: " + locale + ", suffix: " + suffix); 455 } 456 457 String fullKey = admin.generateEntryKey(compositeKey, request, scope, locale, suffix); 458 459 if ( log.isDebugEnabled() ) { 460 log.debug("Get from cache, full cache key: " + fullKey); 461 } 462 463 Cache cache = admin.getCache(request, scope); 464 Object content = null; 465 try { 466 content = cache.getFromCache(fullKey, refreshPeriod); 467 if ( log.isDebugEnabled() ) { 468 log.debug("Using cached entry for key :" + fullKey); 469 } 470 } catch ( NeedsRefreshException nre ) { 471 if ( log.isDebugEnabled() ) { 472 log.debug("Cache stale or cache scope flushed for key :" + fullKey); 473 } 474 cache.cancelUpdate(fullKey); 475 return null; 476 } 477 return content; 478 } 479 480 493 public void putInCache(Object content, String key, String locale, String suffix, String [] groups, int scope) { 494 if ( log.isDebugEnabled() ) { 495 log.debug("Put into cache compositeKey: " + key + ", locale: " + locale + ", suffix: " + suffix); 496 } 497 Cache cache = admin.getCache(request, scope); 498 String fullKey = admin.generateEntryKey(key, request, scope, locale, suffix); 499 if ( log.isDebugEnabled() ) { 500 log.debug("Put into cache, full cache key: " + fullKey); 501 } 502 cache.putInCache(fullKey, content, groups); 503 } 504 505 515 public void flushCache(String key, String locale, String suffix, int scope) { 516 Cache cache = admin.getCache(request, scope); 517 key = admin.generateEntryKey(key, request, scope, locale, suffix); 518 cache.flushEntry(key); 519 } 520 521 527 public void flushCacheGroup(String group, int scope) { 528 Cache cache = admin.getCache(request, scope); 529 cache.flushGroup(group); 530 } 531 532 537 public void flushCacheScope(int scope) { 538 admin.setFlushTime(scope); 539 } 540 541 544 public void flushAllCache() { 545 admin.flushAll(); 546 if (log.isInfoEnabled()) { 547 log.info("All cache of web layer flushed"); 548 } 549 } 550 551 554 public static class ResourceData implements Serializable { 555 private byte[] data; 556 private String mimeType = null; 557 private String charset = null; 558 private String roles = ""; 559 private long lastModified = -1; 560 561 570 public ResourceData(byte[] array, String mimeType, String roles, long lastModified) { 571 this(array, mimeType, null, roles, lastModified); 572 } 573 574 584 public ResourceData(byte[] array, String mimeType, String charset, String roles, long lastModified) { 585 setData(array); 586 setMimeType(mimeType); 587 setRoles(roles); 588 setCharset(charset); 589 setLastModified(lastModified); 590 } 591 592 597 public byte[] getData() { 598 return data; 599 } 600 601 606 public void setData(byte[] data) { 607 this.data = data; 608 } 609 610 615 public String getMimeType() { 616 return mimeType; 617 } 618 619 624 public void setMimeType(String mimeType) { 625 this.mimeType = mimeType; 626 } 627 628 633 public String getCharset() { 634 return charset; 635 } 636 637 642 public void setCharset(String charset) { 643 this.charset = charset; 644 } 645 646 651 public String getRoles() { 652 return roles; 653 } 654 655 660 public void setRoles(String roles) { 661 this.roles = roles; 662 } 663 664 669 public long getLastModified() { 670 return lastModified; 671 } 672 673 678 public void setLastModified(long lastModified) { 679 this.lastModified = lastModified; 680 } 681 } 682 683 686 public static class ContentPageData implements Serializable { 687 private String cpDefinition = null; 688 private String roles = ""; 689 private long lastModified = -1; 690 private Long cacheMaxAge = new Long (-1); 691 692 700 public ContentPageData(String cpDefinition, String roles, Long cacheMaxAge) { 701 setCpDefinition(cpDefinition); 702 setRoles(roles); 703 setCacheMaxAge(cacheMaxAge); 704 setLastModified(new Date ().getTime()); 705 } 706 707 712 public String getCpDefinition() { 713 return cpDefinition; 714 } 715 716 721 public void setCpDefinition(String cpDefinition) { 722 this.cpDefinition = cpDefinition; 723 } 724 725 730 public String getRoles() { 731 return roles; 732 } 733 734 739 public void setRoles(String roles) { 740 this.roles = roles; 741 } 742 743 748 public long getLastModified() { 749 return lastModified; 750 } 751 752 757 public void setLastModified(long lastModified) { 758 this.lastModified = lastModified; 759 } 760 761 766 public Long getCacheMaxAge() { 767 return cacheMaxAge; 768 } 769 770 775 public void setCacheMaxAge(Long cacheMaxAge) { 776 this.cacheMaxAge = cacheMaxAge; 777 } 778 } 779 780 783 public static class CFVData implements Serializable { 784 private String data = null; 785 private Long contentFieldId = null; 786 private Long contentFieldValueId = null; 787 private byte contentFieldType = 0; 788 789 797 public CFVData(String data, Long contentFieldId, byte contentFieldType, Long contentFieldValueId) { 798 setData(data); 799 setContentFieldType(contentFieldType); 800 setContentFieldId(contentFieldId); 801 setContentFieldValueId(contentFieldValueId); 802 } 803 804 809 public String getData() { 810 return data; 811 } 812 813 818 public void setData(String data) { 819 this.data = data; 820 } 821 822 827 public Long getContentFieldId() { 828 return contentFieldId; 829 } 830 831 836 public void setContentFieldId(Long contentFieldId) { 837 this.contentFieldId = contentFieldId; 838 } 839 840 845 public Long getContentFieldValueId() { 846 return contentFieldValueId; 847 } 848 849 854 public void setContentFieldValueId(Long contentFieldValueId) { 855 this.contentFieldValueId = contentFieldValueId; 856 } 857 858 863 public byte getContentFieldType() { 864 return contentFieldType; 865 } 866 867 872 public void setContentFieldType(byte contentFieldType) { 873 this.contentFieldType = contentFieldType; 874 } 875 876 } 877 878 879 } 880 | Popular Tags |