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.plugins.repository.provider;
017
018import java.io.IOException;
019import java.util.HashMap;
020import java.util.Map;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.avalon.framework.service.Serviceable;
025import org.rrd4j.DsType;
026import org.rrd4j.core.RrdDef;
027import org.rrd4j.core.Sample;
028
029import org.ametys.runtime.plugins.admin.jvmstatus.monitoring.sample.AbstractSampleManager;
030
031/**
032 * Monitoring active JCR sessions.
033 */
034public class SessionSampleManager extends AbstractSampleManager implements Serviceable
035{
036    private JackrabbitRepository _repository;
037    
038    public void service(ServiceManager manager) throws ServiceException
039    {
040        _repository = (JackrabbitRepository) manager.lookup(AbstractRepository.ROLE);
041    } 
042    
043    @Override
044    protected void _configureDatasources(RrdDef rrdDef)
045    {
046        _registerDatasources(rrdDef, "sessions", DsType.GAUGE, 0, Double.NaN);
047    }
048
049    @Override
050    protected String _getGraphTitle()
051    {
052        return "JCR Sessions";
053    }
054
055    @Override
056    protected Map<String, Object> _internalCollect(Sample sample) throws IOException
057    {
058        Map<String, Object> result = new HashMap<>();
059        
060        int sessions = _repository.getSessionCount();
061        sample.setValue("sessions", sessions);
062        result.put("sessions", sessions);
063        return result;
064    }
065}