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