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.JmxAttributeGauge; 034import com.codahale.metrics.MetricRegistry; 035import com.codahale.metrics.SharedMetricRegistries; 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).getCoreSessionRegistrationInfos(); 050 return toInfos(toSortedRegistration(infos)); 051 } 052 053 public CoreSessionRegistrationInfo[] toSortedRegistration(List<CoreSessionRegistrationInfo> infos) { 054 CoreSessionRegistrationInfo[] sortedInfos = infos.toArray(new CoreSessionRegistrationInfo[infos.size()]); 055 Arrays.sort(sortedInfos, new Comparator<CoreSessionRegistrationInfo>() { 056 057 @Override 058 public int compare(CoreSessionRegistrationInfo o1, CoreSessionRegistrationInfo o2) { 059 return o2.getCoreSession().getSessionId().compareTo(o1.getCoreSession().getSessionId()); 060 } 061 062 }); 063 return sortedInfos; 064 } 065 066 public String[] toInfos(CoreSessionRegistrationInfo[] infos) { 067 String[] values = new String[infos.length]; 068 for (int i = 0; i < infos.length; ++i) { 069 values[i] = Defaults.instance.printStackTrace(infos[i]); 070 } 071 return values; 072 } 073 074 protected ServerInstance self; 075 076 @Override 077 public void install() { 078 self = DefaultMonitorComponent.bind(CoreSessionMonitor.class, this); 079 registry.register(MetricRegistry.name("nuxeo.repositories", "sessions"), 080 new JmxAttributeGauge(self.name, "Count")); 081 } 082 083 @Override 084 public void uninstall() { 085 DefaultMonitorComponent.unbind(self); 086 registry.remove(MetricRegistry.name("nuxeo.repositories", "sessions")); 087 self = null; 088 } 089 090}