001/*
002 * Copyright 2002-2017 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
023/**
024 * Base class for monitoring interceptors, such as performance monitors.
025 * Provides configurable "prefix and "suffix" properties that help to
026 * classify/group performance monitoring results.
027 *
028 * <p>In their {@link #invokeUnderTrace} implementation, subclasses should call the
029 * {@link #createInvocationTraceName} method to create a name for the given trace,
030 * including information about the method invocation along with a prefix/suffix.
031 *
032 * @author Rob Harrop
033 * @author Juergen Hoeller
034 * @since 1.2.7
035 * @see #setPrefix
036 * @see #setSuffix
037 * @see #createInvocationTraceName
038 */
039@SuppressWarnings("serial")
040public abstract class AbstractMonitoringInterceptor extends AbstractTraceInterceptor {
041
042        private String prefix = "";
043
044        private String suffix = "";
045
046        private boolean logTargetClassInvocation = false;
047
048
049        /**
050         * Set the text that will get appended to the trace data.
051         * <p>Default is none.
052         */
053        public void setPrefix(String prefix) {
054                this.prefix = (prefix != null ? prefix : "");
055        }
056
057        /**
058         * Return the text that will get appended to the trace data.
059         */
060        protected String getPrefix() {
061                return this.prefix;
062        }
063
064        /**
065         * Set the text that will get prepended to the trace data.
066         * <p>Default is none.
067         */
068        public void setSuffix(String suffix) {
069                this.suffix = (suffix != null ? suffix : "");
070        }
071
072        /**
073         * Return the text that will get prepended to the trace data.
074         */
075        protected String getSuffix() {
076                return this.suffix;
077        }
078
079        /**
080         * Set whether to log the invocation on the target class, if applicable
081         * (i.e. if the method is actually delegated to the target class).
082         * <p>Default is "false", logging the invocation based on the proxy
083         * interface/class name.
084         */
085        public void setLogTargetClassInvocation(boolean logTargetClassInvocation) {
086                this.logTargetClassInvocation = logTargetClassInvocation;
087        }
088
089
090        /**
091         * Create a {@code String} name for the given {@code MethodInvocation}
092         * that can be used for trace/logging purposes. This name is made up of the
093         * configured prefix, followed by the fully-qualified name of the method being
094         * invoked, followed by the configured suffix.
095         * @see #setPrefix
096         * @see #setSuffix
097         */
098        protected String createInvocationTraceName(MethodInvocation invocation) {
099                StringBuilder sb = new StringBuilder(getPrefix());
100                Method method = invocation.getMethod();
101                Class<?> clazz = method.getDeclaringClass();
102                if (this.logTargetClassInvocation && clazz.isInstance(invocation.getThis())) {
103                        clazz = invocation.getThis().getClass();
104                }
105                sb.append(clazz.getName());
106                sb.append('.').append(method.getName());
107                sb.append(getSuffix());
108                return sb.toString();
109        }
110
111}