001/* 002 * (C) Copyright 2010 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Nuxeo - initial API and implementation 016 */ 017 018package org.nuxeo.runtime.management.metrics; 019 020import org.javasimon.CallbackSkeleton; 021import org.javasimon.Counter; 022import org.javasimon.Simon; 023import org.javasimon.Stopwatch; 024import org.javasimon.jmx.SimonSuperMXBean; 025import org.nuxeo.runtime.management.counters.CounterMXBeanImpl; 026import org.nuxeo.runtime.management.stopwatchs.StopwatchMXBeanImpl; 027 028/** 029 * Callback that registers MXBeans for metrics after their creation. 030 */ 031public class MetricRegisteringCallback extends CallbackSkeleton { 032 033 MetricRegister register; 034 035 /** 036 * @param register 037 */ 038 public MetricRegisteringCallback(MetricRegister register) { 039 this.register = register; 040 } 041 042 @Override 043 public void simonCreated(Simon simon) { 044 if (simon.getName() == null) { 045 return; 046 } 047 register(simon); 048 } 049 050 @Override 051 public void simonDestroyed(Simon simon) { 052 register.unregisterMXBean(simon.getName()); 053 } 054 055 @Override 056 public void clear() { 057 register.unregisterAll(); 058 } 059 060 protected final void register(Simon simon) { 061 SimonSuperMXBean mbean; 062 if (simon instanceof Counter) { 063 mbean = new CounterMXBeanImpl((Counter) simon); 064 } else if (simon instanceof Stopwatch) { 065 mbean = new StopwatchMXBeanImpl((Stopwatch) simon); 066 } else { 067 return; 068 } 069 register.registerMXBean(mbean, simon.getName(), mbean.getClass(), mbean.getType()); 070 } 071}