001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.spring.transaction;
016    
017    import com.liferay.portal.kernel.annotation.AnnotationLocator;
018    import com.liferay.portal.kernel.transaction.Transactional;
019    import com.liferay.portal.kernel.util.MethodTargetClassKey;
020    
021    import java.lang.reflect.Method;
022    
023    import java.util.Map;
024    import java.util.concurrent.ConcurrentHashMap;
025    
026    import org.springframework.transaction.interceptor.DefaultTransactionAttribute;
027    import org.springframework.transaction.interceptor.TransactionAttribute;
028    import org.springframework.transaction.interceptor.TransactionAttributeSource;
029    
030    /**
031     * @author Shuyang Zhou
032     */
033    public class AnnotationTransactionAttributeSource
034            implements TransactionAttributeSource {
035    
036            @Override
037            public TransactionAttribute getTransactionAttribute(
038                    Method method, Class<?> targetClass) {
039    
040                    MethodTargetClassKey methodTargetClassKey = new MethodTargetClassKey(
041                            method, targetClass);
042    
043                    TransactionAttribute transactionAttribute = _transactionAttributes.get(
044                            methodTargetClassKey);
045    
046                    if (transactionAttribute != null) {
047                            if (transactionAttribute == _nullTransactionAttribute) {
048                                    return null;
049                            }
050                            else {
051                                    return transactionAttribute;
052                            }
053                    }
054    
055                    Transactional transactional = AnnotationLocator.locate(
056                            method, targetClass, Transactional.class);
057    
058                    transactionAttribute = TransactionAttributeBuilder.build(transactional);
059    
060                    if (transactionAttribute == null) {
061                            _transactionAttributes.put(
062                                    methodTargetClassKey, _nullTransactionAttribute);
063                    }
064                    else {
065                            _transactionAttributes.put(
066                                    methodTargetClassKey, transactionAttribute);
067                    }
068    
069                    return transactionAttribute;
070            }
071    
072            private static TransactionAttribute _nullTransactionAttribute =
073                    new DefaultTransactionAttribute();
074    
075            private Map<MethodTargetClassKey, TransactionAttribute>
076                    _transactionAttributes =
077                            new ConcurrentHashMap<MethodTargetClassKey, TransactionAttribute>();
078    
079    }