001/* 002 * Copyright 2017 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.plugins.externaldata.cache; 017 018import javax.jcr.RepositoryException; 019import javax.jcr.Session; 020 021import org.apache.avalon.framework.service.ServiceException; 022import org.apache.avalon.framework.service.ServiceManager; 023 024import org.ametys.core.observation.Event; 025import org.ametys.plugins.repository.AmetysObjectIterable; 026import org.ametys.web.cache.FOCommHelper; 027import org.ametys.web.cache.pageelement.PageElementCache; 028import org.ametys.web.repository.site.Site; 029import org.ametys.web.repository.site.SiteManager; 030import org.ametys.web.synchronization.AbstractSynchronizeObserver; 031 032/** 033 * Invalidate the site cache and page element cache when 034 */ 035public class InvalidateCacheOnQueryChangesObserver extends AbstractSynchronizeObserver 036{ 037 private PageElementCache _inputDataCache; 038 private PageElementCache _zoneItemCache; 039 private SiteManager _siteManager; 040 private FOCommHelper _foCommHelper; 041 042 @Override 043 public void service(ServiceManager manager) throws ServiceException 044 { 045 super.service(manager); 046 047 _inputDataCache = (PageElementCache) manager.lookup(PageElementCache.ROLE + "/inputData"); 048 _zoneItemCache = (PageElementCache) manager.lookup(PageElementCache.ROLE + "/zoneItem"); 049 _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE); 050 _foCommHelper = (FOCommHelper) manager.lookup(FOCommHelper.ROLE); 051 } 052 053 public boolean supports(Event event) 054 { 055 return event.getId().equals(ObservationConstants.EVENT_QUERY_UPDATED) 056 || event.getId().equals(ObservationConstants.EVENT_QUERY_DELETED); 057 } 058 059 @Override 060 protected void _internalObserve(Event event, Session liveSession) throws RepositoryException 061 { 062 if (getLogger().isInfoEnabled()) 063 { 064 getLogger().info("Database changes: " + event + ", invalidating cache"); 065 } 066 067 // clear the whole FO cache 068 AmetysObjectIterable<Site> sites = _siteManager.getSites(); 069 for (Site site : sites) 070 { 071 try 072 { 073 _foCommHelper.invalidateFOCache(site); 074 } 075 catch (Exception e) 076 { 077 getLogger().error("Unable to clear cache of site " + site.getName(), e); 078 } 079 } 080 081 // clear the page element cache 082 _inputDataCache.clear(); 083 _zoneItemCache.clear(); 084 } 085 086}