Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,9 @@ public void destroy(T instance, CreationalContext<T> creationalContext) {
} catch (Exception e) {
BeanLogger.LOG.errorDestroying(instance, this);
BeanLogger.LOG.catchingDebug(e);
} finally {
if (getDeclaringBean().isDependent()) {
creationalContext.release();
}
}
if (creationalContext != null) {
creationalContext.release();
}
}

Expand Down
15 changes: 11 additions & 4 deletions impl/src/main/java/org/jboss/weld/bean/ManagedBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,19 +194,26 @@ public T create(CreationalContext<T> creationalContext) {
@Override
public void destroy(T instance, CreationalContext<T> creationalContext) {
super.destroy(instance, creationalContext);
InjectionTarget<T> injectionTarget = getProducer();
try {
InjectionTarget<T> injectionTarget = getProducer();
injectionTarget.preDestroy(instance);
} catch (Exception e) {
BeanLogger.LOG.errorDestroying(instance, this);
BeanLogger.LOG.catchingDebug(e);
}
try {
injectionTarget.dispose(instance);
} catch (Exception e) {
BeanLogger.LOG.errorDestroying(instance, this);
BeanLogger.LOG.catchingDebug(e);
}
if (creationalContext != null) {
// WELD-1010 hack?
if (creationalContext instanceof CreationalContextImpl) {
((CreationalContextImpl<T>) creationalContext).release(this, instance);
} else {
creationalContext.release();
}
} catch (Exception e) {
BeanLogger.LOG.errorDestroying(instance, this);
BeanLogger.LOG.catchingDebug(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public static class BeanAttributesBuilder<T> {
private boolean alternative;
private boolean reserve;
private boolean eager;
private boolean autoClose;
private String name;
private Set<Annotation> qualifiers;
private Set<Type> types;
Expand All @@ -83,6 +84,7 @@ public BeanAttributesBuilder(EnhancedAnnotated<T, ?> annotated, Set<Type> types,
initAlternative(annotated);
initReserve(annotated);
initEager(annotated);
initAutoClose(annotated);
initName(annotated);
initQualifiers(annotated);
initScope(annotated);
Expand All @@ -109,6 +111,10 @@ protected void initEager(EnhancedAnnotated<T, ?> annotated) {
this.eager = Beans.isEager(annotated, mergedStereotypes);
}

protected void initAutoClose(EnhancedAnnotated<T, ?> annotated) {
this.autoClose = Beans.isAutoClose(annotated, mergedStereotypes);
}

/**
* Initializes the name
*/
Expand Down Expand Up @@ -246,8 +252,8 @@ protected boolean initScopeFromStereotype() {
}

public BeanAttributes<T> build() {
return new ImmutableBeanAttributes<T>(mergedStereotypes.getStereotypes(), alternative, reserve, eager, name,
qualifiers, types, scope);
return new ImmutableBeanAttributes<T>(mergedStereotypes.getStereotypes(), alternative, reserve, eager,
autoClose, name, qualifiers, types, scope);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private ExternalBeanAttributesFactory() {
public static <T> BeanAttributes<T> of(BeanAttributes<T> source, BeanManager manager) {
validateBeanAttributes(source, manager);
BeanAttributes<T> attributes = new ImmutableBeanAttributes<T>(defensiveCopy(source.getStereotypes()),
source.isAlternative(), source.isReserve(), source.isEager(), source.getName(),
source.isAlternative(), source.isReserve(), source.isEager(), source.isAutoClose(), source.getName(),
defensiveCopy(source.getQualifiers()), defensiveCopy(source.getTypes()), source.getScope());
return attributes;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,30 @@ public class ImmutableBeanAttributes<T> implements BeanAttributes<T> {
private final boolean alternative;
private final boolean reserve;
private final boolean eager;
private final boolean autoClose;
private final String name;
private final Set<Annotation> qualifiers;
private final Set<Type> types;
private final Class<? extends Annotation> scope;

public ImmutableBeanAttributes(Set<Class<? extends Annotation>> stereotypes, boolean alternative, boolean reserve,
String name, Set<Annotation> qualifiers, Set<Type> types, Class<? extends Annotation> scope) {
this(stereotypes, alternative, reserve, false, name, qualifiers, types, scope);
this(stereotypes, alternative, reserve, false, false, name, qualifiers, types, scope);
}

public ImmutableBeanAttributes(Set<Class<? extends Annotation>> stereotypes, boolean alternative, boolean reserve,
boolean eager, String name, Set<Annotation> qualifiers, Set<Type> types, Class<? extends Annotation> scope) {
this(stereotypes, alternative, reserve, eager, false, name, qualifiers, types, scope);
}

public ImmutableBeanAttributes(Set<Class<? extends Annotation>> stereotypes, boolean alternative, boolean reserve,
boolean eager, boolean autoClose, String name, Set<Annotation> qualifiers, Set<Type> types,
Class<? extends Annotation> scope) {
this.stereotypes = stereotypes;
this.alternative = alternative;
this.reserve = reserve;
this.eager = eager;
this.autoClose = autoClose;
this.name = name;
this.qualifiers = qualifiers;
this.types = types;
Expand All @@ -63,8 +71,8 @@ public ImmutableBeanAttributes(Set<Class<? extends Annotation>> stereotypes, boo
* Utility constructor used for overriding Bean qualifiers and name for specialization purposes.
*/
public ImmutableBeanAttributes(Set<Annotation> qualifiers, String name, BeanAttributes<T> attributes) {
this(attributes.getStereotypes(), attributes.isAlternative(), attributes.isReserve(), attributes.isEager(), name,
qualifiers, attributes.getTypes(), attributes.getScope());
this(attributes.getStereotypes(), attributes.isAlternative(), attributes.isReserve(), attributes.isEager(),
attributes.isAutoClose(), name, qualifiers, attributes.getTypes(), attributes.getScope());
}

@Override
Expand All @@ -87,6 +95,11 @@ public boolean isEager() {
return eager;
}

@Override
public boolean isAutoClose() {
return autoClose;
}

@Override
public String getName() {
return name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ public class BeanAttributesConfiguratorImpl<T> implements BeanAttributesConfigur

private boolean isEager;

private boolean isAutoClose;

public BeanAttributesConfiguratorImpl(BeanManagerImpl beanManager) {
this.beanManager = beanManager;
this.qualifiers = new HashSet<Annotation>();
Expand All @@ -94,7 +96,9 @@ public BeanAttributesConfigurator<T> read(BeanAttributes<?> beanAttributes) {
stereotypes(beanAttributes.getStereotypes());
types(beanAttributes.getTypes());
alternative(beanAttributes.isAlternative());
reserve(beanAttributes.isReserve());
eager(beanAttributes.isEager());
autoClose(beanAttributes.isAutoClose());
return this;
}

Expand Down Expand Up @@ -236,9 +240,16 @@ public BeanAttributesConfigurator<T> eager(boolean value) {
return this;
}

@Override
public BeanAttributesConfigurator<T> autoClose(boolean value) {
this.isAutoClose = value;
return this;
}

@Override
public BeanAttributes<T> complete() {
return new ImmutableBeanAttributes<T>(ImmutableSet.copyOf(stereotypes), isAlternative, isReserve, isEager, name,
return new ImmutableBeanAttributes<T>(ImmutableSet.copyOf(stereotypes), isAlternative, isReserve, isEager, isAutoClose,
name,
Bindings.normalizeBeanQualifiers(qualifiers),
ImmutableSet.copyOf(types),
initScope());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import org.jboss.weld.logging.BeanLogger;
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.serialization.spi.BeanIdentifier;
import org.jboss.weld.util.Beans;
import org.jboss.weld.util.ForwardingWeldInstance;
import org.jboss.weld.util.bean.ForwardingBeanAttributes;
import org.jboss.weld.util.collections.ImmutableSet;
Expand Down Expand Up @@ -351,6 +352,12 @@ public BeanConfigurator<T> eager(boolean value) {
return this;
}

@Override
public BeanConfigurator<T> autoClose(boolean value) {
this.attributes.autoClose(value);
return this;
}

public Bean<T> complete() {
if (createCallback == null) {
// no callback specified, Weld does not know how to instantiate this new custom bean
Expand Down Expand Up @@ -546,6 +553,14 @@ public void destroy(T instance, CreationalContext<T> creationalContext) {
if (destroyCallback != null) {
destroyCallback.destroy(instance, creationalContext, beanManager);
}
if (isAutoClose() && instance instanceof AutoCloseable) {
try {
Beans.invokeAutoClose(instance);
} catch (Exception e) {
BeanLogger.LOG.errorDestroying(instance, this);
BeanLogger.LOG.catchingDebug(e);
}
}
// release dependent beans from create/destroy callbacks
if (creationalContext instanceof CreationalContextImpl) {
// release dependent instances linked with this bean but avoid double invocation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ protected <T> void addDependentInstance(T instance, Contextual<T> contextual, We
if (managedBean.getProducer() instanceof BasicInjectionTarget<?> injectionTarget) {
boolean hasPreDestroyMethods = injectionTarget.getLifecycleCallbackInvoker().hasPreDestroyMethods();
boolean hasPreDestroyInt = hasPreDestroyInterceptor(managedBean);
if (!hasPreDestroyMethods && !hasPreDestroyInt) {
// there is no @PreDestroy callback to call when destroying this dependent instance
if (!hasPreDestroyMethods && !hasPreDestroyInt && !managedBean.isAutoClose()) {
// there is no @PreDestroy callback or @AutoClose to handle when destroying this dependent instance
// therefore, we do not need to keep the reference
// Note that we need to account for @PreDestroy on the bean as well as interceptors
return;
Expand All @@ -96,8 +96,8 @@ protected <T> void addDependentInstance(T instance, Contextual<T> contextual, We
}
if (contextual instanceof AbstractProducerBean<?, ?, ?> producerBean) {
if (producerBean.getProducer() instanceof AbstractMemberProducer<?, ?> producer) {
if (producer.getDisposalMethod() == null) {
// there is no disposal method to call when destroying this dependent instance
if (producer.getDisposalMethod() == null && !producerBean.isAutoClose()) {
// there is no disposal method or @AutoClose to handle when destroying this dependent instance
// therefore, we do not need to keep the reference
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.jboss.weld.exceptions.DefinitionException;
import org.jboss.weld.logging.BeanLogger;
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.util.Beans;

/**
* Common functionality for {@link Producer}s backing producer fields and producer methods.
Expand Down Expand Up @@ -131,28 +132,42 @@ protected Object getReceiver(CreationalContext<?> productCreationalContext,

public void dispose(T instance) {
if (disposalMethod != null) {
// CreationalContext is only created if we need it to obtain the receiver
// MethodInvocationStrategy takes care of creating CC for parameters, if needed
if (disposalMethod.getAnnotated().isStatic()) {
disposalMethod.invokeDisposeMethod(null, instance, null);
} else {
WeldCreationalContext<X> ctx = null;
try {
Object receiver = ContextualInstance.getIfExists(getDeclaringBean(), getBeanManager());
if (receiver == null) {
ctx = getBeanManager().createCreationalContext(null);
// Create child CC so that a dependent reciever may be destroyed after the disposer method completes
receiver = ContextualInstance.get(getDeclaringBean(), getBeanManager(),
ctx.getCreationalContext(getDeclaringBean()));
}
if (receiver != null) {
disposalMethod.invokeDisposeMethod(receiver, instance, ctx);
}
} finally {
if (ctx != null) {
ctx.release();
try {
// CreationalContext is only created if we need it to obtain the receiver
// MethodInvocationStrategy takes care of creating CC for parameters, if needed
if (disposalMethod.getAnnotated().isStatic()) {
disposalMethod.invokeDisposeMethod(null, instance, null);
} else {
WeldCreationalContext<X> ctx = null;
try {
Object receiver = ContextualInstance.getIfExists(getDeclaringBean(), getBeanManager());
if (receiver == null) {
ctx = getBeanManager().createCreationalContext(null);
// Create child CC so that a dependent reciever may be destroyed after the disposer method completes
receiver = ContextualInstance.get(getDeclaringBean(), getBeanManager(),
ctx.getCreationalContext(getDeclaringBean()));
}
if (receiver != null) {
disposalMethod.invokeDisposeMethod(receiver, instance, ctx);
}
} finally {
if (ctx != null) {
ctx.release();
}
}
}
} catch (Exception e) {
BeanLogger.LOG.errorDestroying(instance, getBean());
BeanLogger.LOG.catchingDebug(e);
}
}
Bean<T> bean = getBean();
if (bean != null && bean.isAutoClose() && instance instanceof AutoCloseable) {
try {
Beans.invokeAutoClose(instance);
} catch (Exception e) {
BeanLogger.LOG.errorDestroying(instance, bean);
BeanLogger.LOG.catchingDebug(e);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.jboss.weld.logging.BeanLogger;
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.manager.api.WeldInjectionTarget;
import org.jboss.weld.util.Beans;
import org.jboss.weld.util.collections.ImmutableSet;
import org.jboss.weld.util.reflection.Reflections;

Expand Down Expand Up @@ -73,6 +74,7 @@ public static <T> BasicInjectionTarget<T> createNonCdiInterceptor(EnhancedAnnota
protected final BeanManagerImpl beanManager;
private final SlimAnnotatedType<T> type;
private final Set<InjectionPoint> injectionPoints;
private final Bean<T> bean;

// Instantiation
private Instantiator<T> instantiator;
Expand All @@ -87,6 +89,7 @@ protected BasicInjectionTarget(EnhancedAnnotatedType<T> type, Bean<T> bean, Bean
protected BasicInjectionTarget(EnhancedAnnotatedType<T> type, Bean<T> bean, BeanManagerImpl beanManager,
Injector<T> injector, LifecycleCallbackInvoker<T> invoker, Instantiator<T> instantiator) {
this.beanManager = beanManager;
this.bean = bean;
this.type = type.slim();
this.injector = injector;
this.invoker = invoker;
Expand Down Expand Up @@ -138,7 +141,14 @@ public void preDestroy(T instance) {

@Override
public void dispose(T instance) {
// No-op
if (bean != null && bean.isAutoClose() && instance instanceof AutoCloseable) {
try {
Beans.invokeAutoClose(instance);
} catch (Exception e) {
BeanLogger.LOG.errorDestroying(instance, bean);
BeanLogger.LOG.catchingDebug(e);
}
}
}

@Override
Expand Down Expand Up @@ -217,6 +227,6 @@ public String toString() {

@Override
public Bean<T> getBean() {
return null;
return bean;
}
}
Loading
Loading