Hello.

I have a somewhat unusual problem that would be rather laborious to
explain here. In order to preserve the sanity of those reading this
email, I'll try to distill it down to something more abstract.

I have a situation where I want to instantiate a tree of services, each
of which share a common "tag" property. When a service A declares a
reference to another service B, the exact instance of B that is used
depends on the value of the tag property on A. This needs to work
transitively.

To try to illustrate what I mean, a contrived (and obviously
non-working example):

  @Component
  public class D
  {

  }

  @Component
  public class B
  {
    private @Reference D d; 
  }

  @Component(property = "tag = chocolate", immediate = true)
  public class A
  {
    private @Reference B b; 
  }

  @Component(property = "tag = vanilla", immediate = true)
  public class C
  {
    private @Reference B b; 
  }

  @Component(property = "tag = vanilla", immediate = true)
  public class E
  {
    private @Reference D d; 
  }

The A, C, and E components are the "root" components in this example,
and the value of the "tag" property will define which specific
instances of D and B get referenced. For example, assuming an OSGi
system that magically implemented what I'm trying to do, the above
arrangement would produce a tree of instances like the following:

  http://ataxia.io7m.com/2017/07/14/groups.svg.png

The process would probably go something like this:

The A component has a "chocolate" tag and refers to B. The system
instantiates a new B with tag "chocolate" (because there isn't an
existing one) and returns a reference to it. The same occurs with D 
for the reference in B.

The C component has a "vanilla" tag and refers to B. The existing
instance of B with tag "chocolate" is ignored and a new B with tag
"vanilla" is instantiated and returned to C. The same occurs for the
reference to D in the "vanilla" B.

The E component has a "vanilla" tag and refers to B. The existing
instance of B with tag "vanilla" satisfies the reference.

Note that at no point do the definitions of B or D refer to "chocolate"
or "vanilla"; the tag is somehow magically propagated by those instances
that attempt to get a reference to B or D. Naturally, if a hypothetical
component G with tag "strawberry" asked for a reference to A, new
instances of A, B, and D would be instantiated with tag "strawberry".

Is there some way to do this in DS without dropping down to the OSGi
APIs like ServiceTracker? I could probably do all of the above manually
somehow, but I'd prefer not to have to.

M

Attachment: pgpHEs25HtCKw.pgp
Description: OpenPGP digital signature

_______________________________________________
OSGi Developer Mail List
osgi-dev@mail.osgi.org
https://mail.osgi.org/mailman/listinfo/osgi-dev

Reply via email to