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.pageelement;
017
018import java.util.Map;
019
020import org.apache.avalon.framework.logger.AbstractLogEnabled;
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.avalon.framework.service.Serviceable;
024
025import org.ametys.core.observation.Event;
026import org.ametys.core.observation.Observer;
027import org.ametys.web.ObservationConstants;
028import org.ametys.web.repository.site.Site;
029
030/**
031 * Clear all caches when a site's configuration is updated.
032 */
033public class InvalidatePageElementCacheOnSiteChangeObserver extends AbstractLogEnabled implements Observer, Serviceable
034{
035    private PageElementCache _inputDataCache;
036    private PageElementCache _zoneItemCache;
037
038    @Override
039    public void service(ServiceManager manager) throws ServiceException
040    {
041        _zoneItemCache = (PageElementCache) manager.lookup(PageElementCache.ROLE + "/zoneItem");
042        _inputDataCache = (PageElementCache) manager.lookup(PageElementCache.ROLE + "/inputData");
043    }
044
045    @Override
046    public boolean supports(Event event)
047    {
048        return event.getId().equals(ObservationConstants.EVENT_SITE_UPDATED);
049    }
050
051    @Override
052    public int getPriority(Event event)
053    {
054        // processed just before front-office cache invalidation
055        return MAX_PRIORITY + 3500;
056    }
057    
058    @Override
059    public void observe(Event event, Map<String, Object> transientVars) throws Exception
060    {
061        Site site = (Site) event.getArguments().get(ObservationConstants.ARGS_SITE);
062        String siteName = site.getName();
063        
064        _inputDataCache.clear("default", siteName);
065        _inputDataCache.clear("live", siteName);
066        _zoneItemCache.clear("default", siteName);
067        _zoneItemCache.clear("live", siteName);
068        
069    }
070
071}