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.util.Map;
019
020import org.apache.avalon.framework.parameters.Parameters;
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.thread.ThreadSafe;
023import org.apache.cocoon.acting.ServiceableAction;
024import org.apache.cocoon.environment.Redirector;
025import org.apache.cocoon.environment.SourceResolver;
026import org.slf4j.Logger;
027import org.slf4j.LoggerFactory;
028
029import org.ametys.plugins.site.SiteInformationCache;
030
031/**
032 * Clear the {@link SiteInformationCache} informations.
033 */
034public class ResetSiteCacheAction extends ServiceableAction implements ThreadSafe
035{
036    private SiteInformationCache _siteCache;
037    private CacheAccessCounter _cacheAccessCounter;
038    private Logger _logger = LoggerFactory.getLogger("site.cache.log");
039    
040    /**
041     * Get the cache access counter
042     * @return the CacheAccessCounter
043     */
044    protected CacheAccessCounter _getCacheAccessCounter()
045    {
046        if (_cacheAccessCounter == null)
047        {
048            try
049            {
050                _cacheAccessCounter = (CacheAccessCounter) manager.lookup(CacheAccessCounter.ROLE);
051            }
052            catch (ServiceException e)
053            {
054                throw new IllegalStateException("Cannot get CacheAccessCounter", e);
055            }
056        }
057        return _cacheAccessCounter;
058    }
059    
060    /**
061     * Get the site information cache
062     * @return the SiteInformationCache
063     */
064    protected SiteInformationCache _getSiteInformationCache()
065    {
066        if (_siteCache == null)
067        {
068            try
069            {
070                _siteCache = (SiteInformationCache) manager.lookup(SiteInformationCache.ROLE);
071            }
072            catch (ServiceException e)
073            {
074                throw new IllegalStateException("Cannot get SiteInformationCache", e);
075            }
076        }
077        return _siteCache;
078    }
079    
080    @Override
081    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
082    {
083        if (_logger.isInfoEnabled())
084        {
085            _logger.info("Reset sites cache");
086        
087            int count = _getCacheAccessCounter().getAskedResources();
088                
089            _logger.info("Since last reset or site cache invalidation', " + count + " resource(s) have been generated from back-office.");
090        }
091        
092        _getSiteInformationCache().resetSitesCache();
093        return EMPTY_MAP;
094    }
095}