osgi / osgi

OSGi Specification Project Build Repository. Specification, API, implementation, and TCK source code.
https://docs.osgi.org/
Apache License 2.0
104 stars 40 forks source link

Provide Service Capability Annotation #403

Closed juergen-albert closed 2 years ago

juergen-albert commented 2 years ago

DS Annotations conveniently result in Requirements and Capabilities for Services. This often causes resolver problems, when a DS Components references a Service that is registered manually and the usual solution is to add the Capability manually with something like @Capability(namespace = ServiceNamespace.SERVICE_NAMESPACE, attribute = ServiceNamespace.CAPABILITY_OBJECTCLASS_ATTRIBUTE + ":List<String>=\"foo.bar.Service,fizz.buzz.AnotherService\"").

Thus it would be great to provide a more Specific Annotation like

@ServiceCapability({Service.class, AnotherService.class})

timothyjward commented 2 years ago

This is slightly more complex than other cases because of the relationship between the objectclass attribute and the uses constraint directive.

bjhargrave commented 2 years ago

the uses constraint directive

Yes, I was going to point out that the example Capability lacks the uses element.

uses={foo.bar.Service.class,fizz.buzz.AnotherService.class}

It seems to me that this is just sugar and not additional functionality.

I was able to author my own ServiceCapability annotation using existing Bnd function:

@Retention(RetentionPolicy.CLASS)
@Target({
    ElementType.TYPE, ElementType.PACKAGE
})
@Capability(namespace = ServiceNamespace.SERVICE_NAMESPACE, //
    attribute = {
        ServiceNamespace.CAPABILITY_OBJECTCLASS_ATTRIBUTE + ":List<String>=\"${uniq;${#value}}\"", //
        Namespace.CAPABILITY_USES_DIRECTIVE + ":=\"${uniq;${replace;${#value};(.*)\\.[^.]+;$1}}\""
    })
public @interface ServiceCapability {
    Class<?>[] value() default Target.class;
}
bjhargrave commented 2 years ago

To be done in the Bnd annotations project.