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();
  }
}
Igor P
  • 85
  • 1
  • 5

1 Answers1

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

Milen Dyankov
  • 2,972
  • 14
  • 25
  • 1
    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. – Igor P Dec 22 '16 at 11:53
  • 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. – Igor P Dec 22 '16 at 11:55
  • 2
    With the Maven Bundle Plugin you can use the following configuration: ` <_dsannotations-options>inherit ` – Puce Aug 03 '17 at 08:30
  • 1
    @Galigator I don't know why the page is no longer available. I updated my answer with link to the source. – Milen Dyankov Feb 23 '21 at 16:12