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.service.ServiceManager;
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;
028
029/**
030 * Invalidates cached data for a given site.
031 */
032public class InvalidateSiteAction extends ServiceableAction implements ThreadSafe
033{
034    private CacheAccessManager _cacheAccess;
035    private CacheAccessCounter _cacheAccessCounter;
036    
037    @Override
038    public void service(ServiceManager sManager) throws ServiceException
039    {
040        super.service(sManager);
041        _cacheAccess = (CacheAccessManager) sManager.lookup(CacheAccessManager.ROLE);
042        _cacheAccessCounter = (CacheAccessCounter) sManager.lookup(CacheAccessCounter.ROLE);
043    }
044    
045    @Override
046    public Map act(Redirector redirector, org.apache.cocoon.environment.SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
047    {
048        String site = parameters.getParameter("site");
049        
050        File root = SiteCacheHelper.getRootCache();
051        File siteRoot = new File(root, site);
052        
053        if (siteRoot.exists())
054        {
055            FileUtils.forceDelete(siteRoot);
056        }
057        
058        if (getLogger().isInfoEnabled())
059        {
060            getLogger().info("Invalidate cache for site '" + site + "'");
061        }
062        
063        int count = _cacheAccessCounter.getAskedResources(site);
064        
065        if (getLogger().isInfoEnabled())
066        {
067            getLogger().info("Since last cache invalidation of site '" + site + "', " + count + " resource(s) have been generated from back-office.");
068        }
069        
070        _cacheAccessCounter.resetCount(site);
071        
072        // Reset the cache access manager.
073        _cacheAccess.reset();
074        
075        return EMPTY_MAP;
076    }
077}