kwin commented on a change in pull request #74:
URL:
https://github.com/apache/sling-org-apache-sling-feature-cpconverter/pull/74#discussion_r622930586
##########
File path:
src/main/java/org/apache/sling/feature/cpconverter/handlers/BundleEntryHandler.java
##########
@@ -109,111 +154,324 @@ public void handle(@NotNull String path, @NotNull
Archive archive, @NotNull Entr
logger.debug("Start level {} was extracted from path {}",
startLevel, path);
}
- try (JarInputStream jarInput = new
JarInputStream(Objects.requireNonNull(archive.openInputStream(entry)))) {
- Properties properties = readGav(entry.getName(), jarInput);
- manifest = jarInput.getManifest();
-
- if (!properties.isEmpty()) {
- groupId = getCheckedProperty(properties, NAME_GROUP_ID);
- artifactId = getCheckedProperty(properties, NAME_ARTIFACT_ID);
- version = getCheckedProperty(properties, NAME_VERSION);
- classifier = properties.getProperty(NAME_CLASSIFIER);
- } else { // maybe the included jar is just an OSGi bundle but not
a valid Maven artifact
- groupId = getCheckedProperty(manifest, BUNDLE_SYMBOLIC_NAME);
- // Make sure there are not spaces in the name to adhere to the
Maven Group Id specification
- groupId = groupId.replace(' ', '_').replace(':',
'_').replace('/', '_').replace('\\', '_');
- if (groupId.indexOf('.') != -1) {
- artifactId = groupId.substring(groupId.lastIndexOf('.') +
1);
- groupId = groupId.substring(0, groupId.lastIndexOf('.'));
+ String bundleName = entry.getName();
+ // Remove the leading path
+ int idx = bundleName.lastIndexOf('/');
+ if (idx >= 0) {
+ bundleName = bundleName.substring(idx + 1);
+ }
+ // Remove the extension
+ int edx = bundleName.lastIndexOf('.');
+ if (edx > 0) {
+ bundleName = bundleName.substring(0, edx);
+ }
+
+ // create a temporary JAR file (extracted from archive)
+ Path tmpBundleJar =
Files.createTempFile(converter.getTempDirectory().toPath(), "extracted",
bundleName + ".jar");
+ try (OutputStream output = Files.newOutputStream(tmpBundleJar);
+ InputStream input =
Objects.requireNonNull(archive.openInputStream(entry))) {
+ IOUtils.copy(input, output);
+ }
+ try {
+ processBundleInputStream(tmpBundleJar, bundleName, runMode,
startLevel, converter);
+ } finally {
+ Files.delete(tmpBundleJar);
+ }
+ }
+
+ void processBundleInputStream(@NotNull Path originalBundleFile, @NotNull
String bundleName, @Nullable String runMode, @Nullable Integer startLevel,
@NotNull ContentPackage2FeatureModelConverter converter) throws Exception {
+ try (JarFile jarFile = new JarFile(originalBundleFile.toFile())) {
+ // first extract bundle metadata from JAR input stream
+ ArtifactId id = extractArtifactId(bundleName, jarFile);
+
+ try (InputStream strippedBundleInput =
extractSlingInitialContent(id, jarFile, converter, runMode)) {
+ if (strippedBundleInput != null && slingInitialContentPolicy
== SlingInitialContentPolicy.EXTRACT_AND_REMOVE) {
+ id = id.changeVersion(id.getVersion() + "-" +
ContentPackage2FeatureModelConverter.PACKAGE_CLASSIFIER);
+
Objects.requireNonNull(converter.getArtifactsDeployer()).deploy(new
InputStreamArtifactWriter(strippedBundleInput), id);
+ } else {
+ try (InputStream originalBundleInput =
Files.newInputStream(originalBundleFile)) {
+
Objects.requireNonNull(converter.getArtifactsDeployer()).deploy(new
InputStreamArtifactWriter(originalBundleInput), id);
+ }
}
- if (artifactId == null || artifactId.isEmpty()) {
- artifactId = groupId;
+ }
+
Objects.requireNonNull(converter.getFeaturesManager()).addArtifact(runMode, id,
startLevel);
+ String exportHeader =
Objects.requireNonNull(jarFile.getManifest()).getMainAttributes().getValue(Constants.EXPORT_PACKAGE);
+ if (exportHeader != null) {
+ for (Clause clause : Parser.parseHeader(exportHeader)) {
+ converter.getFeaturesManager().addAPIRegionExport(runMode,
clause.getName());
}
- Version osgiVersion =
Version.parseVersion(getCheckedProperty(manifest, BUNDLE_VERSION));
- version = osgiVersion.getMajor() + "." +
osgiVersion.getMinor() + "." + osgiVersion.getMicro() +
(osgiVersion.getQualifier().isEmpty() ? "" : "-" + osgiVersion.getQualifier());
}
}
+ }
- try (InputStream input = archive.openInputStream(entry)) {
- if (input != null) {
- ArtifactId id = new ArtifactId(groupId, artifactId, version,
classifier, JAR_TYPE);
+ static Version getModifiedOsgiVersion(Version originalVersion) {
+ return new Version(originalVersion.getMajor(),
originalVersion.getMinor(), originalVersion.getMicro(),
originalVersion.getQualifier() + "_" +
ContentPackage2FeatureModelConverter.PACKAGE_CLASSIFIER);
+ }
-
Objects.requireNonNull(converter.getArtifactsDeployer()).deploy(new
InputStreamArtifactWriter(input), id);
+ @Nullable InputStream extractSlingInitialContent(@NotNull ArtifactId
bundleArtifactId, @NotNull JarFile jarFile, @NotNull
ContentPackage2FeatureModelConverter converter, @Nullable String runMode)
throws Exception {
+ if (slingInitialContentPolicy == SlingInitialContentPolicy.KEEP) {
+ return null;
+ }
+ // parse "Sling-Initial-Content" header
+ Manifest manifest = Objects.requireNonNull(jarFile.getManifest());
+ Iterator<PathEntry> pathEntries = PathEntry.getContentPaths(manifest,
-1);
+ if (pathEntries == null) {
+ return null;
+ }
+ logger.info("Extracting Sling-Initial-Content from '{}'",
bundleArtifactId);
+ Collection<PathEntry> pathEntryList = new ArrayList<>();
+ pathEntries.forEachRemaining(pathEntryList::add);
+
+ // remove header
+ manifest.getMainAttributes().remove(new
Attributes.Name(PathEntry.CONTENT_HEADER));
+ // change version to have suffix
+ Version originalVersion = new
Version(Objects.requireNonNull(manifest.getMainAttributes().getValue(Constants.BUNDLE_VERSION)));
+ manifest.getMainAttributes().putValue(Constants.BUNDLE_VERSION,
getModifiedOsgiVersion(originalVersion).toString());
+ Path newBundleFile =
Files.createTempFile(converter.getTempDirectory().toPath(), "newBundle",
".jar");
+
+ // create JAR file to prevent extracting it twice and for random access
+ JcrNamespaceRegistry namespaceRegistry =
createNamespaceRegistry(manifest, jarFile,
converter.getFeaturesManager().getNamespaceUriByPrefix());
+
+ Map<PackageType, VaultPackageAssembler> packageAssemblers = new
EnumMap<>(PackageType.class);
+ try (OutputStream fileOutput = Files.newOutputStream(newBundleFile,
StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING);
+ JarOutputStream bundleOutput = new JarOutputStream(fileOutput,
manifest)) {
+
+ for (Enumeration<JarEntry> e = jarFile.entries();
e.hasMoreElements();) {
+ JarEntry jarEntry = e.nextElement();
+ if (!jarEntry.isDirectory()) {
+ try (InputStream input = jarFile.getInputStream(jarEntry))
{
+ if (!extractSlingInitialContent(jarEntry, input,
bundleArtifactId, pathEntryList, packageAssemblers, namespaceRegistry,
converter)) {
+ // skip manifest, as already written in the
constructor (as first entry)
+ if
(jarEntry.getName().equals(JarFile.MANIFEST_NAME)) {
+ continue;
+ }
+ // copy entry as is to the stripped bundle
+ ZipEntry ze = new ZipEntry(jarEntry.getName());
Review comment:
Done in
https://github.com/apache/sling-org-apache-sling-feature-cpconverter/pull/74/commits/54c321f0f178e6adef4f2e95f853e2bb0231a27c.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]