users@jersey.java.net

Re: [Jersey] Re: EJB Injection in Jersey

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Mon, 05 Jan 2009 12:12:20 +0100

Hi Abe,

This is not really anything to do with Jersey and instead it is to do
with JNDI look up, you will reproduce the same behavior when using
servlet when doing:

    final Object ejb = ic.lookup(c.getName());

When i have played around i manage to get local and remote to work,
but perhaps because i am using GF v3?

Note that this is using non-standard look up of an EJB reference using
the fully qualified class name that only works for GF. It might be you
need to use the portable JNDI name for the EJB, for that i suggest you
email the users_at_glassfish.dev.java.net list because i cannot recall
how to do that.

Paul.

On Dec 28, 2008, at 9:48 AM, Abe Zafar wrote:

> Finally figured out the issue which had nothing to do with the
> provider or the jndi lookup directly. I wish Jersey's docs would
> mention this in big bold font. Since they don't do it I will do it
> in case others run into this problem. PLEASE MAKE SURE THAT YOUR EJB
> INTERFACE HAS THE @Remote ANNOTATION. I HAD @Local AND IT WAS NOT
> WORKING.
>
> My question to the Jersey team. Why doesn't @Local work?
>
>
> Also here is the last version of the provider class that worked:
>
> package com.intuit.ctg.fafsa.service.rest;
>
> import java.lang.reflect.Type;
> import java.util.Hashtable;
>
> import javax.ejb.EJB;
> import javax.naming.InitialContext;
> import javax.ws.rs.ext.Provider;
>
> import com.sun.jersey.core.spi.component.ComponentContext;
> import com.sun.jersey.core.spi.component.ComponentScope;
> import com.sun.jersey.spi.inject.Injectable;
> import com.sun.jersey.spi.inject.InjectableProvider;
>
> @Provider
> public class EJBProvider implements InjectableProvider<EJB, Type> {
>
> @Override
> public Injectable getInjectable(ComponentContext cc, EJB arg1,
> Type t) {
>
> try {
> if(!(t instanceof Class)) {
> return null;
> }
>
>
> Class c = (Class)t;
>
> InitialContext ic = new InitialContext();
>
> final Object ejb = ic.lookup(c.getName());
>
> return new Injectable() {
> @Override
> public Object getValue() {
> return ejb;
> }
> };
> } catch (Exception e) {
> e.printStackTrace();
> return null;
> }
>
> }
>
> @Override
> public ComponentScope getScope() {
> return ComponentScope.Singleton;
> }
> }
>
>
> On Sat, Dec 27, 2008 at 1:14 AM, Abe Zafar <abezafar_at_gmail.com> wrote:
> Here is the exact code that I am using. The app is deployed on
> Glassfish v2:
>
> To me it seems like that the InjectableProvider is based on the
> assumption that the ejb's are first registered with JNDI before the
> callback method of this Provider is called. I am not sure if that's
> a correct assumption.
>
> package com.intuit.ctg.fafsa.service.rest;
>
> import java.lang.reflect.Type;
> import java.util.Hashtable;
>
> import javax.ejb.EJB;
> import javax.naming.InitialContext;
> import javax.ws.rs.ext.Provider;
>
> import com.sun.jersey.core.spi.component.ComponentContext;
> import com.sun.jersey.core.spi.component.ComponentScope;
> import com.sun.jersey.spi.inject.Injectable;
> import com.sun.jersey.spi.inject.InjectableProvider;
>
> @Provider
> public class EJBProvider implements InjectableProvider<EJB, Type> {
>
> @Override
> public Injectable getInjectable(ComponentContext cc, EJB arg1,
> Type t) {
>
> try {
> if(!(t instanceof Class)) {
> return null;
> }
>
>
> Class c = (Class)t;
>
> Hashtable<String, String> props = new Hashtable<String,
> String>();
> props.put("java.naming.factory.initial",
> "com.sun.enterprise.naming.SerialInitContextFactory");
> props.put("java.naming.factory.url.pkgs",
> "com.sun.enterprise.naming");
> props.put("java.naming.factory.state",
> "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
> props.put("org.omg.CORBA.ORBInitialHost", "localhost");
> props.put("org.omg.CORBA.ORBInitialPort", "3700");
>
> InitialContext ic = new InitialContext(props);
>
> final Object ejb = ic.lookup("ejb/" + c.getName());
> System.out.println("--------- " + ejb);
>
> return new Injectable() {
> @Override
> public Object getValue() {
> return ejb;
> }
> };
> } catch (Exception e) {
> e.printStackTrace();
> return null;
> }
>
> }
>
> @Override
> public ComponentScope getScope() {
> return ComponentScope.Singleton;
>
> }
> }
>
>
> On Fri, Dec 26, 2008 at 11:18 PM, Abe Zafar <abezafar_at_gmail.com>
> wrote:
> I followed the code provided in this blog to have ejb injection in
> my rest service:
>
> users_at_jersey.dev.java.net
>
> http://blogs.sun.com/sandoz/entry/ejb_injection
>
> My code fails at:
>
> InitialContext ic = new InitialContext();
> final Object o = ic.lookup(c.getName());
>
> I get a NameNotFoundException at this point.
>
> Please advise.
>
> thank you.
>
>
>