public Object invoke(MethodInvocation invocation) throws Throwable {
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 invocation.proceed();
} finally {
//reset the previous class loader