View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.proxy.interceptor;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.proxy.ProxyUtils;
22  import org.apache.commons.proxy.Interceptor;
23  import org.apache.commons.proxy.Invocation;
24  
25  /**
26   * An interceptor which logs each method invocation.
27   * <b>Note</b>: The implementation of this class was borrowed from
28   * HiveMind's logging interceptor.
29   *
30   * <p>
31   * <b>Dependencies</b>:
32   * <ul>
33   *   <li>Apache Commons Logging version 1.0.4 or greater</li>
34   * </ul>
35   * </p>
36   * @author James Carman
37   * @since 1.0
38   */
39  public class LoggingInterceptor implements Interceptor
40  {
41  //----------------------------------------------------------------------------------------------------------------------
42  // Fields
43  //----------------------------------------------------------------------------------------------------------------------
44  
45      private static final int BUFFER_SIZE = 100;
46      private Log log;
47  
48  //----------------------------------------------------------------------------------------------------------------------
49  // Constructors
50  //----------------------------------------------------------------------------------------------------------------------
51  
52      public LoggingInterceptor( Log log )
53      {
54          this.log = log;
55      }
56  
57  //----------------------------------------------------------------------------------------------------------------------
58  // MethodInterceptor Implementation
59  //----------------------------------------------------------------------------------------------------------------------
60  
61      public Object intercept( Invocation invocation ) throws Throwable
62      {
63          if( log.isDebugEnabled() )
64          {
65              final String methodName = invocation.getMethod().getName();
66              entry( methodName, invocation.getArguments() );
67              try
68              {
69                  Object result = invocation.proceed();
70                  if( Void.TYPE.equals( invocation.getMethod().getReturnType() ) )
71                  {
72                      voidExit( methodName );
73                  }
74                  else
75                  {
76                      exit( methodName, result );
77                  }
78                  return result;
79              }
80              catch( Throwable t )
81              {
82                  exception( methodName, t );
83                  throw t;
84              }
85          }
86          else
87          {
88              return invocation.proceed();
89          }
90      }
91  
92  //----------------------------------------------------------------------------------------------------------------------
93  // Other Methods
94  //----------------------------------------------------------------------------------------------------------------------
95  
96      private void entry( String methodName, Object[] args )
97      {
98          StringBuffer buffer = new StringBuffer( BUFFER_SIZE );
99          buffer.append( "BEGIN " );
100         buffer.append( methodName );
101         buffer.append( "(" );
102         int count = args.length;
103         for( int i = 0; i < count; i++ )
104         {
105             Object arg = args[i];
106             if( i > 0 )
107             {
108                 buffer.append( ", " );
109             }
110             convert( buffer, arg );
111         }
112         buffer.append( ")" );
113         log.debug( buffer.toString() );
114     }
115 
116     private void convert( StringBuffer buffer, Object input )
117     {
118         if( input == null )
119         {
120             buffer.append( "<null>" );
121             return;
122         }
123 
124         // Primitive types, and non-object arrays
125         // use toString().  Less than ideal for int[], etc., but
126         // that's a lot of work for a rare case.
127         if( !( input instanceof Object[] ) )
128         {
129             buffer.append( input.toString() );
130             return;
131         }
132         buffer.append( "(" );
133         buffer.append( ProxyUtils.getJavaClassName( input.getClass() ) );
134         buffer.append( "){" );
135         Object[] array = ( Object[] ) input;
136         int count = array.length;
137         for( int i = 0; i < count; i++ )
138         {
139             if( i > 0 )
140             {
141                 buffer.append( ", " );
142             }
143 
144             // We use convert() again, because it could be a multi-dimensional array
145             // (god help us) where each element must be converted.
146             convert( buffer, array[i] );
147         }
148         buffer.append( "}" );
149     }
150 
151     private void exception( String methodName, Throwable t )
152     {
153         StringBuffer buffer = new StringBuffer( BUFFER_SIZE );
154         buffer.append( "EXCEPTION " );
155         buffer.append( methodName );
156         buffer.append( "() -- " );
157         buffer.append( t.getClass().getName() );
158         log.debug( buffer.toString(), t );
159     }
160 
161     private void exit( String methodName, Object result )
162     {
163         StringBuffer buffer = new StringBuffer( BUFFER_SIZE );
164         buffer.append( "END " );
165         buffer.append( methodName );
166         buffer.append( "() [" );
167         convert( buffer, result );
168         buffer.append( "]" );
169         log.debug( buffer.toString() );
170     }
171 
172     private void voidExit( String methodName )
173     {
174         StringBuffer buffer = new StringBuffer( BUFFER_SIZE );
175         buffer.append( "END " );
176         buffer.append( methodName );
177         buffer.append( "()" );
178         log.debug( buffer.toString() );
179     }
180 }
181