/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package com.liferay.portal.spring.transaction;
import java.lang.reflect.Method;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.interceptor.TransactionAttribute;
import org.springframework.transaction.interceptor.TransactionAttributeSource;
/**
* @author Shuyang Zhou
*/
public class TransactionInterceptor implements MethodInterceptor {
public TransactionAttributeSource getTransactionAttributeSource() {
return transactionAttributeSource;
}
@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
Method method = methodInvocation.getMethod();
Class<?> targetClass = null;
Object targetBean = methodInvocation.getThis();
if (targetBean != null) {
targetClass = targetBean.getClass();
}
TransactionAttribute transactionAttribute =
transactionAttributeSource.getTransactionAttribute(
method, targetClass);
if (transactionAttribute == null) {
return methodInvocation.proceed();
}
TransactionAttributeAdapter transactionAttributeAdapter =
new TransactionAttributeAdapter(transactionAttribute);
return transactionExecutor.execute(
platformTransactionManager, transactionAttributeAdapter,
methodInvocation);
}
public void setPlatformTransactionManager(
PlatformTransactionManager platformTransactionManager) {
this.platformTransactionManager = platformTransactionManager;
}
public void setTransactionAttributeSource(
TransactionAttributeSource transactionAttributeSource) {
this.transactionAttributeSource = transactionAttributeSource;
}
public void setTransactionExecutor(
TransactionExecutor transactionExecutor) {
this.transactionExecutor = transactionExecutor;
}
protected PlatformTransactionManager platformTransactionManager;
protected TransactionAttributeSource transactionAttributeSource;
protected TransactionExecutor transactionExecutor;
}