001/*
002 * (C) Copyright 2013 Nuxeo SA (http://nuxeo.com/) and others.
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 *
016 * Contributors:
017 *     Stephane Lacoin
018 */
019package org.nuxeo.ecm.core.management.jtajca.internal;
020
021import org.nuxeo.ecm.core.api.CoreSessionService;
022import org.nuxeo.ecm.core.management.jtajca.CoreSessionMonitor;
023import org.nuxeo.ecm.core.management.jtajca.internal.DefaultMonitorComponent.ServerInstance;
024import org.nuxeo.runtime.api.Framework;
025import org.nuxeo.runtime.metrics.MetricsService;
026
027import io.dropwizard.metrics5.MetricName;
028import io.dropwizard.metrics5.MetricRegistry;
029import io.dropwizard.metrics5.SharedMetricRegistries;
030import io.dropwizard.metrics5.jvm.JmxAttributeGauge;
031
032public class DefaultCoreSessionMonitor implements CoreSessionMonitor {
033
034    // @since 5.7.2
035    protected final MetricRegistry registry = SharedMetricRegistries.getOrCreate(MetricsService.class.getName());
036
037    protected MetricName sessionGauge;
038
039    @Override
040    public int getCount() {
041        return Framework.getService(CoreSessionService.class).getNumberOfOpenCoreSessions();
042    }
043
044    @Override
045    public String[] getInfos() {
046        return new String[0];
047    }
048
049    protected ServerInstance self;
050
051    @Override
052    public void install() {
053        self = DefaultMonitorComponent.bind(CoreSessionMonitor.class, this);
054        sessionGauge = MetricRegistry.name("nuxeo.repositories.sessions");
055        registry.register(sessionGauge, new JmxAttributeGauge(self.name, "Count"));
056    }
057
058    @Override
059    public void uninstall() {
060        DefaultMonitorComponent.unbind(self);
061        registry.remove(sessionGauge);
062        self = null;
063    }
064
065}