001
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.TransactionStatus;
026 import org.springframework.transaction.interceptor.TransactionAttribute;
027
028
031 public class TransactionHandlerUtil {
032
033 public static void commit(
034 TransactionAttribute transactionAttribute,
035 TransactionStatus transactionStatus) {
036
037 if (_transactionHandler == null) {
038 throw new IllegalStateException(
039 _platformTransactionManager + " does not support " +
040 "programmatic transaction handling");
041 }
042
043 _transactionHandler.commit(
044 _platformTransactionManager, transactionAttribute,
045 transactionStatus);
046 }
047
048 public static <T> T invoke(
049 TransactionAttribute transactionAttribute, Callable<T> callable)
050 throws Throwable {
051
052 return (T)_transactionExecutor.execute(
053 _platformTransactionManager, transactionAttribute,
054 new CallableMethodInvocation(callable));
055 }
056
057 public static void rollback(
058 Throwable throwable, TransactionAttribute transactionAttribute,
059 TransactionStatus transactionStatus)
060 throws Throwable {
061
062 if (_transactionHandler == null) {
063 throw new IllegalStateException(
064 _platformTransactionManager + " does not support " +
065 "programmatic transaction handling");
066 }
067
068 _transactionHandler.rollback(
069 _platformTransactionManager, throwable, transactionAttribute,
070 transactionStatus);
071 }
072
073 public static TransactionStatus start(
074 TransactionAttribute transactionAttribute) {
075
076 if (_transactionHandler == null) {
077 throw new IllegalStateException(
078 _platformTransactionManager + " does not support " +
079 "programmatic transaction handling");
080 }
081
082 return _transactionHandler.start(
083 _platformTransactionManager, transactionAttribute);
084 }
085
086 public void setPlatformTransactionManager(
087 PlatformTransactionManager platformTransactionManager) {
088
089 _platformTransactionManager = platformTransactionManager;
090 }
091
092 public void setTransactionExecutor(
093 TransactionExecutor transactionExecutor) {
094
095 _transactionExecutor = transactionExecutor;
096
097 if (transactionExecutor instanceof TransactionHandler) {
098 _transactionHandler = (TransactionHandler)transactionExecutor;
099 }
100 }
101
102 private static PlatformTransactionManager _platformTransactionManager;
103 private static TransactionExecutor _transactionExecutor;
104 private static TransactionHandler _transactionHandler;
105
106 private static class CallableMethodInvocation implements MethodInvocation {
107
108 @Override
109 public Object[] getArguments() {
110 throw new UnsupportedOperationException();
111 }
112
113 @Override
114 public Method getMethod() {
115 throw new UnsupportedOperationException();
116 }
117
118 @Override
119 public AccessibleObject getStaticPart() {
120 throw new UnsupportedOperationException();
121 }
122
123 @Override
124 public Object getThis() {
125 throw new UnsupportedOperationException();
126 }
127
128 @Override
129 public Object proceed() throws Throwable {
130 return _callable.call();
131 }
132
133 private CallableMethodInvocation(Callable<?> callable) {
134 _callable = callable;
135 }
136
137 private final Callable<?> _callable;
138
139 }
140
141 }