users@glassfish.java.net

Re: JAX-RS in GF 2.1 and EJB injection?

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Wed, 21 Apr 2010 12:07:36 +0200

On Apr 21, 2010, at 11:29 AM, glassfish_at_javadesktop.org wrote:

> Hi Witold,
> Hi Paul,
>
> thanks for your replies. I will try this again. I thought that I
> still tried this szenario. My EJBs are separated in jars and
> provided in the root of my EAR (the ejbs are used also by an
> additional web main application).
> The main different in my test seems to be that I always tried to
> inject the local inferfaces form my EJBs - like I do in a servlet or
> JSF managedBean class.
> But you do inject the remote interface in your example.
> Its this the trick to inject the remote interface and not the local
> interface?
>

Yes.

With some experimentation i think i have found a way to get lookup
working with local beans. See attached for a modified version of
web.xml and EJBProvider.

You need to ensure the ejb-ref-name is the class name of the local
interface.

Note that i will need to fix a bug in Jersey if you want the local
bean reference to be a root resource class itself.

Paul.

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd
">
     <servlet>
         <servlet-name>Jersey Web Application</servlet-name>
         <servlet-
class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-
class>
         <init-param>
             <param-name>com.sun.jersey.config.property.packages</
param-name>
             <param-value>org.jersey.ejb;org.jersey.web</param-value>
         </init-param>
         <load-on-startup>1</load-on-startup>
     </servlet>
     <servlet-mapping>
         <servlet-name>Jersey Web Application</servlet-name>
         <url-pattern>/webresources/*</url-pattern>
     </servlet-mapping>
     <ejb-local-ref>
         <ejb-ref-name>org.jersey.ejb.SSBResource</ejb-ref-name>
         <ejb-ref-type>Session</ejb-ref-type>
         <local-home/>
         <local>org.jersey.ejb.SSBResource</local>
     </ejb-local-ref>
</web-app>

@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();

             final Object o = lookup(ic, c);

             return new Injectable<Object>() {

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

     private Object lookup(Context ic, Class c) throws Exception {
         try {
             return ic.lookup(c.getName());
         } catch (Exception e) {
             try {
                 return ic.lookup("java:comp/env/" + c.getName());
             } catch (Exception ex) {
                 throw ex;
             }
         }
     }
}