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 java.util.Arrays;
022import java.util.Comparator;
023import java.util.List;
024
025import org.nuxeo.ecm.core.api.CoreSessionService;
026import org.nuxeo.ecm.core.api.CoreSessionService.CoreSessionRegistrationInfo;
027import org.nuxeo.ecm.core.management.jtajca.CoreSessionMonitor;
028import org.nuxeo.ecm.core.management.jtajca.Defaults;
029import org.nuxeo.ecm.core.management.jtajca.internal.DefaultMonitorComponent.ServerInstance;
030import org.nuxeo.runtime.api.Framework;
031import org.nuxeo.runtime.metrics.MetricsService;
032
033import com.codahale.metrics.MetricRegistry;
034import com.codahale.metrics.SharedMetricRegistries;
035import com.codahale.metrics.jvm.JmxAttributeGauge;
036
037public class DefaultCoreSessionMonitor implements CoreSessionMonitor {
038
039    // @since 5.7.2
040    protected final MetricRegistry registry = SharedMetricRegistries.getOrCreate(MetricsService.class.getName());
041
042    @Override
043    public int getCount() {
044        return Framework.getService(CoreSessionService.class).getNumberOfOpenCoreSessions();
045    }
046
047    @Override
048    public String[] getInfos() {
049        List<CoreSessionRegistrationInfo> infos = Framework.getService(CoreSessionService.class)
050                                                           .getCoreSessionRegistrationInfos();
051        return toInfos(toSortedRegistration(infos));
052    }
053
054    public CoreSessionRegistrationInfo[] toSortedRegistration(List<CoreSessionRegistrationInfo> infos) {
055        CoreSessionRegistrationInfo[] sortedInfos = infos.toArray(new CoreSessionRegistrationInfo[infos.size()]);
056        Arrays.sort(sortedInfos, new Comparator<CoreSessionRegistrationInfo>() {
057
058            @Override
059            public int compare(CoreSessionRegistrationInfo o1, CoreSessionRegistrationInfo o2) {
060                return o2.getCoreSession().getSessionId().compareTo(o1.getCoreSession().getSessionId());
061            }
062
063        });
064        return sortedInfos;
065    }
066
067    public String[] toInfos(CoreSessionRegistrationInfo[] infos) {
068        String[] values = new String[infos.length];
069        for (int i = 0; i < infos.length; ++i) {
070            values[i] = Defaults.instance.printStackTrace(infos[i]);
071        }
072        return values;
073    }
074
075    protected ServerInstance self;
076
077    @Override
078    public void install() {
079        self = DefaultMonitorComponent.bind(CoreSessionMonitor.class, this);
080        registry.register(MetricRegistry.name("nuxeo.repositories", "sessions"),
081                new JmxAttributeGauge(self.name, "Count"));
082    }
083
084    @Override
085    public void uninstall() {
086        DefaultMonitorComponent.unbind(self);
087        registry.remove(MetricRegistry.name("nuxeo.repositories", "sessions"));
088        self = null;
089    }
090
091}