001    /**
002     * Copyright (c) 2000-2013 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                    private CounterCallbackPreferringTransactionCallback(
042                            TransactionAttribute transactionAttribute,
043                            MethodInvocation methodInvocation) {
044    
045                            _transactionAttribute = transactionAttribute;
046                            _methodInvocation = methodInvocation;
047                    }
048    
049                    @Override
050                    public Object doInTransaction(TransactionStatus transactionStatus) {
051                            try {
052                                    return _methodInvocation.proceed();
053                            }
054                            catch (Throwable throwable) {
055                                    if (_transactionAttribute.rollbackOn(throwable)) {
056                                            if (throwable instanceof RuntimeException) {
057                                                    throw (RuntimeException)throwable;
058                                            }
059                                            else {
060                                                    throw new ThrowableHolderException(throwable);
061                                            }
062                                    }
063                                    else {
064                                            return new ThrowableHolder(throwable);
065                                    }
066                            }
067                    }
068    
069                    private MethodInvocation _methodInvocation;
070                    private TransactionAttribute _transactionAttribute;
071    
072            }
073    
074    }