I had to write an injection provider for mapping the annotation @EJB to
an actual container lookup:
/**
* EJB Dependency Injection Provider. This class will inject the
appropriate
* remote bean using the {_at_link javax.ejb.EJB} annotation.
*/
@Provider
public class EJBProvider implements InjectableProvider<EJB, Type> {
/**
* @return The scope instance.
* @see
com.sun.jersey.spi.inject.InjectableProvider#getScope()
*/
public ComponentScope getScope() {
return ComponentScope.Singleton;
}
/**
* @param cc The current component context.
* @param ejb The current annotated EJB.
* @param t The type of the
annotated ejb interface.
* @return An injectable
instance of the EJB remote.
* @see
com.sun.jersey.spi.inject.InjectableProvider#getInjectable(com.sun.jerse
y.core.spi.component.ComponentContext, java.lang.annotation.Annotation,
java.lang.Object)
*/
public Injectable<Object> getInjectable(ComponentContext
cc, EJB ejb, Type t) {
if (!(t instanceof Class)) {
return null;
}
try {
Class<?> c = (Class<?>)
t;
Context ic = new
InitialContext();
for (Object o :
ic.getEnvironment().keySet()) {
String s
= String.format("ENV key=%s val=%s", o.toString(),
ic.getEnvironment().get(o));
System.out.println(s);
}
// In our case, the JNDI
name of the remote interface is also the simple name of the remote
interface/class
// Our JNDI naming
conventions may change so there should be a way to map from the Class to
your JNDI
// name.
String simpleName =
c.getSimpleName();
System.out.println("Looking up: " + simpleName);
final Object o =
ic.lookup(simpleName);
return new
Injectable<Object>() {
public
Object getValue() {
return o;
}
};
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
_______________________________________________
Mark A. Rabick - Software Engineer
Em: mark.rabick_at_ngc.com
From: Turner, George [mailto:george.turner_at_lmco.com]
Sent: Friday, June 05, 2009 11:52 AM
To: users_at_jersey.dev.java.net
Subject: [Jersey] Resource injection
I am just asking whether normal EJB 3.0 resource injection is available
yet on a Jersey service class? Everything I read looks like all RI is
through method parameters, and I would really like to just put a
@Resource(name = "jdbc/dataSource")
private DataSource dataSource;
or a
@EJB
private AuditDataService auditSvc;
or even a
@WebServiceRef
private ValidationService validationService;
But I have not found ANY ability to do this yet. I think I read
somewhere that this was coming by making these classes to be able to be
recognized by the container as managed classes, but looking through the
1.1.0-ea still looks like it isn't there yet. Am I missing
something????
Thanks
Gene