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 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    }