001/* 002 * Copyright 2010 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.web.cache; 017 018import javax.jcr.Node; 019import javax.jcr.Session; 020 021import org.ametys.core.observation.Event; 022import org.ametys.core.observation.Observer; 023import org.ametys.plugins.repository.jcr.JCRAmetysObject; 024import org.ametys.web.ObservationConstants; 025import org.ametys.web.repository.page.Page; 026import org.ametys.web.repository.page.SitemapElement; 027import org.ametys.web.repository.page.ZoneItem; 028import org.ametys.web.repository.site.Site; 029 030/** 031 * {@link Observer} for observing page creation or modification in order to 032 * invalidate cache on front-office. 033 */ 034public class InvalidateCacheOnPageModificationObserver extends AbstractSiteCacheObserver 035{ 036 @Override 037 public boolean supports(Event event) 038 { 039 return event.getId().equals(ObservationConstants.EVENT_PAGE_ADDED) 040 || event.getId().equals(ObservationConstants.EVENT_PAGE_CHANGED) 041 || event.getId().equals(ObservationConstants.EVENT_PAGE_UPDATED) 042 || event.getId().equals(ObservationConstants.EVENT_PAGE_RENAMED) 043 || event.getId().equals(ObservationConstants.EVENT_VIEW_PARAMETERS_MODIFIED); 044 } 045 046 @Override 047 protected Site _getSite(Event event) 048 { 049 Page page = _getPage(event); 050 return page != null ? page.getSite() : null; 051 } 052 053 private Page _getPage(Event event) 054 { 055 Page page = (Page) event.getArguments().get(ObservationConstants.ARGS_PAGE); 056 SitemapElement sitemapElement = (SitemapElement) event.getArguments().get(ObservationConstants.ARGS_SITEMAP_ELEMENT); 057 ZoneItem zoneItem = (ZoneItem) event.getArguments().get(ObservationConstants.ARGS_ZONE_ITEM); 058 059 if (page != null) 060 { 061 return page; 062 } 063 else if (sitemapElement != null && sitemapElement instanceof Page sitemapPage) 064 { 065 return sitemapPage; 066 } 067 else if (zoneItem != null && zoneItem.getZone().getSitemapElement() instanceof Page zoneItemPage) 068 { 069 return zoneItemPage; 070 } 071 else 072 { 073 return null; 074 } 075 } 076 077 @Override 078 protected void _internalObserve(Event event, Site site, Session liveSession) throws Exception 079 { 080 Page page = _getPage (event); 081 082 if (page instanceof JCRAmetysObject) 083 { 084 JCRAmetysObject jcrPage = (JCRAmetysObject) page; 085 Node pageNode = jcrPage.getNode(); 086 087 if (liveSession.itemExists(pageNode.getPath()) || event.getId().equals(ObservationConstants.EVENT_PAGE_CHANGED)) 088 { 089 if (getLogger().isInfoEnabled()) 090 { 091 getLogger().info("Page modification: " + event + ", invalidating cache"); 092 } 093 094 _cachePolicy.invalidateCacheOnPageModification(page); 095 } 096 } 097 } 098}