001/* 002 * Copyright (c) 2015 Maxim Yunusov 003 * Licensed under the Apache License, Version 2.0 (the "License"); 004 * you may not use this file except in compliance with the License. 005 * You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software 010 * distributed under the License is distributed on an "AS IS" BASIS, 011 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012 * See the License for the specific language governing permissions and 013 * limitations under the License. 014 */ 015 016package org.maxur.perfmodel.backend.service; 017 018import com.ecyrd.speed4j.StopWatch; 019import com.ecyrd.speed4j.StopWatchFactory; 020import org.aopalliance.intercept.MethodInterceptor; 021import org.aopalliance.intercept.MethodInvocation; 022 023import java.lang.reflect.Method; 024 025/** 026 * This is an interceptor that will implement benchmark of method latency. 027 * <p> 028 * This interceptor does not import any hk2 API and thus 029 * is a pure AOP Alliance method interceptor that might be 030 * used in any software that enabled AOP Alliance interceptors 031 * 032 * @author myunusov 033 * @version 1.0 034 * @since <pre>02.09.2015</pre> 035 */ 036public class BenchmarkMethodInterceptor implements MethodInterceptor { 037 038 private final StopWatchFactory stopWatchFactory = StopWatchFactory.getInstance("loggingFactory"); 039 040 041 /* 042 * (non-Javadoc) 043 * @see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation) 044 */ 045 @Override 046 public Object invoke(MethodInvocation invocation) throws Throwable { 047 final Method method = invocation.getMethod(); 048 final StopWatch stopWatch = stopWatchFactory.getStopWatch(); 049 final String className = method.getDeclaringClass().getSimpleName(); 050 final String methodName = method.getName(); 051 try { 052 Object result = invocation.proceed(); 053 stopWatch.stop(className + " " + methodName + " : success"); 054 return result; 055 } catch (Exception e) { 056 stopWatch.stop(className + " " + methodName + " : error"); 057 throw e; 058 } 059 } 060 061}