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 com.liferay.portal.kernel.transaction.TransactionConfig;
018    import com.liferay.portal.kernel.transaction.TransactionInvoker;
019    
020    import java.lang.reflect.AccessibleObject;
021    import java.lang.reflect.Method;
022    
023    import java.util.concurrent.Callable;
024    
025    import org.aopalliance.intercept.MethodInvocation;
026    
027    import org.springframework.transaction.PlatformTransactionManager;
028    
029    /**
030     * @author Shuyang Zhou
031     */
032    public class TransactionInvokerImpl implements TransactionInvoker {
033    
034            @Override
035            public <T> T invoke(
036                            TransactionConfig transactionConfig, Callable<T> callable)
037                    throws Throwable {
038    
039                    return (T)_transactionExecutor.execute(
040                            _platformTransactionManager,
041                            new TransactionAttributeAdapter(
042                                    TransactionAttributeBuilder.build(
043                                            true, transactionConfig.getIsolation(),
044                                            transactionConfig.getPropagation(),
045                                            transactionConfig.isReadOnly(),
046                                            transactionConfig.getTimeout(),
047                                            transactionConfig.getRollbackForClasses(),
048                                            transactionConfig.getRollbackForClassNames(),
049                                            transactionConfig.getNoRollbackForClasses(),
050                                            transactionConfig.getNoRollbackForClassNames())),
051                            new CallableMethodInvocation(callable));
052            }
053    
054            public void setPlatformTransactionManager(
055                    PlatformTransactionManager platformTransactionManager) {
056    
057                    _platformTransactionManager = platformTransactionManager;
058            }
059    
060            public void setTransactionExecutor(
061                    TransactionExecutor transactionExecutor) {
062    
063                    _transactionExecutor = transactionExecutor;
064            }
065    
066            private static PlatformTransactionManager _platformTransactionManager;
067            private static TransactionExecutor _transactionExecutor;
068    
069            private static class CallableMethodInvocation implements MethodInvocation {
070    
071                    @Override
072                    public Object[] getArguments() {
073                            throw new UnsupportedOperationException();
074                    }
075    
076                    @Override
077                    public Method getMethod() {
078                            throw new UnsupportedOperationException();
079                    }
080    
081                    @Override
082                    public AccessibleObject getStaticPart() {
083                            throw new UnsupportedOperationException();
084                    }
085    
086                    @Override
087                    public Object getThis() {
088                            throw new UnsupportedOperationException();
089                    }
090    
091                    @Override
092                    public Object proceed() throws Throwable {
093                            return _callable.call();
094                    }
095    
096                    private CallableMethodInvocation(Callable<?> callable) {
097                            _callable = callable;
098                    }
099    
100                    private final Callable<?> _callable;
101    
102            }
103    
104    }