Skip to content

Commit

Permalink
WELD-2269 Implement BeforeDestroyed
Browse files Browse the repository at this point in the history
- replace Initialized/Destroyed literals by those from CDI API
- introduce ContainerBeforeShutdown; enhance SE test to include @BeforeDestroyed; fix firing @BeforeDestroyed for non-web modules.
- upgrade Arquillian Weld container to 2.0.0.Beta4
- add test exclude (bug in TCK)
  • Loading branch information
manovotn authored and mkouba committed Dec 14, 2016
1 parent 4be337d commit 767043d
Show file tree
Hide file tree
Showing 17 changed files with 160 additions and 170 deletions.
Expand Up @@ -20,6 +20,9 @@
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import javax.enterprise.context.BeforeDestroyed;
import javax.enterprise.context.Destroyed;
import javax.enterprise.context.Initialized;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.event.Event;
import javax.enterprise.inject.Instance;
Expand All @@ -36,12 +39,11 @@
import org.jboss.weld.bootstrap.spi.Deployment;
import org.jboss.weld.environment.ContainerInstance;
import org.jboss.weld.environment.deployment.WeldDeployment;
import org.jboss.weld.environment.se.events.ContainerBeforeShutdown;
import org.jboss.weld.environment.se.events.ContainerInitialized;
import org.jboss.weld.environment.se.events.ContainerShutdown;
import org.jboss.weld.environment.se.logging.WeldSELogger;
import org.jboss.weld.inject.WeldInstance;
import org.jboss.weld.literal.DestroyedLiteral;
import org.jboss.weld.literal.InitializedLiteral;
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.util.collections.ImmutableList;

Expand Down Expand Up @@ -207,7 +209,7 @@ private void complete() {
this.creationalContext = beanManager().createCreationalContext(null);
this.instance = beanManager().getInstance(creationalContext);
this.event = beanManager().event();
beanManager().fireEvent(new ContainerInitialized(id), InitializedLiteral.APPLICATION);
beanManager().fireEvent(new ContainerInitialized(id), Initialized.Literal.APPLICATION);
WeldSELogger.LOG.weldContainerInitialized(id);
}

Expand Down Expand Up @@ -260,11 +262,12 @@ public BeanManager getBeanManager() {
public synchronized void shutdown() {
checkIsRunning();
try {
beanManager().fireEvent(new ContainerShutdown(id), DestroyedLiteral.APPLICATION);
beanManager().fireEvent(new ContainerBeforeShutdown(id), BeforeDestroyed.Literal.APPLICATION);
} finally {
discard(id);
// Destroy all the dependent beans correctly
creationalContext.release();
beanManager().fireEvent(new ContainerShutdown(id), Destroyed.Literal.APPLICATION);
bootstrap.shutdown();
WeldSELogger.LOG.weldContainerShutdown(id);
}
Expand Down
@@ -0,0 +1,30 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2016, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.weld.environment.se.events;

/**
* An event that is fired when the Weld SE container is about to be shut down.
*
* @author <a href="mailto:manovotn@redhat.com">Matej Novotny</a>
*/
public class ContainerBeforeShutdown extends WeldContainerEvent {

public ContainerBeforeShutdown(String containerId) {
super(containerId);
}

}
Expand Up @@ -17,7 +17,7 @@
package org.jboss.weld.environment.se.events;

/**
* An event that is fired when the Weld SE container is about to be shut down.
* An event that is fired after Weld SE container shutdown.
*
* @author Martin Kouba
*/
Expand Down
Expand Up @@ -26,6 +26,7 @@

import org.jboss.weld.environment.se.Weld;
import org.jboss.weld.environment.se.WeldContainer;
import org.jboss.weld.environment.se.events.ContainerBeforeShutdown;
import org.jboss.weld.environment.se.events.ContainerInitialized;
import org.jboss.weld.environment.se.events.ContainerShutdown;
import org.jboss.weld.test.util.ActionSequence;
Expand All @@ -45,16 +46,19 @@ public void testEventsFired() {
assertFalse(container.select(ContainerObserver.class).isUnsatisfied());
}
List<String> sequenceData = ActionSequence.getSequenceData();
assertEquals(4, sequenceData.size());
assertEquals(6, sequenceData.size());
assertTrue(sequenceData.contains(ContainerInitialized.class.getName() + id));
assertTrue(sequenceData.contains(ContainerInitialized.class.getName() + ApplicationScoped.class.getName() + id));
assertTrue(sequenceData.contains(ContainerBeforeShutdown.class.getName() + id));
assertTrue(sequenceData.contains(ContainerBeforeShutdown.class.getName() + ApplicationScoped.class.getName() + id));
assertTrue(sequenceData.contains(ContainerShutdown.class.getName() + id));
assertTrue(sequenceData.contains(ContainerShutdown.class.getName() + ApplicationScoped.class.getName() + id));

sequenceData = ActionSequence.getSequenceData(ApplicationScoped.class.getName());
assertEquals(2, sequenceData.size());
assertEquals(3, sequenceData.size());
assertTrue(sequenceData.get(0).equals(ContainerInitialized.class.getName() + ApplicationScoped.class.getName()));
assertTrue(sequenceData.get(1).equals(ContainerShutdown.class.getName() + ApplicationScoped.class.getName()));
assertTrue(sequenceData.get(1).equals(ContainerBeforeShutdown.class.getName() + ApplicationScoped.class.getName()));
assertTrue(sequenceData.get(2).equals(ContainerShutdown.class.getName() + ApplicationScoped.class.getName()));
}

}
Expand Up @@ -17,10 +17,12 @@
package org.jboss.weld.environment.se.test.container.events;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.BeforeDestroyed;
import javax.enterprise.context.Destroyed;
import javax.enterprise.context.Initialized;
import javax.enterprise.event.Observes;

