1. /*
  2. * Copyright 2002-2004 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.aop.interceptor;
  17. import org.aopalliance.intercept.MethodInterceptor;
  18. import org.aopalliance.intercept.MethodInvocation;
  19. import org.apache.commons.logging.Log;
  20. import org.apache.commons.logging.LogFactory;
  21. import org.springframework.util.StopWatch;
  22. /**
  23. * Trivial performance monitor interceptor.
  24. * This interceptor has no effect on the intercepted method call.
  25. *
  26. * <p>Presently logs information using Commons Logging, at "info" level.
  27. * Could make this much more sophisticated, storing information etc.
  28. *
  29. * @author Rod Johnson
  30. * @author Dmitriy Kopylenko
  31. * @version $Id: PerformanceMonitorInterceptor.java,v 1.2 2004/03/18 02:46:09 trisberg Exp $
  32. */
  33. public class PerformanceMonitorInterceptor implements MethodInterceptor {
  34. protected final Log logger = LogFactory.getLog(getClass());
  35. public Object invoke(MethodInvocation invocation) throws Throwable {
  36. String name = invocation.getMethod().getDeclaringClass().getName() + "." + invocation.getMethod().getName();
  37. logger.debug("Begin performance monitoring of method '" + name + "'");
  38. StopWatch sw = new StopWatch(name);
  39. sw.start(name);
  40. Object rval = invocation.proceed();
  41. sw.stop();
  42. logger.info(sw.shortSummary());
  43. logger.debug("End performance monitoring of method '" + name + "'");
  44. return rval;
  45. }
  46. }