001/*
002 * Copyright 2002-2012 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.transaction.interceptor;
018
019import java.beans.PropertyEditorSupport;
020import java.util.Enumeration;
021import java.util.Properties;
022
023import org.springframework.beans.propertyeditors.PropertiesEditor;
024import org.springframework.util.StringUtils;
025
026/**
027 * Property editor that converts a String into a {@link TransactionAttributeSource}.
028 * The transaction attribute string must be parseable by the
029 * {@link TransactionAttributeEditor} in this package.
030 *
031 * <p>Strings are in property syntax, with the form:<br>
032 * {@code FQCN.methodName=&lt;transaction attribute string&gt;}
033 *
034 * <p>For example:<br>
035 * {@code com.mycompany.mycode.MyClass.myMethod=PROPAGATION_MANDATORY,ISOLATION_DEFAULT}
036 *
037 * <p><b>NOTE:</b> The specified class must be the one where the methods are
038 * defined; in case of implementing an interface, the interface class name.
039 *
040 * <p>Note: Will register all overloaded methods for a given name.
041 * Does not support explicit registration of certain overloaded methods.
042 * Supports "xxx*" mappings, e.g. "notify*" for "notify" and "notifyAll".
043 *
044 * @author Rod Johnson
045 * @author Juergen Hoeller
046 * @since 26.04.2003
047 * @see TransactionAttributeEditor
048 */
049public class TransactionAttributeSourceEditor extends PropertyEditorSupport {
050
051        @Override
052        public void setAsText(String text) throws IllegalArgumentException {
053                MethodMapTransactionAttributeSource source = new MethodMapTransactionAttributeSource();
054                if (StringUtils.hasLength(text)) {
055                        // Use properties editor to tokenize the hold string.
056                        PropertiesEditor propertiesEditor = new PropertiesEditor();
057                        propertiesEditor.setAsText(text);
058                        Properties props = (Properties) propertiesEditor.getValue();
059
060                        // Now we have properties, process each one individually.
061                        TransactionAttributeEditor tae = new TransactionAttributeEditor();
062                        Enumeration<?> propNames = props.propertyNames();
063                        while (propNames.hasMoreElements()) {
064                                String name = (String) propNames.nextElement();
065                                String value = props.getProperty(name);
066                                // Convert value to a transaction attribute.
067                                tae.setAsText(value);
068                                TransactionAttribute attr = (TransactionAttribute) tae.getValue();
069                                // Register name and attribute.
070                                source.addTransactionalMethod(name, attr);
071                        }
072                }
073                setValue(source);
074        }
075
076}