{
if (!injected)
{
if (injecting)
{
throw new CyclicDependencyException();
}
injecting = true;
try
{
component.inject(invocation.getTarget(), enforceRequired);
}
finally
{
injecting = false;
}
injected = true;
}
counter++;
}
finally
{
lock.unlock();
}
Object result = invocation.proceed();
lock.lock();
try
{
counter--;
if (counter == 0)
{
try
{
component.outject( invocation.getTarget(), enforceRequired );
}
finally
{
// Avoid an extra lock by disinjecting here instead of the finally block
if (injected)
{
injected = false;
component.disinject( invocation.getTarget() );
}
}
}
}
finally
{
lock.unlock();
}
return result;
}
catch (Exception e)
{
Exception root = e;
while (Exceptions.getCause(root) != null)
{
root = Exceptions.getCause(root);
}
if (root instanceof CyclicDependencyException)
{
CyclicDependencyException cyclicDependencyException = (CyclicDependencyException) root;
cyclicDependencyException.addInvocation(getComponent().getName(), invocation.getMethod());
}
throw e;
}
finally
{