Skip to content

Commit

Permalink
Refactoring infra for collecting dependent instances -> DependentCont…
Browse files Browse the repository at this point in the history
…ext.

git-svn-id: http://anonsvn.jboss.org/repos/weld/ri/trunk@1200 1c488680-804c-0410-94cd-c6b725194a0e
  • Loading branch information
nickarls committed Jan 23, 2009
1 parent e8c2680 commit ff7f8d5
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 105 deletions.
Expand Up @@ -37,6 +37,7 @@
import javax.webbeans.manager.Bean;

import org.jboss.webbeans.ManagerImpl;
import org.jboss.webbeans.context.DependentInstancesStore;
import org.jboss.webbeans.injection.InjectionPointImpl;
import org.jboss.webbeans.introspector.AnnotatedItem;
import org.jboss.webbeans.introspector.AnnotationStore.AnnotationMap;
Expand All @@ -62,6 +63,7 @@ public abstract class AbstractBean<T, E> extends Bean<T>
private static Set<Class<?>> STANDARD_WEB_BEAN_CLASSES = new HashSet<Class<?>>(Arrays.asList(Event.class, ManagerImpl.class));

private boolean proxyable;
protected DependentInstancesStore dependentInstancesStore;

/**
* Helper class for getting deployment type
Expand Down Expand Up @@ -121,6 +123,7 @@ public AbstractBean(ManagerImpl manager)
super(manager);
this.manager = manager;
annotatedInjectionPoints = new HashSet<AnnotatedItem<?, ?>>();
dependentInstancesStore = new DependentInstancesStore();
}

/**
Expand Down Expand Up @@ -184,7 +187,7 @@ protected void initBindingTypes()
* Initializes the deployment types
*/
protected abstract void initDeploymentType();

protected void initDeploymentTypeFromStereotype()
{
AnnotationMap possibleDeploymentTypes = getMergedStereotypes().getPossibleDeploymentTypes();
Expand Down Expand Up @@ -229,14 +232,14 @@ else if (isSpecializing())
this.name = getSpecializedBean().getName();
return;
}

if (beanNameDefaulted || getMergedStereotypes().isBeanNameDefaulted())
{
this.name = getDefaultName();
return;
}
}

