⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 customizabletraceinterceptor.java

📁 有关此类编程有心德的高手 希望能够多多给予指教
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Copyright 2002-2006 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.aop.interceptor;

import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;

import org.springframework.core.Constants;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.StopWatch;
import org.springframework.util.StringUtils;

/**
 * <code>MethodInterceptor</code> implementation that allows for highly customizable
 * method-level tracing, using placeholders.
 *
 * <p>Trace messages are written on method entry, and if the method invocation succeeds
 * on method exit. If an invocation results in an exception, then an exception message
 * is written. The contents of these trace messages is fully customizable and special
 * placeholders are available to allow you to include runtime information in your log
 * messages. The placeholders available are:
 *
 * <p><ul>
 * <li><code>$[methodName]</code> - replaced with the name of the method being invoked</li>
 * <li><code>$[targetClassName]</code> - replaced with the name of the class that is
 * the target of the invocation</li>
 * <li><code>$[targetClassShortName]</code> - replaced with the short name of the class
 * that is the target of the invocation</li>
 * <li><code>$[returnValue]</code> - replaced with the value returned by the invocation</li>
 * <li><code>$[argumentTypes]</code> - replaced with a comma-separated list of the
 * short class names of the method arguments</li>
 * <li><code>$[arguments]</code> - replaced with a comma-separated list of the
 * <code>String</code> representation of the method arguments</li>
 * <li><code>$[exception]</code> - replaced with the <code>String</code> representation
 * of any <code>Throwable</code> raised during the invocation</li>
 * <li><code>$[invocationTime]</code> - replaced with the time, in milliseconds,
 * taken by the method invocation</li>
 * </ul>
 *
 * <p>There are restrictions on which placeholders can be used in which messages:
 * see the individual message properties for details on the valid placeholders.
 *
 * <p><b>NOTE: This class requires JDK 1.4 or later.</b> It uses the
 * <code>java.util.regex</code> package for regular expression matching,
 * which is only available on JDK 1.4+.
 *
 * @author Rob Harrop
 * @since 1.2
 * @see #setEnterMessage
 * @see #setExitMessage
 * @see #setExceptionMessage
 * @see SimpleTraceInterceptor
 */
public class CustomizableTraceInterceptor extends AbstractTraceInterceptor {

	/**
	 * The <code>$[methodName]</code> placeholder.
	 * Replaced with the name of the method being invoked.
	 */
	public static final String PLACEHOLDER_METHOD_NAME = "$[methodName]";

	/**
	 * The <code>$[targetClassName]</code> placeholder.
	 * Replaced with the fully-qualifed name of the <code>Class</code>
	 * of the method invocation target.
	 */
	public static final String PLACEHOLDER_TARGET_CLASS_NAME = "$[targetClassName]";

	/**
	 * The <code>$[targetClassShortName]</code> placeholder.
	 * Replaced with the short name of the <code>Class</code> of the
	 * method invocation target.
	 */
	public static final String PLACEHOLDER_TARGET_CLASS_SHORT_NAME = "$[targetClassShortName]";

	/**
	 * The <code>$[returnValue]</code> placeholder.
	 * Replaced with the <code>String</code> representation of the value
	 * returned by the method invocation.
	 */
	public static final String PLACEHOLDER_RETURN_VALUE = "$[returnValue]";

	/**
	 * The <code>$[argumentTypes]</code> placeholder.
	 * Replaced with a comma-separated list of the argument types for the
	 * method invocation. Argument types are written as short class names.
	 */
	public static final String PLACEHOLDER_ARGUMENT_TYPES = "$[argumentTypes]";

	/**
	 * The <code>$[arguments]</code> placeholder.
	 * Replaced with a comma separated list of the argument values for the
	 * method invocation. Relies on the <code>toString()</code> method of
	 * each argument type.
	 */
	public static final String PLACEHOLDER_ARGUMENTS = "$[arguments]";

	/**
	 * The <code>$[exception]</code> placeholder.
	 * Replaced with the <code>String</code> representation of any
	 * <code>Throwable</code> raised during method invocation.
	 */
	public static final String PLACEHOLDER_EXCEPTION = "$[exception]";

	/**
	 * The <code>$[invocationTime]</code> placeholder.
	 * Replaced with the time taken by the invocation (in milliseconds).
	 */
	public static final String PLACEHOLDER_INVOCATION_TIME = "$[invocationTime]";

