KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > aop > interceptor > AbstractTraceInterceptor


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.aop.interceptor;
18
19 import java.io.Serializable JavaDoc;
20
21 import org.aopalliance.intercept.MethodInterceptor;
22 import org.aopalliance.intercept.MethodInvocation;
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25
26 import org.springframework.aop.support.AopUtils;
27
28 /**
29  * Base <code>MethodInterceptor</code> implementation for tracing.
30  *
31  * <p>By default, log messages are written to the log for the interceptor class,
32  * not the class which is being intercepted. Setting the <code>useDynamicLogger</code>
33  * bean property to <code>true</code> causes all log messages to be written to
34  * the <code>Log</code> for the target class being intercepted.
35  *
36  * <p>Subclasses must implement the <code>invokeUnderTrace</code> method, which
37  * is invoked by this class ONLY when a particular invocation SHOULD be traced.
38  * Subclasses should write to the <code>Log</code> instance provided.
39  *
40  * @author Rob Harrop
41  * @author Juergen Hoeller
42  * @since 1.2
43  * @see #setUseDynamicLogger
44  * @see #invokeUnderTrace(org.aopalliance.intercept.MethodInvocation, org.apache.commons.logging.Log)
45  */

46 public abstract class AbstractTraceInterceptor implements MethodInterceptor, Serializable JavaDoc {
47
48     /**
49      * The default <code>Log</code> instance used to write trace messages.
50      * This instance is mapped to the implementing <code>Class</code>.
51      */

52     protected transient Log defaultLogger = LogFactory.getLog(getClass());
53
54     /**
55      * Indicates whether or not proxy class names should be hidden when using dynamic loggers.
56      * @see #setUseDynamicLogger
57      */

58     private boolean hideProxyClassNames = false;
59
60
61     /**
62      * Set whether to use a dynamic logger or a static logger.
63      * Default is a static logger for this trace interceptor.
64      * <p>Used to determine which <code>Log</code> instance should be used to write
65      * log messages for a particular method invocation: a dynamic one for the
66      * <code>Class</code> getting called, or a static one for the <code>Class</code>
67      * of the trace interceptor.
68      * <p><b>NOTE:</b> Specify either this property or "loggerName", not both.
69      * @see #getLoggerForInvocation(org.aopalliance.intercept.MethodInvocation)
70      */

71     public void setUseDynamicLogger(boolean useDynamicLogger) {
72         // Release default logger if it is not being used.
73
this.defaultLogger = (useDynamicLogger ? null : LogFactory.getLog(getClass()));
74     }
75
76     /**
77      * Set the name of the logger to use. The name will be passed to the
78      * underlying logger implementation through Commons Logging, getting
79      * interpreted as log category according to the logger's configuration.
80      * <p>This can be specified to not log into the category of a class
81      * (whether this interceptor's class or the class getting called)
82      * but rather into a specific named category.
83      * <p><b>NOTE:</b> Specify either this property or "useDynamicLogger", not both.
84      * @see org.apache.commons.logging.LogFactory#getLog(String)
85      * @see org.apache.log4j.Logger#getLogger(String)
86      * @see java.util.logging.Logger#getLogger(String)
87      */

88     public void setLoggerName(String JavaDoc loggerName) {
89         this.defaultLogger = LogFactory.getLog(loggerName);
90     }
91
92     /**
93      * Set to "true" to have {@link #setUseDynamicLogger dynamic loggers} hide
94      * proxy class names wherever possible. Default is "false".
95      */

96     public void setHideProxyClassNames(boolean hideProxyClassNames) {
97         this.hideProxyClassNames = hideProxyClassNames;
98     }
99
100
101     /**
102      * Determines whether or not logging is enabled for the particular <code>MethodInvocation</code>.
103      * If not, the method invocation proceeds as normal, otherwise the method invocation is passed
104      * to the <code>invokeUnderTrace</code> method for handling.
105      * @see #invokeUnderTrace(org.aopalliance.intercept.MethodInvocation, org.apache.commons.logging.Log)
106      */

107     public Object JavaDoc invoke(MethodInvocation invocation) throws Throwable JavaDoc {
108         Log logger = getLoggerForInvocation(invocation);
109         if (isInterceptorEnabled(invocation, logger)) {
110             return invokeUnderTrace(invocation, logger);
111         }
112         else {
113             return invocation.proceed();
114         }
115     }
116
117     /**
118      * Return the appropriate <code>Log</code> instance to use for the given
119      * <code>MethodInvocation</code>. If the <code>useDynamicLogger</code> flag
120      * is set, the <code>Log</code> instance will be for the target class of the
121      * <code>MethodInvocation</code>, otherwise the <code>Log</code> will be the
122      * default static logger.
123      * @param invocation the <code>MethodInvocation</code> being traced
124      * @return the <code>Log</code> instance to use
125      * @see #setUseDynamicLogger
126      */

127     protected Log getLoggerForInvocation(MethodInvocation invocation) {
128         if (this.defaultLogger != null) {
129             return this.defaultLogger;
130         }
131         else {
132             Object JavaDoc target = invocation.getThis();
133             Class JavaDoc logCategoryClass = target.getClass();
134             if (this.hideProxyClassNames) {
135                 logCategoryClass = AopUtils.getTargetClass(target);
136             }
137             return LogFactory.getLog(logCategoryClass);
138         }
139     }
140
141     /**
142      * Determine whether the interceptor should kick in, that is,
143      * whether the <code>invokeUnderTrace</code> method should be called.
144      * <p>Default behavior is to check whether the given <code>Log</code>
145      * instance is enabled. Subclasses can override this to apply the
146      * interceptor in other cases as well.
147      * @param invocation the <code>MethodInvocation</code> being traced
148      * @param logger the <code>Log</code> instance to check
149      * @see #invokeUnderTrace
150      * @see #isLogEnabled
151      */

152     protected boolean isInterceptorEnabled(MethodInvocation invocation, Log logger) {
153         return isLogEnabled(logger);
154     }
155
156     /**
157      * Determine whether the given {@link Log} instance is enabled.
158      * <p>Default is <code>true</code> when the "trace" level is enabled.
159      * Subclasses can override this to change the level under which 'tracing' occurs.
160      * @param logger the <code>Log</code> instance to check
161      */

162     protected boolean isLogEnabled(Log logger) {
163         return logger.isTraceEnabled();
164     }
165
166
167     /**
168      * Subclasses must override this method to perform any tracing around the
169      * supplied <code>MethodInvocation</code>. Subclasses are responsible for
170      * ensuring that the <code>MethodInvocation</code> actually executes by
171      * calling <code>MethodInvocation.proceed()</code>.
172      * <p>By default, the passed-in <code>Log</code> instance will have log level
173      * "trace" enabled. Subclasses do not have to check for this again, unless
174      * they overwrite the <code>isInterceptorEnabled</code> method to modify
175      * the default behavior.
176      * @param logger the <code>Log</code> to write trace messages to
177      * @return the result of the call to <code>MethodInvocation.proceed()</code>
178      * @throws Throwable if the call to <code>MethodInvocation.proceed()</code>
179      * encountered any errors
180      * @see #isInterceptorEnabled
181      * @see #isLogEnabled
182      */

183     protected abstract Object JavaDoc invokeUnderTrace(MethodInvocation invocation, Log logger) throws Throwable JavaDoc;
184
185 }
186
Popular Tags