All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.springframework.transaction.interceptor.MethodMapTransactionAttributeSource Maven / Gradle / Ivy

There is a newer version: 5.3.34
Show newest version
/*
 * 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.transaction.interceptor;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

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

import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.PatternMatchUtils;

/**
 * Simple {@link TransactionAttributeSource} implementation that
 * allows attributes to be stored per method in a {@link Map}.
 * 
 * @author Rod Johnson
 * @author Juergen Hoeller
 * @since 24.04.2003
 * @see #isMatch
 * @see NameMatchTransactionAttributeSource
 */
public class MethodMapTransactionAttributeSource
		implements TransactionAttributeSource, BeanClassLoaderAware, InitializingBean {

	/** Logger available to subclasses */
	protected final Log logger = LogFactory.getLog(getClass());

	/** Map from method name to attribute value */
	private Map methodMap;

	private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader();

	private boolean eagerlyInitialized = false;

	private boolean initialized = false;

	/** Map from Method to TransactionAttribute */
	private final Map transactionAttributeMap = new HashMap();

	/** Map from Method to name pattern used for registration */
	private final Map methodNameMap = new HashMap();


	/**
	 * Set a name/attribute map, consisting of "FQCN.method" method names
	 * (e.g. "com.mycompany.mycode.MyClass.myMethod") and
	 * {@link TransactionAttribute} instances (or Strings to be converted
	 * to TransactionAttribute instances).
	 * 

Intended for configuration via setter injection, typically within * a Spring bean factory. Relies on {@link #afterPropertiesSet()} * being called afterwards. * @param methodMap said {@link Map} from method name to attribute value * @see TransactionAttribute * @see TransactionAttributeEditor */ public void setMethodMap(Map methodMap) { this.methodMap = methodMap; } public void setBeanClassLoader(ClassLoader beanClassLoader) { this.beanClassLoader = beanClassLoader; } /** * Eagerly initializes the specified * {@link #setMethodMap(java.util.Map) "methodMap"}, if any. * @see #initMethodMap(java.util.Map) */ public void afterPropertiesSet() { initMethodMap(this.methodMap); this.eagerlyInitialized = true; this.initialized = true; } /** * Initialize the specified {@link #setMethodMap(java.util.Map) "methodMap"}, if any. * @param methodMap Map from method names to TransactionAttribute instances * (or Strings to be converted to TransactionAttribute instances) * @see #setMethodMap */ protected void initMethodMap(Map methodMap) { if (methodMap != null) { Iterator it = methodMap.entrySet().iterator(); while (it.hasNext()) { Map.Entry entry = (Map.Entry) it.next(); Object key = entry.getKey(); if (!(key instanceof String)) { throw new IllegalArgumentException( "Invalid method map key [" + key + "]: only Strings allowed"); } Object value = entry.getValue(); // Check whether we need to convert from String to TransactionAttribute. TransactionAttribute attr = null; if (value instanceof TransactionAttribute) { attr = (TransactionAttribute) value; } else if (value instanceof String) { TransactionAttributeEditor editor = new TransactionAttributeEditor(); editor.setAsText((String) value); attr = (TransactionAttribute) editor.getValue(); } else { throw new IllegalArgumentException("Value [" + value + "] is neither of type [" + TransactionAttribute.class.getName() + "] nor a String"); } addTransactionalMethod((String) key, attr); } } } /** * Add an attribute for a transactional method. *

Method names can end or start with "*" for matching multiple methods. * @param name class and method name, separated by a dot * @param attr attribute associated with the method * @throws IllegalArgumentException in case of an invalid name */ public void addTransactionalMethod(String name, TransactionAttribute attr) { Assert.notNull(name, "Name must not be null"); int lastDotIndex = name.lastIndexOf("."); if (lastDotIndex == -1) { throw new IllegalArgumentException("'" + name + "' is not a valid method name: format is FQN.methodName"); } String className = name.substring(0, lastDotIndex); String methodName = name.substring(lastDotIndex + 1); Class clazz = ClassUtils.resolveClassName(className, this.beanClassLoader); addTransactionalMethod(clazz, methodName, attr); } /** * Add an attribute for a transactional method. * Method names can end or start with "*" for matching multiple methods. * @param clazz target interface or class * @param mappedName mapped method name * @param attr attribute associated with the method */ public void addTransactionalMethod(Class clazz, String mappedName, TransactionAttribute attr) { Assert.notNull(clazz, "Class must not be null"); Assert.notNull(mappedName, "Mapped name must not be null"); String name = clazz.getName() + '.' + mappedName; // TODO address method overloading? At present this will // simply match all methods that have the given name. // Consider EJB syntax (int, String) etc.? Method[] methods = clazz.getDeclaredMethods(); List matchingMethods = new ArrayList(); for (int i = 0; i < methods.length; i++) { if (isMatch(methods[i].getName(), mappedName)) { matchingMethods.add(methods[i]); } } if (matchingMethods.isEmpty()) { throw new IllegalArgumentException( "Couldn't find method '" + mappedName + "' on class [" + clazz.getName() + "]"); } // register all matching methods for (Iterator it = matchingMethods.iterator(); it.hasNext();) { Method method = (Method) it.next(); String regMethodName = (String) this.methodNameMap.get(method); if (regMethodName == null || (!regMethodName.equals(name) && regMethodName.length() <= name.length())) { // No already registered method name, or more specific // method name specification now -> (re-)register method. if (logger.isDebugEnabled() && regMethodName != null) { logger.debug("Replacing attribute for transactional method [" + method + "]: current name '" + name + "' is more specific than '" + regMethodName + "'"); } this.methodNameMap.put(method, name); addTransactionalMethod(method, attr); } else { if (logger.isDebugEnabled() && regMethodName != null) { logger.debug("Keeping attribute for transactional method [" + method + "]: current name '" + name + "' is not more specific than '" + regMethodName + "'"); } } } } /** * Add an attribute for a transactional method. * @param method the method * @param attr attribute associated with the method */ public void addTransactionalMethod(Method method, TransactionAttribute attr) { Assert.notNull(method, "Method must not be null"); Assert.notNull(attr, "TransactionAttribute must not be null"); if (logger.isDebugEnabled()) { logger.debug("Adding transactional method [" + method + "] with attribute [" + attr + "]"); } this.transactionAttributeMap.put(method, attr); } /** * Return if the given method name matches the mapped name. *

The default implementation checks for "xxx*", "*xxx" and "*xxx*" * matches, as well as direct equality. * @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 TransactionAttribute getTransactionAttribute(Method method, Class targetClass) { if (this.eagerlyInitialized) { return (TransactionAttribute) this.transactionAttributeMap.get(method); } else { synchronized (this.transactionAttributeMap) { if (!this.initialized) { initMethodMap(this.methodMap); this.initialized = true; } return (TransactionAttribute) this.transactionAttributeMap.get(method); } } } public boolean equals(Object other) { if (this == other) { return true; } if (!(other instanceof MethodMapTransactionAttributeSource)) { return false; } MethodMapTransactionAttributeSource otherTas = (MethodMapTransactionAttributeSource) other; return ObjectUtils.nullSafeEquals(this.methodMap, otherTas.methodMap); } public int hashCode() { return MethodMapTransactionAttributeSource.class.hashCode(); } public String toString() { return getClass().getName() + ": " + this.methodMap; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy