mavenjsfjettycdiweld

Jetty Maven plugin (jetty:run goal) + Weld CDI + JSF 2.2 = NPE on ViewScoped beans


Using the following (pretty up to date) components in the web application:

Jetty maven plugin 9.2.6.v20141205

Weld CDI 2.2.9.Final

Mojarra JSF 2.2.10

The app's web.xml contains the following listeners:

org.jboss.weld.environment.servlet.BeanManagerResourceBindingListener
com.sun.faces.config.ConfigureListener

The first listener registers BeanManager with JNDI. The second listener initializes JSF and looks up for BeanManager in the JNDI.

This setup works perfectly ok when jetty maven plugin runs with "jetty:run-war" goal or when the application is running as standalone with embedded jetty.

However when running it with "jetty:run" goal the BeanManager registered in the first listener is not visible in the JSF resulting in NPE's on the ViewScoped beans.

This seems to me like a scope or visibility issue but I can't figure that out - what is the difference between jetty:run and jetty:run-war.

I have tried adding "resource-env-ref" entries in the web.xml and creating the BeanManager in various Jetty's xml files (jetty.xml, jetty-env.xml, jetty-context.xml) but it didn't change anything.

UPDATE:

NPE occurs upon session invalidation; as there is no BeanManager registered in the JSF the ViewScoped beans cannot be destroyed. See below stack trace:

Caused by: java.lang.NullPointerException at com.sun.faces.application.view.ViewScopeContextManager.destroyBeans(ViewScopeContextManager.java:171) at com.sun.faces.application.view.ViewScopeContextManager.sessionDestroyed(ViewScopeContextManager.java:339) at com.sun.faces.application.view.ViewScopeManager.sessionDestroyed(ViewScopeManager.java:369) at com.sun.faces.application.WebappLifecycleListener.sessionDestroyed(WebappLifecycleListener.java:181) at com.sun.faces.config.ConfigureListener.sessionDestroyed(ConfigureListener.java:399) at org.eclipse.jetty.server.session.AbstractSessionManager.removeSession(AbstractSessionManager.java:772) at org.eclipse.jetty.server.session.AbstractSession.invalidate(AbstractSession.java:326) at com.sun.faces.context.ExternalContextImpl.invalidateSession(ExternalContextImpl.java:783)


Solution

  • Dimitri raised a bug on the jetty project: https://bugs.eclipse.org/bugs/show_bug.cgi?id=462179

    The bug contains a full description of the cause of the problem. In short, the way Weld makes the BeanManager available to JSF does not work with unassembled webapps (which is the case with mvn jetty:run), only war files (ie mvn jetty:run-war will work).

    Jan