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