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 java.lang.reflect.AccessibleObject;
018    import java.lang.reflect.Method;
019    
020    import java.util.concurrent.Callable;
021    
022    import org.aopalliance.intercept.MethodInvocation;
023    
024    import org.springframework.transaction.PlatformTransactionManager;
025    import org.springframework.transaction.interceptor.TransactionAttribute;
026    
027    /**
028     * @author Shuyang Zhou
029     */
030    public class TransactionalCallableUtil {
031    
032            public static <T> T call(
033                            TransactionAttribute transactionAttribute, Callable<T> callable)
034                    throws Throwable {
035    
036                    return (T)_transactionExecutor.execute(
037                            _platformTransactionManager, transactionAttribute,
038                            new CallableMethodInvocation(callable));
039            }
040    
041            public void setPlatformTransactionManager(
042                    PlatformTransactionManager platformTransactionManager) {
043    
044                    _platformTransactionManager = platformTransactionManager;
045            }
046    
047            public void setTransactionExecutor(
048                    TransactionExecutor transactionExecutor) {
049    
050                    _transactionExecutor = transactionExecutor;
051            }
052    
053            private static PlatformTransactionManager _platformTransactionManager;
054            private static TransactionExecutor _transactionExecutor;
055    
056            private static class CallableMethodInvocation implements MethodInvocation {
057    
058                    private CallableMethodInvocation(Callable<?> callable) {
059                            _callable = callable;
060                    }
061    
062                    @Override
063                    public Object[] getArguments() {
064                            throw new UnsupportedOperationException();
065                    }
066    
067                    @Override
068                    public Method getMethod() {
069                            throw new UnsupportedOperationException();
070                    }
071    
072                    @Override
073                    public AccessibleObject getStaticPart() {
074                            throw new UnsupportedOperationException();
075                    }
076    
077                    @Override
078                    public Object getThis() {
079                            throw new UnsupportedOperationException();
080                    }
081    
082                    @Override
083                    public Object proceed() throws Throwable {
084                            return _callable.call();
085                    }
086    
087                    private Callable<?> _callable;
088    
089            }
090    
091    }