avoid hard-coded AOP dependency for ScopedObject check

git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@2687 50f2f4bb-b051-0410-bef5-90022cba6387
This commit is contained in:
Juergen Hoeller 2009-12-15 21:20:51 +00:00
parent 53a71fe962
commit 5b59b9a0dc
1 changed files with 29 additions and 6 deletions

View File

@ -21,9 +21,10 @@ import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.scope.ScopedObject;
import org.springframework.core.InfrastructureProxy;
import org.springframework.util.Assert;
import org.springframework.aop.scope.ScopedObject;
import org.springframework.util.ClassUtils;
/**
* Utility methods for triggering specific {@link TransactionSynchronization}
@ -38,6 +39,9 @@ public abstract class TransactionSynchronizationUtils {
private static final Log logger = LogFactory.getLog(TransactionSynchronizationUtils.class);
private static final boolean aopAvailable = ClassUtils.isPresent(
"org.springframework.aop.scope.ScopedObject", TransactionSynchronizationUtils.class.getClassLoader());
/**
* Check whether the given resource transaction managers refers to the given
@ -57,12 +61,15 @@ public abstract class TransactionSynchronizationUtils {
static Object unwrapResourceIfNecessary(Object resource) {
Assert.notNull(resource, "Resource must not be null");
Object resourceRef = resource;
if (resource instanceof ScopedObject) {
// First unwrap a scoped proxy.
resourceRef = ((ScopedObject) resource).getTargetObject();
// unwrap infrastructure proxy
if (resourceRef instanceof InfrastructureProxy) {
resourceRef = ((InfrastructureProxy) resourceRef).getWrappedObject();
}
// Now unwrap infrastructure proxy
return (resourceRef instanceof InfrastructureProxy ? ((InfrastructureProxy) resourceRef).getWrappedObject() : resourceRef);
if (aopAvailable) {
// now unwrap scoped proxy
resourceRef = ScopedProxyUnwrapper.unwrapIfNecessary(resource);
}
return resourceRef;
}
@ -167,4 +174,20 @@ public abstract class TransactionSynchronizationUtils {
}
}
/**
* Inner class to avoid hard-coded dependency on AOP module.
*/
private static class ScopedProxyUnwrapper {
public static Object unwrapIfNecessary(Object resource) {
if (resource instanceof ScopedObject) {
return ((ScopedObject) resource).getTargetObject();
}
else {
return resource;
}
}
}
}