You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by jc...@apache.org on 2008/02/27 19:57:59 UTC

svn commit: r631677 - in /commons/proper/proxy/trunk: ./ src/main/java/org/apache/commons/proxy/interceptor/ src/main/java/org/apache/commons/proxy/interceptor/logging/

Author: jcarman
Date: Wed Feb 27 10:57:56 2008
New Revision: 631677

URL: http://svn.apache.org/viewvc?rev=631677&view=rev
Log:
PROXY-9: Provide Interceptor Support for More Logging Frameworks

Added:
    commons/proper/proxy/trunk/src/main/java/org/apache/commons/proxy/interceptor/logging/
    commons/proper/proxy/trunk/src/main/java/org/apache/commons/proxy/interceptor/logging/AbstractLoggingInterceptor.java
    commons/proper/proxy/trunk/src/main/java/org/apache/commons/proxy/interceptor/logging/CommonsLoggingInterceptor.java
    commons/proper/proxy/trunk/src/main/java/org/apache/commons/proxy/interceptor/logging/JdkLoggingInterceptor.java
    commons/proper/proxy/trunk/src/main/java/org/apache/commons/proxy/interceptor/logging/Slf4jInterceptor.java
Modified:
    commons/proper/proxy/trunk/pom.xml
    commons/proper/proxy/trunk/src/main/java/org/apache/commons/proxy/interceptor/LoggingInterceptor.java

Modified: commons/proper/proxy/trunk/pom.xml
URL: http://svn.apache.org/viewvc/commons/proper/proxy/trunk/pom.xml?rev=631677&r1=631676&r2=631677&view=diff
==============================================================================
--- commons/proper/proxy/trunk/pom.xml (original)
+++ commons/proper/proxy/trunk/pom.xml Wed Feb 27 10:57:56 2008
@@ -184,6 +184,12 @@
             <optional>true</optional>
         </dependency>
         <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-api</artifactId>
+            <version>[1.3.0,)</version>
+            <optional>true</optional>
+        </dependency>
+        <dependency>
             <groupId>commons-collections</groupId>
             <artifactId>commons-collections</artifactId>
             <version>3.1</version>

Modified: commons/proper/proxy/trunk/src/main/java/org/apache/commons/proxy/interceptor/LoggingInterceptor.java
URL: http://svn.apache.org/viewvc/commons/proper/proxy/trunk/src/main/java/org/apache/commons/proxy/interceptor/LoggingInterceptor.java?rev=631677&r1=631676&r2=631677&view=diff
==============================================================================
--- commons/proper/proxy/trunk/src/main/java/org/apache/commons/proxy/interceptor/LoggingInterceptor.java (original)
+++ commons/proper/proxy/trunk/src/main/java/org/apache/commons/proxy/interceptor/LoggingInterceptor.java Wed Feb 27 10:57:56 2008
@@ -36,6 +36,7 @@
  *
  * @author James Carman
  * @since 1.0
