Author: nicolas Date: Fri Mar 7 06:04:46 2008 New Revision: 634679 URL: http://svn.apache.org/viewvc?rev=634679&view=rev Log: add support for commons-proxy interceptors AbstractPerformanceInterceptor for common method interception features, to be derived according to method interception APIs
Added: commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/aop/AbstractPerformanceInterceptor.java (with props) commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/aop/CommonsProxyPerformanceInterceptor.java (with props) commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/aop/SpringPerformanceInterceptor.java (contents, props changed) - copied, changed from r634228, commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/aop/PerformanceInterceptor.java Removed: commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/aop/PerformanceInterceptor.java Modified: commons/sandbox/monitoring/trunk/pom.xml Modified: commons/sandbox/monitoring/trunk/pom.xml URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/pom.xml?rev=634679&r1=634678&r2=634679&view=diff ============================================================================== --- commons/sandbox/monitoring/trunk/pom.xml (original) +++ commons/sandbox/monitoring/trunk/pom.xml Fri Mar 7 06:04:46 2008 @@ -48,6 +48,12 @@ <version>1.0</version> <optional>true</optional> </dependency> + <dependency> + <groupId>org.apache.commons</groupId> + <artifactId>commons-proxy</artifactId> + <version>1.0</version> + <optional>true</optional> + </dependency> <dependency> <groupId>junit</groupId> Added: commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/aop/AbstractPerformanceInterceptor.java URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/aop/AbstractPerformanceInterceptor.java?rev=634679&view=auto ============================================================================== --- commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/aop/AbstractPerformanceInterceptor.java (added) +++ commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/aop/AbstractPerformanceInterceptor.java Fri Mar 7 06:04:46 2008 @@ -0,0 +1,138 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.monitoring.aop; + +import java.lang.reflect.Method; + +import org.apache.commons.monitoring.Monitor; +import org.apache.commons.monitoring.Repository; +import org.apache.commons.monitoring.StopWatch; +import org.apache.commons.monitoring.Unit; + +/** + * A method interceptor that compute method invocation performances. + * <p> + * Concrete implementation will adapt the method interception API to + * this class requirement. + * + * @author <a href="mailto:[EMAIL PROTECTED]">Nicolas De Loof</a> + */ +public abstract class AbstractPerformanceInterceptor<T> +{ + + /** Role for the invocation failure counter */ + private static final String FAILURE = "failure"; + + protected Repository repository; + + protected String category; + + protected String subsystem; + + public AbstractPerformanceInterceptor() + { + super(); + } + + /** + * API neutral method invocation + */ + protected Object doInvoke( T invocation ) + throws Throwable + { + String name = getMonitorName( invocation ); + if ( name == null ) + { + return proceed( invocation ); + } + Monitor monitor = repository.getMonitor( name, category, subsystem ); + StopWatch stopwatch = repository.start( monitor ); + Throwable error = null; + try + { + return proceed( invocation ); + } + catch ( Throwable t ) + { + error = t; + throw t; + } + finally + { + stopwatch.stop(); + beforeReturning( monitor, error, stopwatch.getElapsedTime() ); + } + } + + /** + * @param invocation + * @return + */ + protected abstract Object proceed( T invocation ) + throws Throwable; + + /** + * @param invocation + * @return + */ + protected abstract String getMonitorName( T invocation ); + + /** + * Compute the monitor name associated to this method invocation + * + * @param method method being invoked + * @return monitor name. If <code>null</code>, nothing will be monitored + */ + protected String getMonitorName( Method method ) + { + return method.getDeclaringClass().getSimpleName() + "." + method.getName(); + } + + /** + * @param monitor the monitor associated to the method invocation + * @param error Throwable thrown by the method invocation if any + * @param duration the duration of the method invocation + */ + protected void beforeReturning( Monitor monitor, Throwable error, long duration ) + { + if ( error != null ) + { + monitor.getCounter( FAILURE ).add( duration, Unit.NANOS ); + } + } + + /** + * Set a custom application-defined repository + * + * @param repository + */ + public void setRepository( Repository repository ) + { + this.repository = repository; + } + + public void setCategory( String category ) + { + this.category = category; + } + + public void setSubsystem( String subsystem ) + { + this.subsystem = subsystem; + } +} \ No newline at end of file Propchange: commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/aop/AbstractPerformanceInterceptor.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/aop/AbstractPerformanceInterceptor.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Propchange: commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/aop/AbstractPerformanceInterceptor.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/aop/CommonsProxyPerformanceInterceptor.java URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/aop/CommonsProxyPerformanceInterceptor.java?rev=634679&view=auto ============================================================================== --- commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/aop/CommonsProxyPerformanceInterceptor.java (added) +++ commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/aop/CommonsProxyPerformanceInterceptor.java Fri Mar 7 06:04:46 2008 @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.monitoring.aop; + +import org.apache.commons.proxy.Interceptor; +import org.apache.commons.proxy.Invocation; + +/** + * Commons-proxy implementation of PerformanceInterceptor. + * + * @author <a href="mailto:[EMAIL PROTECTED]">Nicolas De Loof</a> + */ +public class CommonsProxyPerformanceInterceptor + extends AbstractPerformanceInterceptor<Invocation> + implements Interceptor +{ + + /** + * [EMAIL PROTECTED] + * + * @see org.apache.commons.proxy.Interceptor#intercept(org.apache.commons.proxy.Invocation) + */ + public Object intercept( Invocation invocation ) + throws Throwable + { + return doInvoke( invocation ); + } + + /** + * [EMAIL PROTECTED] + * + * @see org.apache.commons.monitoring.aop.AbstractPerformanceInterceptor#getMonitorName(java.lang.Object) + */ + @Override + protected String getMonitorName( Invocation invocation ) + { + return getMonitorName( invocation.getMethod() ); + } + + /** + * [EMAIL PROTECTED] + * + * @see org.apache.commons.monitoring.aop.AbstractPerformanceInterceptor#proceed(java.lang.Object) + */ + @Override + protected Object proceed( Invocation invocation ) + throws Throwable + { + return invocation.proceed(); + } +} Propchange: commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/aop/CommonsProxyPerformanceInterceptor.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/aop/CommonsProxyPerformanceInterceptor.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Propchange: commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/aop/CommonsProxyPerformanceInterceptor.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Copied: commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/aop/SpringPerformanceInterceptor.java (from r634228, commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/aop/PerformanceInterceptor.java) URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/aop/SpringPerformanceInterceptor.java?p2=commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/aop/SpringPerformanceInterceptor.java&p1=commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/aop/PerformanceInterceptor.java&r1=634228&r2=634679&rev=634679&view=diff ============================================================================== --- commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/aop/PerformanceInterceptor.java (original) +++ commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/aop/SpringPerformanceInterceptor.java Fri Mar 7 06:04:46 2008 @@ -19,104 +19,48 @@ import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; -import org.apache.commons.monitoring.Monitor; -import org.apache.commons.monitoring.Repository; -import org.apache.commons.monitoring.StopWatch; -import org.apache.commons.monitoring.Unit; /** - * An aopalliance interceptor for method invocation to monitor performances and - * failures. + * Spring-aop implementation of PerformanceInterceptor. * * @author <a href="mailto:[EMAIL PROTECTED]">Nicolas De Loof</a> */ -public class PerformanceInterceptor +public class SpringPerformanceInterceptor + extends AbstractPerformanceInterceptor<MethodInvocation> implements MethodInterceptor { - /** Role for the invocation failure counter */ - private static final String FAILURE = "failure"; - - private Repository repository; - - private String category; - - private String subsystem; /** * [EMAIL PROTECTED] * * @see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation) */ - public final Object invoke( MethodInvocation invocation ) + public Object invoke( MethodInvocation invocation ) throws Throwable { - String name = getMonitorName( invocation ); - if ( name == null ) - { - return invocation.proceed(); - } - Monitor monitor = repository.getMonitor( name, category, subsystem ); - StopWatch stopwatch = repository.start( monitor ); - - Throwable error = null; - try - { - return invocation.proceed(); - } - catch ( Throwable t ) - { - error = t; - throw t; - } - finally - { - stopwatch.stop(); - beforeReturning( monitor, error, stopwatch.getElapsedTime() ); - } - } - - /** - * @param monitor the monitor associated to the method invocation - * @param error Throwable thrown by the method invocation if any - * @param duration the duration of the method invocation - */ - private void beforeReturning( Monitor monitor, Throwable error, long duration ) - { - if ( error != null ) - { - monitor.getCounter( FAILURE ).add( duration, Unit.NANOS ); - } + return doInvoke( invocation ); } /** - * Compute the monitor name associated to this method invocation + * [EMAIL PROTECTED] * - * @param invocation method being invoked - * @return monitor name. If <code>null</code>, nothing will be monitored + * @see org.apache.commons.monitoring.aop.AbstractPerformanceInterceptor#getMonitorName(java.lang.Object) */ + @Override protected String getMonitorName( MethodInvocation invocation ) { - return invocation.getClass().getSimpleName() + "." + invocation.getMethod().getName(); + return getMonitorName( invocation.getMethod() ); } /** - * Set a custom application-defined repository + * [EMAIL PROTECTED] * - * @param repository + * @see org.apache.commons.monitoring.aop.AbstractPerformanceInterceptor#proceed(java.lang.Object) */ - public void setRepository( Repository repository ) - { - this.repository = repository; - } - - public void setCategory( String category ) - { - this.category = category; - } - - public void setSubsystem( String subsystem ) + @Override + protected Object proceed( MethodInvocation invocation ) { - this.subsystem = subsystem; + return invocation.proceed(); } } Propchange: commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/aop/SpringPerformanceInterceptor.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/aop/SpringPerformanceInterceptor.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Propchange: commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/aop/SpringPerformanceInterceptor.java ------------------------------------------------------------------------------ svn:mergeinfo = Propchange: commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/aop/SpringPerformanceInterceptor.java ------------------------------------------------------------------------------ svn:mime-type = text/plain