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 java.util.Map; 019 020import javax.jcr.Node; 021import javax.jcr.Session; 022 023import org.ametys.core.observation.Event; 024import org.ametys.core.observation.Observer; 025import org.ametys.plugins.repository.jcr.JCRAmetysObject; 026import org.ametys.web.ObservationConstants; 027import org.ametys.web.repository.page.Page; 028import org.ametys.web.repository.page.ZoneItem; 029import org.ametys.web.repository.site.Site; 030 031/** 032 * {@link Observer} for observing page creation or modification in order to 033 * invalidate cache on front-office. 034 */ 035public class InvalidateCacheOnPageModificationObserver extends AbstractSiteCacheObserver 036{ 037 @Override 038 public boolean supports(Event event) 039 { 040 return event.getId().equals(ObservationConstants.EVENT_PAGE_ADDED) 041 || event.getId().equals(ObservationConstants.EVENT_PAGE_CHANGED) 042 || event.getId().equals(ObservationConstants.EVENT_PAGE_UPDATED) 043 || event.getId().equals(ObservationConstants.EVENT_PAGE_RENAMED) 044 || event.getId().equals(ObservationConstants.EVENT_VIEW_PARAMETERS_MODIFIED); 045 } 046 047 @Override 048 protected Site _getSite(Event event) 049 { 050 Page page = _getPage(event); 051 return page != null ? page.getSite() : null; 052 } 053 054 private Page _getPage (Event event) 055 { 056 Map<String, Object> args = event.getArguments(); 057 if (args.containsKey(ObservationConstants.ARGS_ZONE_ITEM)) 058 { 059 ZoneItem zoneItem = (ZoneItem) args.get(ObservationConstants.ARGS_ZONE_ITEM); 060 return zoneItem.getZone().getPage(); 061 } 062 else 063 { 064 return (Page) args.get(ObservationConstants.ARGS_PAGE); 065 } 066 } 067 068 @Override 069 protected void _internalObserve(Event event, Site site, Session liveSession) throws Exception 070 { 071 Page page = _getPage (event); 072 073 if (page instanceof JCRAmetysObject) 074 { 075 JCRAmetysObject jcrPage = (JCRAmetysObject) page; 076 Node pageNode = jcrPage.getNode(); 077 078 if (liveSession.itemExists(pageNode.getPath()) || event.getId().equals(ObservationConstants.EVENT_PAGE_CHANGED)) 079 { 080 if (getLogger().isInfoEnabled()) 081 { 082 getLogger().info("Page modification: " + event + ", invalidating cache"); 083 } 084 085 _cachePolicy.invalidateCacheOnPageModification(page); 086 } 087 } 088 } 089}