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.site;
017
018import java.io.File;
019import java.io.FilenameFilter;
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 InvalidateSharedResourcesAction extends ServiceableAction implements ThreadSafe
035{
036    private Logger _logger = LoggerFactory.getLogger("site.cache.log");
037    
038    private CacheAccessManager _cacheAccess;
039    
040    /**
041     * Get the cache access manager
042     * @return the CacheAccessManager
043     */
044    protected CacheAccessManager _getCacheAccessManager()
045    {
046        if (_cacheAccess == null)
047        {
048            try
049            {
050                _cacheAccess = (CacheAccessManager) manager.lookup(CacheAccessManager.ROLE);
051            }
052            catch (ServiceException e)
053            {
054                throw new IllegalStateException("Cannot get CacheAccessManager", e);
055            }
056        }
057        return _cacheAccess;
058    }
059    
060    @Override
061    public Map act(Redirector redirector, org.apache.cocoon.environment.SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
062    {
063        String path = parameters.getParameter("path");
064        
065        File root = SiteCacheHelper.getRootCache();
066        
067        for (File site : root.listFiles(new SiteFilter()))
068        {
069            String fullPath = site.getName() + "/" + path;
070            
071            File pageFile = new File(root, fullPath);
072
073            if (pageFile.exists())
074            {
075                FileUtils.forceDelete(pageFile);
076            }
077            
078            _logger.info("Invalidate cache for path '" + fullPath + "'");
079            
080            _getCacheAccessManager().resetPage(fullPath);
081        }
082        
083        return EMPTY_MAP;
084    }
085    
086    class SiteFilter implements FilenameFilter
087    {
088        @Override
089        public boolean accept(File dir, String name)
090        {
091            return !name.equals("plugins") && !name.equals("skins");
092        }
093    }
094}