001/*
002 *  Copyright 2010 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;
017
018import java.util.Map;
019
020import javax.jcr.Session;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024
025import org.ametys.cms.ObservationConstants;
026import org.ametys.core.observation.Event;
027import org.ametys.core.observation.Observer;
028import org.ametys.web.repository.site.Site;
029import org.ametys.web.repository.site.SiteManager;
030
031/**
032 * {@link Observer} for observing content deletion in order to 
033 * invalidate cache on front-office.
034 */
035public class InvalidateCacheOnContentDeletionObserver extends AbstractSiteCacheObserver
036{
037    private SiteManager _siteManager;
038
039    @Override
040    public void service(ServiceManager smanager) throws ServiceException
041    {
042        super.service(smanager);
043        _siteManager = (SiteManager) smanager.lookup(SiteManager.ROLE);
044    }
045    
046    @Override
047    public boolean supports(Event event)
048    {
049        return event.getId().equals(ObservationConstants.EVENT_CONTENT_DELETED);
050    }
051    
052    @Override
053    protected Site _getSite(Event event)
054    {
055        Map<String, Object> args = event.getArguments();
056        
057        String siteName = (String) args.get(org.ametys.web.ObservationConstants.ARGS_SITE_NAME);
058        if (siteName != null)
059        {
060            return _siteManager.getSite(siteName);
061        }
062        else
063        {
064            return null;
065        }
066    }
067
068    @Override
069    protected void _internalObserve(Event event, Site site, Session liveSession) throws Exception
070    {
071        if (getLogger().isInfoEnabled())
072        {
073            getLogger().info("Content deleted: " + event + ", invalidating cache");
074        }
075        
076        String contentId = (String) event.getArguments().get(ObservationConstants.ARGS_CONTENT_ID);
077        _cachePolicy.invalidateCacheOnContentDeletion(site, contentId); 
078    }
079}