import org.jboss.weld.environment.se.events.ContainerBeforeShutdown;
import org.jboss.weld.environment.se.events.ContainerInitialized;
import org.jboss.weld.environment.se.events.ContainerShutdown;
import org.jboss.weld.test.util.ActionSequence;
Expand All @@ -36,6 +38,10 @@ public void onAppScopeDestroy(@Observes @Destroyed(ApplicationScoped.class) Obje
ActionSequence.addAction(ApplicationScoped.class.getName(), event.getClass().getName() + ApplicationScoped.class.getName());
}

public void onAppScopeBeforeDestroy(@Observes @BeforeDestroyed(ApplicationScoped.class) Object event) {
ActionSequence.addAction(ApplicationScoped.class.getName(), event.getClass().getName() + ApplicationScoped.class.getName());
}

public void onContainerInitWithQualifier(@Observes @Initialized(ApplicationScoped.class) ContainerInitialized event) {
ActionSequence.addAction(event.getClass().getName() + ApplicationScoped.class.getName() + event.getContainerId());
}
Expand All @@ -52,4 +58,12 @@ public void onContainerShutdown(@Observes ContainerShutdown event) {
ActionSequence.addAction(event.getClass().getName() + event.getContainerId());
}

public void onContainerBeforeShutdown(@Observes ContainerBeforeShutdown event) {
ActionSequence.addAction(event.getClass().getName() + event.getContainerId());
}

public void onContainerBeforeShutdownWithQualifier(@Observes @BeforeDestroyed(ApplicationScoped.class) ContainerBeforeShutdown event) {
ActionSequence.addAction(event.getClass().getName() + ApplicationScoped.class.getName() + event.getContainerId());
}

}
40 changes: 27 additions & 13 deletions impl/src/main/java/org/jboss/weld/bootstrap/WeldRuntime.java
Expand Up @@ -16,16 +16,20 @@
*/
package org.jboss.weld.bootstrap;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.concurrent.ConcurrentMap;

import javax.enterprise.context.BeforeDestroyed;
import javax.enterprise.context.Destroyed;

import org.jboss.weld.Container;
import org.jboss.weld.ContainerState;
import org.jboss.weld.bootstrap.events.BeforeShutdownImpl;
import org.jboss.weld.bootstrap.spi.BeanDeploymentArchive;
import org.jboss.weld.context.ApplicationContext;
import org.jboss.weld.context.SingletonContext;
import org.jboss.weld.event.ContextEvent;
import org.jboss.weld.literal.DestroyedLiteral;
import org.jboss.weld.manager.BeanManagerImpl;

