users@hk2.java.net

Re: ServiceLocator.getService() failing for _at_Service-annotated classes

From: Mason Taube <mason.taube_at_oracle.com>
Date: Thu, 5 Dec 2013 20:49:05 -0500

On Dec 5, 2013, at 4:37 PM, cowwoc <cowwoc_at_bbs.darktech.org> wrote:

> What prevents you from creating the same binding at runtime? Surely if hk2-locator can locate this relationship at build-time, you can do the same at runtime.

We don’t always know at runtime. When an OSGi bundle is installed into GlassFish, the descriptor present in that bundle gets parsed and the HK2 bindings are created on the fly (with no classloading happening from the new bundle). As an example, this could be a new asadmin command (all asadmin commands implement a common contract) whose bundle gets copied to a running server’s module directory. The new command is now available since a call to ServiceLocator.getAllServices(Command.class) now includes this dynamically discovered service and the server can invoke on it. The cli framework doesn’t know anything about the specific commands (or what they are) it invokes on behalf of the user.