0

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)

dimitri
  • 874
  • 8
  • 11
  • Please could you add the NPE to your posting? – DiZzZz Mar 10 '15 at 11:56
  • I've updated the question with a stack trace. Apparently it is caused by the absent BeanManager instance inside the JSF. – dimitri Mar 11 '15 at 13:00
  • 1
    Hi Dimitri, I'm also very much interested in an **embedded** Jetty with JSF and CDI (preferably latest versions), but after spending many hours I still could not get it working. You wrote you have this scenario working (I do not care much for the `mvn jetty:run` scenario, I just want to have my application deployed as lightweight as possible, hence Jetty). Could you somehow **share your project**? – Oscar Mar 12 '15 at 11:21

2 Answers2

0

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

Jan
  • 1,287
  • 7
  • 7
0

Just add this listener with your web.xml. I used openwebbeans as a CDI container but seems it should work with any other constainers.

package com.devadmin.computec.config;

import com.sun.faces.RIConstants;

import javax.enterprise.inject.spi.CDI;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class CustomServletContextListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        sce.getServletContext().setAttribute(RIConstants.CDI_BEAN_MANAGER, CDI.current().getBeanManager());
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
    }
}