protected void initProxyable()
{
proxyable = Beans.apiTypesAreProxyable(getTypes());
Expand Down Expand Up @@ -270,7 +273,7 @@ protected boolean injectionPointsAreSerializable()
* Initializes the scope type
*/
protected abstract void initScopeType();

protected boolean initScopeTypeFromStereotype()
{
Set<Annotation> possibleScopeTypes = getMergedStereotypes().getPossibleScopeTypes();
Expand Down Expand Up @@ -309,7 +312,7 @@ else if (deploymentType.equals(Standard.class) && !STANDARD_WEB_BEAN_CLASSES.con
throw new DefinitionException(getAnnotatedItem().getName() + " cannot have deployment type @Standard");
}
}

/**
* Validates that the required types are implemented
*/
Expand Down Expand Up @@ -558,7 +561,16 @@ public boolean isProxyable()
{
return proxyable;
}

public DependentInstancesStore getDependentInstancesStore()
{
return dependentInstancesStore;
}

public boolean isDependent()
{
return Dependent.class.equals(scopeType);
}

public boolean isSpecializing()
{
return getAnnotatedItem().isAnnotationPresent(Specializes.class);
Expand Down
Expand Up @@ -61,7 +61,6 @@ public abstract class AbstractClassBean<T> extends AbstractBean<T, Class<T>>
private Set<AnnotatedField<?>> injectableFields;
// The initializer methods
private Set<AnnotatedMethod<?>> initializerMethods;
protected DependentInstancesStore dependentInstancesStore;

/**
* Constructor
Expand All @@ -73,7 +72,6 @@ protected AbstractClassBean(AnnotatedClass<T> type, ManagerImpl manager)
{
super(manager);
this.annotatedItem = type;
this.dependentInstancesStore = new DependentInstancesStore();
}

/**
Expand Down Expand Up @@ -269,7 +267,7 @@ protected void checkBeanImplementation()
throw new DefinitionException("Web Bean implementation class " + type + " cannot be declared abstract");
}
}

@Override
protected void preCheckSpecialization()
{
Expand Down Expand Up @@ -346,11 +344,6 @@ protected Class<? extends Annotation> getDefaultDeploymentType()
return Production.class;
}

public DependentInstancesStore getDependentInstancesStore()
{
return dependentInstancesStore;
}

@Override
public boolean equals(Object other)
{
Expand Down
Expand Up @@ -53,7 +53,7 @@ public abstract class AbstractProducerBean<T, S> extends AbstractBean<T, S>
{
// The declaring bean
protected AbstractClassBean<?> declaringBean;

private static final LogProvider log = Logging.getLogProvider(AbstractProducerBean.class);

/**
Expand Down Expand Up @@ -162,8 +162,7 @@ protected void init()
*/
protected void checkReturnValue(T instance)
{
boolean dependent = Dependent.class.equals(getScopeType());
if (instance == null && !dependent)
if (instance == null && !isDependent())
{
throw new IllegalProductException("Cannot return null from a non-dependent producer method");
}
Expand All @@ -177,7 +176,7 @@ protected void checkReturnValue(T instance)
{
return;
}
if (dependent && Beans.isPassivatingBean(injectionPoint.getBean()))
if (isDependent() && Beans.isPassivatingBean(injectionPoint.getBean()))
{
if (injectionPoint.isField())
{
Expand Down Expand Up @@ -208,7 +207,7 @@ else if (injectionPoint.isConstructor())
}
}
}

@Override
protected void initScopeType()
{
Expand All @@ -223,16 +222,16 @@ protected void initScopeType()
log.trace("Scope " + scopeType + " specified by annotation");
return;
}

initScopeTypeFromStereotype();

if (this.scopeType == null)
{
this.scopeType = Dependent.class;
log.trace("Using default @Dependent scope");
}
}

@Override
protected void initDeploymentType()
{
Expand Down Expand Up @@ -276,15 +275,25 @@ protected Object getReceiver()
@Override
public T create()
{
Object dependentCollector = new Object();
try
{
if (getDeclaringBean().isDependent())
{
DependentContext.INSTANCE.setCurrentInjectionInstance(dependentCollector);
}
DependentContext.INSTANCE.setActive(true);
T instance = produceInstance();
checkReturnValue(instance);
return instance;
}
finally
{
if (getDeclaringBean().isDependent())
{
DependentContext.INSTANCE.clearCurrentInjectionInstance(dependentCollector);
dependentInstancesStore.destroyDependentInstances(dependentCollector);
}
DependentContext.INSTANCE.setActive(false);
}
}
Expand Down
Expand Up @@ -309,6 +309,7 @@ public void postConstruct(T instance)
try
{
manager.getInjectionPointProvider().pushBean(this);
DependentContext.INSTANCE.setCurrentInjectionInstance(instance);
DependentContext.INSTANCE.setActive(true);
bindDecorators();
bindInterceptors();
Expand All @@ -318,6 +319,7 @@ public void postConstruct(T instance)
}
finally
{
DependentContext.INSTANCE.clearCurrentInjectionInstance(instance);
manager.getInjectionPointProvider().popBean();
DependentContext.INSTANCE.setActive(false);
}
Expand All @@ -341,7 +343,7 @@ public InternalEjbDescriptor<T> getEjbDescriptor()

public boolean canCallRemoveMethods()
{
return getEjbDescriptor().isStateful() && Dependent.class.equals(getScopeType());
return getEjbDescriptor().isStateful() && isDependent();
}

@Override
Expand Down
Expand Up @@ -124,7 +124,7 @@ public T create()
try
{
instance = constructor.newInstance(manager);
injectionPointProvider.setCurrentInjectionInstance(instance);
DependentContext.INSTANCE.setCurrentInjectionInstance(instance);
bindDecorators();
bindInterceptors();
injectEjbAndCommonFields(instance);
Expand All @@ -134,7 +134,7 @@ public T create()
}
finally
{
injectionPointProvider.clearCurrentInjectionInstance(instance);
DependentContext.INSTANCE.clearCurrentInjectionInstance(instance);
injectionPointProvider.popBean();
}
return instance;
Expand Down Expand Up @@ -504,8 +504,7 @@ public String toString()
@Override
public boolean isSerializable()
{
boolean dependent = Dependent.class.equals(getScopeType());
if (dependent)
if (isDependent())
{
return Reflections.isSerializable(getType());
}
Expand Down
@@ -1,7 +1,5 @@
package org.jboss.webbeans.context;

import javax.webbeans.Dependent;
import javax.webbeans.manager.Bean;
import javax.webbeans.manager.Contextual;

public class ContextualInstance<T>
Expand All @@ -25,13 +23,4 @@ public void destroy()
contextual.destroy(instance);
}

public boolean isDependent()
{
return contextual instanceof Bean && Dependent.class.equals(((Bean<T>)contextual).getScopeType());
}

public Object getInstance()
{
return instance;
}
}
Expand Up @@ -23,9 +23,7 @@
import javax.webbeans.Dependent;
import javax.webbeans.manager.Contextual;

import org.jboss.webbeans.CurrentManager;
import org.jboss.webbeans.bean.AbstractClassBean;
import org.jboss.webbeans.injection.InjectionPointProvider;

/**
* The dependent context
Expand All @@ -38,6 +36,8 @@ public class DependentContext extends AbstractContext
public static DependentContext INSTANCE = new DependentContext();

private ThreadLocal<AtomicInteger> reentrantActiveCount;
private ThreadLocal<Object> currentInjectionInstance;


/**
* Constructor
Expand All @@ -54,6 +54,7 @@ protected AtomicInteger initialValue()
return new AtomicInteger(0);
}
};
this.currentInjectionInstance = new ThreadLocal<Object>();
}

/**
Expand All @@ -69,11 +70,10 @@ public <T> T get(Contextual<T> bean, boolean create)
throw new ContextNotActiveException();
}
T instance = create == false ? null : bean.create();
InjectionPointProvider injectionPointProvider = CurrentManager.rootManager().getInjectionPointProvider();
if (bean instanceof AbstractClassBean && injectionPointProvider.isInjecting())
if (bean instanceof AbstractClassBean && (currentInjectionInstance.get() != null))
{
DependentInstancesStore dependentInstancesStore = ((AbstractClassBean<?>) bean).getDependentInstancesStore();
dependentInstancesStore.addDependentInstance(injectionPointProvider.getCurrentInjectionInstance(), ContextualInstance.of(bean, instance));
dependentInstancesStore.addDependentInstance(currentInjectionInstance.get(), ContextualInstance.of(bean, instance));
}
return instance;
}
Expand Down Expand Up @@ -103,5 +103,21 @@ public void setActive(boolean active)
}
}
}

public void setCurrentInjectionInstance(Object currentInjectionInstance)
{
if (this.currentInjectionInstance.get() == null)
{
this.currentInjectionInstance.set(currentInjectionInstance);
}
}

public void clearCurrentInjectionInstance(Object instance)
{
if (this.currentInjectionInstance.get() == instance)
{
this.currentInjectionInstance.set(null);
}
}

}
Expand Up @@ -25,7 +25,6 @@

import org.jboss.webbeans.CurrentManager;
import org.jboss.webbeans.context.DependentContext;
import org.jboss.webbeans.injection.InjectionPointProvider;

public class WebBeansELResolver extends ELResolver
{
Expand Down Expand Up @@ -67,11 +66,10 @@ public Object getValue(ELContext context, Object base, Object property)
{
// TODO Any other way than creating a dummy parent to collect the created dependent objects under?
Object dependentsCollector = new Object();
InjectionPointProvider injectionPointProvider = CurrentManager.rootManager().getInjectionPointProvider();
try
{
DependentContext.INSTANCE.setActive(true);
injectionPointProvider.setCurrentInjectionInstance(dependentsCollector);
DependentContext.INSTANCE.setCurrentInjectionInstance(dependentsCollector);
Object value = CurrentManager.rootManager().getInstanceByName(property.toString());
if (value != null)
{
Expand All @@ -81,7 +79,7 @@ public Object getValue(ELContext context, Object base, Object property)
}
finally
{
injectionPointProvider.clearCurrentInjectionInstance(dependentsCollector);
DependentContext.INSTANCE.clearCurrentInjectionInstance(dependentsCollector);
DependentContext.INSTANCE.setActive(false);
}
}
Expand Down

0 comments on commit ff7f8d5

Please sign in to comment.