Skip to content

Commit

Permalink
synchronized internal provider
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanenicolas committed May 7, 2016
1 parent b374d17 commit 0062453
Showing 1 changed file with 42 additions and 47 deletions.
89 changes: 42 additions & 47 deletions toothpick-runtime/src/main/java/toothpick/InternalProviderImpl.java
Expand Up @@ -46,65 +46,60 @@ public InternalProviderImpl(Class<?> factoryKeyClass, boolean isProviderFactoryC

//we lock on the unbound provider itself to prevent concurrent usage
//of the unbound provider (
public T get(Scope scope) {
lockGet.lock();
try {
if (instance != null) {
public synchronized T get(Scope scope) {
if (instance != null) {
return instance;
}

if (providerInstance != null) {
if (isLazy) {
instance = providerInstance.get();
//gc
providerInstance = null;
return instance;
}
return providerInstance.get();
}

if (providerInstance != null) {
if (isLazy) {
instance = providerInstance.get();
//gc
providerInstance = null;
return instance;
}
return providerInstance.get();
}
if (factoryClass != null && factory == null) {
factory = FactoryRegistryLocator.getFactory(factoryClass);
//gc
factoryClass = null;
}

if (factoryClass != null && factory == null) {
factory = FactoryRegistryLocator.getFactory(factoryClass);
//gc
factoryClass = null;
if (factory != null) {
if (!factory.hasScopeAnnotation()) {
return factory.createInstance(scope);
}
instance = factory.createInstance(scope);
//gc
factory = null;
return instance;
}

if (factory != null) {
if (!factory.hasScopeAnnotation()) {
return factory.createInstance(scope);
}
instance = factory.createInstance(scope);
if (providerFactoryClass != null && providerFactory == null) {
providerFactory = FactoryRegistryLocator.getFactory(providerFactoryClass);
//gc
providerFactoryClass = null;
}

if (providerFactory != null) {
if (providerFactory.hasScopeInstancesAnnotation()) {
instance = providerFactory.createInstance(scope).get();
//gc
factory = null;
providerFactory = null;
return instance;
}

if (providerFactoryClass != null && providerFactory == null) {
providerFactory = FactoryRegistryLocator.getFactory(providerFactoryClass);
if (providerFactory.hasScopeAnnotation()) {
providerInstance = providerFactory.createInstance(scope);
//gc
providerFactoryClass = null;
}

if (providerFactory != null) {
if (providerFactory.hasScopeInstancesAnnotation()) {
instance = providerFactory.createInstance(scope).get();
//gc
providerFactory = null;
return instance;
}
if (providerFactory.hasScopeAnnotation()) {
providerInstance = providerFactory.createInstance(scope);
//gc
providerFactory = null;
return providerInstance.get();
}

return providerFactory.createInstance(scope).get();
providerFactory = null;
return providerInstance.get();
}

throw new IllegalStateException("A provider can only be used with an instance, a provider, a factory or a provider factory.");
} finally {
lockGet.unlock();
return providerFactory.createInstance(scope).get();
}

throw new IllegalStateException("A provider can only be used with an instance, a provider, a factory or a provider factory.");
}
}

0 comments on commit 0062453

Please sign in to comment.