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.service.ServiceManager;
025import org.apache.avalon.framework.thread.ThreadSafe;
026import org.apache.cocoon.acting.ServiceableAction;
027import org.apache.cocoon.environment.Redirector;
028import org.apache.commons.io.FileUtils;
029import org.slf4j.Logger;
030import org.slf4j.LoggerFactory;
031
032/**
033 * Invalidates cached data for a given site.
034 */
035public class InvalidateSharedResourcesAction extends ServiceableAction implements ThreadSafe
036{
037    private Logger _logger = LoggerFactory.getLogger("site.cache.log");
038    
039    private CacheAccessManager _cacheAccess;
040    
041    @Override
042    public void service(ServiceManager sManager) throws ServiceException
043    {
044        super.service(sManager);
045        _cacheAccess = (CacheAccessManager) sManager.lookup(CacheAccessManager.ROLE);
046    }
047    
048    @Override
049    public Map act(Redirector redirector, org.apache.cocoon.environment.SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
050    {
051        String path = parameters.getParameter("path");
052        
053        File root = SiteCacheHelper.getRootCache();
054        
055        for (File site : root.listFiles(new SiteFilter()))
056        {
057            String fullPath = site.getName() + "/" + path;
058            
059            File pageFile = new File(root, fullPath);
060
061            if (pageFile.exists())
062            {
063                FileUtils.forceDelete(pageFile);
064            }
065            
066            _logger.info("Invalidate cache for path '" + fullPath + "'");
067            
068            _cacheAccess.resetPage(fullPath);
069        }
070        
071        return EMPTY_MAP;
072    }
073    
074    class SiteFilter implements FilenameFilter
075    {
076        @Override
077        public boolean accept(File dir, String name)
078        {
079            return !name.equals("plugins") && !name.equals("skins");
080        }
081    }
082}