users@jersey.java.net

Re: EJB Injection in Jersey

From: Abe Zafar <abezafar_at_gmail.com>
Date: Sat, 27 Dec 2008 01:14:16 -0800

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