001/* 002 * (C) Copyright 2010 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 * Nuxeo - initial API and implementation 018 */ 019 020package org.nuxeo.runtime.management.metrics; 021 022import org.javasimon.CallbackSkeleton; 023import org.javasimon.Counter; 024import org.javasimon.Simon; 025import org.javasimon.Stopwatch; 026import org.javasimon.jmx.SimonSuperMXBean; 027import org.nuxeo.runtime.management.counters.CounterMXBeanImpl; 028import org.nuxeo.runtime.management.stopwatchs.StopwatchMXBeanImpl; 029 030/** 031 * Callback that registers MXBeans for metrics after their creation. 032 */ 033public class MetricRegisteringCallback extends CallbackSkeleton { 034 035 MetricRegister register; 036 037 /** 038 * @param register 039 */ 040 public MetricRegisteringCallback(MetricRegister register) { 041 this.register = register; 042 } 043 044 @Override 045 public void simonCreated(Simon simon) { 046 if (simon.getName() == null) { 047 return; 048 } 049 register(simon); 050 } 051 052 @Override 053 public void simonDestroyed(Simon simon) { 054 register.unregisterMXBean(simon.getName()); 055 } 056 057 @Override 058 public void clear() { 059 register.unregisterAll(); 060 } 061 062 protected final void register(Simon simon) { 063 SimonSuperMXBean mbean; 064 if (simon instanceof Counter) { 065 mbean = new CounterMXBeanImpl((Counter) simon); 066 } else if (simon instanceof Stopwatch) { 067 mbean = new StopwatchMXBeanImpl((Stopwatch) simon); 068 } else { 069 return; 070 } 071 register.registerMXBean(mbean, simon.getName(), mbean.getClass(), mbean.getType()); 072 } 073}