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 public MetricRegisteringCallback(MetricRegister register) { 038 this.register = register; 039 } 040 041 @Override 042 public void simonCreated(Simon simon) { 043 if (simon.getName() == null) { 044 return; 045 } 046 register(simon); 047 } 048 049 @Override 050 public void simonDestroyed(Simon simon) { 051 register.unregisterMXBean(simon.getName()); 052 } 053 054 @Override 055 public void clear() { 056 register.unregisterAll(); 057 } 058 059 protected final void register(Simon simon) { 060 SimonSuperMXBean mbean; 061 if (simon instanceof Counter) { 062 mbean = new CounterMXBeanImpl((Counter) simon); 063 } else if (simon instanceof Stopwatch) { 064 mbean = new StopwatchMXBeanImpl((Stopwatch) simon); 065 } else { 066 return; 067 } 068 register.registerMXBean(mbean, simon.getName(), mbean.getClass(), mbean.getType()); 069 } 070}