001/*
002 *  Copyright 2021 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.mbean;
017
018import java.util.List;
019
020import javax.management.MBeanAttributeInfo;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.avalon.framework.service.Serviceable;
025import org.apache.commons.io.FileUtils;
026import org.apache.commons.lang3.StringUtils;
027
028import org.ametys.core.cache.AbstractCacheManager;
029import org.ametys.core.cache.Cache;
030
031/**
032 * MBean providing information about Ametys cache usage
033 */
034public class CacheUsageMBean extends AbstractAmetysDynamicMBean implements Serviceable
035{
036    /** the ROLE of the component */
037    public static final String ROLE = CacheUsageMBean.class.getName();
038    /** The cache manager */
039    AbstractCacheManager _cacheManager;
040    
041    @Override
042    public void service(ServiceManager manager) throws ServiceException
043    {
044        _cacheManager = (AbstractCacheManager) manager.lookup(AbstractCacheManager.ROLE);
045    }
046    
047    /**
048     * Get the total size of all Ametys cache
049     * @return the cache size
050     */
051    public long getTotalCacheSize()
052    {
053        long sum = _cacheManager.getAllCaches()
054                .values()
055                .stream()
056                .flatMap(List::stream)
057                .filter(Cache::isComputableSize)
058                .mapToLong(Cache::getMemorySize)
059                .sum();
060        return sum;
061    }
062    
063    @Override
064    protected Object _getAttribute(String attributeName)
065    {
066        // Call the corresponding getter for a recognized attribute_name
067        if (attributeName.equals("TotalCacheSize"))
068        {
069            return FileUtils.byteCountToDisplaySize(getTotalCacheSize());
070        }
071        if (attributeName.equals("TotalCacheSizeRaw"))
072        {
073            return getTotalCacheSize();
074        }
075        return null; 
076    }
077
078    @Override
079    protected String getMBeanName()
080    {
081        return StringUtils.substringBefore(StringUtils.substringAfterLast(ROLE, "."), "MBean");
082    }
083
084    @Override
085    protected String getMBeanDescription()
086    {
087        return "Cache Memory Usage";
088    }
089
090    @Override
091    protected MBeanAttributeInfo[] getMBeanAttributes()
092    {
093        MBeanAttributeInfo[] attributes = new MBeanAttributeInfo[2];
094        attributes[0] = new MBeanAttributeInfo("TotalCacheSize", "String", "Total size of the different Ametys cache", true, false, false);
095        attributes[1] = new MBeanAttributeInfo("TotalCacheSizeRaw", "float", "Total size of the different Ametys cache in byte", true, false, false);
096        return attributes;
097    }
098}