users@jersey.java.net

RE: [Jersey] Resource injection

From: Turner, George <george.turner_at_lmco.com>
Date: Fri, 05 Jun 2009 14:12:41 -0600

Thanks Paul,

 

Mark Rabick responded with a nick piece of code using a Provider class
that was pretty close and I have included the modified code for other
user if they wish it. The only trick of this code is that it requires a
web.xml entry because the lookup for a local EJB doesn't seem to work
without it.

 

Web.xml -------

  <ejb-local-ref>

    <ejb-ref-name>ejb/ParticipantDataService</ejb-ref-name>

    <ejb-ref-type>Session</ejb-ref-type>

 
<local>com.lmco.energyc2.seeload.participantsvc.ejb.ParticipantDataServi
ce</local>

  </ejb-local-ref>

 

EJBProvider.java

 

/**

 * EJB Dependency Injection Provider. This class will inject the
appropriate

 * remote bean using the {_at_link javax.ejb.EJB} annotation.

 */

@Provider

public class EJBProvider implements InjectableProvider<EJB, Type>

{

 

  /**

   * @return The scope instance.

   * @see com.sun.jersey.spi.inject.InjectableProvider#getScope()

   */

  public ComponentScope getScope()

  {

    return ComponentScope.Singleton;

  }

 

  /**

   * @param cc The current component context.

   * @param ejb The current annotated EJB.

   * @param t The type of the annotated ejb interface.

   * @return An injectable instance of the EJB remote.

   * @see
com.sun.jersey.spi.inject.InjectableProvider#getInjectable(com.sun.jerse
y.core.spi.component.ComponentContext, java.lang.annotation.Annotation,
java.lang.Object)

   */

  public Injectable<Object> getInjectable(ComponentContext cc, EJB ejb,
Type t)

  {

    if (!(t instanceof Class))

    {

      return null;

    }

 

    try

    {

      Class<?> c = (Class<?>) t;

 

      Context ic = new InitialContext();

      String simpleName = c.getSimpleName();

      final Object o = ic.lookup("java:comp/env/ejb/" + simpleName);

 

      return new Injectable<Object>()

      {

        public Object getValue()

        {

          return o;

        }

      };

    }

    catch (Exception e)

    {

      e.printStackTrace();

      return null;

    }

  }

}

 

 

From: Paul.Sandoz_at_Sun.COM [mailto:Paul.Sandoz_at_Sun.COM]
Sent: Friday, June 05, 2009 13:20
To: users_at_jersey.dev.java.net
Subject: Re: [Jersey] Resource injection

 

Hi George,

 

It is not ready yet. Basically i am waiting for JSR-299 and managed
beans support in Glassfish v3 (since i do not want to re-implement
stuff).

 

You can if you wish utilize GuiceyFruit:

 

  http://code.google.com/p/guiceyfruit/

 

and the Jersey Guice support to get support for @EJB etc.

 

Paul.

 

On Jun 5, 2009, at 9:52 AM, Turner, George wrote:

 

I am just asking whether normal EJB 3.0 resource injection is available
yet on a Jersey service class? Everything I read looks like all RI is
through method parameters, and I would really like to just put a

 

  @Resource(name = "jdbc/dataSource")

  private DataSource dataSource;

 

or a

 

  @EJB

  private AuditDataService auditSvc;

 

or even a

 

  @WebServiceRef

  private ValidationService validationService;

 

But I have not found ANY ability to do this yet. I think I read
somewhere that this was coming by making these classes to be able to be
recognized by the container as managed classes, but looking through the
1.1.0-ea still looks like it isn't there yet. Am I missing
something????

 

Thanks

 

Gene