You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nifi.apache.org by bb...@apache.org on 2019/10/10 15:36:53 UTC

[nifi] branch master updated: NIFI-6758: allow pre-unpacked nar's to be recorded in ExtensionMapping - fixing resource leak

This is an automated email from the ASF dual-hosted git repository.

bbende pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nifi.git


The following commit(s) were added to refs/heads/master by this push:
     new 5238dc2  NIFI-6758: allow pre-unpacked nar's to be recorded in ExtensionMapping - fixing resource leak
5238dc2 is described below

commit 5238dc20b1868ada96dc55d38dcbdf401a129a97
Author: Endre Zoltan Kovacs <ek...@hortonworks.com>
AuthorDate: Wed Oct 9 17:57:46 2019 +0200

    NIFI-6758: allow pre-unpacked nar's to be recorded in ExtensionMapping
    - fixing resource leak
    
    This closes #3806.
    
    Signed-off-by: Bryan Bende <bb...@apache.org>
---
 .../main/java/org/apache/nifi/nar/NarUnpacker.java | 49 ++++++++++++++++------
 1 file changed, 37 insertions(+), 12 deletions(-)

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 5830f88..13d6f9d 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<File, BundleCoordinate> createUnpackedNarBundleCoordinateMap(File extensionsWorkingDir) {
+        Map<File, BundleCoordinate> 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<File, BundleCoordinate> unpackedNars, final File docsDirectory, final ExtensionMapping mapping) throws IOException {
         for (final Map.Entry<File, BundleCoordinate> entry : unpackedNars.entrySet()) {
             final File unpackedNar = entry.getKey();