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.Interceptor;
22 import org.apache.commons.proxy.Invocation;
23 import org.apache.commons.proxy.ProxyUtils;
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 * <p/>
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 *
37 * @author James Carman
38 * @since 1.0
39 * @deprecated please use {@link org.apache.commons.proxy.interceptor.logging.CommonsLoggingInterceptor} instead
40 */
41 public class LoggingInterceptor implements Interceptor
42 {
43 //**********************************************************************************************************************
44 // Fields
45 //**********************************************************************************************************************
46
47 private static final int BUFFER_SIZE = 100;
48 private Log log;
49
50 //**********************************************************************************************************************
51 // Constructors
52 //**********************************************************************************************************************
53
54 public LoggingInterceptor( Log log )
55 {
56 this.log = log;
57 }
58
59 //**********************************************************************************************************************
60 // Interceptor Implementation
61 //**********************************************************************************************************************
62
63 public Object intercept( Invocation invocation ) throws Throwable
64 {
65 if( log.isDebugEnabled() )
66 {
67 final String methodName = invocation.getMethod().getName();
68 entry(methodName, invocation.getArguments());
69 try
70 {
71 Object result = invocation.proceed();
72 if( Void.TYPE.equals(invocation.getMethod().getReturnType()) )
73 {
74 voidExit(methodName);
75 }
76 else
77 {
78 exit(methodName, result);
79 }
80 return result;
81 }
82 catch( Throwable t )
83 {
84 exception(methodName, t);
85 throw t;
86 }
87 }
88 else
89 {
90 return invocation.proceed();
91 }
92 }
93
94 //**********************************************************************************************************************
95 // Other Methods
96 //**********************************************************************************************************************
97
98 private void convert( StringBuffer buffer, Object input )
99 {
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