Abstract components via org.osgi.service.component annotations
Asked Answered
T

1

8

I am migrating from org.apache.felix.scr annotations to org.osgi.service.component annotations. I have a set of Components that inherit from a common abstract class. In the felix case, I can use a @Component annotation with the option componentAbstract=true on the super class, and then use @Reference annotation in the super class. I cannot find how to migrate this to osgi annotations.

Is it possible to use Component annotations in a super class of a Component? And if so, what is then the appropriate way to handle the properties and metatype generation?

So, what I am looking for, is something like this

/* No component definition should be generated for the parent, as it is
   abstract and cannot be instantiated */
@Component(property="parent.property=parentValue")
public abstract class Parent {
  @Reference
  protected Service aService;

  protected activate(Map<String,Object> props) {
    System.out.println("I have my parent property: "+props.get("parent.property"));

  @Override
  public abstract void doSomething();
}

/* For this class, the proper Component definition should be generated, also
   including the information coming from the annotations in the parent */
@Component(property="child.property=childValue")
public class Child extends Parent {

  @Activate
  public activate(Map<String,Object> props) {
    super.activate(props);
    System.out.println("I have my child property: "+props.get("child.property"));
  }

  public void doSomething() {
    aService.doSomething();
  }
}
Tenstrike answered 22/12, 2016 at 9:42 Comment(0)
C
12

By default BND will not process DS annotations in parent classes. You can change that with -dsannotations-options: inherit but please see http://enroute.osgi.org/faq/ds-inheritance.html why you shouldn't!


2021-02-23 UPDATE: It seems like the page mentioned above is no longer available. I don't know if it was moved elsewhere or simply removed but its content (in Markdown format) is still available on GitHub: https://github.com/osgi/osgi.enroute.site/blob/pre-R7/_faq/ds-inheritance.md

Corriecorriedale answered 22/12, 2016 at 10:27 Comment(4)
Thanks for the link. The explanation is clear, but is really focussing on parents in different bundles. In my case, both classes are part of the same bundle, so the arguments do not really hold.Tenstrike
However, I will rewrite the abstract class into a factory component, and make adapters that will do all the specialized implementation from the child class.Tenstrike
With the Maven Bundle Plugin you can use the following configuration: <configuration> <instructions> <_dsannotations-options>inherit</_dsannotations-options> </instructions> </configuration>Collogue
@Galigator I don't know why the page is no longer available. I updated my answer with link to the source.Corriecorriedale

© 2022 - 2024 — McMap. All rights reserved.