001/* 002 * Copyright 2012 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.zoneitem; 017 018import java.util.Map; 019 020import javax.jcr.Node; 021import javax.jcr.PropertyIterator; 022import javax.jcr.RepositoryException; 023 024import org.apache.avalon.framework.service.ServiceException; 025import org.apache.avalon.framework.service.ServiceManager; 026 027import org.ametys.cms.ObservationConstants; 028import org.ametys.cms.repository.Content; 029import org.ametys.core.observation.Event; 030import org.ametys.plugins.repository.AmetysObject; 031import org.ametys.plugins.repository.AmetysObjectResolver; 032import org.ametys.plugins.repository.jcr.JCRAmetysObject; 033import org.ametys.web.repository.content.WebContent; 034import org.ametys.web.repository.page.ZoneItem; 035 036/** 037 * Invalidates the ZoneItem containing a Content which has just been modified. 038 */ 039public class InvalidateZoneItemCacheOnContentModificationObserver extends AbstractZoneItemCacheObserver 040{ 041 private AmetysObjectResolver _resolver; 042 043 @Override 044 public void service(ServiceManager manager) throws ServiceException 045 { 046 super.service(manager); 047 _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE); 048 } 049 050 @Override 051 public boolean supports(Event event) 052 { 053 return event.getId().equals(ObservationConstants.EVENT_CONTENT_MODIFIED); 054 } 055 056 @Override 057 public void observe(Event event, Map<String, Object> transientVars) throws Exception 058 { 059 Content content = (Content) event.getArguments().get(ObservationConstants.ARGS_CONTENT); 060 061 if (content instanceof JCRAmetysObject && content instanceof WebContent) 062 { 063 try 064 { 065 String site = ((WebContent) content).getSiteName(); 066 067 PropertyIterator it = ((JCRAmetysObject) content).getNode().getReferences(); 068 while (it.hasNext()) 069 { 070 Node refererNode = it.nextProperty().getParent(); 071 072 AmetysObject ao = _resolver.resolve(refererNode, true); 073 074 if (ao != null && ao instanceof ZoneItem) 075 { 076 _zoneItemCache.removeItem("default", site, "CONTENT", ao.getId()); 077 } 078 } 079 } 080 catch (RepositoryException e) 081 { 082 getLogger().error("Unable to get referencing ZoneItems for content '" + content.getId() + "' while processing event " + event, e); 083 } 084 } 085 086 } 087 088}