	/**
	 * The default message used for writing method entry messages.
	 */
	private static final String DEFAULT_ENTER_MESSAGE =
			"Entering method '" + PLACEHOLDER_METHOD_NAME + "' of class [" + PLACEHOLDER_TARGET_CLASS_NAME + "]";

	/**
	 * The default message used for writing method exit messages.
	 */
	private static final String DEFAULT_EXIT_MESSAGE =
			"Exiting method '" + PLACEHOLDER_METHOD_NAME + "' of class [" + PLACEHOLDER_TARGET_CLASS_NAME + "]";

	/**
	 * The default method used for writing exception messages.
	 */
	private static final String DEFAULT_EXCEPTION_MESSAGE =
			"Exception thrown in method '" + PLACEHOLDER_METHOD_NAME + "' of class [" + PLACEHOLDER_TARGET_CLASS_NAME + "]";

	/**
	 * The <code>Pattern</code> used to match placeholders.
	 */
	private static final Pattern PATTERN = Pattern.compile("\\$\\[\\p{Alpha}+\\]");

	/**
	 * The <code>Pattern</code> used to escape regex values in class names -
	 * specifically <code>$</code>.
	 */
	private static final Pattern ESCAPE_PATTERN = Pattern.compile("\\$");

	/**
	 * The <code>Set</code> of allowed placeholders.
	 */
	private static final Set ALLOWED_PLACEHOLDERS =
			new Constants(CustomizableTraceInterceptor.class).getValues("PLACEHOLDER_");


	/**
	 * The message for method entry.
	 */
	private String enterMessage = DEFAULT_ENTER_MESSAGE;

	/**
	 * The message for method exit.
	 */
	private String exitMessage = DEFAULT_EXIT_MESSAGE;

	/**
	 * The message for exceptions during method execution.
	 */
	private String exceptionMessage = DEFAULT_EXCEPTION_MESSAGE;


	/**
	 * Ses the template used for method entry log messages.
	 * This template can contain any of the following placeholders:
	 * <ul>
	 * <li><code>$[targetClassName]</code></li>
	 * <li><code>$[targetClassShortName]</code></li>
	 * <li><code>$[argumentTypes]</code></li>
	 * <li><code>$[arguments]</code></li>
	 * </ul>
	 * @throws IllegalArgumentException if the message template is empty
	 * or contains any invalid placeholders
	 */
	public void setEnterMessage(String enterMessage) throws IllegalArgumentException {
		Assert.hasText(enterMessage, "'enterMessage' must not be empty");
		checkForInvalidPlaceholders(enterMessage);
		Assert.doesNotContain(enterMessage, PLACEHOLDER_RETURN_VALUE,
				"enterMessage cannot contain placeholder [" + PLACEHOLDER_RETURN_VALUE + "]");
		Assert.doesNotContain(enterMessage, PLACEHOLDER_EXCEPTION,
				"enterMessage cannot contain placeholder [" + PLACEHOLDER_EXCEPTION + "]");
		Assert.doesNotContain(enterMessage, PLACEHOLDER_INVOCATION_TIME,
				"enterMessage cannot contain placeholder [" + PLACEHOLDER_INVOCATION_TIME + "]");
		this.enterMessage = enterMessage;
	}

	/**
	 * Set the template used for method exit log messages.
	 * This template can contain any of the following placeholders:
	 * <ul>
	 * <li><code>$[targetClassName]</code></li>
	 * <li><code>$[targetClassShortName]</code></li>
	 * <li><code>$[argumentTypes]</code></li>
	 * <li><code>$[arguments]</code></li>
	 * <li><code>$[returnValue]</code></li>
	 * <li><code>$[invocationTime]</code></li>
	 * </ul>
	 * @throws IllegalArgumentException if the message template is empty
	 * or contains any invalid placeholders
	 */
	public void setExitMessage(String exitMessage) {
		Assert.hasText(exitMessage, "'exitMessage' must not be empty");
		checkForInvalidPlaceholders(exitMessage);
		Assert.doesNotContain(exitMessage, PLACEHOLDER_EXCEPTION,
				"exitMessage cannot contain placeholder [" + PLACEHOLDER_EXCEPTION + "]");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -