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 } 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 Map<String, Object> args = event.getArguments(); 056 if (args.containsKey(ObservationConstants.ARGS_ZONE_ITEM)) 057 { 058 ZoneItem zoneItem = (ZoneItem) args.get(ObservationConstants.ARGS_ZONE_ITEM); 059 return zoneItem.getZone().getPage(); 060 } 061 else 062 { 063 return (Page) args.get(ObservationConstants.ARGS_PAGE); 064 } 065 } 066 067 @Override 068 protected void _internalObserve(Event event, Site site, Session liveSession) throws Exception 069 { 070 Page page = _getPage (event); 071 072 if (page instanceof JCRAmetysObject) 073 { 074 JCRAmetysObject jcrPage = (JCRAmetysObject) page; 075 Node pageNode = jcrPage.getNode(); 076 077 if (liveSession.itemExists(pageNode.getPath()) || event.getId().equals(ObservationConstants.EVENT_PAGE_CHANGED)) 078 { 079 if (getLogger().isInfoEnabled()) 080 { 081 getLogger().info("Page modification: " + event + ", invalidating cache"); 082 } 083 084 _cachePolicy.invalidateCacheOnPageModification(page); 085 } 086 } 087 } 088}