Skip to content

Commit

Permalink
move logic to MetaInfServiceParser
Browse files Browse the repository at this point in the history
it leverages the recent addition of PR #5912 that allowed to add annotations in comments of files in META-INF/services. But in this PR we basically pretent and add a aQute.bnd.annotation.spi.ServiceProvider annotation artificially. this causes bnd to generate Provide-Capability manifest headers for the services

Signed-off-by: Christoph Rueger <[email protected]>
  • Loading branch information
chrisrueger committed Oct 7, 2024
1 parent 5f76bc9 commit 07bb70c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
9 changes: 9 additions & 0 deletions biz.aQute.bndlib/src/aQute/bnd/osgi/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,14 @@ public interface Constants {
String SERVICELOADER_REGISTER_DIRECTIVE = "register:";
String SERVICELOADER_NAMESPACE = "osgi.serviceloader";

/*
* processing of META-INF/services folder section.
*/
String METAINF_SERVICES = "-metainf-services";
String METAINF_SERVICES_STRATEGY_ANNOTATION = "annotation";
String METAINF_SERVICES_STRATEGY_AUTO = "auto";
String METAINF_SERVICES_STRATEGY_NONE = "none";

/**
* Launch constants that should be shared by launchers
*/
Expand Down Expand Up @@ -602,6 +610,7 @@ public interface Constants {
String INTERNAL_PREFIX = "-internal-";



/*
* Deprecated Section
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ public MetaInfService(String serviceName, String source) {
// just comment
continue;
}

implementations.put(line, new Implementation(line, annotations, comments));
annotations.clear();
comments.clear();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package aQute.bnd.osgi.metainf;

import static aQute.bnd.osgi.Constants.METAINF_SERVICES;
import static aQute.bnd.osgi.Constants.METAINF_SERVICES_STRATEGY_ANNOTATION;
import static aQute.bnd.osgi.Constants.METAINF_SERVICES_STRATEGY_AUTO;
import static aQute.bnd.osgi.Constants.METAINF_SERVICES_STRATEGY_NONE;

import java.lang.annotation.RetentionPolicy;
import java.util.Map;

import aQute.bnd.header.Attrs;
import aQute.bnd.header.Parameters;
import aQute.bnd.osgi.Analyzer;
import aQute.bnd.osgi.Annotation;
import aQute.bnd.osgi.Annotation.ElementType;
Expand All @@ -25,14 +31,33 @@ public class MetaInfServiceParser implements AnalyzerPlugin {
*/
@Override
public boolean analyzeJar(Analyzer analyzer) throws Exception {

String strategy = strategy(analyzer);

if (METAINF_SERVICES_STRATEGY_NONE.equals(strategy)) {
// do not process META-INF/services files
return false;
}

MetaInfService.getServiceFiles(analyzer.getJar())
.values()
.stream()
.flatMap(mis -> mis.getImplementations()
.values()
.stream())
.forEach(impl -> {
impl.getAnnotations()
Parameters annotations = impl.getAnnotations();

if (annotations.isEmpty() && METAINF_SERVICES_STRATEGY_AUTO.equals(strategy)) {
// if there are no annotations at the impl
// we add one artificially to create the capabilities for
// Service without any attributes in the manifest e.g.
// Provide-Capability',
// "osgi.serviceloader;osgi.serviceloader=serviceName
annotations.add("aQute.bnd.annotation.spi.ServiceProvider", Attrs.EMPTY_ATTRS);
}

annotations
.forEach((annotationName, attrs) -> {
doAnnotationsforMetaInf(analyzer, impl, Processor.removeDuplicateMarker(annotationName), attrs);
});
Expand All @@ -57,4 +82,7 @@ private void doAnnotationsforMetaInf(Analyzer analyzer, Implementation impl, Str
}
}

private String strategy(Analyzer analyzer) {
return analyzer.getProperty(METAINF_SERVICES, METAINF_SERVICES_STRATEGY_ANNOTATION);
}
}

0 comments on commit 07bb70c

Please sign in to comment.