You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by jc...@apache.org on 2005/10/13 19:02:03 UTC

svn commit: r320806 - in /jakarta/commons/sandbox/proxy/trunk: build.xml commons-proxy.iml src/java/org/apache/commons/proxy/interceptor/LoggingMethodInterceptor.java src/test/org/apache/commons/proxy/interceptor/TestLoggingMethodInterceptor.java

Author: jcarman
Date: Thu Oct 13 10:01:59 2005
New Revision: 320806

URL: http://svn.apache.org/viewcvs?rev=320806&view=rev
Log:
Added LoggingMethodInterceptor (based on HiveMind's similar class).

Added:
    jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/interceptor/LoggingMethodInterceptor.java   (with props)
    jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/interceptor/TestLoggingMethodInterceptor.java   (with props)
Modified:
    jakarta/commons/sandbox/proxy/trunk/build.xml
    jakarta/commons/sandbox/proxy/trunk/commons-proxy.iml

Modified: jakarta/commons/sandbox/proxy/trunk/build.xml
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/build.xml?rev=320806&r1=320805&r2=320806&view=diff
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/build.xml (original)
+++ jakarta/commons/sandbox/proxy/trunk/build.xml Thu Oct 13 10:01:59 2005
@@ -71,6 +71,7 @@
         <runtime-dependency groupId="commons-discovery" version="0.2" />
         <runtime-dependency groupId="javassist" version="3.0"/>
         <build-dependency groupId="junit" version="3.8.1"/>
+        <build-dependency groupId="jmock" version="1.0.1" />
     </target>
 
     <target name="clean">

Modified: jakarta/commons/sandbox/proxy/trunk/commons-proxy.iml
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/commons-proxy.iml?rev=320806&r1=320805&r2=320806&view=diff
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/commons-proxy.iml (original)
+++ jakarta/commons/sandbox/proxy/trunk/commons-proxy.iml Thu Oct 13 10:01:59 2005
@@ -156,6 +156,15 @@
         <SOURCES />
       </library>
     </orderEntry>
+    <orderEntry type="module-library">
+      <library>
+        <CLASSES>
+          <root url="jar://$MODULE_DIR$/lib/build/jmock/jmock.jar!/" />
+        </CLASSES>
+        <JAVADOC />
+        <SOURCES />
+      </library>
+    </orderEntry>
     <orderEntryProperties />
   </component>
 </module>

Added: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/interceptor/LoggingMethodInterceptor.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/interceptor/LoggingMethodInterceptor.java?rev=320806&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/interceptor/LoggingMethodInterceptor.java (added)
+++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/interceptor/LoggingMethodInterceptor.java Thu Oct 13 10:01:59 2005
@@ -0,0 +1,157 @@
+/* $Id$
+ *
+ * Copyright 2005 The Apache Software Foundation.
+ *
+ * Licensed 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.proxy.interceptor;
+
+import org.aopalliance.intercept.MethodInterceptor;
+import org.aopalliance.intercept.MethodInvocation;
+import org.apache.commons.logging.Log;
+import org.apache.commons.proxy.factory.javassist.JavassistUtils;
+
+/**
+ * An interceptor which logs each method invocation.
+ * <b>Note</b>: The implementation of this class was borrowed from
+ * HiveMind's logging interceptor.
+ * @author James Carman
+ * @version 1.0
+ */
+public class LoggingMethodInterceptor implements MethodInterceptor
+{
+    private Log log;
+
+    public LoggingMethodInterceptor( Log log )
+    {
+        this.log = log;
+    }
+
+    public Object invoke( MethodInvocation methodInvocation ) throws Throwable
+    {
+        if( log.isDebugEnabled() )
+        {
+            final String methodName = methodInvocation.getMethod().getName();
+            entry( methodName, methodInvocation.getArguments() );
+            try
+            {
+                Object result = methodInvocation.proceed();
+                if( Void.TYPE.equals( methodInvocation.getMethod().getReturnType() ) )
+                {
+                    voidExit( methodName );
+                }
+                else
+                {
+                    exit( methodName, result );
+                }
+                return result;
+            }
+            catch( Throwable t )
+            {
+                exception( methodName, t );
+                throw t;
+            }
+        }
+        else
+        {
+            return methodInvocation.proceed();
+        }
+    }
+
+    private static final int BUFFER_SIZE = 100;
+
+    public void entry( String methodName, Object[] args )
+    {
+        StringBuffer buffer = new StringBuffer( BUFFER_SIZE );
+        buffer.append( "BEGIN " );
+        buffer.append( methodName );
+        buffer.append( "(" );
+        int count = ( args == null ) ? 0 : args.length;
+        for( int i = 0; i < count; i++ )
+        {
+            Object arg = args[i];
+            if( i > 0 )
+            {
+                buffer.append( ", " );
+            }
+            convert( buffer, arg );
+        }
+        buffer.append( ")" );
+        log.debug( buffer.toString() );
+    }
+
+    public void exit( String methodName, Object result )
+    {
+        StringBuffer buffer = new StringBuffer( BUFFER_SIZE );
+        buffer.append( "END " );
+        buffer.append( methodName );
+        buffer.append( "() [" );
+        convert( buffer, result );
+        buffer.append( "]" );
+        log.debug( buffer.toString() );
+    }
+
+    public void voidExit( String methodName )
+    {
+        StringBuffer buffer = new StringBuffer( BUFFER_SIZE );
+        buffer.append( "END " );
+        buffer.append( methodName );
+        buffer.append( "()" );
+        log.debug( buffer.toString() );
+    }
+
+    public void exception( String methodName, Throwable t )
+    {
+        StringBuffer buffer = new StringBuffer( BUFFER_SIZE );
+        buffer.append( "EXCEPTION " );
+        buffer.append( methodName );
+        buffer.append( "() -- " );
+        buffer.append( t.getClass().getName() );
+        log.debug( buffer.toString(), t );
+    }
+
+    public void convert( StringBuffer buffer, Object input )
+    {
+        if( input == null )
+        {
+            buffer.append( "<null>" );
+            return;
+        }
+
+        // Primitive types, and non-object arrays
+        // use toString().  Less than ideal for int[], etc., but
+        // that's a lot of work for a rare case.
+        if( !( input instanceof Object[] ) )
+        {
+            buffer.append( input.toString() );
+            return;
+        }
+        buffer.append( "(" );
+        buffer.append( JavassistUtils.getJavaClassName( input.getClass() ) );
+        buffer.append( "){" );
+        Object[] array = ( Object[] ) input;
+        int count = array.length;
+        for( int i = 0; i < count; i++ )
+        {
+            if( i > 0 )
+            {
+                buffer.append( ", " );
+            }
+
+            // We use convert() again, because it could be a multi-dimensional array
+            // (god help us) where each element must be converted.
+            convert( buffer, array[i] );
+        }
+        buffer.append( "}" );
+    }
+}

Propchange: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/interceptor/LoggingMethodInterceptor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/interceptor/LoggingMethodInterceptor.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/interceptor/TestLoggingMethodInterceptor.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/interceptor/TestLoggingMethodInterceptor.java?rev=320806&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/interceptor/TestLoggingMethodInterceptor.java (added)
+++ jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/interceptor/TestLoggingMethodInterceptor.java Thu Oct 13 10:01:59 2005
@@ -0,0 +1,86 @@
+/* $Id$
+ *
+ * Copyright 2005 The Apache Software Foundation.
+ *
+ * Licensed 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.proxy.interceptor;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.proxy.factory.cglib.CglibProxyFactory;
+import org.apache.commons.proxy.util.Echo;
+import org.apache.commons.proxy.util.EchoImpl;
+import org.jmock.Mock;
+import org.jmock.MockObjectTestCase;
+
+import java.io.IOException;
+
+public class TestLoggingMethodInterceptor extends MockObjectTestCase
+{
+    private Mock logMock;
+    private Echo echo;
+
+    protected void setUp() throws Exception
+    {
+        logMock = mock( Log.class );
+        echo = ( Echo ) new CglibProxyFactory()
+                .createInterceptorProxy( new EchoImpl(), new LoggingMethodInterceptor( ( Log ) logMock.proxy() ),
+                                         new Class[]{ Echo.class } );
+        logMock.expects( once() ).method( "isDebugEnabled" ).will( returnValue( true ) );
+    }
+
+    public void testNonVoidMethod()
+    {
+        logMock.expects( once() ).method( "debug" ).with( eq( "BEGIN echoBack(Hello)" ) );
+        logMock.expects( once() ).method( "debug" ).with( eq( "END echoBack() [Hello]" ) );
+        echo.echoBack( "Hello" );
+    }
+
+    public void testException()
+    {
+        logMock.expects( once() ).method( "debug" ).with( eq( "BEGIN ioException()" ) );
+        logMock.expects( once() ).method( "debug" ).with( eq( "EXCEPTION ioException() -- java.io.IOException" ), isA( IOException.class ) );
+        try
+        {
+            echo.ioException();
+            fail();
+        }
+        catch( IOException e )
+        {
+
+        }
+    }
+
+    public void testRuntimeException()
+    {
+        logMock.expects( once() ).method( "debug" ).with( eq( "BEGIN illegalArgument()" ) );
+        logMock.expects( once() ).method( "debug" ).with( eq( "EXCEPTION illegalArgument() -- java.lang.IllegalArgumentException" ), isA( IllegalArgumentException.class ) );
+        try
+        {
+            echo.illegalArgument();
+            fail();
+        }
+        catch( IllegalArgumentException e )
+        {
+
+        }
+    }
+
+    public void testVoidMethod()
+    {
+        logMock.expects( once() ).method( "debug" ).with( eq( "BEGIN echo()" ) );
+        logMock.expects( once() ).method( "debug" ).with( eq( "END echo()" ) );
+        echo.echo();
+    }
+
+}
\ No newline at end of file

Propchange: jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/interceptor/TestLoggingMethodInterceptor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/interceptor/TestLoggingMethodInterceptor.java
------------------------------------------------------------------------------
    svn:keywords = Id



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org