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 java.lang.reflect.InvocationHandler; 021import java.lang.reflect.InvocationTargetException; 022import java.lang.reflect.Method; 023import java.lang.reflect.Proxy; 024 025import org.javasimon.SimonManager; 026import org.javasimon.Split; 027import org.javasimon.Stopwatch; 028 029public class MetricInvocationHandler<T> implements InvocationHandler { 030 031 protected final T proxied; 032 033 protected MetricInvocationHandler(T proxied) { 034 this.proxied = proxied; 035 } 036 037 @SuppressWarnings("unchecked") 038 public static <T> T newProxy(T proxied, Class<?>... classes) { 039 MetricInvocationHandler<T> handler = new MetricInvocationHandler<T>(proxied); 040 return (T) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), classes, handler); 041 } 042 043 protected String formatParms(Object... parms) { 044 if (parms == null) { 045 return ""; 046 } 047 StringBuffer buffer = new StringBuffer(); 048 for (Object parm : parms) { 049 buffer.append(".").append(parm); 050 } 051 return buffer.toString(); 052 } 053 054 protected String formatName(Method m, Object[] parms) { 055 Class<?> declaringClass = m.getDeclaringClass(); 056 return String.format("%s.%s", declaringClass.getSimpleName(), m.getName()); 057 } 058 059 protected String formatNote(Method m, Object[] parms) { 060 return String.format("%s#%s(%s)", m.getDeclaringClass().getSimpleName(), m.getName(), formatParms(parms)); 061 } 062 063 protected Stopwatch getStopwatch(Method m, Object[] parms) { 064 String name = formatName(m, parms); 065 Stopwatch stopwatch = SimonManager.getStopwatch(name); 066 stopwatch.setNote(formatNote(m, parms)); 067 return stopwatch; 068 } 069 070 @Override 071 public Object invoke(Object proxy, Method m, Object[] parms) throws Throwable { 072 Split split = getStopwatch(m, parms).start(); 073 try { 074 return m.invoke(proxied, parms); 075 } catch (InvocationTargetException e) { 076 throw e.getTargetException(); 077 } finally { 078 split.stop(); 079 } 080 } 081 082}