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.plugins.repository.jcr;
017
018import javax.management.MBeanAttributeInfo;
019
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022import org.apache.avalon.framework.service.Serviceable;
023import org.apache.commons.lang3.StringUtils;
024import org.apache.jackrabbit.api.stats.RepositoryStatistics.Type;
025
026import org.ametys.core.mbean.AbstractAmetysDynamicMBean;
027import org.ametys.plugins.repository.provider.AbstractRepository;
028import org.ametys.plugins.repository.provider.JackrabbitRepository;
029
030/**
031 * Provide information on JackRabbit real-time performance and usage
032 */
033public class JackrabbitMBean extends AbstractAmetysDynamicMBean implements Serviceable
034{
035    /** the ROLE of the component */
036    public static final String ROLE = JackrabbitMBean.class.getName();
037
038    /** The jackRabbit repository */
039    private JackrabbitRepository _jackRepo;
040    
041    @Override
042    public void service(ServiceManager manager) throws ServiceException
043    {
044        _jackRepo = (JackrabbitRepository) manager.lookup(AbstractRepository.ROLE);
045    }
046    
047    @Override
048    protected Object _getAttribute(String attributeName)
049    {
050        // Call the corresponding getter for a recognized attribute_name
051        if (attributeName.equals("QueryCount"))
052        {
053            return _jackRepo.getRepositoryStatistics().getTimeSeries(Type.QUERY_COUNT).getValuePerMinute()[0];
054        } 
055        if (attributeName.equals("QueryAverageDuration"))
056        {
057            return _jackRepo.getRepositoryStatistics().getTimeSeries(Type.QUERY_AVERAGE).getValuePerMinute()[0];
058        }
059        if (attributeName.equals("QueryTotalDuration"))
060        {
061            return _jackRepo.getRepositoryStatistics().getTimeSeries(Type.QUERY_DURATION).getValuePerMinute()[0];
062        }
063        if (attributeName.equals("SessionCount"))
064        {
065            return _jackRepo.getRepositoryStatistics().getTimeSeries(Type.SESSION_COUNT).getValuePerMinute()[0];
066        }
067        return null;
068    }
069
070    @Override
071    protected String getMBeanName()
072    {
073        return StringUtils.substringBefore(StringUtils.substringAfterLast(ROLE, "."), "MBean");
074    }
075
076    @Override
077    protected String getMBeanDescription()
078    {
079        return "JackRabbit performance information";
080    }
081
082    @Override
083    protected MBeanAttributeInfo[] getMBeanAttributes()
084    {
085        MBeanAttributeInfo[] attributes = new MBeanAttributeInfo[4];
086        attributes[0] = new MBeanAttributeInfo("QueryCount", "Long", "Number of queries executed the last minute", true, false, false);
087        attributes[1] = new MBeanAttributeInfo("QueryTotalDuration", "Long", "Total time spent evaluating queries in milli seconds the last minute", true, false, false);
088        attributes[2] = new MBeanAttributeInfo("QueryAverageDuration", "Long", "Average time spent evaluating queries in milli seconds the last minute", true, false, false);
089        attributes[3] = new MBeanAttributeInfo("SessionCount", "Long", "Number of currently logged in sessions the last minute", true, false, false);
090
091        return attributes;
092    }
093
094}