001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.db.timing; 029 030import java.lang.reflect.InvocationHandler; 031import java.lang.reflect.InvocationTargetException; 032import java.lang.reflect.Method; 033 034/** 035 * Invocation handler used to measure method calls durations.<p> 036 */ 037public class CmsProfilingInvocationHandler implements InvocationHandler { 038 039 /** The profiling handler to which we send the data. */ 040 private I_CmsProfilingHandler m_profilingHandler; 041 042 /** The target object which we are proxying. */ 043 private Object m_target; 044 045 /** 046 * Creates a new handler instance.<p> 047 * 048 * @param target the object we are proxying 049 * @param timingConsumer the handler to which we send the measured data 050 */ 051 public CmsProfilingInvocationHandler(Object target, I_CmsProfilingHandler timingConsumer) { 052 053 m_target = target; 054 m_profilingHandler = timingConsumer; 055 } 056 057 /** 058 * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[]) 059 */ 060 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 061 062 long start = 0; 063 long end = 0; 064 try { 065 start = System.nanoTime(); 066 return method.invoke(m_target, args); 067 } catch (InvocationTargetException e) { 068 Throwable cause = e.getCause(); 069 if (cause != null) { 070 throw cause; 071 } else { 072 throw new RuntimeException(e); 073 } 074 } catch (Throwable e) { 075 throw e; 076 } finally { 077 end = System.nanoTime(); 078 long nanos = end - start; 079 String key = method.toString(); 080 m_profilingHandler.putTime(key, nanos); 081 } 082 } 083 084}