Hi Jan,
On Mar 2, 2009, at 4:43 PM, Jan Verbeke wrote:
> Dear sir,
>
> I’m currently a student working for a large it company. My task is
> to create back end and a to experiment with an RESTFul web service.
> Also to develop with ejb 3.0 and perhaps the 3.1 but that’s for later.
Yes, i am currently working on infrastructure to support EJB 3.1 and
root resource classes and session beans. Stay tuned for more details.
>
> I read a blog from Paul Sandoz, http://blogs.sun.com/sandoz/entry/ejb_injection
>
> I currently am running a RESTful web service, everything works fine
> but I have no idea how to inject a bean .
> Do I inject it in the entity classes, in the converter in the service?
> Some more info on how to instantiate the class and some info about
> the required parameters.
> I have already worked with EJBeans so I recognized the lookup syntax.
> I am also working with some beans atm.
>
> The context. lookup has the following value
> ctx.lookup("Bean30#ejb.Bean30Remote");
> It’s a stateless session bean.
>
> What I’m actually asking for is if you could provide a simple
> example with an entity, the corresponding service and converter and
> a simple EJB that is injected in the RESTFul web service.
>
Attached is a modified NetBeans project on EJBs i presented at Devoxx
[1].
I added an EJB injectable provider and a root resource class, in the
war, that references a remote session bean via injection with @EJB.
For this to work you need to resolve your project dependencies to add
the jersey 1.0.2 jars, see the dependencies document [2]. Specifically
for the web project you require:
http://download.java.net/maven/2/com/sun/jersey/jersey-server/1.0.2/jersey-server-1.0.2.jar
http://download.java.net/maven/2/com/sun/jersey/jersey-core/1.0.2/jersey-core-1.0.2.jar
http://download.java.net/maven/2/javax/ws/rs/jsr311-api/1.0/jsr311-api-1.0.jar
http://repo1.maven.org/maven2/asm/asm/3.1/asm-3.1.jar
For the EJB project you require:
http://download.java.net/maven/2/javax/ws/rs/jsr311-api/1.0/jsr311-api-1.0.jar
Hope this helps,
Paul.
[1]
http://blogs.sun.com/sandoz/entry/devoxx_slides_and_examples_for
[2]
https://jersey.dev.java.net/source/browse/*checkout*/jersey/tags/jersey-1.0.2/jersey/dependencies.html
> The class from the blog:
>
> package entities;
>
> 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;
>
> @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 = ic.lookup(c.getName());
>
> return new Injectable<Object>() {
> public Object getValue() {
> return o;
> }
> };
> } catch (Exception e) {
> e.printStackTrace();
> return null;
> }
> }
> }
>
> Greetz!
>
>