retrodev
retrodev

Reputation: 2393

Inject EJB 3.0 into Jersey 1.9 on WebLogic 10.3.6

I'm attempting to inject and EJB 3.0 bean into a Jersey 1.9 Servet, running on WebLogic 10.3.6.

I have tried the techniques listed here: Inject an EJB into JAX-RS (RESTful service)

The direct injection techniques here simply give a NullPointerException. The @Provider technique gives a NameNotFoundException because it seems to be pulling out the fully qualified name of the Local Interface. Changing the code to use just the Interface name doesn't seem to help.

I'm packaging in an EAR. The EJB is in the JAR and the Jersey Resources are in the WAR.

Is EJB injection into Jersey on Java EE 5 on WebLogic 10.3.6 even possible?

Upvotes: 3

Views: 1260

Answers (1)

retrodev
retrodev

Reputation: 2393

Evidently WebLogic 10.3.6 does not inject Local business interfaces into the JNDI registry.

According to Oracle Support note 1175123.1, one must add an ejb-local-ref to web.xml:

  <ejb-local-ref>
    <ejb-ref-name>[Name of EJB local interface here]</ejb-ref-name>
    <ejb-ref-type>Session</ejb-ref-type>
    <local>[Fully qualified path to EJB local interface]</local>
  </ejb-local-ref>

It's important that the ejb-ref-name matches the interface name because that is what is obtained by the code below to allow the injection.

The code below is modified from the link above to obtain the simple name for the Interface, prefixed with java:comp/env/ to conform the the WebLogic 10.3.6 naming standard.

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;

import java.lang.reflect.Type;

import javax.ejb.EJB;

import javax.naming.Context;
import javax.naming.InitialContext;

import javax.ws.rs.ext.Provider;


/**
 * JAX-RS EJB Injection provider.
 */
@Provider
public class EJBProvider implements InjectableProvider<EJB, Type> {

    public ComponentScope getScope() {
        return ComponentScope.Singleton;
    }

    public Injectable getInjectable(ComponentContext cc, EJB ejb, Type t) {
        if (!(t instanceof Class))
            return null;

        try {
            Class c = (Class)t;

            Context ic = new InitialContext();

            String simpleName = String.format("java:comp/env/%s", c.getSimpleName());
            final Object o = ic.lookup(simpleName);

            return new Injectable<Object>() {
                public Object getValue() {
                    return o;
                }
            };
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

Upvotes: 3

Related Questions