001/*
002 * Copyright 2002-2020 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.aop.interceptor;
018
019import java.lang.reflect.Method;
020
021import org.aopalliance.intercept.MethodInvocation;
022
023import org.springframework.lang.Nullable;
024
025/**
026 * Base class for monitoring interceptors, such as performance monitors.
027 * Provides configurable "prefix and "suffix" properties that help to
028 * classify/group performance monitoring results.
029 *
030 * <p>In their {@link #invokeUnderTrace} implementation, subclasses should call the
031 * {@link #createInvocationTraceName} method to create a name for the given trace,
032 * including information about the method invocation along with a prefix/suffix.
033 *
034 * @author Rob Harrop
035 * @author Juergen Hoeller
036 * @since 1.2.7
037 * @see #setPrefix
038 * @see #setSuffix
039 * @see #createInvocationTraceName
040 */
041@SuppressWarnings("serial")
042public abstract class AbstractMonitoringInterceptor extends AbstractTraceInterceptor {
043
044        private String prefix = "";
045
046        private String suffix = "";
047
048        private boolean logTargetClassInvocation = false;
049
050
051        /**
052         * Set the text that will get appended to the trace data.
053         * <p>Default is none.
054         */
055        public void setPrefix(@Nullable String prefix) {
056                this.prefix = (prefix != null ? prefix : "");
057        }
058
059        /**
060         * Return the text that will get appended to the trace data.
061         */
062        protected String getPrefix() {
063                return this.prefix;
064        }
065
066        /**
067         * Set the text that will get prepended to the trace data.
068         * <p>Default is none.
069         */
070        public void setSuffix(@Nullable String suffix) {
071                this.suffix = (suffix != null ? suffix : "");
072        }
073
074        /**
075         * Return the text that will get prepended to the trace data.
076         */
077        protected String getSuffix() {
078                return this.suffix;
079        }
080
081        /**
082         * Set whether to log the invocation on the target class, if applicable
083         * (i.e. if the method is actually delegated to the target class).
084         * <p>Default is "false", logging the invocation based on the proxy
085         * interface/class name.
086         */
087        public void setLogTargetClassInvocation(boolean logTargetClassInvocation) {
088                this.logTargetClassInvocation = logTargetClassInvocation;
089        }
090
091
092        /**
093         * Create a {@code String} name for the given {@code MethodInvocation}
094         * that can be used for trace/logging purposes. This name is made up of the
095         * configured prefix, followed by the fully-qualified name of the method being
096         * invoked, followed by the configured suffix.
097         * @see #setPrefix
098         * @see #setSuffix
099         */
100        protected String createInvocationTraceName(MethodInvocation invocation) {
101                Method method = invocation.getMethod();
102                Class<?> clazz = method.getDeclaringClass();
103                if (this.logTargetClassInvocation && clazz.isInstance(invocation.getThis())) {
104                        clazz = invocation.getThis().getClass();
105                }
106                String className = clazz.getName();
107                return getPrefix() + className + '.' + method.getName() + getSuffix();
108        }
109
110}