001/* 002 * Copyright 2019 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.inputdata; 017 018import java.util.Arrays; 019import java.util.List; 020import java.util.Map; 021 022import org.apache.avalon.framework.service.ServiceException; 023import org.apache.avalon.framework.service.ServiceManager; 024import org.apache.avalon.framework.service.Serviceable; 025 026import org.ametys.core.observation.Event; 027import org.ametys.core.observation.Observer; 028import org.ametys.runtime.plugin.component.AbstractLogEnabled; 029import org.ametys.web.ObservationConstants; 030import org.ametys.web.WebHelper; 031import org.ametys.web.repository.site.Site; 032import org.ametys.web.repository.site.SiteManager; 033 034/** 035 * Invalidates the {@link SitemapSaxer}'s cache used by the {@link SitemapInputData}. 036 */ 037public class InvalidateSitemapSaxerCacheObserver extends AbstractLogEnabled implements Observer, Serviceable 038{ 039 private SiteManager _siteManager; 040 private SitemapSaxer _sitemapSaxer; 041 private List<String> _defaultWorkspaceEvents = Arrays.asList(ObservationConstants.EVENT_PAGE_ADDED, 042 ObservationConstants.EVENT_PAGE_CHANGED, 043 ObservationConstants.EVENT_PAGE_DELETED, 044 ObservationConstants.EVENT_PAGE_MOVED, 045 ObservationConstants.EVENT_PAGE_RENAMED, 046 ObservationConstants.EVENT_PAGE_UPDATED, 047 org.ametys.core.ObservationConstants.EVENT_ACL_UPDATED); 048 private List<String> _liveWorkspaceEvents = Arrays.asList(org.ametys.cms.ObservationConstants.EVENT_CONTENT_ADDED, 049 org.ametys.cms.ObservationConstants.EVENT_CONTENT_VALIDATED, 050 org.ametys.cms.ObservationConstants.EVENT_CONTENT_DELETED, 051 org.ametys.cms.ObservationConstants.EVENT_CONTENT_UNTAG_LIVE, 052 ObservationConstants.EVENT_ZONEITEM_ADDED, 053 ObservationConstants.EVENT_ZONEITEM_DELETED, 054 ObservationConstants.EVENT_ZONEITEM_MODIFIED); 055 056 public void service(ServiceManager manager) throws ServiceException 057 { 058 _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE); 059 _sitemapSaxer = (SitemapSaxer) manager.lookup(SitemapSaxer.ROLE); 060 } 061 062 public int getPriority() 063 { 064 // Will be processed after Solr update 065 return Observer.MAX_PRIORITY + 4000; 066 } 067 068 public boolean supports(Event event) 069 { 070 return _defaultWorkspaceEvents.contains(event.getId()) 071 || _liveWorkspaceEvents.contains(event.getId()); 072 } 073 074 public void observe(Event event, Map<String, Object> transientVars) throws Exception 075 { 076 boolean defaultWorkspace = _defaultWorkspaceEvents.contains(event.getId()); 077 boolean liveWorkspace = defaultWorkspace || _liveWorkspaceEvents.contains(event.getId()); 078 079 Site site = WebHelper.findSite(event); 080 081 if (site != null) 082 { 083 _clearCache(site, defaultWorkspace, liveWorkspace); 084 } 085 else 086 { 087 for (Site s : _siteManager.getSites()) 088 { 089 _clearCache(s, defaultWorkspace, liveWorkspace); 090 } 091 } 092 } 093 094 private void _clearCache(Site site, boolean defaultWorkspace, boolean liveWorkspace) 095 { 096 if (defaultWorkspace) 097 { 098 _clearCache("default", site); 099 } 100 101 if (liveWorkspace) 102 { 103 _clearCache("live", site); 104 } 105 } 106 107 private void _clearCache(String workspace, Site site) 108 { 109 getLogger().info("Clearing SitemapSaxer cache for site {} and workspace {}.", site.getName(), workspace); 110 _sitemapSaxer.clearCache(site.getName(), workspace); 111 } 112}