final Method method = invocation.getMethod();
final Object[] arguments = invocation.getArguments();
final boolean setCCCL = setContextClassLoader;
ServiceRegistryEntry serviceReference = targetSource.getServiceRegistryReference();
int retriesUsed = 0;
while (serviceReference == null && retriesUsed < retryCount) {
try {
Thread.sleep(retryInterval);
}
catch (InterruptedException e) {
}
serviceReference = targetSource.getServiceRegistryReference();
retriesUsed++;
}
if (serviceReference != null) {
final Thread currentThread;
final ClassLoader existingClassLoader;
if (setCCCL) {
currentThread = Thread.currentThread();
existingClassLoader= currentThread.getContextClassLoader();
} else {
currentThread = null;
existingClassLoader = null;
}
try {
if (setCCCL) {
currentThread.setContextClassLoader(serviceReference.getBeanClassLoader());
}
return AopUtils.invokeJoinpointUsingReflection(serviceReference.getServiceBeanReference().getService(), method, arguments);
} finally {
//reset the previous class loader
if (setCCCL) {
Thread.currentThread().setContextClassLoader(existingClassLoader);