+ * @deprecated please use {@link org.apache.commons.proxy.interceptor.logging.CommonsLoggingInterceptor} instead
  */
 public class LoggingInterceptor implements Interceptor
 {

Added: commons/proper/proxy/trunk/src/main/java/org/apache/commons/proxy/interceptor/logging/AbstractLoggingInterceptor.java
URL: http://svn.apache.org/viewvc/commons/proper/proxy/trunk/src/main/java/org/apache/commons/proxy/interceptor/logging/AbstractLoggingInterceptor.java?rev=631677&view=auto
==============================================================================
--- commons/proper/proxy/trunk/src/main/java/org/apache/commons/proxy/interceptor/logging/AbstractLoggingInterceptor.java (added)
+++ commons/proper/proxy/trunk/src/main/java/org/apache/commons/proxy/interceptor/logging/AbstractLoggingInterceptor.java Wed Feb 27 10:57:56 2008
@@ -0,0 +1,159 @@
+/*
+ * 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.proxy.interceptor.logging;
+
+import org.apache.commons.proxy.Interceptor;
+import org.apache.commons.proxy.Invocation;
+import org.apache.commons.proxy.ProxyUtils;
+
+import java.lang.reflect.Method;
+
+/**
+ * @auothor James Carman
+ * @since 1.1
+ */
+public abstract class AbstractLoggingInterceptor implements Interceptor
+{
+//**********************************************************************************************************************
+// Fields
+//**********************************************************************************************************************
+
+    private static final int BUFFER_SIZE = 100;
+
+//**********************************************************************************************************************
+// Abstract Methods
+//**********************************************************************************************************************
+
+    protected abstract boolean isLoggingEnabled();
+
+    protected abstract void logMessage( String message );
+
+    protected abstract void logMessage( String message, Throwable t );
+
+//**********************************************************************************************************************
+// Interceptor Implementation
+//**********************************************************************************************************************
+
+    public Object intercept( Invocation invocation ) throws Throwable
+    {
+        if( isLoggingEnabled() )
+        {
+            final Method method = invocation.getMethod();
+            entering(method, invocation.getArguments());
+            try
+            {
+                Object result = invocation.proceed();
+                exiting(method, result);
+                return result;
+            }
+            catch( Throwable t )
+            {
+                throwing(method, t);
+                throw t;
+            }
+        }
+        else
+        {
+            return invocation.proceed();
+        }
+    }
+
+//**********************************************************************************************************************
+// Other Methods
+//**********************************************************************************************************************
+
+    protected void entering( Method method, Object[] args )
+    {
+        StringBuffer buffer = new StringBuffer(BUFFER_SIZE);
+        buffer.append("BEGIN ");
+        buffer.append(method.getName());
+        buffer.append("(");
+        int count = args.length;
+        for( int i = 0; i < count; i++ )
+        {
+            Object arg = args[i];
+            if( i > 0 )
+            {
+                buffer.append(", ");
+            }
+            convert(buffer, arg);
+        }
+        buffer.append(")");
+        logMessage(buffer.toString());
+    }
+
+    protected 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(ProxyUtils.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("}");
+    }
+
+    protected void exiting( Method method, Object result )
+    {
+        StringBuffer buffer = new StringBuffer(BUFFER_SIZE);
+        buffer.append("END ");
+        buffer.append(method.getName());
+        buffer.append("()");
+        if( !Void.TYPE.equals(method.getReturnType()) )
+        {
+            buffer.append(" [");
+            convert(buffer, result);
+            buffer.append("]");
+        }
+        logMessage(buffer.toString());
+    }
+
+    protected void throwing( Method method, Throwable t )
+    {
+        StringBuffer buffer = new StringBuffer(BUFFER_SIZE);
+        buffer.append("EXCEPTION ");
+        buffer.append(method);
+        buffer.append("() -- ");
+        buffer.append(t.getClass().getName());
+        logMessage(buffer.toString(), t);
+    }
+}

Added: commons/proper/proxy/trunk/src/main/java/org/apache/commons/proxy/interceptor/logging/CommonsLoggingInterceptor.java
URL: http://svn.apache.org/viewvc/commons/proper/proxy/trunk/src/main/java/org/apache/commons/proxy/interceptor/logging/CommonsLoggingInterceptor.java?rev=631677&view=auto
==============================================================================
--- commons/proper/proxy/trunk/src/main/java/org/apache/commons/proxy/interceptor/logging/CommonsLoggingInterceptor.java (added)
+++ commons/proper/proxy/trunk/src/main/java/org/apache/commons/proxy/interceptor/logging/CommonsLoggingInterceptor.java Wed Feb 27 10:57:56 2008
@@ -0,0 +1,72 @@
+/*
+ * 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.proxy.interceptor.logging;
+
+import org.apache.commons.logging.LogFactory;
+import org.apache.commons.logging.Log;
+
+/**
+ * @auothor James Carman
+ * @since 1.1
+ */
+public class CommonsLoggingInterceptor extends AbstractLoggingInterceptor
+{
+//**********************************************************************************************************************
+// Fields
+//**********************************************************************************************************************
+
+    private final String logName;
+
+//**********************************************************************************************************************
+// Constructors
+//**********************************************************************************************************************
+
+    public CommonsLoggingInterceptor( String logName )
+    {
+        this.logName = logName;
+    }
+
+    public CommonsLoggingInterceptor(Class clazz)
+    {
+        this(clazz.getName());
+    }
+
+//**********************************************************************************************************************
+// Other Methods
+//**********************************************************************************************************************
+
+    private Log getLog()
+    {
+        return LogFactory.getLog(logName);
+    }
+
+    protected boolean isLoggingEnabled()
+    {
+        return getLog().isTraceEnabled();
+    }
+
+    protected void logMessage( String message )
+    {
+        getLog().trace(message);
+    }
+
+    protected void logMessage( String message, Throwable t )
+    {
+        getLog().trace(message, t);
+    }
+}

Added: commons/proper/proxy/trunk/src/main/java/org/apache/commons/proxy/interceptor/logging/JdkLoggingInterceptor.java
URL: http://svn.apache.org/viewvc/commons/proper/proxy/trunk/src/main/java/org/apache/commons/proxy/interceptor/logging/JdkLoggingInterceptor.java?rev=631677&view=auto
==============================================================================
--- commons/proper/proxy/trunk/src/main/java/org/apache/commons/proxy/interceptor/logging/JdkLoggingInterceptor.java (added)
+++ commons/proper/proxy/trunk/src/main/java/org/apache/commons/proxy/interceptor/logging/JdkLoggingInterceptor.java Wed Feb 27 10:57:56 2008
@@ -0,0 +1,90 @@
+/*
+ * 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.proxy.interceptor.logging;
+
+import java.lang.reflect.Method;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ * An interceptor which logs method invocations using a {@link Logger JDK logger} using the "finer" logging level.
+ * 
+ * @auothor James Carman
+ * @since 1.1
+ */
+public class JdkLoggingInterceptor extends AbstractLoggingInterceptor
+{
+//**********************************************************************************************************************
+// Fields
+//**********************************************************************************************************************
+
+    private final String loggerName;
+
+//**********************************************************************************************************************
+// Constructors
+//**********************************************************************************************************************
+
+    public JdkLoggingInterceptor( Class clazz )
+    {
+        this(clazz.getName());
+    }
+
+    public JdkLoggingInterceptor( String loggerName )
+    {
+        this.loggerName = loggerName;
+    }
+
+//**********************************************************************************************************************
+// Other Methods
+//**********************************************************************************************************************
+
+    private Logger getLogger()
+    {
+        return Logger.getLogger(loggerName);
+    }
+
+    protected void entering( Method method, Object[] args )
+    {
+        getLogger().entering(method.getDeclaringClass().getName(), method.getName(), args);
+    }
+
+    protected void exiting( Method method, Object result )
+    {
+        getLogger().exiting(method.getDeclaringClass().getName(), method.getName(), result);
+    }
+
+    protected boolean isLoggingEnabled()
+    {
+        return getLogger().isLoggable(Level.FINER);
+    }
+
+    protected void logMessage( String message )
+    {
+        // Do nothing!
+    }
+
+    protected void logMessage( String message, Throwable t )
+    {
+        // Do nothing!
+    }
+
+    protected void throwing( Method method, Throwable t )
+    {
+        getLogger().throwing(method.getDeclaringClass().getName(), method.getName(), t);
+    }
+}

Added: commons/proper/proxy/trunk/src/main/java/org/apache/commons/proxy/interceptor/logging/Slf4jInterceptor.java
URL: http://svn.apache.org/viewvc/commons/proper/proxy/trunk/src/main/java/org/apache/commons/proxy/interceptor/logging/Slf4jInterceptor.java?rev=631677&view=auto
==============================================================================
--- commons/proper/proxy/trunk/src/main/java/org/apache/commons/proxy/interceptor/logging/Slf4jInterceptor.java (added)
+++ commons/proper/proxy/trunk/src/main/java/org/apache/commons/proxy/interceptor/logging/Slf4jInterceptor.java Wed Feb 27 10:57:56 2008
@@ -0,0 +1,75 @@
+/*
+ * 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.proxy.interceptor.logging;
+
+import org.slf4j.LoggerFactory;
+import org.slf4j.Logger;
+
+/**
+ * An interceptor which logs method invocations using <a href="http://www.slf4j.org/">SLF4J</a> using the
+ * trace logging level.
+ * 
+ * @auothor James Carman
+ * @since 1.1
+ */
+public class Slf4jInterceptor extends AbstractLoggingInterceptor
+{
+//**********************************************************************************************************************
+// Fields
+//**********************************************************************************************************************
+
+    private final String loggerName;
+
+//**********************************************************************************************************************
+// Constructors
+//**********************************************************************************************************************
+
+    public Slf4jInterceptor( Class clazz )
+    {
+        this(clazz.getName());
+    }
+
+    public Slf4jInterceptor( String loggerName )
+    {
+        this.loggerName = loggerName;
+    }
+
+//**********************************************************************************************************************
+// Other Methods
+//**********************************************************************************************************************
+
+    private Logger getLogger()
+    {
+        return LoggerFactory.getLogger(loggerName);
+    }
+
+    protected boolean isLoggingEnabled()
+    {
+        return getLogger().isTraceEnabled();
+    }
+
+    protected void logMessage( String message )
+    {
+        getLogger().debug(message);
+    }
+
+    protected void logMessage( String message, Throwable t )
+    {
+        getLogger().debug(message, t);
+    }
+}