users@jersey.java.net

[Jersey] Re: Weld + Jersey + Tomcat?

From: Paul Sandoz <Paul.Sandoz_at_oracle.com>
Date: Wed, 26 Jan 2011 17:59:42 +0100

Hi,

Thanks for sending a sample.

You need to override the configure method or do the work before the
super. initiate method is called.

This code works for me:

public class TomcatCDIServlet extends ServletContainer {
         static {
              
System
.setProperty
("com.sun.jersey.server.impl.cdi.lookupExtensionInBeanManager", "true");
         }

        private static final long serialVersionUID = 1L;

        private static final Logger LOG =
Logger.getLogger(TomcatCDIServlet.class);

         @Override
         protected void configure(WebConfig wc, ResourceConfig rc,
WebApplication wa) {
             super.configure(wc, rc, wa);
                InitialContext ic = InitialContextHelper.getInitialContext();
                if (ic == null)
                        return;

                try {
                        Object beanManager = ic.lookup("java:comp/env/BeanManager");
                        if (beanManager == null) {
                                LOG.info("The CDI BeanManager is not available. JAX-RS CDI
support is disabled");
                                return;
                        }

                        rc.getSingletons().add(new
CDIComponentProviderFactory(beanManager,rc,wa));
                        LOG.info("JAX-RS CDI support is enabled");
                } catch (NamingException e) {
                        LOG.log(Level.WARNING, "The CDI BeanManager is not available. JAX-
RS CDI support is disabled.", e);
                }
         }
}

Paul.

On Jan 26, 2011, at 11:07 AM, Paul Sandoz wrote:

> Hi,
>
> I cannot explain why that does not work for you. It seems that JNDI
> lookup is working, so it should work in the Jersey code too.
>
> Can you package things up as a maven app and send to the list and i
> can investigate further?
>
> The "injection of null" (which likely means no injection is
> occurring) seems to imply that CDI is not managing the class.
>
> Paul.
>
> On Jan 25, 2011, at 8:08 PM, aaron_at_acsiri.com wrote:
>
>> So I overrode ServletContainer with the following code and changed
>> web.xml to use it:
>>
>> public class TomcatCDIServlet extends ServletContainer {
>>
>> private static final long serialVersionUID = 1L;
>>
>> private static final Logger LOG =
>> Logger.getLogger(TomcatCDIServlet.class);
>>
>> @Override
>> protected void initiate(ResourceConfig rc, WebApplication wa) {
>> super.initiate(rc, wa);
>>
>> InitialContext ic =
>> InitialContextHelper.getInitialContext();
>> if (ic == null)
>> return;
>>
>> try {
>> Object beanManager =
>> ic.lookup("java:comp/env/BeanManager");
>> if (beanManager == null) {
>> LOG.info("The CDI BeanManager is not
>> available. JAX-RS CDI support is disabled");
>> return;
>> }
>>
>> rc.getSingletons().add(new
>> CDIComponentProviderFactory(beanManager,rc,wa));
>> LOG.info("JAX-RS CDI support is enabled");
>> } catch (NamingException e) {
>> LOG.log(Level.WARNING, "The CDI BeanManager is
>> not available. JAX-RS CDI support is disabled.", e);
>> }
>>
>> }
>>
>> }
>>
>> It now reports "JAX-RS CDI support is enabled" in the log but my bean
>> is still @Injecting null.
>>
>> -Aaron
>