001/*
002 *  Copyright 2018 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.core.cache;
017
018import java.util.ArrayList;
019import java.util.Collections;
020import java.util.List;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024
025import org.ametys.core.ObservationConstants;
026import org.ametys.core.observation.Event;
027import org.ametys.core.observation.ObservationManager;
028import org.ametys.core.ui.Callable;
029import org.ametys.core.ui.StaticClientSideElement;
030
031/**
032 * Client side element for the cache buttons
033 */
034public class CacheClientSideElement extends StaticClientSideElement
035{
036    private AbstractCacheManager _cacheManager;
037    private ObservationManager _observationManager;
038
039    @Override
040    public void service(ServiceManager smanager) throws ServiceException
041    {
042        super.service(smanager);
043        _cacheManager = (AbstractCacheManager) smanager.lookup(AbstractCacheManager.ROLE);
044        
045        if (smanager.hasService(ObservationManager.ROLE))
046        {
047            _observationManager = (ObservationManager) smanager.lookup(ObservationManager.ROLE);
048        }
049    }
050
051    /**
052     * clear the cache related to given id
053     * @param ids the list of all caches ids
054     */
055    @Callable
056    public void clearCaches(List<String> ids)
057    {
058        for (String id : ids)
059        {
060            Cache<Object, Object> cache = _cacheManager.get(id);
061            cache.resetCache();
062            
063            if (_observationManager != null)
064            {
065                _observationManager.notify(new Event(ObservationConstants.EVENT_CACHE_RESET, 
066                                                     _currentUserProvider.getUser(), 
067                                                     Collections.singletonMap(ObservationConstants.ARGS_CACHE_ID, id)));
068            }
069        }
070    }
071    
072    /**
073     * clear all the memory caches
074     * @return the list of all cleaned caches
075     */
076    @Callable
077    public List<String> clearAllCaches()
078    {
079        List<String> cleanedCacheIds = new ArrayList<>();
080        for (Cache cache : _cacheManager.getAllMemoryCaches())
081        {
082            cache.resetCache();
083            
084            cleanedCacheIds.add(cache.getId());
085            if (_observationManager != null)
086            {
087                _observationManager.notify(new Event(ObservationConstants.EVENT_CACHE_RESET, 
088                                                     _currentUserProvider.getUser(), 
089                                                     Collections.singletonMap(ObservationConstants.ARGS_CACHE_ID, cache.getId())));
090            }
091        }
092        return cleanedCacheIds;
093    }
094}