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.Collection;
019import java.util.Map;
020
021import org.apache.avalon.framework.logger.AbstractLogEnabled;
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.ObservationConstants;
027import org.ametys.core.observation.Event;
028import org.ametys.core.observation.Observer;
029import org.ametys.core.right.RightManager;
030import org.ametys.web.repository.page.Page;
031import org.ametys.web.repository.sitemap.Sitemap;
032
033/**
034 * Clear zone item cache on contents when a page's access is updated.
035 */
036public class InvalidatePageElementCacheOnPageACLUpdateListener extends AbstractLogEnabled implements Observer, Serviceable
037{
038    private PageElementCache _zoneItemCache;
039
040    @Override
041    public void service(ServiceManager manager) throws ServiceException
042    {
043        _zoneItemCache = (PageElementCache) manager.lookup(PageElementCache.ROLE + "/zoneItem");
044    }
045
046    @Override
047    public boolean supports(Event event)
048    {
049        return event.getId().equals(ObservationConstants.EVENT_ACL_UPDATED);
050    }
051
052    @Override
053    public int getPriority(Event event)
054    {
055        // processed just before front-office cache invalidation
056        return MAX_PRIORITY + 3500;
057    }
058
059    @Override
060    public void observe(Event event, Map<String, Object> transientVars) throws Exception
061    {
062        Map<String, Object> arguments = event.getArguments();
063        Object aclContext = arguments.get(ObservationConstants.ARGS_ACL_CONTEXT);
064        
065        @SuppressWarnings("unchecked")
066        Collection<String> profileIds = (Collection<String>) arguments.get(org.ametys.core.ObservationConstants.ARGS_ACL_PROFILES);
067        
068        String siteName = null;
069        if (aclContext instanceof Page)
070        {
071            siteName = ((Page) aclContext).getSiteName();
072        }
073        else if (aclContext instanceof Sitemap)
074        {
075            siteName = ((Sitemap) aclContext).getSiteName();
076        }
077        
078        if (siteName != null && profileIds.contains(RightManager.READER_PROFILE_ID))
079        {
080            _zoneItemCache.clear("default", siteName, "CONTENT");
081            _zoneItemCache.clear("live", siteName, "CONTENT");
082        }
083    }
084}