001/*
002 *  Copyright 2012 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.runtime.plugins.admin.jvmstatus.monitoring.sample;
017
018import java.io.IOException;
019import java.util.HashMap;
020import java.util.Map;
021
022import org.rrd4j.DsType;
023import org.rrd4j.core.RrdDef;
024import org.rrd4j.core.Sample;
025
026import org.ametys.runtime.plugins.admin.jvmstatus.SessionCountListener;
027import org.ametys.runtime.plugins.admin.jvmstatus.monitoring.SampleManager;
028
029/**
030 * {@link SampleManager} for collecting the number of active HTTP sessions.
031 */
032public class HttpSessionSampleManager extends AbstractSampleManager
033{
034    @Override
035    protected void _configureDatasources(RrdDef rrdDef)
036    {
037        _registerDatasources(rrdDef, "count", DsType.GAUGE, 0, Double.NaN);
038    }
039
040    @Override
041    protected Map<String, Object> _internalCollect(Sample sample) throws IOException
042    {
043        Map<String, Object> result = new HashMap<>();
044        try
045        {
046            int count = Math.max(0, SessionCountListener.getSessionCount());
047            sample.setValue("count", count);
048            result.put("count", count);
049        }
050        catch (IllegalStateException e)
051        {
052            // empty : no value means an error
053        }
054        return result;
055    }
056
057    @Override
058    protected String _getGraphTitle()
059    {
060        return "Active HTTP session";
061    }
062}