users@glassfish.java.net

Re: Servcies registered with OSGi bundle context NOT available via JNDI

From: Sanjeeb Sahoo <Sahoo_at_sun.com>
Date: Wed, 19 May 2010 08:56:38 +0530

[I wrote this email 16th May, but I just realised that none of my emails
are reaching users alias because of a mis-configuration of email client.]

glassfish_at_javadesktop.org wrote:
> From everything I have read on the web, when I register a service using the BundleContext.registerService() in a BundleActivator.start() method, in an OSGi bundle deployed to a glassfish server (I'm using v3.0.1b18), the service is supposed to be registered in JNDI so that I can use resource injection in a servlet (via @Resource). In fact, it seems that the services are NOT being registered in JNDI (as the name lookup fails, and browsing the JNDI tree confirms). If I register the services manually, then the @Resource injection works as it should.
>
> Is this a bug or do I need some special OSGi module installed to realize this behavior? Does this ONLY happen when using declarative services or spring? Shouldn't it work with manual registrations too?
>
> See http://blogs.sun.com/dochez/entry/glassfish_v3_extensions_part_3 and http://blogs.sun.com/dochez/entry/glassfish_v3_extensions_part_4
>
> Thanks in advance.
> [Message sent by forum member 'npahucki']
>
> http://forums.java.net/jive/thread.jspa?messageID=469987
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>
>
Actually, the way OSGi service injection is currently implemented, you
will never see them under JNDI tree. There is some special code in
injection path which falls back to searching a special registry to look
up OSGi services. I think this is a short-cut taken to satisfy some
immediate use cases. I will let the developer who has implemented this
feature provide more details here and correct me if I am wrong. Simple
@Resource annotations will work in most cases. If the service is written
using declarative service spec or Spring OSGi service, then you can use
mappedName in @Resource annotation to map to component.name or
org.springframework.osgi.bean.name respectively. For services registered
directly, I don't think you can use mappedName attribute. If you have
multiple services registered for the same service type, which one gets
injected is not predictable. _If you have one implementation of the
service, then @Resource should just be fine_. Are you seeing any issues?

The longer term goal is to implement OSGi RFC #142 which adds a new JNDI
url scheme called osgiservice or something similar to look up OSGi services.

Finally, can I ask what you are trying to accomplish? Why don't you turn
your web app into an OSGi bundle (WAB) so that you can benefit from OSGi
and Java EE in the same app? You can lookup OSGi service using any of
the available OSGi techniques.

Thanks,
Sahoo