001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.commons.proxy.interceptor;
019    
020    import org.apache.commons.logging.Log;
021    import org.apache.commons.proxy.Interceptor;
022    import org.apache.commons.proxy.Invocation;
023    import org.apache.commons.proxy.ProxyUtils;
024    
025    /**
026     * An interceptor which logs each method invocation.
027     * <b>Note</b>: The implementation of this class was borrowed from
028     * HiveMind's logging interceptor.
029     * <p/>
030     * <p>
031     * <b>Dependencies</b>:
032     * <ul>
033     * <li>Apache Commons Logging version 1.0.4 or greater</li>
034     * </ul>
035     * </p>
036     *
037     * @author James Carman
038     * @since 1.0
039     * @deprecated please use {@link org.apache.commons.proxy.interceptor.logging.CommonsLoggingInterceptor} instead
040     */
041    public class LoggingInterceptor implements Interceptor
042    {
043    //**********************************************************************************************************************
044    // Fields
045    //**********************************************************************************************************************
046    
047        private static final int BUFFER_SIZE = 100;
048        private Log log;
049    
050    //**********************************************************************************************************************
051    // Constructors
052    //**********************************************************************************************************************
053    
054        public LoggingInterceptor( Log log )
055        {
056            this.log = log;
057        }
058    
059    //**********************************************************************************************************************
060    // Interceptor Implementation
061    //**********************************************************************************************************************
062    
063        public Object intercept( Invocation invocation ) throws Throwable
064        {
065            if( log.isDebugEnabled() )
066            {
067                final String methodName = invocation.getMethod().getName();
068                entry(methodName, invocation.getArguments());
069                try
070                {
071                    Object result = invocation.proceed();
072                    if( Void.TYPE.equals(invocation.getMethod().getReturnType()) )
073                    {
074                        voidExit(methodName);
075                    }
076                    else
077                    {
078                        exit(methodName, result);
079                    }
080                    return result;
081                }
082                catch( Throwable t )
083                {
084                    exception(methodName, t);
085                    throw t;
086                }
087            }
088            else
089            {
090                return invocation.proceed();
091            }
092        }
093    
094    //**********************************************************************************************************************
095    // Other Methods
096    //**********************************************************************************************************************
097    
098        private void convert( StringBuffer buffer, Object input )
099        {
100            if( input == null )
101            {
102                buffer.append("<null>");
103                return;
104            }
105    
106            // Primitive types, and non-object arrays
107            // use toString().  Less than ideal for int[], etc., but
108            // that's a lot of work for a rare case.
109            if( !( input instanceof Object[] ) )
110            {
111                buffer.append(input.toString());
112                return;
113            }
114            buffer.append("(");
115            buffer.append(ProxyUtils.getJavaClassName(input.getClass()));
116            buffer.append("){");
117            Object[] array = ( Object[] ) input;
118            int count = array.length;
119            for( int i = 0; i < count; i++ )
120            {
121                if( i > 0 )
122                {
123                    buffer.append(", ");
124                }
125    
126                // We use convert() again, because it could be a multi-dimensional array
127                // (god help us) where each element must be converted.
128                convert(buffer, array[i]);
129            }
130            buffer.append("}");
131        }
132    
133        private void entry( String methodName, Object[] args )
134        {
135            StringBuffer buffer = new StringBuffer(BUFFER_SIZE);
136            buffer.append("BEGIN ");
137            buffer.append(methodName);
138            buffer.append("(");
139            int count = args.length;
140            for( int i = 0; i < count; i++ )
141            {
142                Object arg = args[i];
143                if( i > 0 )
144                {
145                    buffer.append(", ");
146                }
147                convert(buffer, arg);
148            }
149            buffer.append(")");
150            log.debug(buffer.toString());
151        }
152    
153        private void exception( String methodName, Throwable t )
154        {
155            StringBuffer buffer = new StringBuffer(BUFFER_SIZE);
156            buffer.append("EXCEPTION ");
157            buffer.append(methodName);
158            buffer.append("() -- ");
159            buffer.append(t.getClass().getName());
160            log.debug(buffer.toString(), t);
161        }
162    
163        private void exit( String methodName, Object result )
164        {
165            StringBuffer buffer = new StringBuffer(BUFFER_SIZE);
166            buffer.append("END ");
167            buffer.append(methodName);
168            buffer.append("() [");
169            convert(buffer, result);
170            buffer.append("]");
171            log.debug(buffer.toString());
172        }
173    
174        private void voidExit( String methodName )
175        {
176            StringBuffer buffer = new StringBuffer(BUFFER_SIZE);
177            buffer.append("END ");
178            buffer.append(methodName);
179            buffer.append("()");
180            log.debug(buffer.toString());
181        }
182    }
183