/**
Expand All @@ -51,32 +55,42 @@ public BeanManagerImpl getManager(BeanDeploymentArchive beanDeploymentArchive) {

public void shutdown() {
try {
// First, the container must destroy all contexts.
// The container must destroy all contexts.
// For non-web modules, fire @BeforeDestroyed event
fireEventForNonWebModules(Object.class, ContextEvent.APPLICATION_BEFORE_DESTROYED, BeforeDestroyed.Literal.APPLICATION);
deploymentManager.instance().select(ApplicationContext.class).get().invalidate();
deploymentManager.instance().select(SingletonContext.class).get().invalidate();

} finally {
// fire @Destroyed(ApplicationScope.class) for non-web modules
fireEventForNonWebModules(Object.class, ContextEvent.APPLICATION_DESTROYED, Destroyed.Literal.APPLICATION);
try {
// Finally, the container must fire an event of type BeforeShutdown.
BeforeShutdownImpl.fire(deploymentManager);
} finally {
Container container = Container.instance(contextId);
container.setState(ContainerState.SHUTDOWN);
container.cleanup();
}
}
}

/**
* Fires given event for non-web modules. Used for @BeforeDestroyed and @Destroyed events.
*/
private void fireEventForNonWebModules(Type eventType, Object event, Annotation... qualifiers) {
try {
BeanDeploymentModules modules = deploymentManager.getServices().get(BeanDeploymentModules.class);
if (modules != null) {
// fire @Destroyed(ApplicationScoped.class) for non-web modules
// fire event for non-web modules
// web modules are handled by HttpContextLifecycle
for (BeanDeploymentModule module : modules) {
if (!module.isWebModule()) {
module.fireEvent(Object.class, ContextEvent.APPLICATION_DESTROYED, DestroyedLiteral.APPLICATION);
module.fireEvent(eventType, event, qualifiers);
}
}
}
} catch (Exception ignored) {
}
try {
// Finally, the container must fire an event of type BeforeShutdown.
BeforeShutdownImpl.fire(deploymentManager);
} finally {
Container container = Container.instance(contextId);
container.setState(ContainerState.SHUTDOWN);
container.cleanup();
}
}
}
}
4 changes: 2 additions & 2 deletions impl/src/main/java/org/jboss/weld/bootstrap/WeldStartup.java
Expand Up @@ -28,6 +28,7 @@
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.ConversationScoped;
import javax.enterprise.context.Dependent;
import javax.enterprise.context.Initialized;
import javax.enterprise.context.NormalScope;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.context.SessionScoped;
Expand Down Expand Up @@ -98,7 +99,6 @@
import org.jboss.weld.injection.CurrentInjectionPoint;
import org.jboss.weld.injection.ResourceInjectionFactory;
import org.jboss.weld.injection.producer.InjectionTargetService;
import org.jboss.weld.literal.InitializedLiteral;
import org.jboss.weld.logging.BootstrapLogger;
import org.jboss.weld.logging.VersionLogger;
import org.jboss.weld.manager.BeanManagerImpl;
Expand Down Expand Up @@ -522,7 +522,7 @@ public void endInitialization() {
// web modules are handled by HttpContextLifecycle
for (BeanDeploymentModule module : modules) {
if (!module.isWebModule()) {
module.fireEvent(Object.class, ContextEvent.APPLICATION_INITIALIZED, InitializedLiteral.APPLICATION);
module.fireEvent(Object.class, ContextEvent.APPLICATION_INITIALIZED, Initialized.Literal.APPLICATION);
}
}
}
Expand Down
Expand Up @@ -38,7 +38,9 @@
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;

import javax.enterprise.context.BeforeDestroyed;
import javax.enterprise.context.ConversationScoped;
import javax.enterprise.context.Destroyed;

import org.jboss.weld.Container;
import org.jboss.weld.bootstrap.api.ServiceRegistry;
Expand All @@ -51,7 +53,6 @@
import org.jboss.weld.context.conversation.ConversationIdGenerator;
import org.jboss.weld.context.conversation.ConversationImpl;
import org.jboss.weld.event.FastEvent;
import org.jboss.weld.literal.DestroyedLiteral;
import org.jboss.weld.logging.ConversationLogger;
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.serialization.BeanIdentifierIndex;
Expand Down Expand Up @@ -83,10 +84,16 @@ public abstract class AbstractConversationContext<R, S> extends AbstractBoundCon
private final BeanManagerImpl manager;

