users@hk2.java.net

Re: HK2 question

From: Jerome Dochez <Jerome.Dochez_at_Sun.COM>
Date: Thu, 16 Aug 2007 17:24:44 -0700

Hello Ferid

thanks for trying HK2. answers inline below.

On Aug 16, 2007, at 3:23 PM, Ferid Sabanovic wrote:

>
> Hello!
>
> My name is Ferid and Im currently writing some small demos for a
> public HK2 presentation (JavaZone 2007). While doing this I ran
> into some small questions regarding the code.
> If you have the time and will, could you please help me out and
> tip me to the following questions?
>
> I have managed to create a DirectoryBasedRepository of where i have
> my simple module with Animal contract and a service winch
> implements it.
> After that i have also managed to ad it to a ModuleRegistry and
> also the repository gots initialized.
> I am also able to get the start RESOLVED (and ready after i invoke
> the start() method).
>
> My problem here is that I'm not sure of what the right way is when
> one wants to "start" this loaded module. How to access the services
> inside of it?

In general, you don't have to explicitly start the module, you should
just rely on the component manager to do so when it finds that it
needs to load a service implementation.

This is why as a developer you should concentrate on the @Contract
and @Service and let the component manager start the relevant modules
when you are requesting services.

> My wild guess would be that they have to share the same Contract
> class for a service and that i have to use the component-manager to
> retrieve it?

that's right.

> Does the ComponentManager also keeps track of services/components
> inside other modules (for exempel in this case, a module from a
> directory-repository)?

component manager will look into all the modules contained in your
repositories which are added to the module registry.

>
> What would be the correct way to do this type of "instantiation" ?
> And module loading/execution etc?

so for instance you have a @Contract interface called I1, then you
have 2 modules M1 and M2 each implementing a @Service annotated
classes C1 and C2 implementing I1. M1 and M2 are contained in a
repository added to the module registry.

if in your code somewhere you do : componentManager.getComponents
(I1.class). it will return you an instance of C1 and an instance of
C2 after starting the modules M1 and M2. The code can completely
ignore in which modules those implementations resides, the runtime
takes care of it. You don't have to explicitly manipulate M1 or M2,
just let the system handle the module management.

Hope that answer your questions. Let me know if you have more.

Jerome
>
>
> Once again, I am sorry to bother you about this, and hope that one
> of you guys have the time to give me answers to my questions.
>
>
> thank you
>
> Greetings!
>
> Ferid Sabanovic
>
> IBS JavaSolutions AB
> Mölndalsvägen 77, SE-412 63 Göteborg, Sweden
> Phone: +46 (0) 31 703 30 00 Mobile: +46 (0) 708 12 21 60
> E-Mail: ferid.sabanovic_at_ibs.se
> www.ibskonsult.se / www.jsolutions.se