How to retrieve instance by name?

12 views
Skip to first unread message

project2501

unread,
Feb 23, 2017, 2:18:30 PM2/23/17
to ipopo-users
Hi,
  I was looking for a way to retrieve an existing component instance by name that was instantiated with @Instantiate?
Is this possible?

thanks.

Thomas Calmant

unread,
Feb 23, 2017, 2:39:13 PM2/23/17
to ipopo...@googlegroups.com
Hi,

Indeed, the iPOPO core service misses a get_instance(name) method.
The idea behind this is to ensure that the instance of an isolated component (providing no service)
won't be accessible by anything but the class which instanciated the component.

This is subject to debate, mostly because the implementation can't ensure that nobody
will access the dictionary holding all references.
As a result, a get_instance(name) method could be added to the core service, pretty easily.


Although, if the component provides a service, the component object will be this service,
and iPOPO automatically adds it an "instance.name" property.

The following snippet does the trick, if the component provides at least one service:

svc_ref = context.get_service_reference(None, "(instance.name=some-name)")
component = context.get_service(svc_ref)

Regards,
Thomas

--
Vous recevez ce message, car vous êtes abonné au groupe Google Groupes "ipopo-users".
Pour vous désabonner de ce groupe et ne plus recevoir d'e-mails le concernant, envoyez un e-mail à l'adresse ipopo-users+unsubscribe@googlegroups.com.
Pour obtenir davantage d'options, consultez la page https://groups.google.com/d/optout.

Darren G

unread,
Feb 23, 2017, 3:23:59 PM2/23/17
to ipopo...@googlegroups.com
Thanks. Because the situation I run into is I like to use my specification namespace name as my instance names when using @Instantiate.

That instance gets injected where it's needed.

If I then create a new instance from outside the framework I can't use the same name and have to them create a second redundant instance to use outside the framework as I can't retrieve the one the framework holds.

Can I use @Instantiate without a name?

Thomas Calmant

unread,
Feb 23, 2017, 3:37:26 PM2/23/17
to ipopo...@googlegroups.com
2017-02-23 21:23 GMT+01:00 Darren G <darre...@gmail.com>:
Thanks. Because the situation I run into is I like to use my specification namespace name as my instance names when using @Instantiate.

That instance gets injected where it's needed.

If I then create a new instance from outside the framework I can't use the same name and have to them create a second redundant instance to use outside the framework as I can't retrieve the one the framework holds.

OK, I see.
 

Can I use @Instantiate without a name?

No, but maybe you can kill the previous instance, if necessary.

I'll add a get_instance(name) method to the core service this weekend, as I'll (finally) have some time to work on the project.
 

Darren G

unread,
Feb 23, 2017, 3:47:42 PM2/23/17
to ipopo...@googlegroups.com
Ok. Thanks. I guess that will help support a singleton pattern and it will be up to the implementor to ensure correct behavior internally.

--

Thomas Calmant

unread,
Feb 23, 2017, 4:17:33 PM2/23/17
to ipopo...@googlegroups.com
If you require a component factory to only have one component instance at a time, no matter its name, you can use the SingletonFactory decorator [1].
With the get_instance(name) method, it should work as you want.

Thomas Calmant

unread,
Feb 25, 2017, 9:41:19 AM2/25/17
to ipopo...@googlegroups.com
This issue corresponding to this discussion can be seen at:

project2501

unread,
Feb 26, 2017, 9:10:08 AM2/26/17
to ipopo-users
Thomas,
   I really appreciate this. Fantastic. Thank you.

Darren


On Saturday, February 25, 2017 at 9:41:19 AM UTC-5, Thomas Calmant wrote:
This issue corresponding to this discussion can be seen at:
2017-02-23 22:17 GMT+01:00 Thomas Calmant <thomas....@gmail.com>:
If you require a component factory to only have one component instance at a time, no matter its name, you can use the SingletonFactory decorator [1].
With the get_instance(name) method, it should work as you want.

2017-02-23 21:47 GMT+01:00 Darren G <darre...@gmail.com>:
Ok. Thanks. I guess that will help support a singleton pattern and it will be up to the implementor to ensure correct behavior internally.
On Feb 23, 2017 3:37 PM, "Thomas Calmant" <thomas....@gmail.com> wrote:


2017-02-23 21:23 GMT+01:00 Darren G <darre...@gmail.com>:
Thanks. Because the situation I run into is I like to use my specification namespace name as my instance names when using @Instantiate.

That instance gets injected where it's needed.

If I then create a new instance from outside the framework I can't use the same name and have to them create a second redundant instance to use outside the framework as I can't retrieve the one the framework holds.

OK, I see.
 

Can I use @Instantiate without a name?

No, but maybe you can kill the previous instance, if necessary.

I'll add a get_instance(name) method to the core service this weekend, as I'll (finally) have some time to work on the project.
 

--
Vous recevez ce message, car vous êtes abonné au groupe Google Groupes "ipopo-users".
Pour vous désabonner de ce groupe et ne plus recevoir d'e-mails le concernant, envoyez un e-mail à l'adresse ipopo-users...@googlegroups.com.

Pour obtenir davantage d'options, consultez la page https://groups.google.com/d/optout.

--
Vous recevez ce message, car vous êtes abonné au groupe Google Groupes "ipopo-users".
Pour vous désabonner de ce groupe et ne plus recevoir d'e-mails le concernant, envoyez un e-mail à l'adresse ipopo-users...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages