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.logging;
019    
020    import org.apache.commons.proxy.Interceptor;
021    import org.apache.commons.proxy.Invocation;
022    import org.apache.commons.proxy.ProxyUtils;
023    
024    import java.lang.reflect.Method;
025    
026    /**
027     * @auothor James Carman
028     * @since 1.1
029     */
030    public abstract class AbstractLoggingInterceptor implements Interceptor
031    {
032    //**********************************************************************************************************************
033    // Fields
034    //**********************************************************************************************************************
035    
036        private static final int BUFFER_SIZE = 100;
037    
038    //**********************************************************************************************************************
039    // Abstract Methods
040    //**********************************************************************************************************************
041    
042        protected abstract boolean isLoggingEnabled();
043    
044        protected abstract void logMessage( String message );
045    
046        protected abstract void logMessage( String message, Throwable t );
047    
048    //**********************************************************************************************************************
049    // Interceptor Implementation
050    //**********************************************************************************************************************
051    
052        public Object intercept( Invocation invocation ) throws Throwable
053        {
054            if( isLoggingEnabled() )
055            {
056                final Method method = invocation.getMethod();
057                entering(method, invocation.getArguments());
058                try
059                {
060                    Object result = invocation.proceed();
061                    exiting(method, result);
062                    return result;
063                }
064                catch( Throwable t )
065                {
066                    throwing(method, t);
067                    throw t;
068                }
069            }
070            else
071            {
072                return invocation.proceed();
073            }
074        }
075    
076    //**********************************************************************************************************************
077    // Other Methods
078    //**********************************************************************************************************************
079    
080        protected void entering( Method method, Object[] args )
081        {
082            StringBuffer buffer = new StringBuffer(BUFFER_SIZE);
083            buffer.append("BEGIN ");
084            buffer.append(method.getName());
085            buffer.append("(");
086            int count = args.length;
087            for( int i = 0; i < count; i++ )
088            {
089                Object arg = args[i];
090                if( i > 0 )
091                {
092                    buffer.append(", ");
093                }
094                convert(buffer, arg);
095            }
096            buffer.append(")");
097            logMessage(buffer.toString());
098        }
099    
100        protected void convert( StringBuffer buffer, Object input )
101        {
102            if( input == null )
103            {
104                buffer.append("<null>");
105                return;
106            }
107    
108            // Primitive types, and non-object arrays
109            // use toString().  Less than ideal for int[], etc., but
110            // that's a lot of work for a rare case.
111            if( !( input instanceof Object[] ) )
112            {
113                buffer.append(input.toString());
114                return;
115            }
116            buffer.append("(");
117            buffer.append(ProxyUtils.getJavaClassName(input.getClass()));
118            buffer.append("){");
119            Object[] array = ( Object[] ) input;
120            int count = array.length;
121            for( int i = 0; i < count; i++ )
122            {
123                if( i > 0 )
124                {
125                    buffer.append(", ");
126                }
127    
128                // We use convert() again, because it could be a multi-dimensional array
129                // (god help us) where each element must be converted.
130                convert(buffer, array[i]);
131            }
132            buffer.append("}");
133        }
134    
135        protected void exiting( Method method, Object result )
136        {
137            StringBuffer buffer = new StringBuffer(BUFFER_SIZE);
138            buffer.append("END ");
139            buffer.append(method.getName());
140            buffer.append("()");
141            if( !Void.TYPE.equals(method.getReturnType()) )
142            {
143                buffer.append(" [");
144                convert(buffer, result);
145                buffer.append("]");
146            }
147            logMessage(buffer.toString());
148        }
149    
150        protected void throwing( Method method, Throwable t )
151        {
152            StringBuffer buffer = new StringBuffer(BUFFER_SIZE);
153            buffer.append("EXCEPTION ");
154            buffer.append(method);
155            buffer.append("() -- ");
156            buffer.append(t.getClass().getName());
157            logMessage(buffer.toString(), t);
158        }
159    }