Skip to content

Commit

Permalink
Issue #249: sanitize existing source bundle manifest (#266)
Browse files Browse the repository at this point in the history
* Issue #249: sanitize existing source bundle manifest

Change-Id: Ie8d16185942228b730b0e143ea551918ed2614b6
  • Loading branch information
emweber authored May 23, 2021
1 parent 88f2a3e commit 6f68c1b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/main/java/org/reficio/p2/bundler/impl/AquteBundler.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.jar.Attributes;
import java.util.jar.Manifest;

Expand Down Expand Up @@ -63,6 +64,7 @@ public AquteBundler(boolean pedantic, boolean ignoreBndErrors) {
this.pedantic = pedantic;
}

@Override
public void execute(ArtifactBundlerRequest request, ArtifactBundlerInstructions instructions) {
log().info("Executing Bundler:");
try {
Expand Down Expand Up @@ -173,6 +175,7 @@ private Manifest getManifest(Jar jar) throws Exception {
}

private void decorateSourceManifest(Manifest manifest, String name, String refrencedBundleSymbolicName, String symbolicName, String version) {
sanitizeSourceManifest(manifest);
Attributes attributes = manifest.getMainAttributes();
attributes.putValue(Analyzer.BUNDLE_SYMBOLICNAME, symbolicName);
attributes.putValue(ECLIPSE_SOURCE_BUNDLE, refrencedBundleSymbolicName + ";version=\"" + version + "\";roots:=\".\"");
Expand All @@ -186,6 +189,19 @@ private void decorateSourceManifest(Manifest manifest, String name, String refre
attributes.putValue(AquteHelper.TOOL_KEY, AquteHelper.TOOL);
}

/**
* Removes the bundle manifest headers that incorrectly cause a source bundle being
* resolved instead of its corresponding classes bundle.
*/
private void sanitizeSourceManifest(Manifest manifest) {
Attributes attributes = manifest.getMainAttributes();
if (!attributes.isEmpty()) {
// note that header is of type Attributes.Name, hence we call header.toString()
attributes.keySet().removeIf(header -> Arrays.asList(Analyzer.EXPORT_PACKAGE,
Analyzer.EXPORT_SERVICE, Analyzer.PROVIDE_CAPABILITY ).contains(header.toString()));
}
}

private Logger log() {
return Logger.getLog();
}
Expand Down
6 changes: 6 additions & 0 deletions src/test/integration/source-it/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@
<source>true</source>
<transitive>false</transitive>
</artifact>

<artifact>
<id>org.jboss.spec.javax.annotation:jboss-annotations-api_1.3_spec:2.0.1.Final</id>
<source>true</source>
<transitive>false</transitive>
</artifact>
</artifacts>
</configuration>

Expand Down
12 changes: 11 additions & 1 deletion src/test/integration/source-it/validate.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import org.reficio.p2.utils.TestUtils as Util;
// verify target
File target = new File(basedir, 'target/repository/plugins')
assert target.exists()
assert target.listFiles().size() == 2
assert target.listFiles().size() == 4

// verify number of artifacts
def files = target.listFiles().collect { it.name }
Expand All @@ -51,3 +51,13 @@ assert Util.tool(source) == "p2-maven-plugin (reficio.org)"
assert Util.eclipseSourceBundle(source) ==
"org.mockito.mockito-core;version=\"1.9.0\";roots:=\".\"";

// verify that broken source bundle manifest gets sanitized
sourceName = "org.jboss.spec.javax.annotation.jboss-annotations-api_1.3_spec.source_2.0.1.Final.jar"
assert files.contains(sourceName)

Jar sourceSan = new Jar(new File(target, sourceName));
assert Util.symbolicName(sourceSan) == "org.jboss.spec.javax.annotation.jboss-annotations-api_1.3_spec.source"
assert Util.eclipseSourceBundle(sourceSan) != null
assert Util.attr(sourceSan, "Export-Package") == null
assert Util.attr(sourceSan, "Export-Service") == null
assert Util.attr(sourceSan, "Provide-Capability") == null

0 comments on commit 6f68c1b

Please sign in to comment.