On Jul 15, 2009, at 5:36 PM, glassfish_at_javadesktop.org wrote:
> Hmm ... I thought it would already work (I'm using v3 b 54). But
> anyhow, if there's some other way to achieve this, I would be glad,
> if you could tell me.
>
See here:
http://kingsfleet.blogspot.com/2009/02/resource-injection-with-jersey.html
However, there is no need to extend the servlet to support this. You
can also do:
@Provider
public static class ResourceInjectable implements
InjectableProvider<Resource, Type> {
public ComponentScope getScope() {
return ComponentScope.Singleton;
}
public Injectable<Object> getInjectable(ComponentContext ic,
Resource r, Type c) {
final Object value = get(r);
return new Injectable<Object>() {
public Object getValue() {
return value;
}
};
}
private Object get(Resource r) {
try {
Context ctx = new InitialContext();
// Look up a data source
try {
return ctx.lookup(r.name());
} catch (NamingException ex) {
return ctx.lookup("java:comp/env/" + r.name());
}
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
}
Make sure your configuration registers the ResourceInjectable class.
The same rules apply to such registration as for root resource classes
(those classes annotated with @Path).
This of course assumes that the types are compatible, if not you will
get an exception when injecting. You could of course modify the code
to check and initialization by comparing the Type "c" with the class
of the Object "value".
Paul.