001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.spring.transaction;
016    
017    import org.aopalliance.intercept.MethodInvocation;
018    
019    import org.springframework.transaction.TransactionStatus;
020    import org.springframework.transaction.interceptor.TransactionAttribute;
021    import org.springframework.transaction.support.TransactionCallback;
022    
023    /**
024     * @author Shuyang Zhou
025     */
026    public class CounterCallbackPreferringTransactionExecutor
027            extends CallbackPreferringTransactionExecutor {
028    
029            @Override
030            protected TransactionCallback<Object> createTransactionCallback(
031                    TransactionAttribute transactionAttribute,
032                    MethodInvocation methodInvocation) {
033    
034                    return new CounterCallbackPreferringTransactionCallback(
035                            transactionAttribute, methodInvocation);
036            }
037    
038            private class CounterCallbackPreferringTransactionCallback
039                    implements TransactionCallback<Object> {
040    
041                    @Override
042                    public Object doInTransaction(TransactionStatus transactionStatus) {
043                            try {
044                                    return _methodInvocation.proceed();
045                            }
046                            catch (Throwable throwable) {
047                                    if (_transactionAttribute.rollbackOn(throwable)) {
048                                            if (throwable instanceof RuntimeException) {
049                                                    throw (RuntimeException)throwable;
050                                            }
051                                            else {
052                                                    throw new ThrowableHolderException(throwable);
053                                            }
054                                    }
055                                    else {
056                                            return new ThrowableHolder(throwable);
057                                    }
058                            }
059                    }
060    
061                    private CounterCallbackPreferringTransactionCallback(
062                            TransactionAttribute transactionAttribute,
063                            MethodInvocation methodInvocation) {
064    
065                            _transactionAttribute = transactionAttribute;
066                            _methodInvocation = methodInvocation;
067                    }
068    
069                    private final MethodInvocation _methodInvocation;
070                    private final TransactionAttribute _transactionAttribute;
071    
072            }
073    
074    }