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 content changes (which do not impact other pages) in order to invalidate cache on front-office. 033 */ 034public class InvalidateCacheOnPageMinorChangeObserver extends AbstractSiteCacheObserver 035{ 036 037 @Override 038 public boolean supports(Event event) 039 { 040 return event.getId().equals(ObservationConstants.EVENT_ZONEITEM_MOVED) 041 || event.getId().equals(ObservationConstants.EVENT_SERVICE_MODIFIED); 042 } 043 044 @Override 045 protected Site _getSite(Event event) 046 { 047 return _getPage(event).getSite(); 048 } 049 050 private Page _getPage (Event event) 051 { 052 Map<String, Object> args = event.getArguments(); 053 if (args.containsKey(ObservationConstants.ARGS_ZONE_ITEM)) 054 { 055 ZoneItem zoneItem = (ZoneItem) args.get(ObservationConstants.ARGS_ZONE_ITEM); 056 return zoneItem.getZone().getPage(); 057 } 058 else 059 { 060 return (Page) args.get(ObservationConstants.ARGS_PAGE); 061 } 062 } 063 064 @Override 065 protected void _internalObserve(Event event, Site site, Session liveSession) throws Exception 066 { 067 Page page = _getPage (event); 068 069 if (page instanceof JCRAmetysObject) 070 { 071 Node pageNode = ((JCRAmetysObject) page).getNode(); 072 if (liveSession.itemExists(pageNode.getPath())) 073 { 074 // If the page is in the live workspace, invalidate the cache. 075 _cachePolicy.invalidateCacheOnPageMinorChange(page); 076 } 077 } 078 } 079 080}