private final BeanIdentifierIndex beanIdentifierIndex;
private final LazyValueHolder<FastEvent<String>> conversationBeforeDestroyedEvent = new LazyValueHolder<FastEvent<String>>() {
@Override
protected FastEvent<String> computeValue() {
return FastEvent.of(String.class, manager, manager.getGlobalLenientObserverNotifier(), BeforeDestroyed.Literal.CONVERSATION);
}
};
private final LazyValueHolder<FastEvent<String>> conversationDestroyedEvent = new LazyValueHolder<FastEvent<String>>() {
@Override
protected FastEvent<String> computeValue() {
return FastEvent.of(String.class, manager, manager.getGlobalLenientObserverNotifier(), DestroyedLiteral.CONVERSATION);
return FastEvent.of(String.class, manager, manager.getGlobalLenientObserverNotifier(), Destroyed.Literal.CONVERSATION);
}
};

Expand Down Expand Up @@ -392,6 +399,7 @@ private void setDestructionQueue(Map<String, ManagedConversation> conversations,

protected void destroyConversation(S session, String id) {
if (session != null) {
conversationBeforeDestroyedEvent.get().fire(id);
setBeanStore(createSessionBeanStore(new ConversationNamingScheme(getNamingSchemePrefix(), id, beanIdentifierIndex), session));
getBeanStore().attach();
destroy();
Expand Down
Expand Up @@ -17,14 +17,14 @@
package org.jboss.weld.context.activator;

import javax.enterprise.context.BeforeDestroyed;
import javax.enterprise.context.Destroyed;
import javax.enterprise.context.Initialized;
import javax.enterprise.context.RequestScoped;
import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;

import org.jboss.weld.context.RequestContext;
import org.jboss.weld.event.FastEvent;
import org.jboss.weld.literal.DestroyedLiteral;
import org.jboss.weld.literal.InitializedLiteral;
import org.jboss.weld.manager.BeanManagerImpl;

/**
Expand All @@ -44,9 +44,9 @@ public abstract class AbstractActivateRequestContextInterceptor {
public AbstractActivateRequestContextInterceptor(RequestContext requestContext, BeanManagerImpl beanManager) {
this.beanManager = beanManager;
this.requestContext = requestContext;
fastEventInit = FastEvent.of(Object.class, beanManager, InitializedLiteral.REQUEST);
fastEventInit = FastEvent.of(Object.class, beanManager, Initialized.Literal.REQUEST);
fastEventBeforeDestroyed = FastEvent.of(Object.class, beanManager, BeforeDestroyed.Literal.REQUEST);
fastEventDestroyed = FastEvent.of(Object.class, beanManager, DestroyedLiteral.REQUEST);
fastEventDestroyed = FastEvent.of(Object.class, beanManager, Destroyed.Literal.REQUEST);
}

@AroundInvoke
Expand Down
2 changes: 2 additions & 0 deletions impl/src/main/java/org/jboss/weld/event/ContextEvent.java
Expand Up @@ -33,8 +33,10 @@ public final class ContextEvent implements Serializable {
private static final long serialVersionUID = -1197351184144276424L;

public static final ContextEvent APPLICATION_INITIALIZED = new ContextEvent("Application context initialized.");
public static final ContextEvent APPLICATION_BEFORE_DESTROYED = new ContextEvent("Application context is about to be destroyed.");
public static final ContextEvent APPLICATION_DESTROYED = new ContextEvent("Application context destroyed.");
public static final Object REQUEST_INITIALIZED_EJB = new ContextEvent("Request context initialized for EJB invocation");
public static final Object REQUEST_BEFORE_DESTROYED_EJB = new ContextEvent("Request context is about to be destroyed after EJB invocation");
public static final Object REQUEST_DESTROYED_EJB = new ContextEvent("Request context destroyed after EJB invocation");

private final String message;
Expand Down
54 changes: 0 additions & 54 deletions impl/src/main/java/org/jboss/weld/literal/DestroyedLiteral.java

This file was deleted.

0 comments on commit 767043d

Please sign in to comment.