Skip to content

Commit

Permalink
#281: revert changes to @eager as it caused deadlock on @startup
Browse files Browse the repository at this point in the history
  • Loading branch information
Bauke Scholtz committed Aug 22, 2016
1 parent fb66460 commit 46ea477
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
5 changes: 3 additions & 2 deletions src/main/java/org/omnifaces/ApplicationListener.java
Expand Up @@ -26,6 +26,7 @@
import javax.servlet.ServletContextEvent;
import javax.servlet.annotation.WebListener;

import org.omnifaces.cdi.Eager;
import org.omnifaces.cdi.GraphicImageBean;
import org.omnifaces.cdi.eager.EagerBeansRepository;
import org.omnifaces.cdi.eager.EagerBeansWebListener;
Expand All @@ -45,7 +46,7 @@
* <li>Check if JSF 2.2 is available, otherwise log and fail.
* <li>Check if CDI 1.1 is available, otherwise log and fail.
* <li>Load {@link Cache} provider and register its filter if necessary.
* <li>Register {@link EagerBeansWebListener} if necessary.
* <li>Instantiate {@link Eager} application scoed beans and register {@link EagerBeansWebListener} if necessary.
* <li>Add {@link FacesViews} mappings to FacesServlet if necessary.
* <li>Register {@link GraphicImageBean} beans in {@link GraphicResource}.
* <li>Register {@link Socket} endpoint if necessary.
Expand Down Expand Up @@ -82,7 +83,7 @@ public void contextInitialized(ServletContextEvent event) {
try {
ServletContext servletContext = event.getServletContext();
CacheInitializer.loadProviderAndRegisterFilter(servletContext);
EagerBeansRepository.registerListenerIfNecessary(servletContext);
EagerBeansRepository.instantiateApplicationScopedAndRegisterListenerIfNecessary(servletContext);
FacesViews.addFacesServletMappings(servletContext);
GraphicResource.registerGraphicImageBeans();
Socket.registerEndpointIfNecessary(servletContext);
Expand Down
20 changes: 15 additions & 5 deletions src/main/java/org/omnifaces/cdi/eager/EagerBeansRepository.java
Expand Up @@ -16,6 +16,7 @@
package org.omnifaces.cdi.eager;

import static java.lang.String.format;
import static java.util.logging.Level.WARNING;
import static org.omnifaces.util.Beans.getReference;
import static org.omnifaces.util.Utils.isAnyEmpty;
import static org.omnifaces.util.Utils.isEmpty;
Expand Down Expand Up @@ -52,6 +53,9 @@ public class EagerBeansRepository {
private static final String MISSING_VIEW_ID =
"Bean '%s' was annotated with @Eager, but required attribute 'viewId' is missing."
+ " Bean will not be eagerly instantiated.";
private static final String WARNING_POSSIBLY_APPLICATION_SCOPE_NOT_ACTIVE =
"Could not instantiate eager application scoped beans. Possibly the CDI application scope is not active."
+ " This is known to be the case in certain Tomcat and Jetty based configurations.";

private static volatile EagerBeansRepository instance;

Expand All @@ -69,14 +73,20 @@ public static EagerBeansRepository getInstance() { // Awkward workaround for it

protected void setEagerBeans(EagerBeans eagerBeans) {
this.eagerBeans = eagerBeans;
}

if (hasAnyApplicationScopedBeans()) {
instantiateApplicationScoped();
public static void instantiateApplicationScopedAndRegisterListenerIfNecessary(ServletContext servletContext) {
if (getInstance() != null && instance.hasAnyApplicationScopedBeans()) {
try {
instance.instantiateApplicationScoped();
}
catch (Exception e) {
logger.log(WARNING, format(WARNING_POSSIBLY_APPLICATION_SCOPE_NOT_ACTIVE), e);
instance = null; // Trigger to add listeners anyway as it may be available at later point.
}
}
}

public static void registerListenerIfNecessary(ServletContext servletContext) {
if (getInstance().hasAnySessionOrRequestURIBeans()) {
if (instance == null || instance.hasAnySessionOrRequestURIBeans()) {
servletContext.addListener(EagerBeansWebListener.class);
}
}
Expand Down
Expand Up @@ -31,8 +31,8 @@
/**
* <p>
* A web listener that instantiates eager session beans and request/view beans by request URI. This is auto-registered
* by {@link EagerBeansRepository#registerListenerIfNecessary(javax.servlet.ServletContext)} when any eager session
* beans or request/view beans by request URI are available.
* by {@link EagerBeansRepository#instantiateApplicationScopedAndRegisterListenerIfNecessary(javax.servlet.ServletContext)}
* when any eager session beans or request/view beans by request URI are available.
*
* @since 2.0
* @author Arjan Tijms
Expand Down

0 comments on commit 46ea477

Please sign in to comment.