home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Spring Framework example source code file (NameMatchTransactionAttributeSource.java)

This example Spring Framework source code file (NameMatchTransactionAttributeSource.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Spring Framework tags/keywords

class, hashmap, io, iterator, map, namematchtransactionattributesource, namematchtransactionattributesource, reflection, serializable, string, string, transactionattribute, transactionattribute, transactionattributeeditor, transactionattributeeditor, transactionattributesource, util

The Spring Framework NameMatchTransactionAttributeSource.java source code

 * 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,
 * See the License for the specific language governing permissions and
 * limitations under the License.

package org.springframework.transaction.interceptor;

import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.util.ObjectUtils;
import org.springframework.util.PatternMatchUtils;

 * Simple {@link TransactionAttributeSource} implementation that
 * allows attributes to be matched by registered name.
 * @author Juergen Hoeller
 * @since 21.08.2003
 * @see #isMatch
 * @see MethodMapTransactionAttributeSource
public class NameMatchTransactionAttributeSource implements TransactionAttributeSource, Serializable {

	 * Logger available to subclasses.
	 * <p>Static for optimal serialization.
	protected static final Log logger = LogFactory.getLog(NameMatchTransactionAttributeSource.class);

	/** Keys are method names; values are TransactionAttributes */
	private Map nameMap = new HashMap();

	 * Set a name/attribute map, consisting of method names
	 * (e.g. "myMethod") and TransactionAttribute instances
	 * (or Strings to be converted to TransactionAttribute instances).
	 * @see TransactionAttribute
	 * @see TransactionAttributeEditor
	public void setNameMap(Map nameMap) {
		Iterator it = nameMap.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry entry = (Map.Entry) it.next();
			String name = (String) entry.getKey();

			// Check whether we need to convert from String to TransactionAttribute.
			TransactionAttribute attr = null;
			if (entry.getValue() instanceof TransactionAttribute) {
				attr = (TransactionAttribute) entry.getValue();
			else {
				TransactionAttributeEditor editor = new TransactionAttributeEditor();
				attr = (TransactionAttribute) editor.getValue();

			addTransactionalMethod(name, attr);

	 * Parses the given properties into a name/attribute map.
	 * Expects method names as keys and String attributes definitions as values,
	 * parsable into TransactionAttribute instances via TransactionAttributeEditor.
	 * @see #setNameMap
	 * @see TransactionAttributeEditor
	public void setProperties(Properties transactionAttributes) {
		TransactionAttributeEditor tae = new TransactionAttributeEditor();
		for (Iterator it = transactionAttributes.keySet().iterator(); it.hasNext(); ) {
			String methodName = (String) it.next();
			String value = transactionAttributes.getProperty(methodName);
			TransactionAttribute attr = (TransactionAttribute) tae.getValue();
			addTransactionalMethod(methodName, attr);

	 * Add an attribute for a transactional method.
	 * <p>Method names can be exact matches, or of the pattern "xxx*",
	 * "*xxx" or "*xxx*" for matching multiple methods.
	 * @param methodName the name of the method
	 * @param attr attribute associated with the method
	public void addTransactionalMethod(String methodName, TransactionAttribute attr) {
		if (logger.isDebugEnabled()) {
			logger.debug("Adding transactional method [" + methodName + "] with attribute [" + attr + "]");
		this.nameMap.put(methodName, attr);

	public TransactionAttribute getTransactionAttribute(Method method, Class targetClass) {
		// look for direct name match
		String methodName = method.getName();
		TransactionAttribute attr = (TransactionAttribute) this.nameMap.get(methodName);

		if (attr == null) {
			// Look for most specific name match.
			String bestNameMatch = null;
			for (Iterator it = this.nameMap.keySet().iterator(); it.hasNext();) {
				String mappedName = (String) it.next();
				if (isMatch(methodName, mappedName) &&
						(bestNameMatch == null || bestNameMatch.length() <= mappedName.length())) {
					attr = (TransactionAttribute) this.nameMap.get(mappedName);
					bestNameMatch = mappedName;

		return attr;

	 * Return if the given method name matches the mapped name.
	 * <p>The default implementation checks for "xxx*", "*xxx" and "*xxx*" matches,
	 * as well as direct equality. Can be overridden in subclasses.
	 * @param methodName the method name of the class
	 * @param mappedName the name in the descriptor
	 * @return if the names match
	 * @see org.springframework.util.PatternMatchUtils#simpleMatch(String, String)
	protected boolean isMatch(String methodName, String mappedName) {
		return PatternMatchUtils.simpleMatch(mappedName, methodName);

	public boolean equals(Object other) {
		if (this == other) {
			return true;
		if (!(other instanceof NameMatchTransactionAttributeSource)) {
			return false;
		NameMatchTransactionAttributeSource otherTas = (NameMatchTransactionAttributeSource) other;
		return ObjectUtils.nullSafeEquals(this.nameMap, otherTas.nameMap);

	public int hashCode() {
		return NameMatchTransactionAttributeSource.class.hashCode();

	public String toString() {
		return getClass().getName() + ": " + this.nameMap;


Other Spring Framework examples (source code examples)

Here is a short list of links related to this Spring Framework NameMatchTransactionAttributeSource.java source code file:

my book on functional programming


new blog posts


Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.