Skip to content

Commit

Permalink
add (disabled) support for disposer methods, WELDX-124
Browse files Browse the repository at this point in the history
  • Loading branch information
pmuir committed Jul 27, 2010
1 parent 8a249e8 commit 5529209
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
Expand Up @@ -78,6 +78,9 @@ class GenericBeanExtension implements Extension
// A map of generic configuration types to producer methods on generic beans
// Used to track the generic bean found
private final Map<Class<? extends Annotation>, Map<AnnotatedMethod<?>, Bean<?>>> genericBeanProducerMethods;

// A map of producer methods on generic beans to their disposer method (if existent)
private final Map<AnnotatedMethod<?>, AnnotatedMethod<?>> genericBeanDisposerMethods;

// A map of generic configuration types to producer fields on generic beans
// Used to track the generic bean found
Expand All @@ -103,6 +106,7 @@ class GenericBeanExtension implements Extension
{
this.genericBeans = new HashMap<Class<? extends Annotation>, Map<AnnotatedType<?>, Bean<?>>>();
this.genericBeanProducerMethods = new HashMap<Class<? extends Annotation>, Map<AnnotatedMethod<?>, Bean<?>>>();
this.genericBeanDisposerMethods = new HashMap<AnnotatedMethod<?>, AnnotatedMethod<?>>();
this.genericBeanProducerFields = new HashMap<Class<? extends Annotation>, Map<AnnotatedField<?>, Bean<?>>>();
this.genericInjectionTargets = new HashMap<Class<? extends Annotation>, Map<AnnotatedType<?>, InjectionTarget<?>>>();
this.genericProducers = new HashMap<Class<? extends Annotation>, Map<Annotation, AnnotatedMember<?>>>();
Expand Down Expand Up @@ -187,6 +191,12 @@ <T, X> void registerGenericBeanProducerMethod(@Observes ProcessProducerMethod<T,
beans.put(method, event.getBean());
genericBeanProducerMethods.put(genericConfigurationType, beans);
}
// Only register a disposer method if it exists
// Blocked by WELD-572
// if (event.getAnnotatedDisposedParameter() instanceof AnnotatedMethod<?>)
// {
// disposerMethods.put(method, (AnnotatedMethod<?>) event.getAnnotatedDisposedParameter());
// }
}
}

Expand Down Expand Up @@ -405,6 +415,7 @@ private <T> Bean<T> createGenericProducerBean(final BeanManager beanManager, fin
Set<Annotation> qualifiers = getQualifiers(genericProducers.get(genericConfiguration.annotationType()).get(genericConfiguration).getAnnotations(), beanManager);

// TODO make this passivation capable?
// TODO Make this wrap the existing bean, replacing the type and qualifiers only
BeanBuilder<T> builder = new BeanBuilder<T>(beanManager).setJavaClass(genericBeanType).setQualifiers(qualifiers).setBeanLifecycle(new BeanLifecycle<T>()
{

Expand Down Expand Up @@ -440,7 +451,7 @@ private <X> Bean<X> createGenericBean(Bean<X> originalBean, Annotation genericCo
private <X, T> Bean<T> createGenericProducerMethod(Bean<T> originalBean, Annotation genericConfiguration, AnnotatedMethod<X> method, BeanManager beanManager)
{
Set<Annotation> qualifiers = getQualifiers(genericProducers.get(genericConfiguration.annotationType()).get(genericConfiguration).getAnnotations(), originalBean, beanManager);
return new GenericProducerMethod<T, X>(originalBean, genericConfiguration, method, qualifiers, syntheticProvider, beanManager);
return new GenericProducerMethod<T, X>(originalBean, genericConfiguration, method, (AnnotatedMethod<X>) genericBeanDisposerMethods.get(method), qualifiers, syntheticProvider, beanManager);
}

private <X, T> Bean<T> createGenericProducerField(Bean<T> originalBean, Annotation genericConfiguration, AnnotatedField<X> field, BeanManager beanManager)
Expand Down
Expand Up @@ -16,14 +16,23 @@
class GenericProducerMethod<T, X> extends AbstactGenericBean<T>
{

private final InjectableMethod<X> method;
private final InjectableMethod<X> producerMethod;
private final InjectableMethod<X> disposerMethod;
private final Type declaringBeanType;
private final Annotation declaringBeanQualifier;

GenericProducerMethod(Bean<T> originalBean, Annotation genericConfiguration, AnnotatedMethod<X> method, Set<Annotation> qualifiers, Synthetic.Provider syntheticProvider, BeanManager beanManager)
GenericProducerMethod(Bean<T> originalBean, Annotation genericConfiguration, AnnotatedMethod<X> method, AnnotatedMethod<X> disposerMethod, Set<Annotation> qualifiers, Synthetic.Provider syntheticProvider, BeanManager beanManager)
{
super(originalBean, qualifiers, beanManager);
this.method = new InjectableMethod<X>(method, this, beanManager);
super(originalBean, qualifiers, beanManager);
this.producerMethod = new InjectableMethod<X>(method, this, beanManager);
if (disposerMethod != null)
{
this.disposerMethod = new InjectableMethod<X>(disposerMethod, this, beanManager);
}
else
{
this.disposerMethod = null;
}
this.declaringBeanType = originalBean.getBeanClass();
this.declaringBeanQualifier = syntheticProvider.get(genericConfiguration);
}
Expand All @@ -33,9 +42,7 @@ public T create(CreationalContext<T> creationalContext)
{
try
{
Bean<?> declaringBean = getBeanManager().resolve(getBeanManager().getBeans(declaringBeanType, declaringBeanQualifier));
Object receiver = getBeanManager().getReference(declaringBean, declaringBean.getBeanClass(), creationalContext);
return method.invoke(receiver, creationalContext);
return producerMethod.invoke(getReceiver(creationalContext), creationalContext);
}
finally
{
Expand All @@ -44,4 +51,27 @@ public T create(CreationalContext<T> creationalContext)
}
}

@Override
public void destroy(T instance, CreationalContext<T> creationalContext)
{
if (disposerMethod != null)
{
try
{
disposerMethod.invoke(getReceiver(creationalContext), creationalContext);
}
finally
{
// Generic managed beans must be dependent
creationalContext.release();
}
}
}

private Object getReceiver(CreationalContext<T> creationalContext)
{
Bean<?> declaringBean = getBeanManager().resolve(getBeanManager().getBeans(declaringBeanType, declaringBeanQualifier));
return getBeanManager().getReference(declaringBean, declaringBean.getBeanClass(), creationalContext);
}

}

0 comments on commit 5529209

Please sign in to comment.