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.site;
017
018import java.io.File;
019import java.util.Map;
020
021import org.apache.avalon.framework.parameters.Parameters;
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.thread.ThreadSafe;
024import org.apache.cocoon.acting.ServiceableAction;
025import org.apache.cocoon.environment.Redirector;
026import org.apache.commons.io.FileUtils;
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029
030/**
031 * Invalidates cached data for a given site.
032 */
033public class InvalidatePageAction extends ServiceableAction implements ThreadSafe
034{
035    private Logger _logger = LoggerFactory.getLogger("site.cache.log");
036    
037    private CacheAccessManager _cacheAccess;
038    
039    /**
040     * Get the cache access manager
041     * @return the CacheAccessManager
042     */
043    protected CacheAccessManager _getCacheAccessManager()
044    {
045        if (_cacheAccess == null)
046        {
047            try
048            {
049                _cacheAccess = (CacheAccessManager) manager.lookup(CacheAccessManager.ROLE);
050            }
051            catch (ServiceException e)
052            {
053                throw new IllegalStateException("Cannot get CacheAccessManager", e);
054            }
055        }
056        return _cacheAccess;
057    }
058    
059    @Override
060    public Map act(Redirector redirector, org.apache.cocoon.environment.SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
061    {
062        String site = parameters.getParameter("site");
063        String page = parameters.getParameter("page");
064        
065        File root = SiteCacheHelper.getRootCache();
066        
067        String path = site + "/" + page;
068        
069        File pageFile = new File(root, path);
070
071        if (pageFile.exists())
072        {
073            FileUtils.forceDelete(pageFile);
074        }
075        
076        _logger.info("Invalidate cache for page '" + path + "'");
077        
078        _getCacheAccessManager().resetPage(path);
079        
080        return EMPTY_MAP;
081    }
082}