users@jersey.java.net

Re: EJB Injection in Jersey

From: Abe Zafar <abezafar_at_gmail.com>
Date: Sun, 28 Dec 2008 00:48:39 -0800

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.
>>
>>
>>
>