diff --git a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-nar-utils/src/main/java/org/apache/nifi/nar/NarUnpacker.java b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-nar-utils/src/main/java/org/apache/nifi/nar/NarUnpacker.java index 5830f8803d..13d6f9d0e5 100644 --- a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-nar-utils/src/main/java/org/apache/nifi/nar/NarUnpacker.java +++ b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-nar-utils/src/main/java/org/apache/nifi/nar/NarUnpacker.java @@ -24,6 +24,8 @@ import org.apache.nifi.util.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import static java.lang.String.format; + import java.io.BufferedReader; import java.io.File; import java.io.FileFilter; @@ -34,6 +36,7 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; @@ -105,35 +108,26 @@ public final class NarUnpacker { // get the manifest for this nar try (final JarFile nar = new JarFile(narFile)) { - final Manifest manifest = nar.getManifest(); - - // lookup the nar id - final Attributes attributes = manifest.getMainAttributes(); - final String groupId = attributes.getValue(NarManifestEntry.NAR_GROUP.getManifestName()); - final String narId = attributes.getValue(NarManifestEntry.NAR_ID.getManifestName()); - final String version = attributes.getValue(NarManifestEntry.NAR_VERSION.getManifestName()); - + BundleCoordinate bundleCoordinate = createBundleCoordinate(nar.getManifest()); // determine if this is the framework - if (NarClassLoaders.FRAMEWORK_NAR_ID.equals(narId)) { + if (NarClassLoaders.FRAMEWORK_NAR_ID.equals(bundleCoordinate.getId())) { if (unpackedFramework != null) { throw new IllegalStateException("Multiple framework NARs discovered. Only one framework is permitted."); } // unpack the framework nar unpackedFramework = unpackNar(narFile, frameworkWorkingDir); - } else if (NarClassLoaders.JETTY_NAR_ID.equals(narId)) { + } else if (NarClassLoaders.JETTY_NAR_ID.equals(bundleCoordinate.getId())) { if (unpackedJetty != null) { throw new IllegalStateException("Multiple Jetty NARs discovered. Only one Jetty NAR is permitted."); } // unpack and record the Jetty nar unpackedJetty = unpackNar(narFile, extensionsWorkingDir); - unpackedNars.put(unpackedJetty, new BundleCoordinate(groupId, narId, version)); unpackedExtensions.add(unpackedJetty); } else { // unpack and record the extension nar final File unpackedExtension = unpackNar(narFile, extensionsWorkingDir); - unpackedNars.put(unpackedExtension, new BundleCoordinate(groupId, narId, version)); unpackedExtensions.add(unpackedExtension); } } @@ -188,6 +182,7 @@ public final class NarUnpacker { } } + unpackedNars.putAll(createUnpackedNarBundleCoordinateMap(extensionsWorkingDir)); final ExtensionMapping extensionMapping = new ExtensionMapping(); mapExtensions(unpackedNars, docsWorkingDir, extensionMapping); @@ -205,6 +200,36 @@ public final class NarUnpacker { return null; } + /** + * Creates a map containing the nar directory mapped to it's bundle-coordinate. + * @param extensionsWorkingDir + * @return + */ + private static Map createUnpackedNarBundleCoordinateMap(File extensionsWorkingDir) { + Map result = new HashMap<>(); + File[] unpackedDirs = extensionsWorkingDir.listFiles(file -> file.isDirectory() && file.getName().endsWith("nar-unpacked")); + for (File unpackedDir : unpackedDirs) { + Path mf = Paths.get(unpackedDir.getAbsolutePath(), "META-INF", "MANIFEST.MF"); + try(InputStream is = Files.newInputStream(mf)) { + Manifest manifest = new Manifest(is); + BundleCoordinate bundleCoordinate = createBundleCoordinate(manifest); + result.put(unpackedDir, bundleCoordinate); + } catch (IOException e) { + logger.error(format("Unable to parse NAR information from unpacked nar directory [%s].", unpackedDir.getAbsoluteFile()), e); + } + } + return result; + } + + private static BundleCoordinate createBundleCoordinate(Manifest manifest) { + Attributes mainAttributes = manifest.getMainAttributes(); + String groupId = mainAttributes.getValue(NarManifestEntry.NAR_GROUP.getManifestName()); + String narId = mainAttributes.getValue(NarManifestEntry.NAR_ID.getManifestName()); + String version = mainAttributes.getValue(NarManifestEntry.NAR_VERSION.getManifestName()); + BundleCoordinate bundleCoordinate = new BundleCoordinate(groupId, narId, version); + return bundleCoordinate; + } + private static void mapExtensions(final Map unpackedNars, final File docsDirectory, final ExtensionMapping mapping) throws IOException { for (final Map.Entry entry : unpackedNars.entrySet()) { final File unpackedNar = entry.getKey();